Skip to content

Migrate EfficientNet fine-tuning tutorial to Keras 3 - #2393

Open
kharshith-k wants to merge 8 commits into
keras-team:masterfrom
kharshith-k:image_classification_via_finetuning_with_efficientnet_migration
Open

Migrate EfficientNet fine-tuning tutorial to Keras 3#2393
kharshith-k wants to merge 8 commits into
keras-team:masterfrom
kharshith-k:image_classification_via_finetuning_with_efficientnet_migration

Conversation

@kharshith-k

Copy link
Copy Markdown
Member

Here's the Github Gist for the migration

This PR Also implements the fixes for this issue

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread examples/vision/image_classification_efficientnet_fine_tuning.py
Comment thread examples/vision/md/image_classification_efficientnet_fine_tuning.md
Comment thread examples/vision/image_classification_efficientnet_fine_tuning.py Outdated
Comment thread examples/vision/md/image_classification_efficientnet_fine_tuning.md
Comment thread examples/vision/md/image_classification_efficientnet_fine_tuning.md
@kirisakow

kirisakow commented Jul 17, 2026

Copy link
Copy Markdown

Plus an extended comment regarding unfreeze_model() function

Comment thread examples/vision/image_classification_efficientnet_fine_tuning.py Outdated
Comment thread examples/vision/image_classification_efficientnet_fine_tuning.py
Comment thread examples/vision/image_classification_efficientnet_fine_tuning.py
- 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
@kharshith-k
kharshith-k force-pushed the image_classification_via_finetuning_with_efficientnet_migration branch from adbbc7b to d934281 Compare August 24, 2026 14:15
@kirisakow

Copy link
Copy Markdown

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?

@kharshith-k @maitry63

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.
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 laxmareddyp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR..! Left few comments. Once address comemnts good to merge.

Comment thread examples/vision/image_classification_efficientnet_fine_tuning.py
Comment thread examples/vision/image_classification_efficientnet_fine_tuning.py Outdated
Comment thread examples/vision/image_classification_efficientnet_fine_tuning.py Outdated
Comment thread examples/vision/image_classification_efficientnet_fine_tuning.py Outdated
Comment thread examples/vision/image_classification_efficientnet_fine_tuning.py Outdated
Comment thread examples/vision/image_classification_efficientnet_fine_tuning.py Outdated
- 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants