Search code examples
pythontensorflowmatplotlibmachine-learningtensorflow-datasets

'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


I'm trying to display images of a dataset on a plot with their predictions. But I have this error: cannot compute Pack as input #1(zero-based) was expected to be a float tensor but is a int32 tensor [Op:Pack] name: packed

This is the code in which I plot:

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")

I have the error on second line, on the tf.squeeze function. I want to remove first dimension of images shape (shape is (18, 360, 360, 3) and I want (360, 360, 3)).


Solution

  • You are forgetting to reference your labels in your loop. Try something like this:

    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")
    

    enter image description here