-1

First, I tried to restore the model as people instructed, but I could not find any clues yet. Following is my code to save the model and model was successfully saved.

import tensorflow as tf
from sklearn.utils import shuffle
EPOCHS = 10
BATCH_SIZE = 128

x = tf.placeholder(tf.float32, (None, 32, 32, 3),name='x')
y = tf.placeholder(tf.int32, (None),name='y')
one_hot_y = tf.one_hot(y, 43)

rate = 0.001

logits = LeNet(x)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=one_hot_y, logits=logits)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate = rate)
training_operation = optimizer.minimize(loss_operation)

correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_y, 1))
accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver()

def evaluate(TrainX, trainLabels):
    num_examples = len(TrainX)
    total_accuracy = 0
    sess = tf.get_default_session()
    for offset in range(0, num_examples, BATCH_SIZE):
        batch_x, batch_y = TrainX[offset:offset+BATCH_SIZE], trainLabels[offset:offset+BATCH_SIZE]
        accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y})
        total_accuracy += (accuracy * len(batch_x))
    return total_accuracy / num_examples

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    num_examples = len(trainImages)

    print("Training...")
    print()
    for i in range(EPOCHS):
        TrainX, trainLabels = shuffle(TrainX, trainLabels)
        for offset in range(0, num_examples, BATCH_SIZE):
            end = offset + BATCH_SIZE
            batch_x, batch_y = TrainX[offset:end], trainLabels[offset:end]
            sess.run(training_operation, feed_dict={x: batch_x, y: batch_y})

        validation_accuracy = evaluate(TrainX, trainLabels)
        print("EPOCH {} ...".format(i+1))
        print("Validation Accuracy = {:.3f}".format(validation_accuracy))
        print()

    saver.save(sess, './lenet')
    print("Model saved")

Training...

EPOCH 1 ... Validation Accuracy = 0.765

EPOCH 2 ... Validation Accuracy = 0.911

EPOCH 3 ... Validation Accuracy = 0.933

EPOCH 4 ... Validation Accuracy = 0.958

EPOCH 5 ... Validation Accuracy = 0.965

EPOCH 6 ... Validation Accuracy = 0.973

EPOCH 7 ... Validation Accuracy = 0.978

EPOCH 8 ... Validation Accuracy = 0.986

EPOCH 9 ... Validation Accuracy = 0.985

EPOCH 10 ... Validation Accuracy = 0.980

Model saved

My question is how I can use this model when I put new test data supposing that I put 5 test data to see how accurate they are when passing through in the trained model. I'd like to see the accuracy of test data and labels which will correctly fit in the trained model. Thank you for your time and I am willing to give you more details if you have things you do not understand.

  • You can check this question: https://stackoverflow.com/questions/33759623/tensorflow-how-to-save-restore-a-model. They have explained this clearly. – qinlong Apr 08 '19 at 21:42

1 Answers1

0

In short, I suggest using tf.data and tf.saved_model APIs. There are 2 mechanisms: tf.train.Saver() or a higher level API tf.saved_model based on the previous one. Differences you can find in other posts link1 link2. I tried to write pseudo code of the later one for you but I came up finally with all following snippets. Hope it helps:

Training part:

# create list of file names: ['0.csv', '1.csv', ...]
totrain_features = [os.path.join('./TrainX/', f) for f in os.listdir('./TrainX/') if f.endswith('.csv')]
totrain_img = [os.path.join('./TrainLabels/', f) for f in os.listdir('./TrainLabels/') if f.endswith('.csv')]
epoch_length = len(totrain_features)

# model starts here
file_names_ph = tf.placeholder(tf.string, shape=(None), name='file_name_ph') #create a ph to put list of file paths
in_pipeline = input_pipeline(file_names_ph) # check standalone code below
nodes = model(in_pipeline['img_next_op'], in_pipeline['label_next_op'])


with tf.Session() as sess:
    sess.run([tf.global_variables_initializer(), in_pipeline['iter_init_op']], feed_dict={file_names_ph: totrain_files})
    for step in tqdm(range(epoch_length)):
        # run train_op
        _ = sess.run(nodes['train_op'])
        # use saver to save weights
        if step % epoch_length == epoch_length - 1: #save last step
            # prepare args for simple_save
            in_dict = {
                'file_names': file_names_ph,
            }
            out_dict = {
                'predict': nodes['predict_op'],
                'diff_op': nodes['diff_op']
            }
            tf.saved_model.simple_save(sess, './savedmodel', in_dict, out_dict) # This is what you need, the pb file of the graph and variables are saved in savedmodel folder

Predict part:

# input pipeline for predict
# create list of file names: ['0.csv', '1.csv', ...]
topredict_files = [os.path.join('./predict/', f) for f in os.listdir('./predict/') if f.endswith('.csv')]
epoch_length = len(topredict_files)

# save prediction images to /results folder
if not os.path.exists('./results'):
    os.makedirs('./results')

# reset another graph as the default graph
graph2 = tf.Graph()
with graph2.as_default():
    with tf.Session() as sess:
        tf.saved_model.loader.load(
            sess,
            [tf.saved_model.tag_constants.SERVING], './savedmodel'
        ) # here's what you need
        # get operation and so on
        file_names_ph = graph2.get_tensor_by_name('file_name_ph:0')
        predict_tensor = graph2.get_tensor_by_name('Conv1/prediction:0')
        diff_tensor = graph2.get_tensor_by_name('Operations/difference:0')
        iter_init_op = graph2.get_operation_by_name('iter_init_op')

        sess.run(iter_init_op, feed_dict={file_names_ph: topredict_files})
        for step in tqdm(range(epoch_length)):
            predict, difference = sess.run([predict_tensor, diff_tensor])
            # then save your prediction and comparison
            ...

Take a look at how I define the pipeline and model:

def input_pipeline(file_names_ph):
    # create new dataset for predict
    dataset = tf.data.Dataset.from_tensor_slices(file_names_ph)

    # apply list of file names to the py function wrapper for reading files
    dataset = dataset.map(_pyfn_wrapper, num_parallel_calls=mp.cpu_count()) # use the tf built-in csv reader or take a look how to use py_func:https://stackoverflow.com/questions/55363728/how-to-feed-h5-files-in-tf-data-pipeline-in-tensorflow-model

    # construct batch size
    dataset = dataset.batch(1).prefetch(mp.cpu_count())

    # initialize iterator
    iterator = tf.data.Iterator.from_structure(dataset.output_types, dataset.output_shapes)
    iterator_initialize_op = iterator.make_initializer(dataset, name='iter_init_op')

    # get image and labels
    image_getnext_op, label_getnext_op = iterator.get_next()
    return {'img_next_op': image_getnext_op, 'label_next_op': label_getnext_op, 'iter_init_op': iterator_initialize_op}


def model(in_ds, out_ds):
    # a simple model
    with tf.name_scope("Conv1"):
        W = tf.get_variable("W", shape=[3, 3, 1, 1],
                             initializer=tf.contrib.layers.xavier_initializer())
        b = tf.get_variable("b", shape=[1], initializer=tf.contrib.layers.xavier_initializer())
        layer1 = tf.nn.conv2d(in_ds, W, strides=[1, 1, 1, 1], padding='SAME') + b
        prediction = tf.nn.relu(layer1, name='prediction')

    with tf.name_scope("Operations"):
        global_step = tf.Variable(0, name='global_step', trainable=False)
        loss = tf.reduce_mean(tf.losses.mean_squared_error(labels=out_ds, predictions=prediction), name='loss')
        train_op = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss, name='train_op', global_step=global_step)
        difference_op = tf.cast(tf.equal(prediction, out_ds), dtype=tf.int32, name='difference')
    # I really like dictionary, it's easy to handle
    return {'global_step': global_step, 'loss': loss, 'train_op': train_op, 'diff_op': difference_op, 'predict_op': prediction}
Zézouille
  • 503
  • 6
  • 21
  • Hi, I tried your code but it says `name 'accuracy_operation' is not defined` that means I have to implement that code again?? – GideokSeong Apr 08 '19 at 21:58
  • I am wondering that I have to implement all codes in the main function to use saved model such as accuracy_operation or correct_prediction.. – GideokSeong Apr 08 '19 at 21:59