图像分割 Tensorflow 教程
Image Segmentation Tensorflow tutorials
在这个tf tutorial中,U-net模型被分成了2个部分,第一个收缩部分使用了Mobilenet,它是不可训练的。在第二部分,我无法理解所有层都在训练什么。据我所知,只有最后一层 conv2dTranspose 似乎是可训练的。我说的对吗?
如果我是,怎么可能只有一层能够完成分割这样复杂的任务?
教程link:https://www.tensorflow.org/tutorials/images/segmentation
来自 Tutorial 的 Image 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
。
模型的第二部分(这是你感兴趣的),在层之前,Conv2DTranspose
是Upsampling
部分,它存在于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
]
这意味着它正在从模块 pix2pix
访问一个名为 upsample
的函数。模块的代码 pix2pix
出现在 Github Link
.
中
函数代码,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
。
在这个tf tutorial中,U-net模型被分成了2个部分,第一个收缩部分使用了Mobilenet,它是不可训练的。在第二部分,我无法理解所有层都在训练什么。据我所知,只有最后一层 conv2dTranspose 似乎是可训练的。我说的对吗?
如果我是,怎么可能只有一层能够完成分割这样复杂的任务?
教程link:https://www.tensorflow.org/tutorials/images/segmentation
来自 Tutorial 的 Image 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
。
模型的第二部分(这是你感兴趣的),在层之前,Conv2DTranspose
是Upsampling
部分,它存在于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
]
这意味着它正在从模块 pix2pix
访问一个名为 upsample
的函数。模块的代码 pix2pix
出现在 Github Link
.
函数代码,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
。