Esempio n. 1
0
def main():
    """Entry point function"""
    adder = tf.Variable(10.)

    life = tfh.Life(
        tfh.NeuralNetwork(
            name="Inner_NN",
            layers=[
                # Even though for ValidationLayer's name isn't required to not include space
                # but it is good to stay consistance since other might require it
                tfh.ValidationLayer(name="Input_Validation", shape=[None], dtype=tf.float32),
                tfh.OpLayer(lambda input: input + adder, [adder]),
                tfh.ValidationLayer(shape=[None], dtype=tf.float32)]),
        optimizer=tf.train.AdamOptimizer(0.3))

    input_x = np.array([
        1, 2, 3, 4
    ], dtype=np.float32)

    expect_y = np.array([
        3, 4, 5, 6
    ], dtype=np.float32)

    life.connect_neural_network(sample_input=input_x, sample_output=expect_y, will_train=True)

    life.init_var()

    hypo = life.feed(input_x)

    print(hypo)

    for counter in range(200):
        cost = life.train(input_x, expect_y, process_list=[adder])
        if counter%10 == 0:
            print(cost)
def main():
    """Entry point function"""
    sess = tf.Session()

    x_input = tf.placeholder(tf.float32, shape=[None])

    my_nn = tfh.NeuralNetwork(
        name="Main_Layer",
        layers=[
            tfh.ValidationLayer(shape=[None], dtype=tf.float32),
            tfh.ReshapeLayer(shape=[-1, 1]),
            tfh.FeedForwardLayer(features_out=2, name="FFLayer"),
            #features_out is the number of neuron in the layer
            tfh.FeedForwardLayer(features_in=2, features_out=5, name="FFLayer"),
            #features_in can also be set to verify
            tfh.ValidationLayer(shape=[None, 5], dtype=tf.float32),
        ]
    )

    y_hypothesis = my_nn.connect(x_input)

    sess.run(tf.initialize_all_variables())

    batch = [1, 2, 3, 4]

    print(sess.run(y_hypothesis, feed_dict={x_input: batch}))
def main():
    """Entry point function"""
    life = tfh.Life(tfh.NeuralNetwork(
        name="Inner_NN",
        layers=[
            tfh.ValidationLayer(name="Input_Validation",
                                shape=[None, 10, 10],
                                dtype=tf.float32),
            tfh.ReshapeLayer(shape=[None, 10, 10, 1]),
            tfh.ConvLayer(depth_out=2, kernel_width=2),
            tfh.ConvLayer(depth_out=1, kernel_width=3, padding=False),
            tfh.ValidationLayer(shape=[None, 8, 8, 1], dtype=tf.float32),
            tfh.ReshapeLayer(shape=[None, 8, 8])
        ]),
                    optimizer=tf.train.AdamOptimizer(0.3))

    input_x = np.random.randn(1, 10, 10).astype(np.float32)

    expect_y = np.random.randn(1, 8, 8).astype(np.float32)

    life.connect_neural_network(sample_input=input_x,
                                sample_output=expect_y,
                                will_train=True)

    life.init_var()

    hypo = life.feed(input_x)

    print(hypo)

    for counter in range(200):
        cost = life.train(input_x, expect_y, process_list=[])
        if counter % 10 == 0:
            print(cost)
Esempio n. 4
0
def main():
    """Entry point function"""
    model_name = "model2-d"
    life = tfh.Life(tfh.NeuralNetwork(layers=[
        tfh.ValidationLayer(shape=[None, 96 + 8, 120 + 8, 3], dtype=tf.uint8),
        tfh.OpLayer(tf.to_float),
        tfh.OpLayer(lambda x: x / 255.),
        tfh.ConvLayer(kernel_width=3, depth_out=30, depth_in=3, padding=False),
        tfh.ConvLayer(kernel_width=3, depth_out=50, padding=False),
        tfh.ConvLayer(kernel_width=3, depth_out=40, padding=False),
        tfh.ConvLayer(kernel_width=3, depth_out=9, padding=False),
        tfh.OpLayer(tf.sigmoid),
        tfh.ValidationLayer(shape=[None, 96, 120, 9], dtype=tf.float32),
    ]),
                    cost_function=conv_cross_entropy,
                    optimizer=tf.train.AdamOptimizer(0.001))

    data = Data.DataFeeder("data/",
                           dynamic_load=False,
                           filename="TrainCache",
                           data_padding=4,
                           label_height=96,
                           label_width=120)

    batch = data.get_batch(60)

    print(batch[0].dtype, batch[0].shape)
    print(batch[1].dtype, batch[1].shape)

    life.connect_neural_network(sample_input=batch[0],
                                sample_output=batch[1],
                                will_train=True)

    # life.load_saved_model("auto-save/"+model_name+"-save-data3.0")
    # life.load_saved_model(model_name+"-save-data")
    life.init_var()

    for counter in range(100):

        batch = data.get_batch(60, shuffle=True)
        result = life.train(input_layer_value=batch[0],
                            output_layer_value=batch[1])

        if counter % 27 == 0:
            print("{} iteration".format(counter), result)
            data_in = batch[0]
            feed_result = life.feed(input_layer_value=data_in)
            hypo = feed_result[0]
            # print("HYPO SHAPE", hypo.shape)
            expect = batch[1][0]
            cv2.imshow("image-in", data_in[0])
            cv2.imshow("hypo", ObjClass.combine_label(hypo))
            cv2.imshow("expect", ObjClass.combine_label(expect))
            cv2.waitKey(20)

        if counter % 100 == 0:
            life.save_current_model("auto-save/" + model_name + "-save-data" +
                                    str(counter / 100))

    life.save_current_model(model_name + "-save-data")
Esempio n. 5
0
def main():
    """Entry point function"""
    model_name = "model4-a"
    padding_size = 0
    train_data_height = 512
    train_data_width = 640
    batch_size = 5

    life = tfh.Life(
        tfh.NeuralNetwork(
            layers=[
                tfh.ValidationLayer(shape=[None, train_data_height + padding_size*2, train_data_width + padding_size*2, 3], dtype=tf.uint8),
                tfh.OpLayer(tf.to_float),
                tfh.OpLayer(lambda x: x/255. - 0.5),
                tfh.ConvLayer(kernel_width=3, depth_out=10, depth_in=3, padding=True),

                tfh.ConvLayer(kernel_width=3, depth_out=10, padding=True),
                tfh.ConvLayer(kernel_width=3, depth_out=20, padding=True),
                tfh.ConvLayer(kernel_width=3, depth_out=10, padding=True),

                tfh.ConvLayer(kernel_width=3, depth_out=2, padding=True),
                tfh.ValidationLayer(shape=[None, train_data_height, train_data_width, 2], dtype=tf.float32),
                # tfh.ReshapeLayer(shape=[None, 2]),
            ]
        )
    )

    data = Data.DataFeeder("data/", dynamic_load=True,
                           filename=model_name+"-FeedCache", data_padding=0,
                           label_height=512, label_width=640)

    batch = data.get_batch(5)

    life.connect_neural_network(sample_input=batch[0], will_train=False)

    # life.load_saved_model(model_name+"-save-data")
    # life.load_saved_model("auto-save/"+model_name+"-save-data1.0")
    life.load_saved_model("auto-save/model4-a-sava-data20.0")
    # life.init_var()

    print(batch[0].dtype, batch[0].shape)
    print(batch[1].dtype, batch[1].shape)

    ##

    # data.get_batch(500)

    for _ in range(100):
        batch = data.get_batch(5, shuffle=True)
        data_in = batch[0]
        feed_result = life.feed(input_layer_value=data_in)
        hypo = feed_result[0]
        print("HYPO SHAPE", hypo.shape)
        for index, (img, expect_label) in enumerate(zip(batch[0], batch[1])):
            cv2.imshow("image-in", img)
            cv2.imshow("hypo", ObjClass.combine_label(feed_result[index]))
            # print(feed_result[index])
            cv2.imshow("expect", ObjClass.combine_label(expect_label))
            cv2.waitKey(0)
def main():
    """Entry point function"""
    adder = tf.Variable(10.)
    adder2 = tf.Variable(7.)

    nn1 = tfh.NeuralNetwork(
        name="Inner_NN",
        layers=[
            tfh.ValidationLayer(name="Input_Validation", shape=[None], dtype=tf.float32),
            tfh.OpLayer(lambda input: input + adder, [adder]),
            tfh.OpLayer(tf.nn.relu),
            tfh.ValidationLayer(shape=[None], dtype=tf.float32)])
    nn2 = tfh.NeuralNetwork(
        name="Inner2_NN",
        layers=[
            tfh.ValidationLayer(name="Input_Validation", shape=[None], dtype=tf.float32),
            tfh.OpLayer(lambda input: input + adder2, [adder2]),
            tfh.ValidationLayer(shape=[None], dtype=tf.float32)])

    life = tfh.Life(
        tfh.NeuralNetwork(
            name="Main_NN",
            layers=[nn1, nn2]),
        optimizer=tf.train.AdamOptimizer(0.3))

    input_x = np.array([
        0, 1, 2, 3, 4
    ], dtype=np.float32)

    expect_y = np.array([
        2, 3, 4, 5, 6
    ], dtype=np.float32)

    life.connect_neural_network(sample_input=input_x, sample_output=expect_y, will_train=True)

    life.init_var()
    # life.init_network([nn1, nn2])
    # life.load_saved_model("test.ckpt", nn2)

    hypo = life.feed(input_x)

    print(hypo)

    life.save_current_model("test.ckpt", nn1)
Esempio n. 7
0
def main():
    """Entry point function"""
    life = tfh.Life(
        tfh.NeuralNetwork(
            layers=[
                tfh.ValidationLayer(shape=[None, 64 + 4, 80 + 4, 3], dtype=tf.uint8),
                tfh.OpLayer(tf.to_float),
                tfh.OpLayer(lambda x: x/255.),
                tfh.ConvLayer(kernel_width=3, depth_out=30, depth_in=3, padding=False),
                tfh.ConvLayer(kernel_width=3, depth_out=9, padding=False),
                tfh.ValidationLayer(shape=[None, 64, 80, 9], dtype=tf.float32),
            ]
        )
    )

    data = Data.DataFeeder("data/",
                           filename="TrainCache", data_padding=2,
                           label_height=64, label_width=80)

    batch = data.get_batch(100)

    print(batch[0].dtype, batch[0].shape)
    print(batch[1].dtype, batch[1].shape)

    life.connect_neural_network(sample_input=batch[0], sample_output=batch[1], will_train=True)

    life.load_saved_model("model1")
    # life.init_var()

    for counter in range(500):

        batch = data.get_batch(100)
        result = life.train(input_layer_value=batch[0], output_layer_value=batch[1])

        if counter%27 == 0:
            print("{} iteration".format(counter), result)
            data_in = batch[0]
            feed_result = life.feed(input_layer_value=data_in)
            hypo = feed_result[0]
            # print("HYPO SHAPE", hypo.shape)
            expect = batch[1][0]
            cv2.imshow("image-in", data_in[0])
            cv2.imshow("hypo",  ObjClass.combine_label(hypo))
            cv2.imshow("expect", ObjClass.combine_label(expect))
            cv2.waitKey(20)

    life.save_current_model("model1")
Esempio n. 8
0
def main():
    """Entry point function"""
    life = tfh.Life(
        tfh.NeuralNetwork(
            layers=[
                tfh.ValidationLayer(shape=[None, 512, 640, 3], dtype=tf.uint8),
                tfh.OpLayer(tf.to_float),
                tfh.OpLayer(lambda x: x/255.),
                tfh.ConvLayer(kernel_width=3, depth_out=30, depth_in=3),
                tfh.ConvLayer(kernel_width=3, depth_out=9, ),
                tfh.ValidationLayer(shape=[None, 512, 640, 9], dtype=tf.float32),
            ]
        )
    )

    data = Data.DataFeeder("data/",
                           filename="FeedCache", data_padding=0,
                           label_height=512, label_width=640)

    batch = data.get_batch(5)

    life.connect_neural_network(sample_input=batch[0], will_train=False)

    life.load_saved_model("model1")
    # life.init_var()

    print(batch[0].dtype, batch[0].shape)
    print(batch[1].dtype, batch[1].shape)

    ##
    data_in = batch[0]
    feed_result = life.feed(input_layer_value=data_in)
    hypo = feed_result[0]
    print("HYPO SHAPE", hypo.shape)

    for _ in range(2):
        for index, (img, expect_label) in enumerate(zip(batch[0], batch[1])):
            cv2.imshow("image-in", img)
            cv2.imshow("hypo", ObjClass.combine_label(feed_result[index]))
            cv2.imshow("expect", ObjClass.combine_label(expect_label))
            cv2.waitKey(0)
Esempio n. 9
0
def main():
    """Entry point function"""

    model_name = "model6-a"
    padding_size = 5
    train_data_height = 160
    train_data_width = 160
    batch_size = 15
    total_number_of_iteration = 3000

    life = tfh.Life(
        tfh.NeuralNetwork(layers=[
            tfh.ValidationLayer(shape=[
                None, train_data_height + padding_size * 2, train_data_width +
                padding_size * 2, 3
            ],
                                dtype=tf.uint8),
            tfh.OpLayer(tf.to_float),
            tfh.OpLayer(lambda x: x / 255. - 0.5),
            tfh.ConvLayer(
                kernel_width=3, depth_out=10, depth_in=3, padding=False),
            tfh.OpLayer(half_sigmoid),
            tfh.ConvLayer(kernel_width=3, depth_out=10, padding=False),
            tfh.OpLayer(half_sigmoid),
            tfh.ConvLayer(kernel_width=3, depth_out=20, padding=False),
            tfh.OpLayer(half_sigmoid),
            tfh.ConvLayer(kernel_width=3, depth_out=10, padding=False),
            tfh.OpLayer(half_sigmoid),
            tfh.ConvLayer(kernel_width=3, depth_out=2, padding=False),
            tfh.ValidationLayer(
                shape=[None, train_data_height, train_data_width, 2],
                dtype=tf.float32),
            # tfh.ReshapeLayer(shape=[None, 2]),
        ]))

    data = Data.DataFeeder("data/",
                           dynamic_load=True,
                           raw_preprocess=raw_preprocess,
                           cache_name=model_name + "-TrainCache",
                           data_padding=padding_size,
                           label_height=train_data_height,
                           label_width=train_data_width)

    batch = data.get_batch(batch_size, shuffle=True, slient=False)

    print(batch[0].dtype, batch[0].shape)
    print(batch[1].dtype, batch[1].shape)

    print(np.amax(batch[0], axis=(0, 1, 2)), np.amax(batch[1], axis=(0, 1, 2)))

    life.connect_neural_network(sample_input=batch[0])

    tf_flatten_label = tf.placeholder(dtype=tf.float32, shape=[None, 2])

    tf_flatten_result = tf.reshape(life.tfvResult_pipe, shape=[-1, 2])

    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(tf_flatten_result,
                                                tf_flatten_label))

    trainer = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)

    # life.load_saved_model("auto-save/"+model_name+"-save-data3.0")
    life.session.run(tf.initialize_all_variables())
    # life.load_saved_model(model_name+"-save-data")
    # life.init_var()

    for counter in range(total_number_of_iteration):
        batch = data.get_batch(batch_size, shuffle=True)
        preview = counter % 20 == 0
        process_list = [cross_entropy, trainer, life.tfvResult_pipe
                        ] if preview else [cross_entropy, trainer]
        result = life.feed(
            batch[0],
            process_list=process_list,
            feed_dict={tf_flatten_label: batch[1].reshape((-1, 2))})
        print(counter, "loss", result[0])
        if preview:
            # print(result[2][0].sum())
            # print(result[2][0])
            # for index in range(batch_size):
            for index in range(2):
                cv2.imshow("image-in", batch[0][index])
                cv2.imshow("hypo", ObjClass.combine_label(result[2][index]))
                cv2.imshow("expecr", ObjClass.combine_label(batch[1][index]))
                cv2.waitKey(30)
        if counter % 100 == 0:
            life.save_current_model("auto-save/" + model_name + "-save-data" +
                                    str(counter / 100))

    #for counter in range(100):

    #    batch = data.get_batch(60, shuffle=True)
    #    result = life.train(input_layer_value=batch[0], output_layer_value=batch[1])

    #    if counter%27 == 0:
    #        print("{} iteration".format(counter), result)
    #        data_in = batch[0]
    #        feed_result = life.feed(input_layer_value=data_in)
    #        hypo = feed_result[0]
    #        # print("HYPO SHAPE", hypo.shape)
    #        expect = batch[1][0]

    #    if counter%100 == 0:
    #        life.save_current_model("auto-save/"+model_name+"-save-data"+str(counter/100))

    life.save_current_model(model_name + "-save-data")