我訓練了一個影像分類器。現在我想使用下面的代碼提供一些影像以獲得一些預測。
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]。我認為我的影像需要轉換為標量形式,但我不知道該怎么做。
uj5u.com熱心網友回復:
的輸入型別tf.io.read_file必須是字串。它不能是一個串列[im]。嘗試:
import tensorflow as tf
imagesss = '/content/result_image.png'
imgr = tf.io.read_file(imagesss)
如果您有多個影像,則可以使用回圈遍歷所有影像。有關更多資訊,請參閱檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/524099.html
