Пример #1
0
def init():
    checkpoint_manager = CheckpointManager(NETWORK_NUMBER)

    init_g = tf.global_variables_initializer()
    init_l = tf.local_variables_initializer()
    with tf.Session() as ses:
        ses.run(init_g)
        ses.run(init_l)
        checkpoint_manager.restore_model(ses)
        sess = ses
Пример #2
0
def test_nn(number,
            input_placeholder,
            output_placeholder,
            accuracy,
            cost,
            limit=None):
    checkpoint_manager = CheckpointManager(number)

    init_g = tf.global_variables_initializer()
    init_l = tf.local_variables_initializer()
    with tf.Session() as sess:
        sess.run(init_g)
        sess.run(init_l)
        checkpoint_manager.restore_model(sess)

        counter = 0
        total_accuracy = 0
        total_cost = 0
        for test_images, test_labels in ds.test_batch_generator(BATCH_SIZE):

            feed = {
                input_placeholder: test_images,
                output_placeholder: test_labels
            }

            test_accuracy, test_cost = sess.run([accuracy, cost],
                                                feed_dict=feed)
            print("Batch {:3}, Accuracy: {:3.1%}, Cost: {}" \
                  .format(counter, test_accuracy, test_cost))

            total_accuracy += test_accuracy
            total_cost += test_cost
            counter += 1

        overall_accuracy = total_accuracy / counter
        overall_cost = total_cost / counter

        print("Total test accuracy: {:5.1%}".format(overall_accuracy))

        return overall_accuracy, overall_cost
Пример #3
0
def test_nn(number, input_placeholder, output_placeholder, accuracy, cost):
    checkpoint_manager = CheckpointManager(number)


    init_g = tf.global_variables_initializer()
    init_l = tf.local_variables_initializer()
    with tf.Session() as sess:
        sess.run(init_g)
        sess.run(init_l)
        checkpoint_manager.restore_model(sess)

        total_accuracy = 0
        total_cost = 0
        batches = None
        for batch_index, test_images, test_labels in ds.test_batch_generator(100, grayscale=True):

            feed = {
                input_placeholder: test_images,
                output_placeholder: test_labels
            }

            test_accuracy, test_cost = sess.run(
                [accuracy, cost], feed_dict=feed)
            print("Batch {:3}, Accuracy: {:3.1%}, Cost: {}" \
                  .format(batch_index, test_accuracy, test_cost))

            total_accuracy += test_accuracy
            total_cost += test_cost
            batches = batch_index

        overall_accuracy = total_accuracy / (batches + 1)
        overall_cost = total_cost / (batches + 1)

        print("Total test accuracy: {:5.1%}".format(overall_accuracy))

        return overall_accuracy, overall_cost
def main():
    tf.reset_default_graph()

    NETWORK_NUMBER = 4

    input_placeholder = tf.placeholder(
        tf.float32, shape=[None, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNEL], name='input_placeholder')

    output_placeholder = tf.placeholder(
        tf.float32, shape=[None, 1], name='output_placeholder')

    layer_conv_1, weights_conv_1 = new_conv_layer(
        input=input_placeholder,
        num_input_channels=IMAGE_CHANNEL,
        filter_size=5,
        num_filters=64,
        pooling=2
    )

    layer_conv_2, weights_conv_2 = new_conv_layer(
        input=layer_conv_1,
        num_input_channels=64,
        filter_size=3,
        num_filters=128,
        pooling=2
    )

    layer_conv_3, weights_conv_3 = new_conv_layer(
        input=layer_conv_2,
        num_input_channels=128,
        filter_size=3,
        num_filters=128,
        pooling=None
    )

    layer_conv_4, weights_conv_4 = new_conv_layer(
        input=layer_conv_3,
        num_input_channels=128,
        filter_size=3,
        num_filters=128,
        pooling=None
    )

    layer_conv_5, weights_conv_5 = new_conv_layer(
        input=layer_conv_4,
        num_input_channels=128,
        filter_size=3,
        num_filters=256,
        pooling=3
    )

    layer_flat, num_features = flatten_layer(layer_conv_5)

    layer_fc_1 = new_fc_layer(
        input=layer_flat, num_inputs=num_features, num_outputs=4096)

    layer_fc_1 = tf.nn.sigmoid(layer_fc_1)

    layer_fc_2 = new_fc_layer(
        input=layer_fc_1, num_inputs=4096, num_outputs=4096)

    layer_fc_2 = tf.nn.sigmoid(layer_fc_2)

    layer_output = new_fc_layer(
        input=layer_fc_2, num_inputs=4096, num_outputs=1)

    layer_output = tf.nn.sigmoid(layer_output)

    checkpoint_manager = CheckpointManager(NETWORK_NUMBER)


    init_g = tf.global_variables_initializer()
    init_l = tf.local_variables_initializer()
    with tf.Session() as sess:
        sess.run(init_g)
        sess.run(init_l)

        checkpoint_manager.restore_model(sess)

        imgs = []
        for file in os.listdir("./"):
            if file.endswith(".png"):
                path = os.path.join("./", file)
                im = Image.open(path)
                imgs.append(np.array(im).reshape(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNEL))

        print(layer_output.eval(feed_dict={input_placeholder: imgs},
                                session=sess))