Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TF] First changes on the road to Keras v3 #1724

Merged
merged 7 commits into from
Oct 1, 2024

Conversation

felixdittrich92
Copy link
Contributor

@felixdittrich92 felixdittrich92 commented Sep 13, 2024

This PR:

  • is part of a series of PR's

  • basic elements to go further forward (only .weights.h5 and .keras (full model) - loading possible in keras v3)

  • change to keras imports

  • Finally: enable fine tuning tf models with pretrained also if the vocab / classes differs !! 💯 (same we have already a long time ago added to PT - which wasn't possible with loading from /weights in TF)

  • all models works as expected

Next steps:

  • Implement build and get_config methods for proper loading & saving (serialization)

Any feedback is welcome 🤗

NOTE: It's still fully backwards compatible - but marking as breaking change because that will be the last release where you can load /weights - so we have to mention this in the next release notes

@odulcy-mindee models upload :)

@felixdittrich92 felixdittrich92 self-assigned this Sep 13, 2024
@felixdittrich92 felixdittrich92 added topic: documentation Improvements or additions to documentation module: models Related to doctr.models ext: tests Related to tests folder type: breaking change Introducing a breaking change framework: tensorflow Related to TensorFlow backend type: misc Miscellaneous ext: docs Related to docs folder labels Sep 13, 2024
@felixdittrich92 felixdittrich92 added this to the 0.9.1 milestone Sep 13, 2024
Copy link

codecov bot commented Sep 13, 2024

Codecov Report

Attention: Patch coverage is 98.52941% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.43%. Comparing base (df762ed) to head (d2494ed).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...etection/differentiable_binarization/tensorflow.py 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1724      +/-   ##
==========================================
- Coverage   96.47%   96.43%   -0.04%     
==========================================
  Files         164      164              
  Lines        7823     7823              
==========================================
- Hits         7547     7544       -3     
- Misses        276      279       +3     
Flag Coverage Δ
unittests 96.43% <98.52%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@felixdittrich92
Copy link
Contributor Author

@odulcy-mindee I see two options:

  1. We go step by step with smaller PRs until everything works as expected
  2. We iterate on this PR and have one giant PR at the end 😅

What do you prefer ?

@felixdittrich92
Copy link
Contributor Author

Next steps:

  • try to remove building models first by passing a dummy input -> build and get_config methods
  • test by upgrading to tf >= 2.16 + keras v3 (NOTE: This will be a breaking change -> 1.0.0 - only possible after the next release)
  • update training pipeline -> To fix the Dataloader part

@felixdittrich92
Copy link
Contributor Author

#1461

@@ -184,6 +184,8 @@ def main(args):

# Resume weights
if isinstance(args.resume, str):
# Build the model first to load the weights
_ = model(tf.zeros((1, args.input_size, args.input_size, 3)), training=False)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't you need to set training=True somewhere after load_weights ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No that's only to build the model training=True is handled in the training scripts :)

And i hope i can remove this part with the next follow up PR where i need to implement the def build methods for most of the layers 😅

tests/tensorflow/test_models_utils_tf.py Outdated Show resolved Hide resolved
doctr/models/recognition/vitstr/tensorflow.py Outdated Show resolved Hide resolved
doctr/models/recognition/vitstr/tensorflow.py Outdated Show resolved Hide resolved
doctr/models/recognition/sar/tensorflow.py Outdated Show resolved Hide resolved
doctr/models/classification/mobilenet/tensorflow.py Outdated Show resolved Hide resolved
doctr/models/classification/mobilenet/tensorflow.py Outdated Show resolved Hide resolved
doctr/models/classification/mobilenet/tensorflow.py Outdated Show resolved Hide resolved
doctr/models/classification/mobilenet/tensorflow.py Outdated Show resolved Hide resolved
doctr/models/classification/mobilenet/tensorflow.py Outdated Show resolved Hide resolved
if not params_path.is_dir() or overwrite:
with ZipFile(archive_path, "r") as f:
f.extractall(path=params_path)
# Build the model
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@odulcy-mindee
I hope i can remove this in a follow up PR if all models have build and get_config methods where it's required

tests/tensorflow/test_models_factory.py Outdated Show resolved Hide resolved
@@ -184,6 +184,8 @@ def main(args):

# Resume weights
if isinstance(args.resume, str):
# Build the model first to load the weights
_ = model(tf.zeros((1, args.input_size, args.input_size, 3)), training=False)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No that's only to build the model training=True is handled in the training scripts :)

And i hope i can remove this part with the next follow up PR where i need to implement the def build methods for most of the layers 😅

@@ -56,6 +56,8 @@ tf = [
# cf. https://github.com/mindee/doctr/pull/1461
"tensorflow>=2.11.0,<2.16.0",
"tf2onnx>=1.16.0,<2.0.0", # cf. https://github.com/onnx/tensorflow-onnx/releases/tag/v1.16.0
# TODO: This is a temporary fix until we can upgrade to a newer version of tensorflow
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Temp pin numpy until we can upgrade tensorflow to fix the issue with poetry and the failing API test (Will have effect after merging)

@felixdittrich92 felixdittrich92 merged commit dccc26b into mindee:main Oct 1, 2024
79 of 81 checks passed
@felixdittrich92 felixdittrich92 deleted the tf-h5-format branch October 1, 2024 08:34
@felixdittrich92 felixdittrich92 modified the milestones: 0.9.1, 0.10.0 Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ext: docs Related to docs folder ext: tests Related to tests folder framework: tensorflow Related to TensorFlow backend module: models Related to doctr.models topic: documentation Improvements or additions to documentation type: breaking change Introducing a breaking change type: misc Miscellaneous
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants