Python 检查目标时出错:将FC层转换为Conv2D

Python 检查目标时出错:将FC层转换为Conv2D,python,tensorflow,keras,deep-learning,conv-neural-network,Python,Tensorflow,Keras,Deep Learning,Conv Neural Network,我试图用卷积层替换VGG16网络末端的FC层。下面是我的代码: model2= Sequential() model2.add(Conv2D(4096, kernel_size=(8,8), activation="relu")) model2.add(Conv2D(4096, kernel_size=(1,1), activation="relu")) model2.add(Conv2D(16, kernel_size=(1,1), activation="softmax")) model

我试图用卷积层替换VGG16网络末端的FC层。下面是我的代码:

model2= Sequential()
model2.add(Conv2D(4096, kernel_size=(8,8), activation="relu"))
model2.add(Conv2D(4096, kernel_size=(1,1), activation="relu"))
model2.add(Conv2D(16, kernel_size=(1,1), activation="softmax"))

model = applications.VGG16(weights='imagenet', include_top=False, input_shape=inputshape)

F2model = Model(inputs=model.input, outputs=model2(model.output))

for layer in F2model.layers[:25]:
   layer.trainable = False

F2model.compile(optimizer=optimizers.Adam(), loss="binary_crossentropy", metrics=["accuracy"])

batch_size = 128
trainsize = 36000
validsize = 12000


F2model.fit_generator(
    train_generator,
    steps_per_epoch=trainsize // batch_size,
    epochs=5,
    validation_data=valid_generator,
    validation_steps=validsize // batch_size,callbacks=[tensorboard_callback])
我使用FC层训练常规网络,运行良好,但当我运行上述操作时,我得到以下错误:

ValueError                                Traceback (most recent call last)in <module>
  4         epochs=5,
  5         validation_data=valid_generator,
  ----> 6         validation_steps=validsize // batch_size,callbacks=[tensorboard_callback])


ValueError: Error when checking target: expected sequential_1 to have 4 dimensions, but got array with shape (32, 16)

keras的Functional API更适合解决此类问题:

model = VGG16(weights='imagenet', include_top=False, input_shape=inputshape)
x = model.output
x = Conv2D(4096, kernel_size=(8, 8), activation="relu")(x)
x = Conv2D(4096, kernel_size=(1, 1), activation="relu")(x)
out = Conv2D(16, kernel_size=(1, 1), activation="softmax")(x)

F2model = Model(inputs=model.inputs, outputs=out)


for layer in F2model.layers[:25]:
    layer.trainable = False
此外,我发现您正在将二进制交叉熵与softmax激活一起使用,这可能会导致一些问题:
-使用softmax和分类交叉熵
-使用sigmoid和binary_交叉熵

注意这个模型,使用4096的卷积将使你的参数数量变得非常拥挤

(本例为1.65亿)

编辑 看起来您的问题只来自标签阵列:

  • 您的最后一层是卷积层,因此它期望4D阵列具有形状
    (批次大小、高度、宽度、通道)
    ,但您给它的是形状
    (批次大小,16)

  • 因此,将最后一层更改为:

out=density(16,activation=“softmax”)(x)
  • 或者将标签数组更改为卷积层可以接受

发布完整错误Traceback@ShubhamShaswat完成。嘿,很抱歉回复太晚,谢谢你的意见。我运行了你的代码(使用函数API),但是我仍然得到了关于列的相同错误。你能用你的输入/标签形状plz更新你的问题吗?嗨,很抱歉回复晚了。我在致密层的正上方添加了一个扁平层,模型工作了。我真蠢,居然把最后一层做为conv层,非常感谢你。很高兴能帮到你!
train_generator=datagen.flow_from_dataframe(dataframe=traindf,directory="data_final",x_col="path",y_col="label",subset="training",batch_size=32,seed=42,shuffle=True,class_mode="categorical",target_size=(256,256))

valid_generator=datagen.flow_from_dataframe(dataframe=traindf,directory="data_final",x_col="path",y_col="label",subset="validation",batch_size=32,seed=42,shuffle=True,class_mode="categorical",target_size=(256,256))

if K.image_data_format()=="channels_first":
  inputshape=(3,imrows,imcols)
else:
  inputshape=(imrows,imcols,3)
model = VGG16(weights='imagenet', include_top=False, input_shape=inputshape)
x = model.output
x = Conv2D(4096, kernel_size=(8, 8), activation="relu")(x)
x = Conv2D(4096, kernel_size=(1, 1), activation="relu")(x)
out = Conv2D(16, kernel_size=(1, 1), activation="softmax")(x)

F2model = Model(inputs=model.inputs, outputs=out)


for layer in F2model.layers[:25]:
    layer.trainable = False