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

阿迪亚

我试图用卷积层替换VGG 16网络末尾的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)

在这一点上,我试图找出这些维度(32,16)的来源。任何帮助,将不胜感激。谢谢

编辑1:对于完全回溯:

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

~/anaconda3/lib/python3.7/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

~/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   1730             use_multiprocessing=use_multiprocessing,
   1731             shuffle=shuffle,
-> 1732             initial_epoch=initial_epoch)
   1733 
   1734     @interfaces.legacy_generator_methods_support

~/anaconda3/lib/python3.7/site-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
    218                                             sample_weight=sample_weight,
    219                                             class_weight=class_weight,
--> 220                                             reset_metrics=False)
    221 
    222                 outs = to_list(outs)

~/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics)
   1506             x, y,
   1507             sample_weight=sample_weight,
-> 1508             class_weight=class_weight)
   1509         if self._uses_dynamic_learning_phase():
   1510             ins = x + y + sample_weights + [1]

~/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    619                 feed_output_shapes,
    620                 check_batch_axis=False,  # Don't enforce the batch size.
--> 621                 exception_prefix='target')
    622 
    623             # Generate sample-wise weight values given the `sample_weight` and

~/anaconda3/lib/python3.7/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    133                         ': expected ' + names[i] + ' to have ' +
    134                         str(len(shape)) + ' dimensions, but got array '
--> 135                         'with shape ' + str(data_shape))
    136                 if not check_batch_axis:
    137                     data_shape = data_shape[1:]

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

编辑2:输入信息:

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)
锡伯克杆菌

keras的函数式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激活的binary_crossentropy,这可能会导致一些问题:
-使用softmax和categorical_crossentropy-
使用Sigmoid和binary_crossentropy

而且,请谨慎使用此模型,使用4096的卷积将使您的参数数量真的很烦!

(在这种情况下为1.65亿)

编辑

看来您的问题仅来自标签数组:

  • 您的最后一层是卷积层,因此它期望具有形状的4D数组,(batch_size, height, width, channel)但是您给了它一个形状数组(batch_size, 16)

  • 因此,要么将您的最后一层更改为:

out = Dense(16, activation="softmax")(x)
  • 或更改卷标层可接受的标签数组。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在 tensorflow Keras 中将图像拟合到 Conv2D 层时出错

来自分类Dev

在 Tensorflow 中查找 conv2d 层的权重

来自分类Dev

使用Conv1D层将keras代码转换为pytorch代码

来自分类Dev

model.fit提供ValueError:检查输入时出错:预期的conv2d获得了形状为()的数组

来自分类Dev

检查目标时出错:预期 conv2d_29 有 4 个维度,但得到了形状为 (1255, 12) 的数组

来自分类Dev

Keras:转置Conv2D层的内核以在另一个Conv2D层中重用

来自分类Dev

角膜或张量流中的Conv2D层之后的ConvLSTM2D

来自分类Dev

将char [2]转换为unsigned short时出错?

来自分类Dev

了解Keras Conv2D层中的参数数量

来自分类Dev

在Keras中是否可以看到Conv2D层之后的输出

来自分类Dev

具有padding ='SAME'的Tensorflow / Keras Conv2D层的行为异常

来自分类Dev

如何理解Keras Conv2D层的第一个论点?

来自分类Dev

如何在特定大小的conv2d层中使用Padding

来自分类Dev

将float转换为datetime时出错

来自分类Dev

将DICOM转换为JPEG时出错

来自分类Dev

将double转换为int时出错

来自分类Dev

将Object []转换为byte []时出错

来自分类Dev

将float转换为datatime时出错

来自分类Dev

将varchar转换为datetime时出错

来自分类Dev

将jpg转换为pdf时出错

来自分类Dev

将jpg转换为pdf时出错

来自分类Dev

将double转换为BigInteger时出错

来自分类Dev

将kml转换为geojson时出错

来自分类Dev

将int转换为NSNumber时出错

来自分类Dev

将mysql转换为mysqli时出错

来自分类Dev

将float转换为datatime时出错

来自分类Dev

将DICOM转换为JPEG时出错

来自分类Dev

将IplImage转换为Mat时出错

来自分类Dev

将varchar转换为bigint时出错

Related 相关文章

热门标签

归档