CourseAI / ML Engineer: The Complete Course · Module 4: Deep Learning · part 30 of 93
Part 30 · Module 4: Deep Learning

Incident: A frozen layer that was not frozen

15 min read·
Module 4 · Incident file · About 35 minutes · Needs: Lesson 1 (batch normalisation, model.train() and model.eval()) and Lesson 4 (PyTorch modules)

What happened

WHAT HAPPENED A layer that behaved differently when frozen Keras 2.1.3 frozen BN stops updating Keras 2.1.5 the test: 100% vs 86% 17 Apr 2018 the blog post PR not merged patch kept on a branch 5 Aug 2020 TF2 behaves as proposed
The story in order: a behaviour change in Keras 2.1.3, a measured failure on 2.1.5 (red), the post, and by the author's August 2020 reply, TF2 working as he proposed (green).

On 17 April 2018 Vasilis Vryniotis published "The Batch Normalization layer of Keras is broken" on the Datumbox blog (Datumbox, 17 April 2018). He had used Keras heavily and contributed to it, and he had been "bitten by the odd behavior of the Batch Normalization layer" many times.

The setting was transfer learning: take a network trained on a large dataset, freeze its lower layers, and fine-tune the top on your own small dataset. Keras marks a frozen layer with trainable = False.

WHAT HAPPENED Same images, two modes 100% training mode 86% prediction mode 87.44% without the patch 92.36% with the patch 200 IMAGES, SCORED TWICE CIFAR-10 FINE-TUNE real applications: 100% down to 50%
On the same 200 images the model scored 100% in training mode and 86% in prediction mode (red); a CIFAR-10 fine-tune gained almost 5 points with the patch (green).

He measured the damage with a deliberate test. On Keras 2.1.5 he fine-tuned ResNet50 on 200 CIFAR-10 images and evaluated it on the same images. In training mode it scored 100% accuracy; in prediction mode, on identical data, 86%. In a larger CIFAR-10 fine-tune the model reached 87.44% without his patch and 92.36% with it, "almost 5 points higher". In real applications he had seen "even more profound effects (from 100% down to 50% accuracy)".

His pull request to change the layer was not accepted. In a reply dated 5 August 2020 he wrote that "TF2 actually changed the semantics of the BN to operate exactly as proposed on my PR, so this problem is resolved in TF2 keras."

The rest of this course is yours to keep

This course is bought on its own, once, and stays readable afterwards, including the parts added to it later.

Or open everything with a plan