[英]Tensorflow Image Generator passing Tensor with dtype=string instead of Tensor with dtype=float32 to loss function
[英]How to create tensor with shape(?,) and dtype=string from image with tensorflow in python
我有一个训练有素的模型,输入层指定如下:
Model input: [<tf.Tensor 'encoded_image_string_tensor:0' shape=(None,) dtype=string>, <tf.Tensor 'key:0' shape=(None,) dtype=string>]
我在创建具有这些属性的张量时遇到问题。 要么我得到正确的 dtype,然后我得到shape() 。 或者我得到一个非空的形状,但dtype=uint8或类似的。 关于如何阅读、创建和输入我的图像到正确格式的任何提示。 我要输入的图像是grayscale 、 jpg 、 3232x583像素。
你可以做
import tensorflow as tf
a = tf.placeholder(dtype=tf.string, shape=[None, ], name="encoder_image_string_tensor")
print(a)
哪个打印
Tensor("encoder_image_string_tensor:0", shape=(?,), dtype=string)
为了给这个张量提供值,你可以在这个函数中使用sess.run
和feed_dict
参数。
要获得正确尺寸的图像,您可以执行以下操作:
import cv2
im = cv2.imread("abc.jpg")
my_img = np.squeeze(np.reshape(im, [-1, 1]))
sess.run([], feed_dict={a: my_img})
我从这里获取我的答案。
我能够在这个 anwser(链接)的帮助下解决它,我用 OpenCV 将它作为图像读取。 将其编码为jpg。 将其转换为字节数组。 并使用 tf.constant() 使其成为 tensorflow。 感觉从文件到字节数组的转换可以以更好的方式进行,但我暂时保留它。
代码:
img = cv2.imread('IMAGEPATH')
flag, bts = cv2.imencode('.jpg', img)
byte_arr = [bts[:,0].tobytes()]
tensor_string = tf.constant(byte_arr)
给出了这个有效的:
张量(“Const_14:0”,形状=(1,),dtype=string)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.