Exemple #1
0
def predict(model: Model, dataset: DatasetBase, restore_path: Path):
    restore_path = restore_path.expanduser().absolute()

    model.build_graph()
    saver = tf.train.Saver(save_relative_paths=True)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        saver.restore(sess, str(restore_path))

        while True:
            sess.run(dataset.test_init_op)

            try:
                pred, x, y = sess.run([model.predict(), dataset.x, dataset.y],
                                      feed_dict={tf.keras.backend.learning_phase(): 0})
                pred = np.argmax(pred)
                print(pred)
                io.imshow(x[0, :, :, 0], cmap="gray")
                io.show()

            except tf.errors.OutOfRangeError:
                continue
    parser = argparse.ArgumentParser()
    parser.add_argument('image_dir')
    parser.add_argument('model')
    args = parser.parse_args()

    model_path = args.model
    data_path = args.image_dir

    print('Loading model from ', model_path)
    model = Model().load(model_path)

    print('Model loaded')
    print(model._config)
    
    print('## Evaluating on test data##')
    prediction, labels = model.predict(data_path, return_labels=True)
    prediction_class = prediction.argmax(axis=-1)

    correct = (prediction_class == labels).sum()
    total = len(labels)

    print('Percentage correct (manual): {:.2f}, {}/{}'.format((correct / total * 100), correct, total))

    #np.save('predictions.npy', {'prediction': prediction, 'true': y_test, 'labels': labels})
    

    """
    # Legacy code

    print('Loading model from ', model_path)
    model = tf.keras.models.load_model(model_path)