图像分割Tensorflow教程

时间:2020-07-04 19:13:01

标签: tensorflow conv-neural-network image-segmentation autoencoder unity3d-unet

在此tf tutorial中,U-net模型分为两部分,第一次收缩是在他们使用Mobilenet的情况下进行的,并且该模型不可训练。在第二部分中,我无法理解正在训练所有层的内容。据我所知,只有最后一层conv2dTranspose似乎是可训练的。我说的对吗?

如果我是怎么做到的,那么只有一层能够完成诸如分割之类的复杂任务?

教程链接:https://www.tensorflow.org/tutorials/images/segmentation

1 个答案:

答案 0 :(得分:1)

TutorialImage Segmentation Model的代码如下所示:

def unet_model(output_channels):
  inputs = tf.keras.layers.Input(shape=[128, 128, 3])
  x = inputs

  # Downsampling through the model
  skips = down_stack(x)
  x = skips[-1]
  skips = reversed(skips[:-1])

  # Upsampling and establishing the skip connections
  for up, skip in zip(up_stack, skips):
    x = up(x)
    concat = tf.keras.layers.Concatenate()
    x = concat([x, skip])

  # This is the last layer of the model
  last = tf.keras.layers.Conv2DTranspose(
      output_channels, 3, strides=2,
      padding='same')  #64x64 -> 128x128

  x = last(x)

  return tf.keras.Model(inputs=inputs, outputs=x)

模型的第一部分是Downsampling,不使用整个Mobilenet Architecture,而仅使用Layers

'block_1_expand_relu',   # 64x64
'block_3_expand_relu',   # 32x32
'block_6_expand_relu',   # 16x16
'block_13_expand_relu',  # 8x8
'block_16_project'

预训练模型Mobilenet,即non-trainable

模型的第二部分(您感兴趣的部分),在该层之前,Conv2DTransposeUpsampling部分,它存在于list中,

up_stack = [
    pix2pix.upsample(512, 3),  # 4x4 -> 8x8
    pix2pix.upsample(256, 3),  # 8x8 -> 16x16
    pix2pix.upsample(128, 3),  # 16x16 -> 32x32
    pix2pix.upsample(64, 3),   # 32x32 -> 64x64
]

这意味着它正在从模块upsample访问名为pix2pix的功能。 Github Link中包含模块pix2pix的代码。

该功能的代码upsample如下所示:

def upsample(filters, size, norm_type='batchnorm', apply_dropout=False):
  """Upsamples an input.
  Conv2DTranspose => Batchnorm => Dropout => Relu
  Args:
    filters: number of filters
    size: filter size
    norm_type: Normalization type; either 'batchnorm' or 'instancenorm'.
    apply_dropout: If True, adds the dropout layer
  Returns:
    Upsample Sequential Model
  """

  initializer = tf.random_normal_initializer(0., 0.02)

  result = tf.keras.Sequential()
  result.add(
      tf.keras.layers.Conv2DTranspose(filters, size, strides=2,
                                      padding='same',
                                      kernel_initializer=initializer,
                                      use_bias=False))

  if norm_type.lower() == 'batchnorm':
    result.add(tf.keras.layers.BatchNormalization())
  elif norm_type.lower() == 'instancenorm':
    result.add(InstanceNormalization())

  if apply_dropout:
    result.add(tf.keras.layers.Dropout(0.5))

  result.add(tf.keras.layers.ReLU())

  return result

这意味着Model的第二部分包含Upsampling Layers,其功能已在上面定义,Filters的数量为512, 256, 128 and 64