Upload model
Browse files- config.json +2 -1
- configuration_efficientnet.py +5 -5
- model.safetensors +1 -1
- modeling_efficientnet.py +51 -45
config.json
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
{
|
| 2 |
-
"_name_or_path": "./efficientnet/temp",
|
| 3 |
"architectures": [
|
| 4 |
"EfficientNetModelForImageClassification"
|
| 5 |
],
|
|
@@ -7,8 +6,10 @@
|
|
| 7 |
"AutoConfig": "configuration_efficientnet.EfficientNetConfig",
|
| 8 |
"AutoModelForImageClassification": "modeling_efficientnet.EfficientNetModelForImageClassification"
|
| 9 |
},
|
|
|
|
| 10 |
"model_name": "efficientnet_b1",
|
| 11 |
"model_type": "efficientnet",
|
|
|
|
| 12 |
"pretrained": true,
|
| 13 |
"torch_dtype": "float32",
|
| 14 |
"transformers_version": "4.49.0"
|
|
|
|
| 1 |
{
|
|
|
|
| 2 |
"architectures": [
|
| 3 |
"EfficientNetModelForImageClassification"
|
| 4 |
],
|
|
|
|
| 6 |
"AutoConfig": "configuration_efficientnet.EfficientNetConfig",
|
| 7 |
"AutoModelForImageClassification": "modeling_efficientnet.EfficientNetModelForImageClassification"
|
| 8 |
},
|
| 9 |
+
"global_pool": "avg",
|
| 10 |
"model_name": "efficientnet_b1",
|
| 11 |
"model_type": "efficientnet",
|
| 12 |
+
"num_classes": 1000,
|
| 13 |
"pretrained": true,
|
| 14 |
"torch_dtype": "float32",
|
| 15 |
"transformers_version": "4.49.0"
|
configuration_efficientnet.py
CHANGED
|
@@ -1,10 +1,6 @@
|
|
| 1 |
from transformers.configuration_utils import PretrainedConfig
|
| 2 |
-
|
| 3 |
-
from optimum.utils.normalized_config import NormalizedVisionConfig
|
| 4 |
-
from optimum.utils.input_generators import DummyVisionInputGenerator
|
| 5 |
from optimum.exporters.onnx.model_configs import ViTOnnxConfig
|
| 6 |
-
|
| 7 |
-
from typing import OrderedDict, Dict
|
| 8 |
|
| 9 |
MODEL_NAMES = [
|
| 10 |
'efficientnet_b0',
|
|
@@ -26,6 +22,8 @@ class EfficientNetConfig(PretrainedConfig):
|
|
| 26 |
self,
|
| 27 |
model_name: str = 'efficientnet_b0',
|
| 28 |
pretrained: bool = False,
|
|
|
|
|
|
|
| 29 |
**kwargs
|
| 30 |
):
|
| 31 |
|
|
@@ -34,6 +32,8 @@ class EfficientNetConfig(PretrainedConfig):
|
|
| 34 |
|
| 35 |
self.model_name = model_name
|
| 36 |
self.pretrained = pretrained
|
|
|
|
|
|
|
| 37 |
|
| 38 |
super().__init__(**kwargs)
|
| 39 |
|
|
|
|
| 1 |
from transformers.configuration_utils import PretrainedConfig
|
|
|
|
|
|
|
|
|
|
| 2 |
from optimum.exporters.onnx.model_configs import ViTOnnxConfig
|
| 3 |
+
from typing import Dict
|
|
|
|
| 4 |
|
| 5 |
MODEL_NAMES = [
|
| 6 |
'efficientnet_b0',
|
|
|
|
| 22 |
self,
|
| 23 |
model_name: str = 'efficientnet_b0',
|
| 24 |
pretrained: bool = False,
|
| 25 |
+
num_classes: int = 1000,
|
| 26 |
+
global_pool: str = 'avg',
|
| 27 |
**kwargs
|
| 28 |
):
|
| 29 |
|
|
|
|
| 32 |
|
| 33 |
self.model_name = model_name
|
| 34 |
self.pretrained = pretrained
|
| 35 |
+
self.num_classes = num_classes
|
| 36 |
+
self.global_pool = global_pool
|
| 37 |
|
| 38 |
super().__init__(**kwargs)
|
| 39 |
|
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 31474952
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f1813e3c9f91308823701bc60e65f1417a1bc776274096c60784f4756a5a1d11
|
| 3 |
size 31474952
|
modeling_efficientnet.py
CHANGED
|
@@ -1,46 +1,52 @@
|
|
| 1 |
-
from torch import nn
|
| 2 |
-
|
| 3 |
-
from transformers import PreTrainedModel
|
| 4 |
-
from transformers.modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
|
| 5 |
-
from timm import create_model
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
class EfficientNetModel(PreTrainedModel):
|
| 10 |
-
config_class = EfficientNetConfig
|
| 11 |
-
|
| 12 |
-
def __init__(self, config):
|
| 13 |
-
super().__init__(config)
|
| 14 |
-
|
| 15 |
-
self.config = config
|
| 16 |
-
self.model = create_model(config.model_name,
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
]
|
|
|
|
| 1 |
+
from torch import nn
|
| 2 |
+
|
| 3 |
+
from transformers import PreTrainedModel
|
| 4 |
+
from transformers.modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
|
| 5 |
+
from timm import create_model
|
| 6 |
+
|
| 7 |
+
from configuration_efficientnet import EfficientNetConfig
|
| 8 |
+
|
| 9 |
+
class EfficientNetModel(PreTrainedModel):
|
| 10 |
+
config_class = EfficientNetConfig
|
| 11 |
+
|
| 12 |
+
def __init__(self, config):
|
| 13 |
+
super().__init__(config)
|
| 14 |
+
|
| 15 |
+
self.config = config
|
| 16 |
+
self.model = create_model(config.model_name,
|
| 17 |
+
pretrained = config.pretrained,
|
| 18 |
+
num_classes = config.num_classes,
|
| 19 |
+
global_pool = config.global_pool)
|
| 20 |
+
|
| 21 |
+
def forward(self, pixel_values):
|
| 22 |
+
last_hidden_state = self.model.forward_features(pixel_values)
|
| 23 |
+
return BaseModelOutputWithPoolingAndNoAttention(
|
| 24 |
+
last_hidden_state = last_hidden_state
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
class EfficientNetModelForImageClassification(PreTrainedModel):
|
| 28 |
+
config_class = EfficientNetConfig
|
| 29 |
+
|
| 30 |
+
def __init__(self, config):
|
| 31 |
+
super().__init__(config)
|
| 32 |
+
|
| 33 |
+
self.config = config
|
| 34 |
+
self.model = create_model(config.model_name,
|
| 35 |
+
pretrained = config.pretrained,
|
| 36 |
+
num_classes = config.num_classes,
|
| 37 |
+
global_pool = config.global_pool)
|
| 38 |
+
|
| 39 |
+
def forward(self, pixel_values, labels=None):
|
| 40 |
+
logits = self.model(pixel_values)
|
| 41 |
+
loss = None
|
| 42 |
+
if labels is not None:
|
| 43 |
+
loss = nn.CrossEntropyLoss(logits, labels)
|
| 44 |
+
return ImageClassifierOutputWithNoAttention(
|
| 45 |
+
loss = loss,
|
| 46 |
+
logits = logits
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
__all__ = [
|
| 50 |
+
"EfficientNetModel",
|
| 51 |
+
"EfficientNetModelForImageClassification"
|
| 52 |
]
|