数据集官网上有,可以自行下载
代码
数据集导入 import pickle training_file = './train.p' testing_file = './test.p' with open(training_file, mode='rb') as f:train = pickle.load(f) with open(testing_file, mode='rb') as f:test = pickle.load(f) X_train, y_train = train['features'], train['labels'] X_test, y_test = test['features'], test['labels'] 探索和可视化数据集 n_train = X_train.shape[0] n_test = X_test.shape[0] image_shape = X_train.shape[1:] n_classes = len(set(y_train)) print("Number of training examples =", n_train) print("Number of testing examples =", n_test) print() print("Image data shape =", image_shape) print("Number of classes =", n_classes) 数据预处理 import numpy as np X_train_rgb = X_train X_train_gry = np.sum(X_train/3, axis=3, keepdims=True) X_test_rgb = X_test X_test_gry = np.sum(X_test/3, axis=3, keepdims=True) print('RGB shape:', X_train_rgb.shape) print('Grayscale shape:', X_train_gry.shape) X_train_normalized = (X_train - 128.)/128. X_test_normalized = (X_test - 128.)/128. from scipy import ndimage def expend_training_data(X_train, y_train): """ Augment training data """ expanded_images = np.zeros([X_train.shape[0] * 5, X_train.shape[1], X_train.shape[2]]) expanded_labels = np.zeros([X_train.shape[0] * 5]) counter = 0 for x, y in zip(X_train, y_train): # register original data expanded_images[counter, :, :] = x expanded_labels[counter] = y counter = counter + 1 # get a value for the background # zero is the expected value, but median() is used to estimate background's value bg_value = np.median(x) # this is regarded as background's value for i in range(4): # rotate the image with random degree angle = np.random.randint(-15, 15, 1) new_img = ndimage.rotate(x, angle, reshape=False, cval=bg_value) # shift the image with random distance shift = np.random.randint(-2, 2, 2) new_img_ = ndimage.shift(new_img, shift, cval=bg_value) # register new training data expanded_images[counter, :, :] = new_img_ expanded_labels[counter] = y counter = counter + 1 return expanded_images, expanded_labels X_train_normalized = np.reshape(X_train_normalized,(-1, 32, 32)) agument_x, agument_y = expend_training_data(X_train_normalized[:], y_train[:]) agument_x = np.reshape(agument_x, (-1, 32, 32, 1)) print(agument_y.shape) print(agument_x.shape) print('agument_y mean:', np.mean(agument_y)) print('agument_x mean:',np.mean(agument_x)) #print(y_train.shape)



