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

Add TF ResNet model #17427

Merged
merged 47 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
b8144f2
Rought TF conversion outline
amyeroberts May 16, 2022
261860e
Merge branch 'main' into add-tf-resnet
amyeroberts May 25, 2022
0313ab4
Tidy up
amyeroberts May 26, 2022
373a549
Merge branch 'main' into add-tf-resnet
amyeroberts May 30, 2022
4d0ece8
Merge branch 'main' into add-tf-resnet
amyeroberts Jun 1, 2022
966d196
Merge branch 'main' into add-tf-resnet
amyeroberts Jun 6, 2022
f44f1a3
Fix padding differences between layers
amyeroberts Jun 6, 2022
5dd5205
Merge branch 'main' into add-tf-resnet
amyeroberts Jun 6, 2022
ec5122b
Resolve conflicts
amyeroberts Jun 9, 2022
d48777d
Merge branch 'main' into add-tf-resnet
amyeroberts Jun 9, 2022
3bd197b
Add back embedder - whoops
amyeroberts Jun 10, 2022
ccc06ec
Merge in main
amyeroberts Jun 10, 2022
a8664a1
Match test file to main
amyeroberts Jun 10, 2022
456a598
Match upstream test file
amyeroberts Jun 10, 2022
a5584e1
Correctly pass and assign image_size parameter
amyeroberts Jun 13, 2022
3cd431b
Add in MainLayer
amyeroberts Jun 13, 2022
68ca81d
Correctly name layer
amyeroberts Jun 13, 2022
0d94522
Tidy up AdaptivePooler
amyeroberts Jun 14, 2022
a2cd69b
Small tidy-up
amyeroberts Jun 14, 2022
a7711ec
Merge branch 'main' into add-tf-resnet
amyeroberts Jun 14, 2022
e2d6055
Merge branch 'add-tf-resnet' of github.com:amyeroberts/transformers i…
amyeroberts Jun 14, 2022
479dd4b
Change AdaptiveAvgPool
amyeroberts Jun 17, 2022
6102a30
Use updated AdaptiveAvgPool
amyeroberts Jun 21, 2022
5ad30f0
Make AdaptiveAvgPool compatible with CPU
amyeroberts Jun 21, 2022
a921d5e
Remove image_size from configuration
amyeroberts Jun 21, 2022
93c1514
Fixup
amyeroberts Jun 21, 2022
9500fc0
Merge branch 'main' into add-tf-resnet
amyeroberts Jun 21, 2022
76a7379
Tensorflow -> TensorFlow
amyeroberts Jun 22, 2022
b6b429f
Fix pt references in tests
amyeroberts Jun 22, 2022
14d9949
Apply suggestions from code review - grammar and wording
amyeroberts Jun 22, 2022
b9c9904
Add TFResNet to doc tests
amyeroberts Jun 22, 2022
7bf8b33
PR comments - GlobalAveragePooling and clearer comments
amyeroberts Jun 22, 2022
1eb8b44
Remove unused import
amyeroberts Jun 22, 2022
056a27f
Add in keepdims argument
amyeroberts Jun 22, 2022
6ecaf39
Merge branch 'main' into add-tf-resnet
amyeroberts Jun 23, 2022
43235dc
Add num_channels check
amyeroberts Jun 23, 2022
07e2606
grammar fix: by -> of
amyeroberts Jun 28, 2022
7481301
Merge branch 'main' into add-tf-resnet
amyeroberts Jun 28, 2022
ef1df6d
Remove transposes - keep NHWC throughout forward pass
amyeroberts Jun 28, 2022
d1aaf59
Merge branch 'add-tf-resnet' of github.com:amyeroberts/transformers i…
amyeroberts Jun 28, 2022
1bf76d1
Fixup look sharp
amyeroberts Jun 28, 2022
43b3326
Merge branch 'main' into add-tf-resnet
amyeroberts Jun 29, 2022
117ae19
Resolve conflicts after RegNet merge
amyeroberts Jun 29, 2022
583583b
Merge branch 'main' into add-tf-resnet
amyeroberts Jun 29, 2022
810d643
Merge branch 'main' into add-tf-resnet
amyeroberts Jun 29, 2022
b47480b
Add missing layer names
amyeroberts Jun 30, 2022
dc963f1
Final tidy up - remove from_pt now weights on hub
amyeroberts Jul 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/en/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ Flax), PyTorch, and/or TensorFlow.
| Reformer | ✅ | ✅ | ✅ | ❌ | ❌ |
| RegNet | ❌ | ❌ | ✅ | ❌ | ❌ |
| RemBERT | ✅ | ✅ | ✅ | ✅ | ❌ |
| ResNet | ❌ | ❌ | ✅ | | ❌ |
| ResNet | ❌ | ❌ | ✅ | | ❌ |
| RetriBERT | ✅ | ✅ | ✅ | ❌ | ❌ |
| RoBERTa | ✅ | ✅ | ✅ | ✅ | ✅ |
| RoFormer | ✅ | ✅ | ✅ | ✅ | ✅ |
Expand Down
16 changes: 14 additions & 2 deletions docs/source/en/model_doc/resnet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The figure below illustrates the architecture of ResNet. Taken from the [origina

<img width="600" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/resnet_architecture.png"/>

This model was contributed by [Francesco](https://huggingface.co/Francesco). The original code can be found [here](https://github.com/KaimingHe/deep-residual-networks).
This model was contributed by [Francesco](https://huggingface.co/Francesco). The TensorFlow version of this model was added by [amyeroberts](https://huggingface.co/amyeroberts). The original code can be found [here](https://github.com/KaimingHe/deep-residual-networks).

## ResNetConfig

Expand All @@ -47,4 +47,16 @@ This model was contributed by [Francesco](https://huggingface.co/Francesco). The
## ResNetForImageClassification

[[autodoc]] ResNetForImageClassification
- forward
- forward


## TFResNetModel

[[autodoc]] TFResNetModel
- call


## TFResNetForImageClassification

[[autodoc]] TFResNetForImageClassification
- call
14 changes: 14 additions & 0 deletions src/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,14 @@
"TFRemBertPreTrainedModel",
]
)
_import_structure["models.resnet"].extend(
[
"TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST",
"TFResNetForImageClassification",
"TFResNetModel",
"TFResNetPreTrainedModel",
]
)
_import_structure["models.roberta"].extend(
[
"TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST",
Expand Down Expand Up @@ -4563,6 +4571,12 @@
TFRemBertModel,
TFRemBertPreTrainedModel,
)
from .models.resnet import (
TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST,
TFResNetForImageClassification,
TFResNetModel,
TFResNetPreTrainedModel,
)
from .models.roberta import (
TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
TFRobertaForCausalLM,
Expand Down
62 changes: 62 additions & 0 deletions src/transformers/modeling_tf_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ class TFBaseModelOutput(ModelOutput):
attentions: Optional[Tuple[tf.Tensor]] = None


@dataclass
class TFBaseModelOutputWithNoAttention(ModelOutput):
"""
Base class for model's outputs, with potential hidden states.

Args:
last_hidden_state (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
Sequence of hidden-states at the output of the last layer of the model.
hidden_states (`tuple(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
Tuple of `tf.Tensor` (one for the output of the embeddings, if the model has an embedding layer, + one for
the output of each layer) of shape `(batch_size, num_channels, height, width)`.

Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
"""

last_hidden_state: tf.Tensor = None
hidden_states: Optional[Tuple[tf.Tensor, ...]] = None


@dataclass
class TFBaseModelOutputWithPooling(ModelOutput):
"""
Expand Down Expand Up @@ -80,6 +99,28 @@ class TFBaseModelOutputWithPooling(ModelOutput):
attentions: Optional[Tuple[tf.Tensor]] = None


@dataclass
class TFBaseModelOutputWithPoolingAndNoAttention(ModelOutput):
"""
Base class for model's outputs that also contains a pooling of the last hidden states.

Args:
last_hidden_state (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
Sequence of hidden-states at the output of the last layer of the model.
pooler_output (`tf.Tensor` of shape `(batch_size, hidden_size)`):
Last layer hidden-state after a pooling operation on the spatial dimensions.
hidden_states (`tuple(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
Tuple of `tf.Tensor` (one for the output of the embeddings, if the model has an embedding layer, + one for
the output of each layer) of shape `(batch_size, num_channels, height, width)`.

Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
"""

last_hidden_state: tf.Tensor = None
pooler_output: tf.Tensor = None
hidden_states: Optional[Tuple[tf.Tensor, ...]] = None


@dataclass
class TFBaseModelOutputWithPoolingAndCrossAttentions(ModelOutput):
"""
Expand Down Expand Up @@ -825,3 +866,24 @@ class TFSequenceClassifierOutputWithPast(ModelOutput):
past_key_values: Optional[List[tf.Tensor]] = None
hidden_states: Optional[Tuple[tf.Tensor]] = None
attentions: Optional[Tuple[tf.Tensor]] = None


@dataclass
class TFImageClassifierOutputWithNoAttention(ModelOutput):
"""
Base class for outputs of image classification models.

Args:
loss (`tf.Tensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
Classification (or regression if config.num_labels==1) loss.
logits (`tf.Tensor` of shape `(batch_size, config.num_labels)`):
Classification (or regression if config.num_labels==1) scores (before SoftMax).
hidden_states (`tuple(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
Tuple of `tf.Tensor` (one for the output of the embeddings, if the model has an embedding layer, + one for
the output of each stage) of shape `(batch_size, num_channels, height, width)`. Hidden-states (also called
feature maps) of the model at the output of each stage.
"""

loss: Optional[tf.Tensor] = None
logits: tf.Tensor = None
hidden_states: Optional[Tuple[tf.Tensor, ...]] = None
2 changes: 2 additions & 0 deletions src/transformers/models/auto/modeling_tf_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
("opt", "TFOPTModel"),
("pegasus", "TFPegasusModel"),
("rembert", "TFRemBertModel"),
("resnet", "TFResNetModel"),
("roberta", "TFRobertaModel"),
("roformer", "TFRoFormerModel"),
("speech_to_text", "TFSpeech2TextModel"),
Expand Down Expand Up @@ -173,6 +174,7 @@
# Model for Image-classsification
("convnext", "TFConvNextForImageClassification"),
("data2vec-vision", "TFData2VecVisionForImageClassification"),
("resnet", "TFResNetForImageClassification"),
("swin", "TFSwinForImageClassification"),
("vit", "TFViTForImageClassification"),
]
Expand Down
28 changes: 27 additions & 1 deletion src/transformers/models/resnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


_import_structure = {
Expand All @@ -38,6 +38,19 @@
"ResNetPreTrainedModel",
]

try:
if not is_tf_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
_import_structure["modeling_tf_resnet"] = [
"TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST",
"TFResNetForImageClassification",
"TFResNetModel",
"TFResNetPreTrainedModel",
]


if TYPE_CHECKING:
from .configuration_resnet import RESNET_PRETRAINED_CONFIG_ARCHIVE_MAP, ResNetConfig, ResNetOnnxConfig
Expand All @@ -55,6 +68,19 @@
ResNetPreTrainedModel,
)

try:
if not is_tf_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
from .modeling_tf_resnet import (
TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST,
TFResNetForImageClassification,
TFResNetModel,
TFResNetPreTrainedModel,
)


else:
import sys
Expand Down
Loading