簡體   English   中英

如何在python中使用張量流從圖像創建形狀(?,)和dtype = string的張量

[英]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或類似的。 關於如何閱讀、創建和輸入我的圖像到正確格式的任何提示。 我要輸入的圖像是grayscalejpg3232x583像素。

你可以做

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM