[Python] How to use the saved model in tensorflow [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 5, 2025.

  1. Stack

    Stack Membro Participativo

    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:eek:ffset+BATCH_SIZE], trainLabels[offset:eek:ffset+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.

    Continue reading...

Compartilhe esta Página