Thastp commited on
Commit
469beaf
·
verified ·
1 Parent(s): fbcd6c6

Upload model

Browse files
config.json CHANGED
@@ -1,11 +1,10 @@
1
  {
2
- "_name_or_path": "./efficientnet/temp",
3
  "architectures": [
4
- "EfficientNetModelForImageClassification"
5
  ],
6
  "auto_map": {
7
  "AutoConfig": "configuration_efficientnet.EfficientNetConfig",
8
- "AutoModelForImageClassification": "modeling_efficientnet.EfficientNetModelForImageClassification"
9
  },
10
  "global_pool": "avg",
11
  "model_name": "efficientnet_b1",
 
1
  {
 
2
  "architectures": [
3
+ "EfficientNetModel"
4
  ],
5
  "auto_map": {
6
  "AutoConfig": "configuration_efficientnet.EfficientNetConfig",
7
+ "AutoModel": "modeling_efficientnet.EfficientNetModel"
8
  },
9
  "global_pool": "avg",
10
  "model_name": "efficientnet_b1",
configuration_efficientnet.py CHANGED
@@ -1,6 +1,7 @@
 
 
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',
@@ -15,18 +16,18 @@ MODEL_NAMES = [
15
  'efficientnet_l2'
16
  ]
17
 
 
18
  class EfficientNetConfig(PretrainedConfig):
19
  model_type = 'efficientnet'
20
 
21
  def __init__(
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
-
30
  if model_name not in MODEL_NAMES:
31
  raise ValueError(f'`model_name` must be one of these: {MODEL_NAMES}, but got {model_name}')
32
 
@@ -34,9 +35,9 @@ class EfficientNetConfig(PretrainedConfig):
34
  self.pretrained = pretrained
35
  self.num_classes = num_classes
36
  self.global_pool = global_pool
37
-
38
  super().__init__(**kwargs)
39
 
 
40
  class EfficientNetOnnxConfig(ViTOnnxConfig):
41
  @property
42
  def outputs(self) -> Dict[str, Dict[int, str]]:
@@ -47,6 +48,7 @@ class EfficientNetOnnxConfig(ViTOnnxConfig):
47
 
48
  return common_outputs
49
 
 
50
  __all__ = [
51
  'MODEL_NAMES',
52
  'EfficientNetConfig',
 
1
+ from typing import Dict
2
+
3
  from transformers.configuration_utils import PretrainedConfig
4
  from optimum.exporters.onnx.model_configs import ViTOnnxConfig
 
5
 
6
  MODEL_NAMES = [
7
  'efficientnet_b0',
 
16
  'efficientnet_l2'
17
  ]
18
 
19
+
20
  class EfficientNetConfig(PretrainedConfig):
21
  model_type = 'efficientnet'
22
 
23
  def __init__(
24
+ self,
25
+ model_name: str = 'efficientnet_b0',
26
+ pretrained: bool = False,
27
+ num_classes: int = 1000,
28
+ global_pool: str = 'avg',
29
+ **kwargs,
30
+ ):
 
31
  if model_name not in MODEL_NAMES:
32
  raise ValueError(f'`model_name` must be one of these: {MODEL_NAMES}, but got {model_name}')
33
 
 
35
  self.pretrained = pretrained
36
  self.num_classes = num_classes
37
  self.global_pool = global_pool
 
38
  super().__init__(**kwargs)
39
 
40
+
41
  class EfficientNetOnnxConfig(ViTOnnxConfig):
42
  @property
43
  def outputs(self) -> Dict[str, Dict[int, str]]:
 
48
 
49
  return common_outputs
50
 
51
+
52
  __all__ = [
53
  'MODEL_NAMES',
54
  'EfficientNetConfig',
model.safetensors CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:58a67887d3b08b775276e0ed4ced6c66aeb03b0d9ad069a98a17440cc41557e6
3
  size 31474952
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f1813e3c9f91308823701bc60e65f1417a1bc776274096c60784f4756a5a1d11
3
  size 31474952
modeling_efficientnet.py CHANGED
@@ -1,52 +1,126 @@
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
  ]
 
1
+ from torch import nn, Tensor, tensor
2
+ from typing import Union, List, Optional
3
+
4
+ from transformers import PreTrainedModel
5
+ from transformers.modeling_outputs import (
6
+ BaseModelOutputWithPoolingAndNoAttention,
7
+ ImageClassifierOutputWithNoAttention
8
+ )
9
+ from timm import create_model
10
+
11
+ from .configuration_efficientnet import EfficientNetConfig
12
+
13
+
14
+ class EfficientNetModel(PreTrainedModel):
15
+ """
16
+ EfficientNet model wrapper using Hugging Face's PreTrainedModel.
17
+
18
+ This class initializes an EfficientNet model from `timm` library
19
+ and defines a forward method that extracts feature representations.
20
+
21
+ Attributes
22
+ ----------
23
+ config:
24
+ Configuration object containing model parameters.
25
+ model:
26
+ Instantiated EfficientNet model.
27
+ """
28
+ config_class = EfficientNetConfig
29
+
30
+ def __init__(self, config):
31
+ super().__init__(config)
32
+ self.config = config
33
+ self.model = create_model(
34
+ config.model_name,
35
+ pretrained = config.pretrained,
36
+ num_classes = config.num_classes,
37
+ global_pool = config.global_pool,
38
+ )
39
+
40
+ def forward(self, pixel_values: Tensor) -> BaseModelOutputWithPoolingAndNoAttention:
41
+ """
42
+ Parameters
43
+ ----------
44
+ pixel_values : torch.Tensor
45
+ Input tensor representing image pixel values.
46
+
47
+ Returns
48
+ -------
49
+ BaseModelOutputWithPoolingAndNoAttention
50
+ Object containing the `last_hidden_state` and `pooled_output`.
51
+ """
52
+ last_hidden_state = self.model.forward_features(pixel_values)
53
+ pooler_output = self.model.forward_head(last_hidden_state, pre_logits=True)
54
+
55
+ return BaseModelOutputWithPoolingAndNoAttention(
56
+ last_hidden_state = last_hidden_state,
57
+ pooler_output=pooler_output
58
+ )
59
+
60
+
61
+ class EfficientNetModelForImageClassification(PreTrainedModel):
62
+ """
63
+ EfficientNet model wrapper using Hugging Face's PreTrainedModel.
64
+
65
+ This class initializes an EfficientNet model from `timm` library
66
+ and defines a forward method that return logits.
67
+
68
+ It supports training when labels are provided
69
+
70
+ Attributes
71
+ ----------
72
+ config :
73
+ Configuration object containing model parameters.
74
+ model :
75
+ Instantiated EfficientNet model.
76
+ """
77
+ config_class = EfficientNetConfig
78
+
79
+ def __init__(self, config):
80
+ super().__init__(config)
81
+ self.config = config
82
+ self.model = create_model(
83
+ config.model_name,
84
+ pretrained = config.pretrained,
85
+ num_classes = config.num_classes,
86
+ global_pool = config.global_pool,
87
+ )
88
+
89
+ def forward(
90
+ self,
91
+ pixel_values: Tensor,
92
+ labels: Optional[Union[List[int], Tensor]] = None
93
+ ) -> ImageClassifierOutputWithNoAttention:
94
+ """
95
+ Parameters
96
+ ----------
97
+ pixel_values : torch.Tensor
98
+ Input tensor representing image pixel values.
99
+ labels : Optional[Union[List[int], torch.Tensor]]
100
+ Ground truth labels for training and computing loss.
101
+ List of integers/tensor representing class IDs.
102
+
103
+ Returns
104
+ -------
105
+ ImageClassifierOutputWithNoAttention
106
+ Object containing `logits` and `loss`.
107
+ """
108
+ self.model.training = False if labels is None else True
109
+
110
+ logits = self.model(pixel_values)
111
+
112
+ loss = None
113
+ if self.model.training:
114
+ labels = tensor(labels)
115
+ ce_loss = nn.CrossEntropyLoss()
116
+ loss = ce_loss(logits, labels)
117
+
118
+ return ImageClassifierOutputWithNoAttention(
119
+ loss = loss,
120
+ logits = logits,
121
+ )
122
+
123
+ __all__ = [
124
+ "EfficientNetModel",
125
+ "EfficientNetModelForImageClassification"
126
  ]