Upload model
Browse files- config.json +3 -4
- modeling_efficientnet.py +14 -8
config.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
{
|
| 2 |
"architectures": [
|
| 3 |
-
"
|
| 4 |
],
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration_efficientnet.EfficientNetConfig",
|
| 7 |
-
"AutoModel": "modeling_efficientnet.EfficientNetModel"
|
| 8 |
-
"AutoModelForImageClassification": "modeling_efficientnet.EfficientNetModelForImageClassification"
|
| 9 |
},
|
| 10 |
"global_pool": "avg",
|
| 11 |
"model_name": "efficientnet_b0",
|
|
@@ -13,5 +12,5 @@
|
|
| 13 |
"num_classes": 1000,
|
| 14 |
"pretrained": true,
|
| 15 |
"torch_dtype": "float32",
|
| 16 |
-
"transformers_version": "4.
|
| 17 |
}
|
|
|
|
| 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_b0",
|
|
|
|
| 12 |
"num_classes": 1000,
|
| 13 |
"pretrained": true,
|
| 14 |
"torch_dtype": "float32",
|
| 15 |
+
"transformers_version": "4.50.3"
|
| 16 |
}
|
modeling_efficientnet.py
CHANGED
|
@@ -85,11 +85,22 @@ class EfficientNetModelForImageClassification(PreTrainedModel):
|
|
| 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
|
|
@@ -105,21 +116,16 @@ class EfficientNetModelForImageClassification(PreTrainedModel):
|
|
| 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 =
|
| 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"
|
|
|
|
| 85 |
num_classes = config.num_classes,
|
| 86 |
global_pool = config.global_pool,
|
| 87 |
)
|
| 88 |
+
|
| 89 |
+
def compute_loss(self, logits, labels=None):
|
| 90 |
+
loss = None
|
| 91 |
+
if labels is None:
|
| 92 |
+
pass
|
| 93 |
+
else:
|
| 94 |
+
labels = tensor(labels)
|
| 95 |
+
ce_loss = nn.CrossEntropyLoss()
|
| 96 |
+
loss = ce_loss(logits, labels)
|
| 97 |
+
|
| 98 |
+
return loss
|
| 99 |
|
| 100 |
def forward(
|
| 101 |
self,
|
| 102 |
pixel_values: Tensor,
|
| 103 |
+
labels: Optional[Union[List[int], Tensor]] = None,
|
| 104 |
) -> ImageClassifierOutputWithNoAttention:
|
| 105 |
"""
|
| 106 |
Parameters
|
|
|
|
| 116 |
ImageClassifierOutputWithNoAttention
|
| 117 |
Object containing `logits` and `loss`.
|
| 118 |
"""
|
|
|
|
|
|
|
| 119 |
logits = self.model(pixel_values)
|
| 120 |
|
| 121 |
+
loss = self.compute_loss(logits, labels)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
return ImageClassifierOutputWithNoAttention(
|
| 124 |
loss = loss,
|
| 125 |
logits = logits,
|
| 126 |
)
|
| 127 |
|
| 128 |
+
|
| 129 |
__all__ = [
|
| 130 |
"EfficientNetModel",
|
| 131 |
"EfficientNetModelForImageClassification"
|