简体   繁体   English

如何在python中使用张量流从图像创建形状(?,)和dtype = string的张量

[英]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.我要输入的图像是grayscalejpg3232x583像素。

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.runfeed_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 am sourcing my answer from here .我从这里获取我的答案。

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.

相关问题 Tensorflow 图像生成器通过 dtype=string 的张量而不是 dtype=float32 的张量来丢失 function - Tensorflow Image Generator passing Tensor with dtype=string instead of Tensor with dtype=float32 to loss function 如何创建给定dtype和形状的任意theano张量? - How do I create an arbitrary theano tensor given a dtype and a shape? Tensorflow:TypeError:预期的二进制或Unicode字符串,得到了 <tf.Tensor 'Placeholder:0' shape=<unknown> dtype = string&gt; - Tensorflow: TypeError: Expected binary or unicode string, got <tf.Tensor 'Placeholder:0' shape=<unknown> dtype=string> 张量流。 张量(“ Mul:0”,shape =(),dtype = int32) - tensorflow . Tensor(“Mul:0”, shape=(), dtype=int32) Tensor(..., shape=(), dtype=int64) 必须与 Tensor(..., shape=(), dtype=resource) Keras 来自同一个图 - Tensor(…, shape=(), dtype=int64) must be from the same graph as Tensor(…, shape=(), dtype=resource) Keras tensorflow:如何创建与占位符形状相同的常量张量 - tensorflow: how create an const tensor the same shape as a placeholder 如何使用 C++ Tensorflow API 创建具有给定形状的随机张量? - How to create a random tensor with a given shape with the C++ Tensorflow API? 从python列表创建动态形状的张量以馈送tensorflow RNN - Creating tensor of dynamic shape from python lists to feed tensorflow RNN python,tensorflow,如何获得具有一半特征的张量形状 - python, tensorflow, how to get a tensor shape with half the features KeyError:tf.Tensor&#39;Placeholder_6:0&#39;shape = <unknown> dtype =字符串 - KeyError: tf.Tensor 'Placeholder_6:0' shape=<unknown> dtype=string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM