-
Notifications
You must be signed in to change notification settings - Fork 106
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
Convnext architecture dev #356
Open
Lorenzobattistela
wants to merge
9
commits into
tensorflow:development
Choose a base branch
from
Lorenzobattistela:convnext-architecture-dev
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8b8b5a3
[feat] adding convnext architecture
Lorenzobattistela 9444478
[feat] adding module to init
Lorenzobattistela 582079c
[test] convnext architecture testing
Lorenzobattistela 8ca0e5f
[fix] tf version check for convnext
Lorenzobattistela a1f10e4
[fix] convnext version check
Lorenzobattistela 300a1e1
[fix] remove useless version check
Lorenzobattistela c1ded82
[lint]
Lorenzobattistela 2b7a553
[lint] black on test file
Lorenzobattistela 3ac3017
[lint] isort
Lorenzobattistela File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
# Copyright 2021 The TensorFlow Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"ConvNeXt backbone for similarity learning" | ||
from __future__ import annotations | ||
|
||
import re | ||
|
||
import tensorflow as tf | ||
from tensorflow.keras import layers | ||
from tensorflow.keras.applications import convnext | ||
|
||
from tensorflow_similarity.layers import GeneralizedMeanPooling2D, MetricEmbedding | ||
from tensorflow_similarity.models import SimilarityModel | ||
|
||
CONVNEXT_ARCHITECTURE = { | ||
"TINY": convnext.ConvNeXtTiny, | ||
"SMALL": convnext.ConvNeXtSmall, | ||
"BASE": convnext.ConvNeXtBase, | ||
"LARGE": convnext.ConvNeXtLarge, | ||
"XLARGE": convnext.ConvNeXtXLarge, | ||
} | ||
|
||
|
||
def ConvNeXtSim( | ||
input_shape: tuple[int, int, int], | ||
embedding_size: int = 128, | ||
variant: str = "BASE", | ||
weights: str = "imagenet", | ||
trainable: str = "frozen", | ||
l2_norm: bool = True, | ||
include_top: bool = True, | ||
pooling: str = "gem", | ||
gem_p: float = 3.0, | ||
) -> SimilarityModel: | ||
""" "Build an ConvNeXt Model backbone for similarity learning | ||
[A ConvNet for the 2020s](https://arxiv.org/pdf/2201.03545.pdf) | ||
Args: | ||
input_shape: Size of the input image. Must match size of ConvNeXt version you use. | ||
See below for version input size. | ||
embedding_size: Size of the output embedding. Usually between 64 | ||
and 512. Defaults to 128. | ||
variant: Which Variant of the ConvNeXt to use. Defaults to "BASE". | ||
weights: Use pre-trained weights - the only available currently being | ||
imagenet. Defaults to "imagenet". | ||
trainable: Make the ConvNeXt backbone fully trainable or partially | ||
trainable. | ||
- "full" to make the entire backbone trainable, | ||
- "partial" to only make the last 3 block trainable | ||
- "frozen" to make it not trainable. | ||
l2_norm: If True and include_top is also True, then | ||
tfsim.layers.MetricEmbedding is used as the last layer, otherwise | ||
keras.layers.Dense is used. This should be true when using cosine | ||
distance. Defaults to True. | ||
include_top: Whether to include the fully-connected layer at the top | ||
of the network. Defaults to True. | ||
pooling: Optional pooling mode for feature extraction when | ||
include_top is False. Defaults to gem. | ||
- None means that the output of the model will be the 4D tensor | ||
output of the last convolutional layer. | ||
- avg means that global average pooling will be applied to the | ||
output of the last convolutional layer, and thus the output of the | ||
model will be a 2D tensor. | ||
- max means that global max pooling will be applied. | ||
- gem means that global GeneralizedMeanPooling2D will be applied. | ||
The gem_p param sets the contrast amount on the pooling. | ||
gem_p: Sets the power in the GeneralizedMeanPooling2D layer. A value | ||
of 1.0 is equivalent to GlobalMeanPooling2D, while larger values | ||
will increase the contrast between activations within each feature | ||
map, and a value of math.inf will be equivalent to MaxPool2d. | ||
""" | ||
inputs = layers.Input(shape=input_shape) | ||
x = inputs | ||
|
||
if variant not in CONVNEXT_ARCHITECTURE: | ||
raise ValueError("Unknown ConvNeXt variant. Valid TINY BASE LARGE SMALL XLARGE") | ||
|
||
x = build_convnext(variant, weights, trainable)(x) | ||
|
||
if pooling == "gem": | ||
x = GeneralizedMeanPooling2D(p=gem_p, name="gem_pool")(x) | ||
elif pooling == "avg": | ||
x = layers.GlobalAveragePooling2D(name="avg_pool")(x) | ||
elif pooling == "max": | ||
x = layers.GlobalMaxPooling2D(name="max_pool")(x) | ||
|
||
if include_top and pooling is not None: | ||
if l2_norm: | ||
outputs = MetricEmbedding(embedding_size)(x) | ||
else: | ||
outputs = layers.Dense(embedding_size)(x) | ||
else: | ||
outputs = x | ||
|
||
return SimilarityModel(inputs, outputs) | ||
|
||
|
||
def build_convnext(variant: str, weights: str | None = None, trainable: str = "full") -> tf.keras.Model: | ||
"""Build the requested ConvNeXt | ||
|
||
Args: | ||
variant: Which Variant of the ConvNeXt to use. | ||
weights: Use pre-trained weights - the only available currently being | ||
imagenet. | ||
trainable: Make the ConvNeXt backbone fully trainable or partially | ||
trainable. | ||
- "full" to make the entire backbone trainable, | ||
- "partial" to only make the last 3 block trainable | ||
- "frozen" to make it not trainable. | ||
Returns: | ||
The output layer of the convnext model | ||
""" | ||
convnext_fn = CONVNEXT_ARCHITECTURE[variant.upper()] | ||
convnext = convnext_fn(weights=weights, include_top=False) | ||
|
||
if trainable == "full": | ||
convnext.trainable = True | ||
elif trainable == "partial": | ||
convnext.trainable = True | ||
for layer in convnext.layers: | ||
# freeze all layeres befor the last 3 blocks | ||
if not re.search("^block[5,6,7]|^top", layer.name): | ||
layer.trainable = False | ||
elif trainable == "frozen": | ||
convnext.trainable = False | ||
else: | ||
raise ValueError(f"{trainable} is not a supported option for 'trainable'.") | ||
|
||
if weights: | ||
for layer in convnext.layers: | ||
if isinstance(layer, layers.experimental.SyncBatchNormalization): | ||
layer.trainable = False | ||
return convnext |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import re | ||
|
||
import pytest | ||
import tensorflow as tf | ||
|
||
|
||
MIN_TF_MAJOR_VERSION = 2 | ||
MIN_TF_MINOR_VERSION = 10 | ||
|
||
major_version = tf.__version__.split(".")[0] | ||
minor_version = tf.__version__.split(".")[1] | ||
|
||
convneXt = pytest.importorskip("tensorflow_similarity.architectures.convnext") | ||
|
||
TF_MAJOR_VERSION = int(tf.__version__.split(".")[0]) | ||
TF_MINOR_VERSION = int(tf.__version__.split(".")[1]) | ||
|
||
|
||
def tf_version_check(major_version, minor_version): | ||
if TF_MAJOR_VERSION <= major_version and TF_MINOR_VERSION < minor_version: | ||
return True | ||
|
||
return False | ||
|
||
|
||
def test_build_convnext_tiny_full(): | ||
input_layer = tf.keras.layers.Input((224, 224, 3)) | ||
output = convneXt.build_convnext("tiny", "imagenet", "full")(input_layer) | ||
|
||
convnext = output._keras_history.layer | ||
assert convnext.name == "convnext_tiny" | ||
assert convnext.trainable | ||
|
||
total_layer_count = 0 | ||
trainable_layer_count = 0 | ||
for layer in convnext._self_tracked_trackables: | ||
total_layer_count += 1 | ||
if layer.trainable: | ||
trainable_layer_count += 1 | ||
|
||
expected_total_layer_count = 151 | ||
expected_trainable_layer_count = 151 | ||
|
||
assert total_layer_count == expected_total_layer_count | ||
assert trainable_layer_count == expected_trainable_layer_count | ||
|
||
|
||
def test_build_convnext_small_partial(): | ||
input_layer = tf.keras.layers.Input((224, 224, 3)) | ||
output = convneXt.build_convnext("small", "imagenet", "partial")(input_layer) | ||
|
||
convnext = output._keras_history.layer | ||
assert convnext.name == "convnext_small" | ||
assert convnext.trainable | ||
|
||
total_layer_count = 0 | ||
trainable_layer_count = 0 | ||
for layer in convnext._self_tracked_trackables: | ||
total_layer_count += 1 | ||
if layer.trainable: | ||
trainable_layer_count += 1 | ||
|
||
expected_total_layer_count = 295 | ||
expected_trainable_layer_count = 0 | ||
|
||
assert total_layer_count == expected_total_layer_count | ||
assert trainable_layer_count == expected_trainable_layer_count | ||
|
||
|
||
def test_build_convnext_base_frozen(): | ||
input_layer = tf.keras.layers.Input((224, 224, 3)) | ||
output = convneXt.build_convnext("base", "imagenet", "frozen")(input_layer) | ||
|
||
convnext = output._keras_history.layer | ||
assert convnext.name == "convnext_base" | ||
assert not convnext.trainable | ||
|
||
total_layer_count = 0 | ||
trainable_layer_count = 0 | ||
for layer in convnext._self_tracked_trackables: | ||
total_layer_count += 1 | ||
if layer.trainable: | ||
trainable_layer_count += 1 | ||
|
||
expected_total_layer_count = 295 | ||
expected_trainable_layer_count = 0 | ||
|
||
assert total_layer_count == expected_total_layer_count | ||
assert trainable_layer_count == expected_trainable_layer_count | ||
|
||
|
||
def test_build_convnext_large_full(): | ||
input_layer = tf.keras.layers.Input((224, 224, 3)) | ||
output = convneXt.build_convnext("large", "imagenet", "full")(input_layer) | ||
|
||
convnext = output._keras_history.layer | ||
assert convnext.name == "convnext_large" | ||
assert convnext.trainable | ||
|
||
total_layer_count = 0 | ||
trainable_layer_count = 0 | ||
for layer in convnext._self_tracked_trackables: | ||
total_layer_count += 1 | ||
if layer.trainable: | ||
trainable_layer_count += 1 | ||
|
||
expected_total_layer_count = 295 | ||
expected_trainable_layer_count = 295 | ||
|
||
assert total_layer_count == expected_total_layer_count | ||
assert trainable_layer_count == expected_trainable_layer_count |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also trying out this architecture. But does this EfficientNetV2 layer naming apply to convnext?
The test also suggests
partial
is not being applied as expected since the number of trainable layers is 0 with partial.edit: another candidate might be
"convnext_base_stage_3_block_2"
, also unfreezing the last layer norm since it comes after the final block.This results in about 10% of weights being unfrozen and only the final block [1].
[1]
![image](https://private-user-images.githubusercontent.com/903366/270753311-d74518c5-3f6f-4c80-b5b0-95a6359d05b2.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5MTI5ODIsIm5iZiI6MTczODkxMjY4MiwicGF0aCI6Ii85MDMzNjYvMjcwNzUzMzExLWQ3NDUxOGM1LTNmNmYtNGM4MC1iNWIwLTk1YTYzNTlkMDViMi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwN1QwNzE4MDJaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1kMjI2ZjZkY2M2ZGE0YzE3ZmQxZTE5NmQ5Y2ExZmE5YjBhNzU0MDRjMGIwYTMzNDdiYzZkZGQ0NjBmYWI0NWZlJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.AwZQk8wwlteaaK_lgoSMYtXCJYVC6a1WMl7CTw2yZGQ)