简体   繁体   English

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

[英]ValueError: Shapes (None, None) and (None, None, None, 43) are incompatible

I know that there were similar threads on this forum already, but though I checked them out I can't seem to find a solution.我知道这个论坛上已经有类似的主题了,但是尽管我检查了它们,但似乎找不到解决方案。

I'm trying to use a VGG model for multi-classification of images.我正在尝试使用 VGG model 对图像进行多分类。 I'm following a tutorial from a book.我正在按照书中的教程进行操作。 I use the last layer from the VGG model as my input for the last sequentail layers.我使用来自 VGG model 的最后一层作为最后一个 sequentail 层的输入。

My images are stored in a folder 'train', inside this folder there are 43 subfolders containing the images belonging to 43 classes.我的图像存储在“train”文件夹中,在该文件夹内有 43 个子文件夹,其中包含属于 43 个类的图像。 Each subfolder's name is a number from 0 to 42.每个子文件夹的名称是一个从 0 到 42 的数字。

I use flow_from_directory() function to load the images, and then finally fit_generator() .我使用flow_from_directory() function 加载图像,然后最后fit_generator()

The last layer in my model is a dense layer model.add(Dense(43, activation='softmax'))我的 model 中的最后一层是密集层model.add(Dense(43, activation='softmax'))

This is my code:这是我的代码:

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()
input_shape = vgg_model.output_shape[1]
model = Sequential()
model.add(InputLayer(input_shape=(input_shape,)))
model.add(Dense(512, activation='relu', input_dim=input_shape))
model.add(Dropout(0.3)) 
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(43, activation='softmax'))

model.compile(optimizer=optimizers.RMSprop(lr=1e-4), loss='categorical_crossentropy', metrics=['accuracy'])

When I try to run the model with this line, I get the error:当我尝试使用此行运行 model 时,出现错误:

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

I really have no idea where it is coming from.我真的不知道它是从哪里来的。 I tried experimenting with input shapes but with no luck.我尝试尝试输入形状,但没有运气。

EDIT编辑

This is my image generator这是我的图像生成器

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)
train_ds = train_datagen.flow_from_directory(
  train_dir,
  seed=123,
  target_size=(224, 224),
  batch_size=32,
  class_mode='categorical')

Found 39209 images belonging to 43 classes.找到属于 43 个类别的 39209 张图像。 But I also specified validation split for this dataset.但我也为这个数据集指定了验证拆分。

EDIT 2编辑 2

vgg_model.output_shape[0]
100352

The output shape of the model after adding the last layers is 43 though.在添加最后一层之后,model 的 output 形状是 43。

Also, I tried changing the loss function to sparse_categorical_crossentropy and got this error:另外,我尝试将损失 function 更改为sparse_categorical_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]

Somethig is wrong either with my model or with the way I'm loading the images, but I simply have no clue.我的 model 或我加载图像的方式有问题,但我根本不知道。

I'd really appreciate your help.我真的很感谢你的帮助。 Thanks!谢谢!

I actually changed my Image Generator to flow_from_dataframe and it worked.我实际上将我的图像生成器更改为flow_from_dataframe并且它有效。

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

I was using MobileNetV3 and got same problem just like you.我使用的是MobileNetV3 ,遇到了和你一样的问题。 I tried this:我试过这个:

model = tf.keras.applications.MobileNetV3Large

You can find more info .您可以找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM