【问题标题】:Image Segmentation U-Net model Assignment图像分割 U-Net 模型分配
【发布时间】:2021-11-02 02:31:30
【问题描述】:

我的 U-Net 模型

def unet_model(input_size=(96, 128, 3), n_filters=32, n_classes=23):

"""
Unet model

Arguments:
    input_size -- Input shape 
    n_filters -- Number of filters for the convolutional layers
    n_classes -- Number of output classes
Returns: 
    model -- tf.keras.Model
"""
inputs = Input(input_size)
# Contracting Path (encoding)
# Add a conv_block with the inputs of the unet_ model and n_filters
### START CODE HERE
cblock1 = conv_block(inputs, n_filters)
# Chain the first element of the output of each block to be the input of the next conv_block. 
# Double the number of filters at each new step
cblock2 = conv_block(cblock1[0], n_filters*2)
cblock3 = conv_block(cblock2[0], n_filters*4)
cblock4 = conv_block(cblock3[0], n_filters*8, dropout_prob=0.3) # Include a dropout_prob of 0.3 for this layer
# Include a dropout_prob of 0.3 for this layer, and avoid the max_pooling layer
cblock5 = conv_block(cblock4[0], n_filters*16, dropout_prob=0.3, max_pooling=False) 
### END CODE HERE

# Expanding Path (decoding)
# Add the first upsampling_block.
# Use the cblock5[0] as expansive_input and cblock4[1] as contractive_input and n_filters * 8
### START CODE HERE
ublock6 = upsampling_block(cblock5[0], cblock4[1],  n_filters*8)
# Chain the output of the previous block as expansive_input and the corresponding contractive block output.
# Note that you must use the second element of the contractive block i.e before the maxpooling layer. 
# At each step, use half the number of filters of the previous block 
ublock7 = upsampling_block(ublock6[0], cblock5[0],  n_filters*4)
ublock8 = upsampling_block(ublock7[0], ublock6[0],  n_filters*2)
ublock9 = upsampling_block(ublock8[0], ublock7[0],  n_filters)
### END CODE HERE

conv9 = Conv2D(n_filters,
             3,
             activation='relu',
             padding='same',
             kernel_initializer='he_normal')(ublock9)

# Add a Conv2D layer with n_classes filter, kernel size of 1 and a 'same' padding
### START CODE HERE
conv10 = Conv2D(n_filters, 1 , padding='same')(conv9)
### END CODE HERE

model = tf.keras.Model(inputs=inputs, outputs=conv10)

return model

... 在上述 Unet 模型中,模型的前半部分已完成,即直到 cblock5 但是从模型的后半部分,即从 cblock6 到 cblock9 我有点困惑 ...

# 将前一个块的输出链接为 expansive_input 和相应的收缩块输出。
# 请注意,您必须使用收缩块的第二个元素,即在 maxpooling 层之前。
# 在每一步,使用前一个块的一半过滤器数量

... 请帮我理解上述指令的含义。 ...

【问题讨论】:

  • 他可能要求你创建残差块,并创建编码和解码块

标签: conv-neural-network image-segmentation


【解决方案1】:

图中的unet有4个编码块(降序)和4个解码块。

在 unet 中,解码块的输入(张量在前一维返回的那些)它是“同一级别”块和前一个块的连接,作业要求您执行此连接(您可以在图片中看到 2 个不同的箭头如何进入解码级别,这是 2 个输入)

at each step use half the filters: 在每个解码层上只使用一半的过滤器(在图片中有 4 个解码层,所以假设你在第一个解码层(较低的一个)上使用 N 个过滤器,然后在第一个解码层上使用 N/2第二解码层等)

Note that you must use the second element of the contractive block i.e before the maxpooling layer. : 很难说,我想他是在说,当你在 3 级获取编码器的输出时,在某些时候,你会想要将此输入提供给 3 级的解码器(水平灰色图中的箭头,您需要连接的输入),您需要在 maxpooling 之前获取此输入,否则它不会具有相同的尺寸(基本上来自编码器有 2 个输出,红色(maxpool)一个和灰色(复制)一个)

【讨论】:

    【解决方案2】:

    问题是在下半场追踪 cblocks

    # UNQ_C3
    # GRADED FUNCTION: unet_model
    def unet_model(input_size=(96, 128, 3), n_filters=32, n_classes=23):
        """
        Unet model
        
        Arguments:
            input_size -- Input shape 
            n_filters -- Number of filters for the convolutional layers
            n_classes -- Number of output classes
        Returns: 
            model -- tf.keras.Model
        """
        inputs = Input(input_size)
        # Contracting Path (encoding)
        # Add a conv_block with the inputs of the unet_ model and n_filters
        ### START CODE HERE
        cblock1 = conv_block(inputs, n_filters)
        # Chain the first element of the output of each block to be the input of the next conv_block. 
        # Double the number of filters at each new step
        cblock2 = conv_block(cblock1[0], 2*n_filters)
        cblock3 = conv_block(cblock2[0], 4*n_filters)
        cblock4 = conv_block(cblock3[0], 8*n_filters, dropout_prob=0.3) # Include a dropout_prob of 0.3 for this layer
        # Include a dropout_prob of 0.3 for this layer, and avoid the max_pooling layer
        cblock5 = conv_block(cblock4[0], 16*n_filters, dropout_prob=0.3, max_pooling=False) 
        ### END CODE HERE
        
        # Expanding Path (decoding)
        # Add the first upsampling_block.
        # Use the cblock5[0] as expansive_input and cblock4[1] as contractive_input and n_filters * 8
        ### START CODE HERE
        ublock6 = upsampling_block(cblock5[0], cblock4[1],  n_filters * 8)
        # Chain the output of the previous block as expansive_input and the corresponding contractive block output.
        # Note that you must use the second element of the contractive block i.e before the maxpooling layer. 
        # At each step, use half the number of filters of the previous block 
        ublock7 = upsampling_block(ublock6, cblock3[1],  n_filters * 4)
        ublock8 = upsampling_block(ublock7, cblock2[1],  n_filters * 2)
        ublock9 = upsampling_block(ublock8, cblock1[1],  n_filters*1)
        ### END CODE HERE
    
        conv9 = Conv2D(n_filters,
                     3,
                     activation='relu',
                     padding='same',
                     kernel_initializer='he_normal')(ublock9)
    
        # Add a Conv2D layer with n_classes filter, kernel size of 1 and a 'same' padding
        ### START CODE HERE
        conv10 = Conv2D(n_classes, 1, padding='same')(conv9)
        ### END CODE HERE
        
        model = tf.keras.Model(inputs=inputs, outputs=conv10)
    
        return model
    

    【讨论】:

    • 你救了我?
    猜你喜欢
    • 2021-03-08
    • 2022-08-04
    • 2021-11-22
    • 2018-07-28
    • 2021-07-05
    • 2018-05-18
    • 2019-01-08
    • 2019-01-09
    • 1970-01-01
    相关资源
    最近更新 更多