2017-03-15 77 views
0

一个selftrained模型,我成功地训练我自己的模型在Tensorflow与下图的图像:标签与tensorflow

my net

在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. 

非常感谢!

+0

你所得到的错误是足够的描述。您无法提供tf.Tensor对象。您必须将图像变量转换为其中一种有效类型,例如numpy数组。 – Kochoba

回答

1

解决这样的:

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)) 
1

由于张量传递到feed_dict,Tensorflow发生错误。如果你print images你会注意到,你没有看到一个numpy数组,而是一个张量,它通常不会在会话运行时计算出来。需要知道您传入feed_dict的任何内容,例如“错误提到的Python标量,字符串,列表或numpy ndarrays”,在你的情况下将会是一个numpy的ndarray。

而不是使用张量流来读取图像并重塑它,尝试使用从scipy,matplotlib或opencv的imread函数,然后numpy重塑。

+0

做'.eval()'或'sess.run(图像)'会评估张量。无需使用其他软件包读取图像。 – Kochoba