[英]Label an image with a selftrained model in 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數據集對其進行了訓練,並保存了模型。 現在,我想用此模型標記一個新圖像。 我當前的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.
非常感謝你!
Tensorflow引發錯誤,因為將Tensor / op傳遞到feed_dict
。 如果您print images
您會發現看不到一個numpy數組,而是一個張量,通常在會話運行時才計算這些張量。 您需要知道傳遞給feed_dict
,例如錯誤所提到的“ Python標量,字符串,列表或numpy ndarrays”,在您的情況下將是numpy ndarray。
與其使用tensorflow讀取圖像並對其重塑, imread
嘗試使用scipy,matplotlib或opencv中的imread
函數,然后使用numpy進行重塑。
像這樣解決它:
checkpoint_file = tf.train.latest_checkpoint("saved_models")
imgSize = 48
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]
img = imread(imagepath, flatten=True)
img = imresize(img, [imgSize, imgSize])
img = img.astype('float32')
img_mean = np.mean(img)
img_var = np.std(img)
img = (img - img_mean)/img_var
#img = (48, 48)
img = np.expand_dims(img, axis=2)
#img = (48, 48, 1)
img = np.expand_dims(img, axis=0)
#img = (1, 48, 48, 1)
#inp expects (?, 48, 48, 1)
res = sess.run(prediction,feed_dict={inp:img})
print(res)
print(res.argmax(axis=1))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.