segmentation_models icon indicating copy to clipboard operation
segmentation_models copied to clipboard

can we add dropout to Unet

Open AceMcAwesome opened this issue 5 years ago • 3 comments

Is there a way to add dropout to the Unet model here? I see there is some dropout in FPN and PSPNet, but none in Unet. I want to train on some limited data and I'm having problems with overfitting, so some dropout could really help if there's a good place to insert dropout layers into this model.

AceMcAwesome avatar Jun 14 '20 21:06 AceMcAwesome

I had a similar question recently and did some experimentation. I didn't get much improvement in my model, but maybe yours will benefit more.

In my branch of segmentation_models, unet takes an additional argument (center_dropout=0.0) which specifies how much dropout to apply at the center of the model, between the encoder and decoder.

I also experimented with adding dropout to the skip connections. This seemed to decrease accuracy a lot so it's commented out. But the line remains in my code and you can un-comment it if you want.

DavidWAbrahams avatar Jun 15 '20 21:06 DavidWAbrahams

I added one dropout before the last activation.

Sample code,

from tensorflow.keras import layers
from tensorflow import keras
import segmentation_models as sm

def get_model_with_dropout(base_model_name='efficientnetb4',activation='sigmoid',dropout = 0.1):
  base_model = sm.Unet(base_model_name, encoder_weights='imagenet')
  base_model_input = base_model.input
  base_model_output = base_model.get_layer('final_conv').output
  #add dropout
  base_model_output = keras.layers.Dropout(dropout)(base_model_output)
  #add activation
  output = keras.layers.Activation(activation, name=activation)(base_model_output)
  model_dp = keras.models.Model(base_model_input, output)
  
  return model_dp

dennywangtenk avatar Dec 19 '20 20:12 dennywangtenk

hello @dennywangtenk did adding dropout help you overcome the problem of overfitting ?

luna9722 avatar May 28 '21 12:05 luna9722