一個selftrained模型,我成功地訓練我自己的模型在Tensorflow與下圖的圖像:標籤與tensorflow
在Python它看起來像:
with tf.name_scope("Reshaping_data") as scope:
x = tf.reshape(x, shape=[-1, imgSize, imgSize, 1], name="inp") #(?, 48, 48, 1)
with tf.name_scope("Conv1") as scope:
conv1 = conv2d(x, weights['wc1'], biases['bc1']) #(?, 48, 48, 32)
conv1 = maxpool2d(conv1, k=2) #(?, 24, 24, 32)
。 ..(更多卷積和完全連接)...
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'], name="out") #(?, 43)
我訓練它與GTSRB Dataset並保存模型。現在我想用這個模型標註一個新的圖像。 我現在label.py:
import tensorflow as tf
checkpoint_file = tf.train.latest_checkpoint("saved_models")
graph = tf.Graph()
with graph.as_default():
sess = tf.Session()
with sess.as_default():
saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
saver.restore(sess,checkpoint_file)
inp = graph.get_operation_by_name("Reshaping_data/inp").outputs[0]
prediction=graph.get_operation_by_name("out").outputs[0]
input_img = tf.image.decode_jpeg(tf.read_file("/home/DB/GTSRB/Test/00021/07406.jpg"), channels=3)
reshaped_image = tf.image.resize_image_with_crop_or_pad(tf.cast(input_img, tf.float32), 48, 48)
float_image = tf.image.per_image_standardization(reshaped_image)
images = tf.expand_dims(float_image, 0)
print(sess.run(prediction,feed_dict={inp:images}))
但讀feed_dict時失敗。我究竟做錯了什麼?
Traceback (most recent call last):
File "label.py", line 23, in <module>
print(sess.run(prediction,feed_dict={inp:images}))
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 767, in run
run_metadata_ptr)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 925, in _run
raise TypeError('The value of a feed cannot be a tf.Tensor object. '
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.
非常感謝!
你所得到的錯誤是足夠的描述。您無法提供tf.Tensor對象。您必須將圖像變量轉換爲其中一種有效類型,例如numpy數組。 – Kochoba