title: How to correctly use batch normalization with U-net in keras?
body: <p>I am trying to use batch normalization layers whith U-net for the segmentation task. Same layers works fine for res-net, vgg, xception etc., and I'm curious if it is an architecture dependent problem? During the training everything is fine, metrics grow losses dpor, but once I'm trying to evaluate the model or predict the mask it generates garbage. Seems like learned weights for those layers keep updating even during test and prediction. How to solve this problem in keras? 
  keras version = 2.2.2</p>
  
  <p>I was trying to use Batch norm layers only in encoder part, doesn't help.
  I was also trying to set layers parameter: trainable=False, doesn't help.</p>
  
  <pre class="lang-py prettyprint-override"><code>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 &gt; 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]) ```
  </code></pre>