def test_calc_weight_deltas(): learning_rate = .1 i = numpy.matrix('0.4 0.9') net = [(numpy.matrix('1 1.2; 0.5 0.5'), numpy.matrix('0 0.5')), (numpy.matrix('0.1; -0.8'), numpy.matrix('-1.3'))] t = numpy.matrix('0.1') zs = feed_forward(i, net) errors = compute_errors(t, zs, net) deltas = calc_weight_deltas(learning_rate, errors, i, zs, net) assert deltas[0][1].shape == (1, 2) assert deltas[1][0].item((0, 0)) == pytest.approx(-.000265, abs=.00001) assert deltas[1][0].item((1, 0)) == pytest.approx(-.000306, abs=.00001) assert deltas[1][1].item((0, 0)) == pytest.approx(-0.000379, abs=.00001) assert deltas[1][1].shape == (1, 1) new_w2 = numpy.add(net[1][0], deltas[1][0]) assert new_w2.item((0, 0)) == pytest.approx(0.09973, abs=.0001)
def run_softmax(N, image_size, patch_size): # open training data print "Training data!" file_train = "data/pickles/train.pickle" file_train_labels = "data/pickles/labels_train.pickle" images_train = helper.unpickle_data(file_train)[:, :, :N] labels_train = helper.unpickle_data(file_train_labels)[:N] print "Test data!" file_test = "data/pickles/test.pickle" file_test_labels = "data/pickles/labels_test.pickle" images_test = helper.unpickle_data(file_test) labels_test = helper.unpickle_data(file_test_labels) # open saved theta parameters theta = np.load('weights_learned/weights.out') # get representations for training data train_map = model.feed_forward(theta, images_train, patch_size, image_size, images_train.shape[2], 2) train_features = (train_map['z3'] * train_map['z3_mask']).reshape(image_size ** 2, N) # get representations for test data test_map = model.feed_forward(theta, images_test, patch_size, image_size, images_test.shape[2], 2) test_features = (test_map['z3'] * test_map['z3_mask']).reshape(image_size ** 2, images_test.shape[2]) # print "Check gradients!" # lambda_ = 0.1 # num_labels = 10 theta = 0.005 * np.random.randn(num_labels * image_size ** 2) l_cost, l_grad = softmax_cost(theta, num_labels, image_size ** 2, lambda_, train_features, labels_train) J = lambda x: softmax_cost(x, num_labels, image_size ** 2, lambda_, train_features, labels_train) # gradient_check.compute_grad(J, theta, l_grad) # run softmax function opt_theta, input_size, num_classes = softmax_train(image_size ** 2, num_labels, lambda_, train_features, labels_train, options_) predictions = softmax_predict((opt_theta, image_size ** 2, num_labels), train_features) labels_train[labels_train == 10] = 0 print "Accuracy (train): {0:.2f}%".format(100 * np.sum(predictions == labels_train, dtype=np.float64) / labels_train.shape[0]) predictions = softmax_predict((opt_theta, image_size ** 2, num_labels), test_features) labels_test[labels_test == 10] = 0 print "Accuracy (test): {0:.2f}%".format(100 * np.sum(predictions == labels_test, dtype=np.float64) / labels_test.shape[0])
def test_feed_forward(): i = numpy.matrix('1 2') net = [ ( numpy.matrix('.5 .5; .5 .5'), numpy.matrix('.1 .1') ), ( numpy.matrix('.25 .75; .75 .25'), numpy.matrix('.2 .2') ) ] [ z1, z2 ] = feed_forward(i, net) assert z2.item((0,0)) == pytest.approx(.737, abs=.001) assert z2.item((0,1)) == pytest.approx(.737, abs=.001)
def test_another_forward(): i = numpy.matrix('0.4 0.9') net = [ ( numpy.matrix('1 1.2; 0.5 0.5'), numpy.matrix('0 0.5') ), ( numpy.matrix('0.1; -0.8'), numpy.matrix('-1.3') ) ] (z1, z2) = feed_forward(i, net) assert z2.item((0,0)) == pytest.approx(0.133, abs=.001) assert z1.item((0,0)) == pytest.approx(0.700, abs=.001) assert z1.item((0,1)) == pytest.approx(0.807, abs=.001)
def test_compute_errors(): i = numpy.matrix('0.4 0.9') t = numpy.matrix('0.1') net = [(numpy.matrix('1 1.2; 0.5 0.5'), numpy.matrix('0 0.5')), (numpy.matrix('0.1; -0.8'), numpy.matrix('-1.3'))] zs = feed_forward(i, net) errors = compute_errors(t, zs, net) assert errors[1].item((0, 0)) == pytest.approx(-0.0037, abs=.0001) assert errors[0].item((0, 0)) == pytest.approx(-0.000079, abs=.00001) assert errors[0].item((0, 1)) == pytest.approx(0.000469, abs=.0001)
def test_calc_errors(): i = numpy.matrix('0.4 0.9') net = [(numpy.matrix('1 1.2; 0.5 0.5'), numpy.matrix('0 0.5')), (numpy.matrix('0.1; -0.8'), numpy.matrix('-1.3'))] t = numpy.matrix('0.1') (z1, z2) = feed_forward(i, net) output_error = calc_output_error(t, z2) hidden_error = calc_hidden_error(output_error, z1, net[1][0]) assert output_error.item((0, 0)) == pytest.approx(-0.0037, abs=.0001) assert hidden_error.item((0, 0)) == pytest.approx(-0.000079, abs=.00001) assert hidden_error.item((0, 1)) == pytest.approx(0.000469, abs=.0001)
def write_mesh_output(weights_path, image_path, out_mesh_path): Ws = {} w_file = h5py.File(weights_path, 'r') for name in model.weight_names: Ws[name] = tf.constant(w_file[name]) image = Image.open(image_path) input_image = np.empty([1, 128, 128, 3]) input_image[0] = np.array(image.resize((128, 128)), dtype=np.float32) / 255 X = tf.constant(input_image, dtype='float32') ff = model.feed_forward(X, Ws) sess = tf.Session() sess.run(tf.global_variables_initializer()) depth = np.array(ff.eval(session=sess)) depth_image = depth[0, :, :, 0] scaled_image = get_scaled_image(image) scaled_width = scaled_image.width scaled_height = scaled_image.height resized_depth = resize_depth(depth_image, scaled_width, scaled_height) plt.imshow(resized_depth, cmap=plt.get_cmap('gray')) plt.show() vertices, faces = create_mesh(resized_depth) mesh = o3d.geometry.TriangleMesh() mesh.vertices = o3d.utility.Vector3dVector(vertices) mesh.triangles = o3d.utility.Vector3iVector(faces.astype(np.int32)) mesh.vertex_colors = o3d.utility.Vector3dVector( np.reshape( np.array(scaled_image, dtype=np.float32) / 255, (scaled_width * scaled_height, 3))) o3d.io.write_triangle_mesh(out_mesh_path, mesh)