100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 德国交通标志识别

德国交通标志识别

时间:2019-05-30 06:51:03

相关推荐

德国交通标志识别

数据集官网上有,可以自行下载

代码

数据集导入import pickletraining_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 npX_train_rgb = X_trainX_train_gry = np.sum(X_train/3, axis=3, keepdims=True)X_test_rgb = X_testX_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 ndimagedef 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 = 0for x, y in zip(X_train, y_train):# register original dataexpanded_images[counter, :, :] = xexpanded_labels[counter] = ycounter = counter + 1# get a value for the background# zero is the expected value, but median() is used to estimate background's valuebg_value = np.median(x) # this is regarded as background's valuefor i in range(4):# rotate the image with random degreeangle = np.random.randint(-15, 15, 1)new_img = ndimage.rotate(x, angle, reshape=False, cval=bg_value)# shift the image with random distanceshift = np.random.randint(-2, 2, 2)new_img_ = ndimage.shift(new_img, shift, cval=bg_value)# register new training dataexpanded_images[counter, :, :] = new_img_expanded_labels[counter] = ycounter = counter + 1return expanded_images, expanded_labelsX_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)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。