我有以下代码:

def fcn8_decoder(convs, n_classes):
  # features from the encoder stage
  f3, f4, f5 = convs

  # number of filters
  n = 512

  # add convolutional layers on top of the CNN extractor.
  o = tf.keras.layers.Conv2D(n , (7 , 7) , activation='relu' , padding='same', name="conv6", data_format=IMAGE_ORDERING)(f5)
  o = tf.keras.layers.Dropout(0.5)(o)

  o = tf.keras.layers.Conv2D(n , (1 , 1) , activation='relu' , padding='same', name="conv7", data_format=IMAGE_ORDERING)(o)
  o = tf.keras.layers.Dropout(0.5)(o)

  o = tf.keras.layers.Conv2D(n_classes,  (1, 1), activation='relu' , padding='same', data_format=IMAGE_ORDERING)(o)

    
  ### START CODE HERE ###

  # Upsample `o` above and crop any extra pixels introduced
  o = tf.keras.layers.Conv2DTranspose(n_classes , kernel_size=(4,4) ,  strides=(2,2) , use_bias=False)(o)
  o = tf.keras.layers.Cropping2D(cropping=(1,1))(o)

  # load the pool 4 prediction and do a 1x1 convolution to reshape it to the same shape of `o` above
  o2 = f4
  o2 = ( tf.keras.layers.Conv2D(n_classes , ( 1 , 1 ) , activation='relu' , padding='same', data_format=IMAGE_ORDERING))(o2)

  # add the results of the upsampling and pool 4 prediction
  o = tf.keras.layers.Add()([o, o2])

  # upsample the resulting tensor of the operation you just did
  o = (tf.keras.layers.Conv2DTranspose( n_classes , kernel_size=(4,4) ,  strides=(2,2) , use_bias=False))(o)
  o = tf.keras.layers.Cropping2D(cropping=(1, 1))(o)

  # load the pool 3 prediction and do a 1x1 convolution to reshape it to the same shape of `o` above
  o2 = f3
  o2 = tf.keras.layers.Conv2D(n_classes , ( 1 , 1 ) , activation='relu' , padding='same', data_format=IMAGE_ORDERING)(o2)

  # add the results of the upsampling and pool 3 prediction
  o = tf.keras.layers.Add()([o, o2])

  # upsample up to the size of the original image
  o = tf.keras.layers.Conv2DTranspose(n_classes , kernel_size=(8,8) ,  strides=(8,8) , use_bias=False )(o)
  o = tf.keras.layers.Cropping2D(((0, 0), (0, 96-84)))(o)

  # append a sigmoid activation
  o = (tf.keras.layers.Activation('sigmoid'))(o)
  ### END CODE HERE ###

  return o

# TEST CODE

test_convs, test_img_input = FCN8()
test_fcn8_decoder = fcn8_decoder(test_convs, 11)

print(test_fcn8_decoder.shape)

del test_convs, test_img_input, test_fcn8_decoder

您可以查看完整的代码here.

我收到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-cff468b82c6a> in <module>
      2 
      3 test_convs, test_img_input = FCN8()
----> 4 test_fcn8_decoder = fcn8_decoder(test_convs, 11)
      5 
      6 print(test_fcn8_decoder.shape)

2 frames
/usr/local/lib/python3.8/dist-packages/keras/layers/merging/base_merge.py in _compute_elemwise_op_output_shape(self, shape1, shape2)
     71       else:
     72         if i != j:
---> 73           raise ValueError(
     74               'Inputs have incompatible shapes. '
     75               f'Received shapes {shape1} and {shape2}')

ValueError: Inputs have incompatible shapes. Received shapes (4, 4, 11) and (4, 5, 11)

我到底做错了什么?

推荐答案

在判断了您的CoLab代码之后,我可以验证您的错误来自typo

在下图中,您可以看到您在FCN8()# Block 1函数中使用了img_input而不是x,这基本上意味着您将输入层传递给conv块,而不是传递填充零的层,这将导致错误.

要解决该错误,您必须在# Block 1内将img_input替换为x

enter image description here

Python相关问答推荐

python sklearn ValueError:使用序列设置数组元素

为什么在FastAPI中创建与数据库的连接时需要使用生成器?

ModuleNotFoundError:没有模块名为x时try 运行我的代码''

以异步方式填充Pandas 数据帧

从嵌套极轴列的列表中删除元素

数据框,如果值在范围内,则获取范围和

多个矩阵的张量积

文本溢出了Kivy的视区

Pandas:使列中的列表大小与另一列中的列表大小相同

大Pandas 每月重新抽样200万只和300万只

S最大值除以最小值,然后减1的结果是什么?

设计添加和搜索词的数据 struct :Leetcode211

元组列表:如果第一个元组元素匹配,则合并两个数据帧列

为什么我在Django中收到错误&无法解包不可迭代的bool对象?

新进程不会在运行FastApi的Docker中启动

如何reshape 极地数据帧?

如何在Pandas 分组处理中执行多个功能

窗口必须是整数0或更大,&q;与';30D';样式滚动计算有关

匹配+字符串的Python RegEx,直到找到下一个匹配项

如何在多维情况下检验NumPy数组切片是否为副本?