def testPooling(): """ 函数功能:检测池化操作是否正常 """ # 用于进行池化操作的卷积特征图像的边长维度 t_patch_dim = 8 # 池化的维度参数 t_pool_dim = 4 # 池化后数据的尺寸维度 pool_size = int(t_patch_dim / t_pool_dim) # 卷积图像的像素总数 conv_size = t_patch_dim * t_patch_dim # 生成用于测试池化的图像数据 test_matrix = np.arange(conv_size).reshape(t_patch_dim, t_patch_dim) # 进行池化操作,计算获得理论上正确的池化结果,结果存储在expected_matrix中 expected_matrix = np.zeros(shape = (pool_size,pool_size)) for i in range(pool_size): for j in range(pool_size): expected_matrix[i,j] = np.mean(test_matrix[i*t_pool_dim:(i+1)*t_pool_dim, j*t_pool_dim:(j+1)*t_pool_dim]) # 对卷积图像维度进行重构,便于使用自己实现的池化函数 test_matrix = np.reshape(test_matrix,(1,1,t_patch_dim,t_patch_dim)) # 利用自己实现的池化函数计算的结果 pooled_features = cnn.cnn_pool(t_pool_dim,test_matrix) # 比较池化函数返回结果与理论值是否相等 if not(pooled_features == expected_matrix).all(): print("Pooling incorrect") print("Expected matrix") print(expected_matrix) print("Got") print(pooled_features) sys.exit("Pooling feature does not match expected matrix. Exiting...") print('Congratulations! Your pooling code passed the test.')
print('Image Row : {}\n'.format(image_row)) print('Image Column : {}\n'.format(image_col)) print('Convolved feature : {:f}\n'.format(convolved_features[feature_num, image_num, image_row, image_col])) print('Sparse AE feature : {:f}\n'.format(features[feature_num, 0])) print('Error! Convolved feature does not match activation from autoencoder') print('Congratulations! Your convolution code passed the test.') """ STEP 2c: Implement pooling Implement pooling in the function cnnPool in cnnPool.m """ # NOTE: Implement cnn_pool first! pooled_features = cnn_pool(pool_dim, convolved_features) """ STEP 2d: Checking your pooling To ensure that you have implemented pooling, we will use your pooling function to pool over a test matrix and check the results. """ test_matrix = np.arange(64, dtype=np.float64).reshape((8, 8)) expected_matrix = np.array([[test_matrix[0:4, 0:4].mean(), test_matrix[0:4, 4:8].mean()], [test_matrix[4:8, 0:4].mean(), test_matrix[4:8, 4:8].mean()]]) test_matrix = test_matrix.reshape((1, 1, 8, 8)) pooled_features = cnn_pool(4, test_matrix)
def trainFeatures(): """ 函数功能: 对原始训练数据和测试数据进行卷积和池化操作,获得隐藏层上的特征数据 """ # 载入线性编码器参数 encoderFile = 'stl10_features.pickle' W,b,zca_white,patch_mean = loadEncoder(encoderFile) # 载入训练数据 trainFile = 'stlTrainSubset.mat' trainParams = ['trainImages','trainLabels','numTrainImages'] train_images,train_labels,n_train_images = loadDate(trainFile,trainParams) # 载入测试数据 testFile = 'stlTestSubset.mat' testParams = ['testImages','testLabels','numTestImages'] test_image,test_labels,n_test_images = loadDate(testFile,testParams) # 测试实现的卷积和池化函数是否正确 if debug == True: # 使用前8幅图像测试卷积操作是否正常 conv_images = train_images[:,:,:,0:8] testConvolution(conv_images,W,b,zca_white,patch_mean) testPooling() # 在训练数据上利用卷积和池化对隐藏层的特征分块进行训练 pooled_features_train = np.zeros(shape = (hidden_size,n_train_images, int(np.floor((image_dim - patch_dim + 1) / pool_dim)), int(np.floor((image_dim - patch_dim + 1) / pool_dim))), dtype = np.float) pooled_features_test = np.zeros(shape = (hidden_size,n_test_images, int(np.floor((image_dim - patch_dim + 1) / pool_dim)), int(np.floor((image_dim - patch_dim + 1) / pool_dim))), dtype = np.float) # 特征步长 step_size = 25 # 检查特征个数能否被步长整除 assert hidden_size % step_size == 0,"step_size should divide hidden_size" feature_part_num = int(hidden_size / step_size) start_time = time.time() for conv_part in range(feature_part_num): feature_start = conv_part * step_size feature_end = (conv_part + 1) * step_size print('Step:',conv_part,'\nfeatures',feature_start,'to',feature_end) # 选取特征参数,用于后续从图像中卷积提取在这些特征上的图像信息 Wt = W[feature_start:feature_end,:] bt = b[feature_start:feature_end] # 在训练数据上卷积并池化 print('Convolving and pooling train_images') convolved_features = cnn.cnn_convolve(patch_dim, step_size, train_images, Wt, bt, zca_white, patch_mean) pooled_features = cnn.cnn_pool(pool_dim, convolved_features) pooled_features_train[feature_start:feature_end,:,:,:] = pooled_features print('Time elapsed:',str(datetime.timedelta(seconds = time.time() - start_time))) # 在测试数据上卷积并池化 print('Convolving and pooling test_images') convolved_features = cnn.cnn_convolve(patch_dim, step_size, test_image, Wt, bt, zca_white, patch_mean) pooled_features = cnn.cnn_pool(pool_dim, convolved_features) pooled_features_test[feature_start:feature_end,:,:,:] = pooled_features print('Time elapsed:',str(datetime.timedelta(seconds = time.time() - start_time))) # 保存池化后的特征数据 print('Saving pooled features...') with open(p.join(PATH,'cnn_pooled_features.pickle'),'wb') as f: pickle.dump(pooled_features_train,f) pickle.dump(pooled_features_test,f) pickle.dump(train_labels,f) pickle.dump(test_labels,f) print('Saved') print('Time elapsed:',str(datetime.timedelta(seconds=time.time() - start_time)))
##====================================================================== ## STEP 2: Implement and test pooling # Implement pooling in the function cnnPool in cnnPool.m ## STEP 2a: Implement pooling # NOTE: Implement cnnPool in cnnPool.m first! # see https://github.com/jatinshah/ufldl_tutorial/blob/master/cnn_exercise.py test_matrix = np.array(range(0,64)).reshape(8,8) expected_matrix = np.array([[np.mean(test_matrix[0:4,0:4]), np.mean(test_matrix[0:4,4:8])], [np.mean(test_matrix[4:8,0:4]), np.mean(test_matrix[4:8,4:8])]]) test_matrix = np.reshape(test_matrix, (8, 8, 1, 1)) pooled_features = cnn.cnn_pool(4, test_matrix).squeeze() ## STEP 2b: Checking your pooling # To ensure that you have implemented pooling, we will use your pooling # function to pool over a test matrix and check the results. if not (pooled_features == expected_matrix).all(): print "Pooling incorrect" print "Expected matrix" print expected_matrix print "Got" print pooled_features exit("Pooling code failed") print 'Congratulations! Your code passed the test.'
## STEP 2c: Implement pooling # Implement pooling in the function cnnPool in cnnPool.m # NOTE: Implement cnnPool in cnnPool.m first! ## STEP 2d: Checking your pooling # To ensure that you have implemented pooling, we will use your pooling # function to pool over a test matrix and check the results. test_matrix = np.arange(64).reshape(8, 8) expected_matrix = np.array([[np.mean(test_matrix[0:4, 0:4]), np.mean(test_matrix[0:4, 4:8])], [np.mean(test_matrix[4:8, 0:4]), np.mean(test_matrix[4:8, 4:8])]]) test_matrix = np.reshape(test_matrix, (1, 1, 8, 8)) pooled_features = cnn.cnn_pool(4, test_matrix) if not (pooled_features == expected_matrix).all(): print "Pooling incorrect" print "Expected matrix" print expected_matrix print "Got" print pooled_features print 'Congratulations! Your pooling code passed the test.' ##====================================================================== ## STEP 3: Convolve and pool with the dataset # In this step, you will convolve each of the features you learned with # the full large images to obtain the convolved features. You will then # pool the convolved features to obtain the pooled features for