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.'
def cnn(images, num_images, w, b, zca_white, avg): global patch_dim, pool_dim num_features = w.shape[0] image_dim = images.shape[0] step_size = 10 steps = int(math.ceil(num_features/step_size)) pool_size = math.floor((image_dim-patch_dim+1)/pool_dim) pooled_features = np.zeros((num_features, num_images, pool_size, pool_size)) for i in range(steps): feature_start = i*step_size; feature_end = (i+1)*step_size; wt = w[feature_start:feature_end, :] bt = b[feature_start:feature_end] convolved_features_this = convolve(patch_dim, step_size, images, wt, bt, zca_white, avg) pooled_features_this = mean_pool(pool_dim, convolved_features_this) pooled_features[feature_start:feature_end, :, :, :] = pooled_features_this return pooled_features