def check(train_images): global patch_dim, image_dim WB, zca_white, avg = lt.load_features('../LinearDecoder/theta.txt') w = WB[0][0] b = WB[0][1] print 'zca_white:', zca_white.shape print 'w:', w.shape print 'b:', b.shape print 'avg:', avg.shape conv_images = train_images[:, :, :, :8] convolved_features = convolve(patch_dim, 400, conv_images, w, b, zca_white, avg) for _ in range(1000): feature_num = random.randint(0, 399) image_num = random.randint(0, 7) image_row = random.randint(0, image_dim - patch_dim) image_col = random.randint(0, image_dim - patch_dim) patch = conv_images[image_row:image_row + patch_dim, image_col:image_col + patch_dim, :, image_num] patch = np.concatenate((patch[:, :, 0].flatten(), patch[:, :, 1].flatten(), patch[:, :, 2].flatten())) patch = patch.reshape((patch.size, 1)) patch = patch - avg patch = zca_white.dot(patch) print patch.shape features = w.dot(patch) + b.reshape((b.shape[0], 1)) print features.shape features = sigmoid(features) print features.shape print '*******************' print features.shape print convolved_features.shape print feature_num, image_num, image_row, image_col if np.abs(features[feature_num, 0] - convolved_features[feature_num, image_num, image_row, image_col]) > 1e-9: print('Convolved feature does not match activation from autoencoder\n') print('Feature Number : %d\n', feature_num) print('Image Number : %d\n', image_num) print('Image Row : %d\n', image_row) print('Image Column : %d\n', image_col) print('Convolved feature : %0.5f\n', convolved_features[feature_num, image_num, image_row, image_col]) print('Sparse AE feature : %0.5f\n', features[feature_num, 0]) print('Convolved feature does not match activation from autoencoder') print 'Congratulations! Your convolution code passed the test.' 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 = mean_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.'
print "Pooling incorrect" print "Expected matrix" print expected_matrix print "Got" print pooled_features print 'Congratulations! Your pooling code passed the test.' if __name__ == '__main__': train_images, train_labels, train_numImages = load_train_images() # train_images = train_images[:,:,:,:100] # train_labels = train_labels[:100] # train_numImages = 100 check(train_images) test_images, test_labels, test_numImages = load_test_images() WB, zcaWhite, avg = lt.load_features('../LinearDecoder/theta.txt') w = WB[0][0] b = WB[0][1] try: pooledTrainFeatures = np.load('pooledTrainFeatures.npy') except: print 'start train cnn...', time.time() pooledTrainFeatures = cnn(train_images, train_numImages, w, b, zcaWhite, avg) print pooledTrainFeatures print pooledTrainFeatures.shape np.save('pooledTrainFeatures', pooledTrainFeatures) print 'train cnn over...', time.time() print pooledTrainFeatures.shape try: mat = np.genfromtxt('softmax.mat') except: