Created
November 17, 2021 14:55
-
-
Save maweigert/48e53b16d1bbb484e6c54f9891517810 to your computer and use it in GitHub Desktop.
fix tensorflow batchorm default momentum for mobilenet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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