gpt4 book ai didi

python - 在 tensorflow 中使用自训练模型标记图像

转载 作者:行者123 更新时间:2023-11-30 09:35:59 24 4
gpt4 key购买 nike

我成功地在 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.

非常感谢!

最佳答案

Tensorflow 抛出错误,因为 Tensor/op 被传递到 feed_dict 中。如果您打印图像,您会注意到您看到的不是 numpy 数组,而是张量,通常直到 session 运行时才会计算张量。您传递到 feed_dict 中的任何内容都需要知道,例如错误中提到的“Python 标量、字符串、列表或 numpy ndarrays”,在您的情况下将是 numpy ndarray。

不要使用tensorflow读取图像并 reshape 它,而是尝试使用scipy、matplotlib或opencv中的imread函数,然后使用numpy来 reshape 。

关于python - 在 tensorflow 中使用自训练模型标记图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42814341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com