Python ds_列车的形状为(2,224,224,3)而不是(无,224,224,3)

Python ds_列车的形状为(2,224,224,3)而不是(无,224,224,3),python,image,tensorflow,matplotlib,keras,Python,Image,Tensorflow,Matplotlib,Keras,我使用以下代码创建了自己的自定义数据集(包含2个类): import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.preprocessing.image import ImageDataGenerator import matplotlib.pyplot as plt ds_train = tf.keras.preprocessin

我使用以下代码创建了自己的自定义数据集(包含2个类):

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.preprocessing.image import ImageDataGenerator

import matplotlib.pyplot as plt
ds_train = tf.keras.preprocessing.image_dataset_from_directory(
        'C:/Users/mydir/Source_Images/',
        labels = 'inferred', # from subfolders in alphabetical order
        label_mode = "int",
        class_names = ["CVS", "No_CVS"],
        color_mode = 'rgb',
        batch_size = 2,
        image_size = (224, 224),
        shuffle = True, # randomized order of images
        seed = 123, #set the seed if  train, valid images are the same when you run again   
        validation_split = 0.1,
        subset = "training"
        )
df_列车结果如下:

<BatchDataset shapes: ((None, 224, 224, 3), (None,)), types: (tf.float32, tf.int32)>
但是,我得到以下错误:

第61行,在

plt.imshow(image.numpy().astype("uint8"))
TypeError: Invalid shape (2, 224, 224, 3) for image data
我正在寻找解决这个问题的方法,并且能够使用matplotlib打印我的图像

编辑:

更重要的是,似乎在训练模型时无法使用数据的数据,因为我得到了以下错误:

   ValueError: Input 0 is incompatible with layer EfficientNet: expected shape=(None, 224, 224, 3), found shape=(2, None, 224, 224, 3)
运行我找到的Keras示例代码后(在这里,我使用
image\u dataset\u从\u目录创建了ds\u列,而不是使用
tdsf.load()
函数)


所以我认为我创造ds_火车的方式出了问题。任何解决方案都是非常受欢迎的。

当您执行以下操作时,您似乎将
批量大小留在了中:

plt.imshow(image.numpy().astype("uint8"))
使用原始代码,由于批处理大小的原因,您仍然无法看到9幅图像。我想如果你像这样做就好了:

不应抛出任何错误,如
TypeError:Invalid shape…

plt.imshow(image[i].numpy().astype("uint8"))
此外,您可以执行以下操作以查看批次大小:

for img_batch_size, labels_batch_size in train_df:
  print(img_batch_size.shape)
  print(labels_batch_size.shape)
对于您的情况,
img\u batch\u size.shape
应该打印(2224224,3),其中该元组对应于图像张量

对于
input\u-shape
问题,您需要添加您的模型,以便我们可以查看
input\u-shape
的错误

for img_batch_size, labels_batch_size in train_df:
  print(img_batch_size.shape)
  print(labels_batch_size.shape)