Python ValueError:形状(无,无)和(无,无,无,43)不兼容

Python ValueError:形状(无,无)和(无,无,无,43)不兼容,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,我知道这个论坛上已经有类似的帖子了,但尽管我查看了它们,但似乎找不到解决方案 我尝试使用VGG模型对图像进行多分类。我正在学习一本书中的教程。我使用VGG模型的最后一层作为最后一个后续层的输入 我的图像存储在“train”文件夹中,在该文件夹中有43个子文件夹,其中包含属于43个类的图像。每个子文件夹的名称是一个从0到42的数字 我使用flow\u from\u directory()函数加载图像,然后最后使用fit\u generator() 我的模型中的最后一层是密集层model.add(密

我知道这个论坛上已经有类似的帖子了,但尽管我查看了它们,但似乎找不到解决方案

我尝试使用VGG模型对图像进行多分类。我正在学习一本书中的教程。我使用VGG模型的最后一层作为最后一个后续层的输入

我的图像存储在“train”文件夹中,在该文件夹中有43个子文件夹,其中包含属于43个类的图像。每个子文件夹的名称是一个从0到42的数字

我使用
flow\u from\u directory()
函数加载图像,然后最后使用
fit\u generator()

我的模型中的最后一层是密集层
model.add(密集层(43,activation='softmax'))

这是我的代码:

input_shape1 = (224, 224, 3)
vgg = vgg16.VGG16(include_top=False, weights='imagenet', input_shape=input_shape1)
output = vgg.layers[-2].output
output = keras.layers.Flatten()(output)
vgg_model = Model(vgg.input, output)
vgg_model.trainable = False
for layer in vgg_model.layers:
  layer.trainable = False
  vgg_model.summary()
当我尝试使用这一行运行模型时,会出现以下错误:

epochs=100
history = model.fit_generator(train_ds, steps_per_epoch=1226, epochs=epochs, verbose=1)

WARNING:tensorflow:Model was constructed with shape (None, 100352) for input KerasTensor(type_spec=TensorSpec(shape=(None, 100352), dtype=tf.float32, name='input_4'), name='input_4', description="created by layer 'input_4'"), but it was called on an input with incompatible shape (None, None, None, None).

ValueError: Shapes (None, None) and (None, None, None, 43) are incompatible
我真的不知道它是从哪里来的。我尝试过输入形状,但没有成功

编辑

这是我的图像发生器

train_datagen = ImageDataGenerator(
        rescale=1./255,
        validation_split=0.3,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
找到了39209张属于43类的图片。但我还为此数据集指定了验证拆分

编辑2

vgg_model.output_shape[0]
100352
但添加最后一层后,模型的输出形状为43

此外,我尝试将损失函数更改为
sparse\u categorical\u crossentropy
,但出现以下错误:

InvalidArgumentError:  Matrix size-incompatible: In[0]: [720000,3], In[1]: [8192,512]
     [[node sequential_8/dense_24/Tensordot/MatMul (defined at <ipython-input-37-e259535ec653>:2) ]] [Op:__inference_train_function_4462]
InvalidArgumentError:矩阵大小不兼容:在[0]:[720000,3],在[1]:[8192512]
[node sequential_8/dense_24/Tensordot/MatMul(定义于:2)][Op:_推理_train_函数_4462]
我的模型或者加载图片的方式都有问题,但我就是不知道

我非常感谢你的帮助。
谢谢

实际上,我将图像生成器从数据帧更改为流,并且它工作正常

train_df = train_datagen.flow_from_dataframe(
  traindf,
  y_col='ClassId',
  x_col='Path',
  directory=None,
  subset='training',
  seed=123,
  target_size=(150, 150),
  batch_size=32,
  class_mode='categorical')

我使用的是
MobileNetV3
,遇到了和你一样的问题。我试过这个:

model = tf.keras.applications.MobileNetV3Large

你可以找到更多。

火车的形状是什么??我相信是39209,43,因为有39209张图片属于43类。但是我也对这个文件夹使用了30%的验证拆分。
vgg_模型的形状是什么。output_形状[1]
?vgg_模型。output_形状[0]给出了这个输出:100352,添加最后一层后模型的输出形状是43。当我改变图像的目标大小时,输出形状会改变。也许这与他们有关?这就解决了问题?虽然这个链接可以回答问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效-
train_df = train_datagen.flow_from_dataframe(
  traindf,
  y_col='ClassId',
  x_col='Path',
  directory=None,
  subset='training',
  seed=123,
  target_size=(150, 150),
  batch_size=32,
  class_mode='categorical')
model = tf.keras.applications.MobileNetV3Large