TF2.1:SegNet 模型架构问题。度量计算错误,保持恒定并收敛到确定值

rkp*_*sia 8 deep-learning conv-neural-network keras tensorflow tf.keras

我正在Tensorflow 中构建自定义模型 ( SegNet ) 2.1.0

我面临的第一个问题是重新利用所需的最大池操作的索引,如论文中所述。基本上,由于它是编码器-解码器架构,因此在解码中需要网络编码部分的池化索引来对特征图进行上采样并保持相应索引的目标值。

现在,在 TF 中,这些索引默认情况下不会被层导出tf.keras.layers.MaxPool2D(例如在 PyTorch 中)。要获得最大池化操作的索引,需要使用tf.nn.max_pool_with_argmax. 无论如何,此操作以扁平格式返回索引(argmax),这需要进一步的操作才能在网络的其他部分有用。

为了实现一个执行 MaxPooling2D 并导出这些索引(扁平化)的层,我在 keras 中定义了一个自定义层。

class MaxPoolingWithArgmax2D(Layer):

def __init__(
        self,
        pool_size=(2, 2),
        strides=2,
        padding='same',
        **kwargs):
    super(MaxPoolingWithArgmax2D, self).__init__(**kwargs)
    self.padding = padding
    self.pool_size = pool_size
    self.strides = strides

def call(self, inputs, **kwargs):
    padding = self.padding
    pool_size = self.pool_size
    strides = self.strides
    output, argmax = tf.nn.max_pool_with_argmax(
        inputs,
        ksize=pool_size,
        strides=strides,
        padding=padding.upper(),
        output_dtype=tf.int64)
    return output, argmax
Run Code Online (Sandbox Code Playgroud)

显然,该层用于网络的编码部分,因此需要解码相应层来执行逆操作(UpSampling2D),并利用索引(论文中有关此操作的更多详细信息)。

经过一番研究,我找到了遗留代码(TF<2.1.0)并对其进行了调整以执行操作。无论如何,我不是 100% 相信这段代码运行良好,事实上有些事情我不喜欢。

class MaxUnpooling2D(Layer):
def __init__(self, size=(2, 2), **kwargs):
    super(MaxUnpooling2D, self).__init__(**kwargs)
    self.size = size

def call(self, inputs, output_shape=None):
    updates, mask = inputs[0], inputs[1]
    with tf.name_scope(self.name):
        mask = tf.cast(mask, 'int32')
        #input_shape = tf.shape(updates, out_type='int32')
        input_shape = updates.get_shape()

        # This statement is required if I don't want to specify a batch size
        if input_shape[0] == None:
            batches = 1
        else:
            batches = input_shape[0]

        #  calculation new shape
        if output_shape is None:
            output_shape = (
                    batches,
                    input_shape[1]*self.size[0],
                    input_shape[2]*self.size[1],
                    input_shape[3])

        # calculation indices for batch, height, width and feature maps
        one_like_mask = tf.ones_like(mask, dtype='int32')
        batch_shape = tf.concat(
                [[batches], [1], [1], [1]],
                axis=0)
        batch_range = tf.reshape(
                tf.range(output_shape[0], dtype='int32'),
                shape=batch_shape)
        b = one_like_mask * batch_range
        y = mask // (output_shape[2] * output_shape[3])
        x = (mask // output_shape[3]) % output_shape[2]
        feature_range = tf.range(output_shape[3], dtype='int32')
        f = one_like_mask * feature_range

        # transpose indices & reshape update values to one dimension
        updates_size = tf.size(updates)
        indices = tf.transpose(tf.reshape(
            tf.stack([b, y, x, f]),
            [4, updates_size]))
        values = tf.reshape(updates, [updates_size])
        ret = tf.scatter_nd(indices, values, output_shape)
        return ret
Run Code Online (Sandbox Code Playgroud)

困扰我的事情是:

  1. 执行展开索引的操作 (MaxUnpooling2D) 与了解特定批次大小严格相关,对于模型验证,我希望为 None 或未指定。
  2. 我不确定这段代码实际上与库的其余部分是否 100% 兼容。事实上,fit如果我使用tf.keras.metrics.MeanIoU该值,0.341除了第一个时期之外,每隔一个时期就会收敛到并保持不变。相反,标准准确度指标工作得很好。

深度网络架构


下面是模型的完整定义。

import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
from tensorflow.keras.layers import Layer


class SegNet:
    def __init__(self, data_shape, classes = 3, batch_size = None):
        self.MODEL_NAME = 'SegNet'
        self.MODEL_VERSION = '0.2'

        self.classes = classes
        self.batch_size = batch_size

        self.build_model(data_shape)

    def build_model(self, data_shape):
        input_shape = (data_shape, data_shape, 3)

        inputs = keras.Input(shape=input_shape, batch_size=self.batch_size, name='Input')

        # Build sequential model

        # Encoding
        encoders = 5
        feature_maps = [64, 128, 256, 512, 512]
        n_convolutions = [2, 2, 3, 3, 3]
        eb_input = inputs
        eb_argmax_indices = []
        for encoder_index in range(encoders):
            encoder_block, argmax_indices = self.encoder_block(
                eb_input, encoder_index, feature_maps[encoder_index], n_convolutions[encoder_index])
            eb_argmax_indices.append(argmax_indices)
            eb_input = encoder_block

        # Decoding
        decoders = encoders
        db_input = encoder_block
        eb_argmax_indices.reverse()
        feature_maps.reverse()
        n_convolutions.reverse()
        d_feature_maps = [512, 512, 256, 128, 64]
        d_n_convolutions = n_convolutions
        for decoder_index in range(decoders):
            decoder_block = self.decoder_block(
                db_input, eb_argmax_indices[decoder_index], decoder_index, d_feature_maps[decoder_index], d_n_convolutions[decoder_index])
            db_input = decoder_block

        output = layers.Softmax()(decoder_block)

        self.model = keras.Model(inputs=inputs, outputs=output, name="SegNet")

    def encoder_block(self, x, encoder_index, feature_maps, n_convolutions):
        bank_input = x
        for conv_index in range(n_convolutions):
            bank = self.eb_layers_bank(
                bank_input, conv_index, feature_maps, encoder_index)
            bank_input = bank

        max_pool, indices = MaxPoolingWithArgmax2D(pool_size=(
            2, 2), strides=2, padding='same', name='EB_{}_MPOOL'.format(encoder_index + 1))(bank)

        return max_pool, indices

    def eb_layers_bank(self, x, bank_index, feature_maps, encoder_index):

        bank_input = x

        conv_l = layers.Conv2D(feature_maps, (3, 3), padding='same', name='EB_{}_BANK_{}_CONV'.format(
            encoder_index + 1, bank_index + 1))(bank_input)
        batch_norm = layers.BatchNormalization(
            name='EB_{}_BANK_{}_BN'.format(encoder_index + 1, bank_index + 1))(conv_l)
        relu = layers.ReLU(name='EB_{}_BANK_{}_RL'.format(
            encoder_index + 1, bank_index + 1))(batch_norm)

        return relu

    def decoder_block(self, x, max_pooling_idices, decoder_index, feature_maps, n_convolutions):
        #bank_input = self.unpool_with_argmax(x, max_pooling_idices)
        bank_input = MaxUnpooling2D(name='DB_{}_UPSAMP'.format(decoder_index + 1))([x, max_pooling_idices])
        #bank_input = layers.UpSampling2D()(x)
        for conv_index in range(n_convolutions):
            if conv_index == n_convolutions - 1:
                last_l_banck = True
            else:
                last_l_banck = False
            bank = self.db_layers_bank(
                bank_input, conv_index, feature_maps, decoder_index, last_l_banck)
            bank_input = bank

        return bank

    def db_layers_bank(self, x, bank_index, feature_maps, decoder_index, last_l_bank):
        bank_input = x

        if (last_l_bank) & (decoder_index == 4):
            conv_l = layers.Conv2D(self.classes, (1, 1), padding='same', name='DB_{}_BANK_{}_CONV'.format(
                decoder_index + 1, bank_index + 1))(bank_input)
            #batch_norm = layers.BatchNormalization(
            #    name='DB_{}_BANK_{}_BN'.format(decoder_index + 1, bank_index + 1))(conv_l)
            return conv_l
        else:

            if (last_l_bank) & (decoder_index > 0):
                conv_l = layers.Conv2D(int(feature_maps / 2), (3, 3), padding='same', name='DB_{}_BANK_{}_CONV'.format(
                    decoder_index + 1, bank_index + 1))(bank_input)
            else:
                conv_l = layers.Conv2D(feature_maps, (3, 3), padding='same', name='DB_{}_BANK_{}_CONV'.format(
                    decoder_index + 1, bank_index + 1))(bank_input)
            batch_norm = layers.BatchNormalization(
                name='DB_{}_BANK_{}_BN'.format(decoder_index + 1, bank_index + 1))(conv_l)
            relu = layers.ReLU(name='DB_{}_BANK_{}_RL'.format(
                decoder_index + 1, bank_index + 1))(batch_norm)

            return relu

    def get_model(self):
        return self.model
Run Code Online (Sandbox Code Playgroud)

这里的输出model.summary()

Model: "SegNet"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
Input (InputLayer)              [(None, 416, 416, 3) 0                                            
__________________________________________________________________________________________________
EB_1_BANK_1_CONV (Conv2D)       (None, 416, 416, 64) 1792        Input[0][0]                      
__________________________________________________________________________________________________
EB_1_BANK_1_BN (BatchNormalizat (None, 416, 416, 64) 256         EB_1_BANK_1_CONV[0][0]           
__________________________________________________________________________________________________
EB_1_BANK_1_RL (ReLU)           (None, 416, 416, 64) 0           EB_1_BANK_1_BN[0][0]             
__________________________________________________________________________________________________
EB_1_BANK_2_CONV (Conv2D)       (None, 416, 416, 64) 36928       EB_1_BANK_1_RL[0][0]             
__________________________________________________________________________________________________
EB_1_BANK_2_BN (BatchNormalizat (None, 416, 416, 64) 256         EB_1_BANK_2_CONV[0][0]           
__________________________________________________________________________________________________
EB_1_BANK_2_RL (ReLU)           (None, 416, 416, 64) 0           EB_1_BANK_2_BN[0][0]             
__________________________________________________________________________________________________
EB_1_MPOOL (MaxPoolingWithArgma ((None, 208, 208, 64 0           EB_1_BANK_2_RL[0][0]             
__________________________________________________________________________________________________
EB_2_BANK_1_CONV (Conv2D)       (None, 208, 208, 128 73856       EB_1_MPOOL[0][0]                 
__________________________________________________________________________________________________
EB_2_BANK_1_BN (BatchNormalizat (None, 208, 208, 128 512         EB_2_BANK_1_CONV[0][0]           
__________________________________________________________________________________________________
EB_2_BANK_1_RL (ReLU)           (None, 208, 208, 128 0           EB_2_BANK_1_BN[0][0]             
__________________________________________________________________________________________________
EB_2_BANK_2_CONV (Conv2D)       (None, 208, 208, 128 147584      EB_2_BANK_1_RL[0][0]             
__________________________________________________________________________________________________
EB_2_BANK_2_BN (BatchNormalizat (None, 208, 208, 128 512         EB_2_BANK_2_CONV[0][0]           
__________________________________________________________________________________________________
EB_2_BANK_2_RL (ReLU)           (None, 208, 208, 128 0           EB_2_BANK_2_BN[0][0]             
__________________________________________________________________________________________________
EB_2_MPOOL (MaxPoolingWithArgma ((None, 104, 104, 12 0           EB_2_BANK_2_RL[0][0]             
__________________________________________________________________________________________________
EB_3_BANK_1_CONV (Conv2D)       (None, 104, 104, 256 295168      EB_2_MPOOL[0][0]                 
__________________________________________________________________________________________________
EB_3_BANK_1_BN (BatchNormalizat (None, 104, 104, 256 1024        EB_3_BANK_1_CONV[0][0]           
__________________________________________________________________________________________________
EB_3_BANK_1_RL (ReLU)           (None, 104, 104, 256 0           EB_3_BANK_1_BN[0][0]             
__________________________________________________________________________________________________
EB_3_BANK_2_CONV (Conv2D)       (None, 104, 104, 256 590080      EB_3_BANK_1_RL[0][0]             
__________________________________________________________________________________________________
EB_3_BANK_2_BN (BatchNormalizat (None, 104, 104, 256 1024        EB_3_BANK_2_CONV[0][0]           
__________________________________________________________________________________________________
EB_3_BANK_2_RL (ReLU)           (None, 104, 104, 256 0           EB_3_BANK_2_BN[0][0]             
__________________________________________________________________________________________________
EB_3_BANK_3_CONV (Conv2D)       (None, 104, 104, 256 590080      EB_3_BANK_2_RL[0][0]             
__________________________________________________________________________________________________
EB_3_BANK_3_BN (BatchNormalizat (None, 104, 104, 256 1024        EB_3_BANK_3_CONV[0][0]           
__________________________________________________________________________________________________
EB_3_BANK_3_RL (ReLU)           (None, 104, 104, 256 0           EB_3_BANK_3_BN[0][0]             
__________________________________________________________________________________________________
EB_3_MPOOL (MaxPoolingWithArgma ((None, 52, 52, 256) 0           EB_3_BANK_3_RL[0][0]             
__________________________________________________________________________________________________
EB_4_BANK_1_CONV (Conv2D)       (None, 52, 52, 512)  1180160     EB_3_MPOOL[0][0]                 
__________________________________________________________________________________________________
EB_4_BANK_1_BN (BatchNormalizat (None, 52, 52, 512)  2048        EB_4_BANK_1_CONV[0][0]           
__________________________________________________________________________________________________
EB_4_BANK_1_RL (ReLU)           (None, 52, 52, 512)  0           EB_4_BANK_1_BN[0][0]             
__________________________________________________________________________________________________
EB_4_BANK_2_CONV (Conv2D)       (None, 52, 52, 512)  2359808     EB_4_BANK_1_RL[0][0]             
__________________________________________________________________________________________________
EB_4_BANK_2_BN (BatchNormalizat (None, 52, 52, 512)  2048        EB_4_BANK_2_CONV[0][0]           
__________________________________________________________________________________________________
EB_4_BANK_2_RL (ReLU)           (None, 52, 52, 512)  0           EB_4_BANK_2_BN[0][0]             
__________________________________________________________________________________________________
EB_4_BANK_3_CONV (Conv2D)       (None, 52, 52, 512)  2359808     EB_4_BANK_2_RL[0][0]             
__________________________________________________________________________________________________
EB_4_BANK_3_BN (BatchNormalizat (None, 52, 52, 512)  2048        EB_4_BANK_3_CONV[0][0]           
__________________________________________________________________________________________________
EB_4_BANK_3_RL (ReLU)           (None, 52, 52, 512)  0           EB_4_BANK_3_BN[0][0]             
__________________________________________________________________________________________________
EB_4_MPOOL (MaxPoolingWithArgma ((None, 26, 26, 512) 0           EB_4_BANK_3_RL[0][0]             
__________________________________________________________________________________________________
EB_5_BANK_1_CONV (Conv2D)       (None, 26, 26, 512)  2359808     EB_4_MPOOL[0][0]                 
__________________________________________________________________________________________________
EB_5_BANK_1_BN (BatchNormalizat (None, 26, 26, 512)  2048        EB_5_BANK_1_CONV[0][0]           
__________________________________________________________________________________________________
EB_5_BANK_1_RL (ReLU)           (None, 26, 26, 512)  0           EB_5_BANK_1_BN[0][0]             
__________________________________________________________________________________________________
EB_5_BANK_2_CONV (Conv2D)       (None, 26, 26, 512)  2359808     EB_5_BANK_1_RL[0][0]             
__________________________________________________________________________________________________
EB_5_BANK_2_BN (BatchNormalizat (None, 26, 26, 512)  2048        EB_5_BANK_2_CONV[0][0]           
__________________________________________________________________________________________________
EB_5_BANK_2_RL (ReLU)           (None, 26, 26, 512)  0           EB_5_BANK_2_BN[0][0]             
__________________________________________________________________________________________________
EB_5_BANK_3_CONV (Conv2D)       (None, 26, 26, 512)  2359808     EB_5_BANK_2_RL[0][0]             
__________________________________________________________________________________________________
EB_5_BANK_3_BN (BatchNormalizat (None, 26, 26, 512)  2048        EB_5_BANK_3_CONV[0][0]           
__________________________________________________________________________________________________
EB_5_BANK_3_RL (ReLU)           (None, 26, 26, 512)  0           EB_5_BANK_3_BN[0][0]             
__________________________________________________________________________________________________
EB_5_MPOOL (MaxPoolingWithArgma ((None, 13, 13, 512) 0           EB_5_BANK_3_RL[0][0]             
__________________________________________________________________________________________________
DB_1_UPSAMP (MaxUnpooling2D)    (1, 26, 26, 512)     0           EB_5_MPOOL[0][0]                 
                                                                 EB_5_MPOOL[0][1]                 
__________________________________________________________________________________________________
DB_1_BANK_1_CONV (Conv2D)       (1, 26, 26, 512)     2359808     DB_1_UPSAMP[0][0]                
__________________________________________________________________________________________________
DB_1_BANK_1_BN (BatchNormalizat (1, 26, 26, 512)     2048        DB_1_BANK_1_CONV[0][0]           
__________________________________________________________________________________________________
DB_1_BANK_1_RL (ReLU)           (1, 26, 26, 512)     0           DB_1_BANK_1_BN[0][0]             
__________________________________________________________________________________________________
DB_1_BANK_2_CONV (Conv2D)       (1, 26, 26, 512)     2359808     DB_1_BANK_1_RL[0][0]             
__________________________________________________________________________________________________
DB_1_BANK_2_BN (BatchNormalizat (1, 26, 26, 512)     2048        DB_1_BANK_2_CONV[0][0]           
__________________________________________________________________________________________________
DB_1_BANK_2_RL (ReLU)           (1, 26, 26, 512)     0           DB_1_BANK_2_BN[0][0]             
__________________________________________________________________________________________________
DB_1_BANK_3_CONV (Conv2D)       (1, 26, 26, 512)     2359808     DB_1_BANK_2_RL[0][0]             
__________________________________________________________________________________________________
DB_1_BANK_3_BN (BatchNormalizat (1, 26, 26, 512)     2048        DB_1_BANK_3_CONV[0][0]           
__________________________________________________________________________________________________
DB_1_BANK_3_RL (ReLU)           (1, 26, 26, 512)     0           DB_1_BANK_3_BN[0][0]             
__________________________________________________________________________________________________
DB_2_UPSAMP (MaxUnpooling2D)    (1, 52, 52, 512)     0           DB_1_BANK_3_RL[0][0]             
                                                                 EB_4_MPOOL[0][1]                 
_______________________________________________________

Dan*_*ler 0

您可以通过两种方式在自定义图层中进行未知批量大小的重塑。

如果您知道形状的其余部分,请使用 -1 作为批量大小来重塑形状:

假设您知道预期数组的大小:

import tensorflow.keras.backend as K
reshaped = K.reshape(original, (-1, x, y, channels))
Run Code Online (Sandbox Code Playgroud)

假设您不知道大小,然后使用K.shape将形状作为张量获取:

inputs_shape = K.shape(inputs)
batch_size = inputs_shape[:1]
x = inputs_shape[1:2]
y = inputs_shape[2:3]
ch = inputs_shape[3:]

#you can then concatenate these and operate them (notice I kept them as 1D vector, not as scalar)
newShape = K.concatenate([batch_size, x, y, ch]) #of course you will make your operations
Run Code Online (Sandbox Code Playgroud)

一旦我做了我自己的 Segnet 版本,我就没有使用索引,而是保留了一个热门版本。确实,这需要额外的操作,但可能效果很好:

def get_indices(original, unpooled):
    is_equal = K.equal(original, unpooled)
    return K.cast(is_equal, K.floatx())

previous_output = ...
pooled = MaxPooling2D()(previous_output)
unpooled = UpSampling2D()(pooled)

one_hot_indices = Lambda(get_indices)([previous_output, unpooled])
Run Code Online (Sandbox Code Playgroud)

然后在上采样之后,我连接这些索引并传递一个新的转换:

some_output = ...
upsampled = UpSampling2D()(some_output)
with_indices = Concatenate([upsampled, one_hot_indices])
upsampled = Conv2D(...)(with_indices)
Run Code Online (Sandbox Code Playgroud)