Python源码示例:keras.datasets.cifar10.load_data()
示例1
def get_mnist():
"""Retrieve the MNIST dataset and process the data."""
# Set defaults.
nb_classes = 10
batch_size = 128
input_shape = (784,)
# Get the data.
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = to_categorical(y_train, nb_classes)
y_test = to_categorical(y_test, nb_classes)
return (nb_classes, batch_size, input_shape, x_train, x_test, y_train, y_test)
示例2
def load_and_preprocess_data_3():
# The data, shuffled and split between train and test sets:
(X_train, y_train), (x_test, y_test) = cifar10.load_data()
logging.debug('X_train shape: {}'.format(X_train.shape))
logging.debug('train samples: {}'.format(X_train.shape[0]))
logging.debug('test samples: {}'.format(x_test.shape[0]))
# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
X_train = X_train.astype('float32')
x_test = x_test.astype('float32')
X_train /= 255
x_test /= 255
input_shape = X_train[0].shape
logging.debug('input_shape {}'.format(input_shape))
input_shape = X_train.shape[1:]
logging.debug('input_shape {}'.format(input_shape))
return X_train, x_test, y_train, y_test, input_shape
示例3
def test_cifar(self):
print('cifar10')
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
print('cifar100 fine')
(X_train, y_train), (X_test, y_test) = cifar100.load_data('fine')
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
print('cifar100 coarse')
(X_train, y_train), (X_test, y_test) = cifar100.load_data('coarse')
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
示例4
def test_imdb(self):
print('imdb')
(X_train, y_train), (X_test, y_test) = imdb.load_data()
示例5
def get_cifar10():
"""Retrieve the CIFAR dataset and process the data."""
# Set defaults.
nb_classes = 10
batch_size = 64
input_shape = (3072,)
# Get the data.
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.reshape(50000, 3072)
x_test = x_test.reshape(10000, 3072)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = to_categorical(y_train, nb_classes)
y_test = to_categorical(y_test, nb_classes)
return (nb_classes, batch_size, input_shape, x_train, x_test, y_train, y_test)
示例6
def get_cifar10():
"""Retrieve the CIFAR dataset and process the data."""
# Set defaults.
nb_classes = 10
batch_size = 64
input_shape = (3072,)
# Get the data.
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.reshape(50000, 3072)
x_test = x_test.reshape(10000, 3072)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = to_categorical(y_train, nb_classes)
y_test = to_categorical(y_test, nb_classes)
return (nb_classes, batch_size, input_shape, x_train, x_test, y_train, y_test)
示例7
def get_mnist():
"""Retrieve the MNIST dataset and process the data."""
# Set defaults.
nb_classes = 10
batch_size = 128
input_shape = (784,)
# Get the data.
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = to_categorical(y_train, nb_classes)
y_test = to_categorical(y_test, nb_classes)
return (nb_classes, batch_size, input_shape, x_train, x_test, y_train, y_test)
示例8
def load_dataset():
# Load the dataset from Keras
from keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Preprocessing the dataset
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train= preprocess_input(x_train)
x_test= preprocess_input(x_test)
x_train = x_train.reshape(-1, 32, 32, 3).astype('float32')
x_test = x_test.reshape(-1, 32, 32, 3).astype('float32')
y_train = to_categorical(y_train.astype('float32'))
y_test = to_categorical(y_test.astype('float32'))
return (x_train, y_train), (x_test, y_test)
示例9
def load_mnist(size=64):
(train_data, train_labels), (test_data, test_labels) = mnist.load_data()
train_data = normalize(train_data)
test_data = normalize(test_data)
x = np.concatenate((train_data, test_data), axis=0)
# y = np.concatenate((train_labels, test_labels), axis=0).astype(np.int)
seed = 777
np.random.seed(seed)
np.random.shuffle(x)
# np.random.seed(seed)
# np.random.shuffle(y)
# x = np.expand_dims(x, axis=-1)
x = np.asarray([scipy.misc.imresize(x_img, [size, size]) for x_img in x])
x = np.expand_dims(x, axis=-1)
return x
示例10
def load_cifar10(size=64) :
(train_data, train_labels), (test_data, test_labels) = cifar10.load_data()
train_data = normalize(train_data)
test_data = normalize(test_data)
x = np.concatenate((train_data, test_data), axis=0)
# y = np.concatenate((train_labels, test_labels), axis=0).astype(np.int)
seed = 777
np.random.seed(seed)
np.random.shuffle(x)
# np.random.seed(seed)
# np.random.shuffle(y)
x = np.asarray([scipy.misc.imresize(x_img, [size, size]) for x_img in x])
return x
示例11
def load_mnist(size=64):
(train_data, train_labels), (test_data, test_labels) = mnist.load_data()
train_data = normalize(train_data)
test_data = normalize(test_data)
x = np.concatenate((train_data, test_data), axis=0)
# y = np.concatenate((train_labels, test_labels), axis=0).astype(np.int)
seed = 777
np.random.seed(seed)
np.random.shuffle(x)
# np.random.seed(seed)
# np.random.shuffle(y)
# x = np.expand_dims(x, axis=-1)
x = np.asarray([scipy.misc.imresize(x_img, [size, size]) for x_img in x])
x = np.expand_dims(x, axis=-1)
return x
示例12
def load_cifar10(size=64) :
(train_data, train_labels), (test_data, test_labels) = cifar10.load_data()
train_data = normalize(train_data)
test_data = normalize(test_data)
x = np.concatenate((train_data, test_data), axis=0)
# y = np.concatenate((train_labels, test_labels), axis=0).astype(np.int)
seed = 777
np.random.seed(seed)
np.random.shuffle(x)
# np.random.seed(seed)
# np.random.shuffle(y)
x = np.asarray([scipy.misc.imresize(x_img, [size, size]) for x_img in x])
return x
示例13
def __init__(self, model, n_labeled_sample, batch_size):
self.n_labeled_sample = n_labeled_sample
self.batch_size = batch_size
self.model = model
self.n_classes = 10
# labeled_unlabeledの作成
(X_train, y_train), (self.X_test, self.y_test) = cifar10.load_data()
indices = np.arange(X_train.shape[0])
np.random.shuffle(indices)
self.X_train_labeled = X_train[indices[:n_labeled_sample]]
self.y_train_labeled = y_train[indices[:n_labeled_sample]]
self.X_train_unlabeled = X_train[indices[n_labeled_sample:]]
self.y_train_unlabeled_groundtruth = y_train[indices[n_labeled_sample:]]
# unlabeledの予測値
self.y_train_unlabeled_prediction = np.random.randint(
10, size=(self.y_train_unlabeled_groundtruth.shape[0], 1))
# steps_per_epoch
self.train_steps_per_epoch = X_train.shape[0] // batch_size
self.test_stepes_per_epoch = self.X_test.shape[0] // batch_size
# unlabeledの重み
self.alpha_t = 0.0
# labeled/unlabeledの一致率推移
self.unlabeled_accuracy = []
self.labeled_accuracy = []
示例14
def __init__(self, model, n_labeled_sample, batch_size):
self.n_labeled_sample = n_labeled_sample
self.batch_size = batch_size
self.model = model
self.n_classes = 10
# labeled_unlabeledの作成
(X_train, y_train), (self.X_test, self.y_test) = cifar10.load_data()
indices = np.arange(X_train.shape[0])
np.random.shuffle(indices)
self.X_train_labeled = X_train[indices[:n_labeled_sample]]
self.y_train_labeled = y_train[indices[:n_labeled_sample]]
self.X_train_unlabeled = X_train[indices[n_labeled_sample:]]
self.y_train_unlabeled_groundtruth = y_train[indices[n_labeled_sample:]]
# unlabeledの予測値
self.y_train_unlabeled_prediction = np.random.randint(
10, size=(self.y_train_unlabeled_groundtruth.shape[0], 1))
# steps_per_epoch
self.train_steps_per_epoch = X_train.shape[0] // batch_size
self.test_stepes_per_epoch = self.X_test.shape[0] // batch_size
# unlabeledの重み
self.alpha_t = 0.05
# labeled/unlabeledの一致率推移
self.unlabeled_accuracy = []
self.labeled_accuracy = []
示例15
def __init__(self, model, n_labeled_sample, batch_size):
self.n_labeled_sample = n_labeled_sample
self.batch_size = batch_size
self.model = model
self.n_classes = 10
# labeled_unlabeledの作成
(X_train, y_train), (self.X_test, self.y_test) = cifar10.load_data()
indices = np.arange(X_train.shape[0])
np.random.shuffle(indices)
self.X_train_labeled = X_train[indices[:n_labeled_sample]]
self.y_train_labeled = y_train[indices[:n_labeled_sample]]
self.X_train_unlabeled = X_train[indices[n_labeled_sample:]]
self.y_train_unlabeled_groundtruth = y_train[indices[n_labeled_sample:]]
# unlabeledの予測値
self.y_train_unlabeled_prediction = np.random.randint(
10, size=(self.y_train_unlabeled_groundtruth.shape[0], 1))
# steps_per_epoch
self.train_steps_per_epoch = X_train.shape[0] // batch_size
self.test_stepes_per_epoch = self.X_test.shape[0] // batch_size
# unlabeledの重み
self.alpha_t = 0.0
# labeled/unlabeledの一致率推移
self.unlabeled_accuracy = []
self.labeled_accuracy = []
示例16
def __init__(self, model, n_labeled_sample, batch_size):
self.n_labeled_sample = n_labeled_sample
self.batch_size = batch_size
self.model = model
self.n_classes = 10
# labeled_unlabeledの作成
(X_train, y_train), (self.X_test, self.y_test) = cifar10.load_data()
indices = np.arange(X_train.shape[0])
np.random.shuffle(indices)
self.X_train_labeled = X_train[indices[:n_labeled_sample]]
self.y_train_labeled = y_train[indices[:n_labeled_sample]]
self.X_train_unlabeled = X_train[indices[n_labeled_sample:]]
self.y_train_unlabeled_groundtruth = y_train[indices[n_labeled_sample:]]
# unlabeledの予測値
self.y_train_unlabeled_prediction = np.random.randint(
10, size=(self.y_train_unlabeled_groundtruth.shape[0], 1))
# steps_per_epoch
self.train_steps_per_epoch = X_train.shape[0] // batch_size
self.test_stepes_per_epoch = self.X_test.shape[0] // batch_size
# unlabeledの重み
self.alpha_t = 0.0
# labeled/unlabeledの一致率推移
self.unlabeled_accuracy = []
self.labeled_accuracy = []
示例17
def generate_training_data(data='mnist'):
if data == 'mnist':
(X_train, _), (_, _) = mnist.load_data()
X_train = np.expand_dims(X_train, -1) / 255.
elif data == 'cifar':
(X_train, _), (_, _) = cifar10.load_data()
X_train = X_train / 255.
else:
raise ValueError('data should be "mnist" or "cifar", got '
'"%s".' % data)
# Downsamples by averaging adjacent pixels.
X_low_dim = mean_bins(X_train)
return X_low_dim, X_train
示例18
def test_reuters(self):
print('reuters')
(X_train, y_train), (X_test, y_test) = reuters.load_data()
示例19
def test_mnist(self):
print('mnist')
(X_train, y_train), (X_test, y_test) = mnist.load_data()
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
示例20
def usps_to_mnist():
from DatasetLoad import usps_digit_dataload
source_traindata, source_trainlabel, source_testdata, source_testlabel = usps_digit_dataload()
source_trainlabel =source_trainlabel-1
source_testlabel =source_testlabel-1
# 2d to 3d for CNN
source_traindata = source_traindata.reshape(-1, 16, 16,1)
source_testdata = source_testdata.reshape(-1,16, 16,1)
from preprocess import zero_mean_unitvarince, resize_data
source_traindata = zero_mean_unitvarince(source_traindata, scaling=True)
source_testdata = zero_mean_unitvarince(source_testdata, scaling=True)
#
from keras.datasets import mnist
(target_traindata, target_trainlabel), (target_testdata, target_testlabel) = mnist.load_data()
target_size = target_traindata.shape
resize = True
resize_size =16
if resize == True:
target_traindata = resize_data(target_traindata, resize_size=resize_size)
target_testdata = resize_data(target_testdata, resize_size=resize_size)
target_size = target_traindata.shape
target_traindata = zero_mean_unitvarince(target_traindata,scaling=True)
target_testdata = zero_mean_unitvarince(target_testdata,scaling=True)
target_traindata = target_traindata.reshape(-1,target_size[1],target_size[2],1)
target_testdata =target_testdata.reshape(-1,target_size[1],target_size[2],1)
return (source_traindata, source_trainlabel, source_testdata, source_testlabel), (target_traindata, target_trainlabel, target_testdata, target_testlabel)
#%% MNIST MNISTM
示例21
def mnist_to_mnistm():
from keras.datasets import mnist
(source_traindata, source_trainlabel), (source_testdata, source_testlabel) = mnist.load_data()
source_size = source_traindata.shape
resize = False
resize_size =32
from preprocess import zero_mean_unitvarince,resize_data
if resize == True:
source_traindata = resize_data(source_traindata, resize_size=resize_size)
source_testdata = resize_data(source_testdata, resize_size=resize_size)
source_size = source_traindata.shape
source_traindata = zero_mean_unitvarince(source_traindata,scaling=True)
source_testdata = zero_mean_unitvarince(source_testdata,scaling=True)
convert_rgb=1
if convert_rgb:
source_traindata = np.stack((source_traindata,source_traindata,source_traindata), axis=3)
source_testdata = np.stack((source_testdata,source_testdata,source_testdata), axis=3)
from DatasetLoad import mnist_m_dataload
from skimage.color import rgb2gray
target_traindata, target_trainlabel, target_testdata, target_testlabel= mnist_m_dataload()
target_size = target_traindata.shape
resize = False
resize_size =28
if resize == True:
target_traindata = resize_data(target_traindata, resize_size=resize_size)
target_testdata = resize_data(target_testdata, resize_size=resize_size)
target_size = target_traindata.shape
target_traindata = zero_mean_unitvarince(target_traindata,scaling=True)
target_testdata = zero_mean_unitvarince(target_testdata,scaling=True)
return (source_traindata, source_trainlabel, source_testdata, source_testlabel), (target_traindata, target_trainlabel, target_testdata, target_testlabel)
#%%
示例22
def get_data(num_classes=10):
"""
Get the CIFAR dataset.
Parameters:
None
Returns:
train_data - training data split
train_labels - training labels
test_data - test data split
test_labels - test labels
"""
print('[INFO] Loading the CIFAR10 dataset...')
(train_data, train_labels), (test_data, test_labels) = cifar10.load_data()
# Transform labels to one hot labels
# Example: '0' will become [1, 0, 0, 0, 0, 0, 0, 0, 0]
# '1' will become [0, 1, 0, 0, 0, 0, 0, 0, 0]
# and so on...
train_labels = np_utils.to_categorical(train_labels, num_classes)
test_labels = np_utils.to_categorical(test_labels, num_classes)
# Change type and normalize data
train_data = train_data.astype('float32')
test_data = test_data.astype('float32')
train_data /= 255
test_data /= 255
return train_data, train_labels, test_data, test_labels
示例23
def load_mnist():
(train_data, train_labels), (test_data, test_labels) = mnist.load_data()
x = np.concatenate((train_data, test_data), axis=0)
x = np.expand_dims(x, axis=-1)
return x
示例24
def load_cifar10() :
(train_data, train_labels), (test_data, test_labels) = cifar10.load_data()
x = np.concatenate((train_data, test_data), axis=0)
return x
示例25
def load_data(dataset_name) :
if dataset_name == 'mnist' :
x = load_mnist()
elif dataset_name == 'cifar10' :
x = load_cifar10()
else :
x = glob(os.path.join("./dataset", dataset_name, '*.*'))
return x
示例26
def load_data(dataset_name, size=64) :
if dataset_name == 'mnist' :
x = load_mnist(size)
elif dataset_name == 'cifar10' :
x = load_cifar10(size)
else :
x = glob(os.path.join("./dataset", dataset_name, '*.*'))
return x
示例27
def load_data(dataset_name, size=64) :
if dataset_name == 'mnist' :
x = load_mnist(size)
elif dataset_name == 'cifar10' :
x = load_cifar10(size)
else :
x = glob(os.path.join("./dataset", dataset_name, '*.*'))
return x
示例28
def main(_):
if FLAGS.dataset == 'cifar10':
(X_train, y_train), (_, _) = cifar10.load_data()
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=0)
else:
with open('data/train.p', mode='rb') as f:
train = pickle.load(f)
X_train, X_val, y_train, y_val = train_test_split(train['features'], train['labels'], test_size=0.33, random_state=0)
train_output_file = "{}_{}_{}.p".format(FLAGS.network, FLAGS.dataset, 'bottleneck_features_train')
validation_output_file = "{}_{}_{}.p".format(FLAGS.network, FLAGS.dataset, 'bottleneck_features_validation')
print("Resizing to", (w, h, ch))
print("Saving to ...")
print(train_output_file)
print(validation_output_file)
with tf.Session() as sess:
K.set_session(sess)
K.set_learning_phase(1)
model = create_model()
print('Bottleneck training')
train_gen = gen(sess, X_train, y_train, batch_size)
bottleneck_features_train = model.predict_generator(train_gen(), X_train.shape[0])
data = {'features': bottleneck_features_train, 'labels': y_train}
pickle.dump(data, open(train_output_file, 'wb'))
print('Bottleneck validation')
val_gen = gen(sess, X_val, y_val, batch_size)
bottleneck_features_validation = model.predict_generator(val_gen(), X_val.shape[0])
data = {'features': bottleneck_features_validation, 'labels': y_val}
pickle.dump(data, open(validation_output_file, 'wb'))
示例29
def __init__(self, batch_size=64, test=False):
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
if test:
images = x_test
else:
images = x_train
self.images = (images - 127.5) / 127.5
self.batch_size = batch_size
self.num_samples = len(self.images)
self.shuffle_samples()
self.next_batch_pointer = 0
示例30
def __init__(self, batch_size=64, test=False):
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
if test:
images = x_test
else:
images = x_train
self.images = (images - 127.5) / 127.5
self.batch_size = batch_size
self.num_samples = len(self.images)
self.shuffle_samples()
self.next_batch_pointer = 0