如何在keras中通过U-net正确使用批处理规范化?

时间:2019-05-20 20:41:53

标签: python keras deep-learning batch-normalization

我正在尝试使用带有U-net的批归一化层进行分段任务。相同的层适用于res-net,vgg,xception等,我很好奇它是否是依赖于体系结构的问题?在训练过程中,一切都很好,指标会增加损失,但是一旦我尝试评估模型或预测掩码,就会产生垃圾。似乎这些层的学习权重即使在测试和预测期间也不断更新。如何在喀拉拉邦解决这个问题? keras版本= 2.2.2

我试图仅在编码器部分使用批处理规范层,但无济于事。 我也在尝试设置图层参数:trainable = False,没有帮助。

from keras.models import Input, Model
from keras.layers import Conv2D, Concatenate, MaxPooling2D
from keras.layers import UpSampling2D, Dropout, BatchNormalization

def conv_block(m, dim, res, do=0):
    n = Conv2D(dim, 3, padding='same')(m)
    n = BatchNormalization()(n)
    n = keras.layers.LeakyReLU(0)(n)
    n = Dropout(do)(n) if do else n
    n = Conv2D(dim, 3, padding='same')(n)
    n = BatchNormalization()(n) 
    n = keras.layers.LeakyReLU(0)(n)
    return Concatenate()([m, n]) if res else n


def conv_block_bn(m, dim, res, do=0):
    n = Conv2D(dim, 3, padding='same')(m)
    n = BatchNormalization()(n)
    n = keras.layers.LeakyReLU(0)(n)
    n = Dropout(do)(n) if do else n
    n = Conv2D(dim, 3, padding='same')(n)
    n = BatchNormalization()(n)
    n = keras.layers.LeakyReLU(0)(n)
    return Concatenate()([m, n]) if res else n

def level_block(m, dim, depth, inc,  do,  mp, up, res):
    if depth > 0:
        n = conv_block_bn(m, dim, res)#(m, dim, acti, bn, res)
        m = MaxPooling2D()(n) if mp else Conv2D(dim, 3, strides=2, padding='same')(n)
        m = level_block(m, int(inc*dim), depth-1, inc,  do,  mp, up, res)
        if up:
            m = UpSampling2D()(m)
            m = Conv2D(dim, 2, padding='same')(m)
            m = BatchNormalization()(m) 
            m = keras.layers.LeakyReLU(0)(m)
        else:
            m = Conv2DTranspose(dim, 3, strides=2, activation='relu', padding='same')(m)
        n = Concatenate()([n, m])
        m = conv_block_bn(n, dim, res)#(n, dim, acti, bn, res)
    else:
        m = conv_block_bn(m, dim, res,do)#(m, dim, acti, bn, res, do)
    return m

def UNet(img_shape, out_ch=1, start_ch=64, depth=4, inc_rate=2., activation='relu', 
         dropout=0.5, batchnorm=False, maxpool=True, upconv=True, residual=False):
    i = Input(shape=img_shape)
    o = level_block(i, start_ch, depth, inc_rate,dropout, maxpool, upconv, residual)
    o = Conv2D(out_ch, 1, activation='sigmoid')(o)
    return Model(inputs=i, outputs=o)


model1 = UNet((512,512,1), out_ch=1, start_ch=64, depth=4, inc_rate=2.,  
         dropout=0.5, maxpool=True, upconv=True, residual=False)
model1 = multi_gpu_model(model1,gpus=6)
model1.compile(Adam(lr = 3.5e-6), loss = custom_losses, metrics = [dice_coef]) ```

1 个答案:

答案 0 :(得分:0)

在每层上尝试进行bach归一化,并消除掉落层。看你得到什么。在预测中,还设置训练标记为False。