Skip to content

Instantly share code, notes, and snippets.

@maweigert
Created November 17, 2021 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maweigert/48e53b16d1bbb484e6c54f9891517810 to your computer and use it in GitHub Desktop.
Save maweigert/48e53b16d1bbb484e6c54f9891517810 to your computer and use it in GitHub Desktop.
fix tensorflow batchorm default momentum for mobilenet
"""
The default momentum of MobileNet (for running average calculation) in tf seems to be very high (0.99?), resulting in huge (and suprising) differences in train vs evaluate loss
https://github.com/keras-team/keras/issues/6977
Setting it to something sensible (e.g. 0.9) fixes this
https://stackoverflow.com/questions/65415799/fit-works-as-expected-but-then-during-evaluate-model-performs-at-chance/66692755#66692755
"""
def fix_batchnorm(model, new_value=0.9, prefix=''):
for ii, layer in enumerate(model.layers):
if hasattr(layer, 'layers'):
fix_batchnorm(layer, new_value, f'{prefix}Layer {ii}/')
continue
elif isinstance(layer, tf.keras.layers.BatchNormalization):
layer.momentum = new_value
...
fix_batchnorm(model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment