TensorFlow(全卷积网络)中的 FCN-8 解码器

FCN-8 decoder in TensorFlow (Fully Convolutional Network)

我正在实现 FCN-8 解码器(deeplearning.ai 使用 Tensorflow 进行深度学习的高级技术作业,计算机视觉课程,第 3 周,语义分割)

我实现了下面的代码,我怀疑存在一些维度问题:运行 它落在行的测试:

o = tf.keras.layers.Add()([o, o2])

错误 ValueError: Operands could not be broadcast together with shapes (8, 12, 11) (4, 6, 11) 所以我想我正在尝试连接具有不同形状的对象。

我也复制下面的测试尸体,FCN8()方法是安全的。

你有什么提示吗?

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)

  # 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 )(f5)
  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')(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 = tf.keras.layers.Conv2D(n_classes , ( 1 , 1 ) , activation='relu' , padding='same')(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)


  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

你必须先加载 pool 3 预测,然后应用 1*1 conv

def fcn8_decoder(转化率,n_classes):

来自编码器阶段的特征

f3、f4、f5 = 转换

过滤器数量

n = 512

在 CNN 提取器之上添加卷积层。

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)

对上方 o 进行上采样并裁剪引入的任何额外像素

o = tf.keras.layers.Conv2DTranspose(n_classes , kernel_size=(4,4) , strides=(2,2) , use_bias=False )( f5) o = tf.keras.layers.Cropping2D(裁剪=(1,1))(o)

加载池 4 预测并进行 1x1 卷积以将其重塑为与上述 o 相同的形状

o2 = f4 o2 = tf.keras.layers.Conv2D(n_classes , ( 1 , 1 ) , activation='relu' , padding='same')(o2)

添加上采样和 pool 4 预测的结果

o = tf.keras.layers.Add()([o, o2])

对您刚刚执行的操作的结果张量进行上采样

o = tf.keras.layers.Conv2DTranspose( n_classes , kernel_size=(4,4) , strides=(2,2) , use_bias=False)( o) o = tf.keras.layers.Cropping2D(裁剪=(1, 1))(o)

加载池 3 预测并进行 1x1 卷积以将其重塑为与上述 o 相同的形状

o2=f3 o2 = tf.keras.layers.Conv2D(n_classes , ( 1 , 1 ) , activation='relu' , padding='same')(o2)

添加上采样和 pool 3 预测的结果

o = tf.keras.layers.Add()([o, o2])

上采样到原始图像的大小

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)

追加一个 sigmoid 激活函数

o = (tf.keras.layers.激活('sigmoid'))(o)

return o