[英]How to create tensor with shape(?,) and dtype=string from image with tensorflow in python
I have a trained model with input layer specified below:我有一个训练有素的模型,输入层指定如下:
Model input: [<tf.Tensor 'encoded_image_string_tensor:0' shape=(None,) dtype=string>, <tf.Tensor 'key:0' shape=(None,) dtype=string>]
I have problem to create a tensor with these properties.我在创建具有这些属性的张量时遇到问题。 Either i get right dtype but then i get shape() .
要么我得到正确的 dtype,然后我得到shape() 。 Or i get a nonempty shape but dtype=uint8 or similar.
或者我得到一个非空的形状,但dtype=uint8或类似的。 Any tip on how to read, create and input my image to right format.
关于如何阅读、创建和输入我的图像到正确格式的任何提示。 The images that I want to input are grayscale , jpg , 3232x583 pixels.
我要输入的图像是grayscale 、 jpg 、 3232x583像素。
You can do你可以做
import tensorflow as tf
a = tf.placeholder(dtype=tf.string, shape=[None, ], name="encoder_image_string_tensor")
print(a)
which prints哪个打印
Tensor("encoder_image_string_tensor:0", shape=(?,), dtype=string)
For feeding value into this Tensor you can use sess.run
and the feed_dict
parameter inside this function.为了给这个张量提供值,你可以在这个函数中使用
sess.run
和feed_dict
参数。
To get the image in the right dimension you can do:要获得正确尺寸的图像,您可以执行以下操作:
import cv2
im = cv2.imread("abc.jpg")
my_img = np.squeeze(np.reshape(im, [-1, 1]))
sess.run([], feed_dict={a: my_img})
I was able to solve it with help of this anwser ( link ) I read it as a image with OpenCV.我能够在这个 anwser(链接)的帮助下解决它,我用 OpenCV 将它作为图像读取。 Encode it to jpg.
将其编码为jpg。 Transform it to byte array.
将其转换为字节数组。 And make it to tensorflow with tf.constant().
并使用 tf.constant() 使其成为 tensorflow。 Feels like the convertion from file to byte array could be made in a better way but i leave it with this for now.
感觉从文件到字节数组的转换可以以更好的方式进行,但我暂时保留它。
code:代码:
img = cv2.imread('IMAGEPATH')
flag, bts = cv2.imencode('.jpg', img)
byte_arr = [bts[:,0].tobytes()]
tensor_string = tf.constant(byte_arr)
gives this which works:给出了这个有效的:
Tensor("Const_14:0", shape=(1,), dtype=string)
张量(“Const_14:0”,形状=(1,),dtype=string)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.