'cannot compute Pack as input #1(zero-based) was expected to be a float tensor but is a int32 tensor [Op:Pack] name: packed'。 tf.squeeze 错误

'cannot compute Pack as input #1(zero-based) was expected to be a float tensor but is a int32 tensor [Op:Pack] name: packed'. Error with tf.squeeze

我正在尝试在图表上显示数据集的图像及其预测。但是我有这个错误:cannot compute Pack as input #1(zero-based) was expected to be a float tensor but is a int32 tensor [Op:Pack] name: packed

这是我绘制的代码:

for images in val_ds.take(1):
    tf.squeeze(images, [0])
    for i in range(18):
        ax = plt.subplot(6, 6, i + 1)
        plt.imshow(images[i].numpy().astype("uint8"))
        #plt.title(predictions[i])
        plt.axis("off")

我在 tf.squeeze 函数的第二行有错误。我想删除图像形状的第一维(形状是 (18, 360, 360, 3),我想要 (360, 360, 3))。

您忘记在循环中引用标签。尝试这样的事情:

import tensorflow as tf
import pathlib
import matplotlib.pyplot as plt

dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)

batch_size = 18

val_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  seed=123,
  image_size=(360, 360),
  batch_size=batch_size)

for images, _ in val_ds.take(1):
  for i in range(18):
    ax = plt.subplot(6, 6, i + 1)
    plt.imshow(images[i].numpy().astype("uint8"))
    plt.axis("off")