我整天都在阅读帖子,试图找出如何从已保存的Tensorflow模型中进行预测,但是我无法在模型中使用任何示例。
我的模型调用了返回预测(输出)和损失的卷积神经网络:
`def my_training_task4(X_train, y_train, X_val, y_val...)
N, height, width, channels = X_train.shape
with tf.name_scope('inputs'):
xs = tf.placeholder(shape=[None, height, width, channels], dtype=tf.float32, name='xs')
ys = tf.placeholder(shape=[None, ], dtype=tf.int64, name='ys')
is_training = tf.placeholder(tf.bool, name='is_training')
decay_tf = tf.placeholder(tf.float32, name='accum_lr_decay')
#This is where I call the Convolution Network:
output, loss = my_LeNet(xs, ys, is_training, etc...)
iters = int(N / batch_size)
#Here I call a Tensorflow Optimizer:
step = train_step(loss, learning_rate*decay_tf, optimizer)
#This just computes the error of the output:
eve = evaluate(output, ys)
iter_total = 0
epc = 0
best_acc = 0
cur_model_name = 'lenet_{}'.format(int(time.time()))
with tf.Session() as sess:
merge = tf.summary.merge_all()
writer = tf.summary.FileWriter("log/{}".format(cur_model_name), sess.graph)
saver = tf.train.Saver()
extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
sess.run([tf.global_variables_initializer()])
decay = 1
generator = ImageGenerator(X_train, y_train)
for training_batch_x, training_batch_y in generator.next_batch_gen(batch_size, shuffle=True):
if iter_total % iters == 0:
print('epoch {}: new learning rate = {}'.format(epc+1, learning_rate*decay))
epc += 1
decay *= learning_decay
output, cur_loss, update = sess.run([step, loss, extra_update_ops], feed_dict={xs: training_batch_x,
ys: training_batch_y,
is_training: True,
decay_tf: decay})
if iter_total % 100 == 0:
# do validation
valid_eve, merge_result, update = sess.run([eve, merge, extra_update_ops], feed_dict={xs: X_val,
ys: y_val,
is_training: False})
# when achieve the best validation accuracy, we store the model parameters (here we save the model):
if valid_acc > best_acc:
best_acc = valid_acc
saver.save(sess, 'model/{}'.format(cur_model_name))
#save_path = saver.save(sess, 'model/{}.ckpt'.format(cur_model_name))
#print("Model saved in path: %s" % save_path)
#decay *= learning_decay
iter_total += 1
if iter_total == iters * epoch:
break
`
现在我想做的就是上传模型,并使用它对测试数据进行预测,但是我尝试了很多方法,但它们不起作用。我最近的人是这样的:
imported_meta = tf.train.import_meta_graph("model/lenet_1540941414.meta")
xs = tf.placeholder(shape=X_test.shape, dtype=tf.float32, name='xs')
is_training = tf.placeholder(tf.bool, name='is_training')
sess = tf.Session()
imported_meta.restore(sess, "model/lenet_1540941414")
P = sess.run([output], feed_dict={"xs:0": X_test, "is_training:0": False})
但是我得到一个错误,说xs不属于图形。
任何帮助将不胜感激!