Migrate EfficientNet fine-tuning tutorial to Keras 3 - #2393
Conversation
There was a problem hiding this comment.
Code Review
This pull request migrates the EfficientNet fine-tuning example from TensorFlow-specific tf.data and tensorflow_datasets to a backend-agnostic Keras 3 implementation using keras.utils.PyDataset and keras.ops, updating the .py, .ipynb, and .md files. While the migration successfully makes the pipeline backend-agnostic, the review highlights two major issues: first, a critical bug in unfreeze_model where accessing model.layers fails to target the nested EfficientNetB0 base model, causing incorrect unfreezing and potential runtime errors; second, a high-severity memory issue where loading the entire dataset of over 20,000 images into memory at once can cause Out-Of-Memory (OOM) crashes, which should be resolved by implementing lazy loading within the custom PyDataset class.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
Plus an extended comment regarding |
- Replace TensorFlow-specific code with backend-agnostic Keras 3 APIs - Remove tensorflow_datasets dependency, implement direct HTTP download from Stanford servers - Replace tf.data.Dataset pipeline with keras.utils.PyDataset classes - Replace tf.image.resize and tf.one_hot with keras.ops equivalents - Enhance unfreeze_model() function with parameterization (fixes keras-team#2385) - Add parameters: layers_to_unfreeze, learning_rate, loss, metrics - Support int (last N layers) and str (block name) patterns - Add return statement and update call site - Add comprehensive docstring with input validation - Update all documentation to reflect Keras 3 backend-agnostic approach - Standardize header format and update dates to 2026/07/13 - Apply changes consistently across .ipynb, .py, and .md files
… lazy loading
Critical fixes:
- Fix unfreeze_model() to access nested EfficientNetB0 base model via model.get_layer('efficientnetb0')
Previously accessed model.layers which only contains 6 top-level layers, not EfficientNet blocks
This caused ValueError with string matching and incorrectly unfroze entire base model with int parameter
High-priority fixes:
- Implement lazy image loading to prevent OOM issues
Replace load_images_and_labels() with prepare_paths_and_labels()
Store file paths instead of loaded images (saves several GB of RAM)
Load images on-demand in PyDataset.__getitem__() from disk
Prevents memory crashes in resource-constrained environments like Colab
Changes applied consistently across .ipynb, .py, and .md files
… markdown formatting - Make base model lookup dynamic instead of hardcoded 'efficientnetb0' - Add validation for layers_to_unfreeze parameter (must be > 0) - Update docstring to reference 'base model's layers' instead of 'model.layers' - Remove excessive blank lines in markdown file - Fix empty line before </div> in markdown fenced code block Addresses comments from @maitry63 and @kirisakow in PR keras-team#2393
adbbc7b to
d934281
Compare
|
Thank you for the update. Can you guys please tell me how long is it going to take for the update to be published online? |
The autogen.py script had a hardcoded Keras version (v3.15.0) that didn't match the current installed package version (3.15.1), causing the CI build to fail.
…fficientnet_migration
The upstream refactored autogen.py to automatically detect package versions instead of hardcoding them. This restores the _build_project_url() function and _MODULE_TO_REPO mapping from upstream.
laxmareddyp
left a comment
There was a problem hiding this comment.
Thanks for the PR..! Left few comments. Once address comemnts good to merge.
- Update code examples to use Keras 3 import paths (keras.applications instead of tensorflow.keras.applications) - Remove unused 'import os' - Move 'import scipy.io' from inline to top-level imports - Remove stale TensorFlow TPU link from documentation - Fix typo: 'arbitray' → 'arbitrary' - Add filter='data' to tar.extractall() for Python 3.12+ compatibility All changes applied consistently across .py, .ipynb, and .md files.
unfreeze_model(model)function in "Image classification via fine-tuning with EfficientNet" tutorial, "Transfer learning from pre-trained weights" section, second step #2385)Here's the Github Gist for the migration
This PR Also implements the fixes for this issue