我有一些用于 tensorflow 資料集的代碼。它以前運行良好,它可能仍然有效。但我不這么認為
img = parse_image(img_paths[0])
img = tf.image.resize(img, [224, 224])
plt.imshow(img)
只輸出一個空白224x224畫布。
img = parse_image(img_paths[0])
plt.imshow(img)
正確輸出影像。
img_paths 是帶有路徑名的字串串列
我試過了:
img = parse_image(img_paths[0])
img = tf.image.resize([img], [224, 224])
plt.imshow(img[0])
和
img = parse_image(img_paths[0])
img = tf.image.resize(img, [224, 224])
plt.imshow(img.numpy())
和
img = parse_image(img_paths[0])
img = tf.image.resize([img], [224, 224])
plt.imshow(img.numpy()[0])
形狀是正確的,這個代碼以前作業過。并且可能仍然有效,我想我可能不再正確使用它(自從我寫它已經有一段時間了)。
感謝您提供任何提示或想法?當然還有解決方案;-)
uj5u.com熱心網友回復:
“問題”出在 Matplotlib 上。當您使用 Tensorflow 調整大小時,它會將您的輸入變為浮動。Matplotlib 接受兩種影像格式,0-255 之間的整數和 0 和 1 之間的浮點數。如果呼叫plt.imshow()大于 1 的浮點數,它將裁剪所有值,您將看到一個白色影像。我懷疑這就是你得到的。
tf.image.convert_image_dtype有一個saturate引數,它的默認值使得 0-255 整數范圍變成 0-1 浮點數。這就是它“有效”的原因,因為 Matplotlib 理解該格式。在此之后,Tensorflow 調整大小操作也將其保持在 0-1 之間,因此它可以作業。
uj5u.com熱心網友回復:
呵呵,
我在別處看到了一些東西并添加了這一行:
img = tf.image.convert_image_dtype(img, tf.float32)
在調整大小之前,它起作用了。
這非常奇怪,因為我以前不需要這條線。可能是因為版本更新?
無論哪種方式都有效:
img = parse_image(train_img_paths[0])
img = tf.image.convert_image_dtype(img, tf.float32)
img = tf.image.resize(img, [224, 224])
plt.imshow(img)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/314822.html
