InvalidArgumentError:输入文件名张量必须是标量,但形状为:[1][Op:ReadFile]



我已经训练了一个图像分类器。现在,我想使用下面的代码来提供一些图像以获得一些预测。

def fd(t_embeddings, imagesss, k=1, normalize=True):
imgr = tf.io.read_file(imagesss)
imgr = tf.expand_dims(imgr, axis=0)
i_embedding = vision_encoder(tf.image.resize(imgr, (299, 299)))
if normalize:
image_embeddings = tf.math.l2_normalize(t_embeddings, axis=1)
query_embedding = tf.math.l2_normalize(i_embedding, axis=1)
dot_similarity = tf.matmul(query_embedding, image_embeddings, transpose_b=True)
results = tf.math.top_k(dot_similarity, k).indices.numpy()
return [[df['findings'][idx] for idx in indices] for indices in results] 

获取输入图像的代码

im = "/content/1000_IM-0003-1001.dcm.png"
matches = fd(t_embeddings, 
[im], 
normalize=True)[1]
for i in range(9):
print((matches[i]))

然而,我得到了这个错误InvalidArgumentError: Input filename tensor must be scalar, but had shape: [1] [Op:ReadFile]。我认为我的图像需要转换成标量形式,但我不知道如何做到

tf.io.read_file的输入类型必须是字符串。它不能是列表[im]。尝试:

import tensorflow as tf
imagesss = '/content/result_image.png'
imgr = tf.io.read_file(imagesss)

如果你有多个图像,你可以使用一个循环来遍历它们。有关详细信息,请参阅文档。

相关内容

最新更新