cesarali commited on
Commit
e4ec6e6
1 Parent(s): 7b54eea

Upload model

Browse files
Files changed (4) hide show
  1. config.json +26 -0
  2. configuration_resnet.py +34 -0
  3. modeling_resnet.py +54 -0
  4. pytorch_model.bin +3 -0
config.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ResnetModelForImageClassification"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration_resnet.ResnetConfig",
7
+ "AutoModelForImageClassification": "modeling_resnet.ResnetModelForImageClassification"
8
+ },
9
+ "avg_down": true,
10
+ "base_width": 64,
11
+ "block_type": "bottleneck",
12
+ "cardinality": 1,
13
+ "input_channels": 3,
14
+ "layers": [
15
+ 3,
16
+ 4,
17
+ 6,
18
+ 3
19
+ ],
20
+ "model_type": "resnet",
21
+ "num_classes": 1000,
22
+ "stem_type": "deep",
23
+ "stem_width": 32,
24
+ "torch_dtype": "float32",
25
+ "transformers_version": "4.29.2"
26
+ }
configuration_resnet.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+ from typing import List
3
+
4
+ class ResnetConfig(PretrainedConfig):
5
+ model_type = "resnet"
6
+
7
+ def __init__(
8
+ self,
9
+ block_type="bottleneck",
10
+ layers: List[int] = [3, 4, 6, 3],
11
+ num_classes: int = 1000,
12
+ input_channels: int = 3,
13
+ cardinality: int = 1,
14
+ base_width: int = 64,
15
+ stem_width: int = 64,
16
+ stem_type: str = "",
17
+ avg_down: bool = False,
18
+ **kwargs,
19
+ ):
20
+ if block_type not in ["basic", "bottleneck"]:
21
+ raise ValueError(f"`block_type` must be 'basic' or bottleneck', got {block_type}.")
22
+ if stem_type not in ["", "deep", "deep-tiered"]:
23
+ raise ValueError(f"`stem_type` must be '', 'deep' or 'deep-tiered', got {stem_type}.")
24
+
25
+ self.block_type = block_type
26
+ self.layers = layers
27
+ self.num_classes = num_classes
28
+ self.input_channels = input_channels
29
+ self.cardinality = cardinality
30
+ self.base_width = base_width
31
+ self.stem_width = stem_width
32
+ self.stem_type = stem_type
33
+ self.avg_down = avg_down
34
+ super().__init__(**kwargs)
modeling_resnet.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import PreTrainedModel
3
+ from configuration_resnet import ResnetConfig
4
+ from timm.models.resnet import BasicBlock, Bottleneck, ResNet
5
+
6
+ BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck}
7
+
8
+
9
+ class ResnetModel(PreTrainedModel):
10
+ config_class = ResnetConfig
11
+
12
+ def __init__(self, config):
13
+ super().__init__(config)
14
+ block_layer = BLOCK_MAPPING[config.block_type]
15
+ self.model = ResNet(
16
+ block_layer,
17
+ config.layers,
18
+ num_classes=config.num_classes,
19
+ in_chans=config.input_channels,
20
+ cardinality=config.cardinality,
21
+ base_width=config.base_width,
22
+ stem_width=config.stem_width,
23
+ stem_type=config.stem_type,
24
+ avg_down=config.avg_down,
25
+ )
26
+
27
+ def forward(self, tensor):
28
+ return self.model.forward_features(tensor)
29
+
30
+
31
+ class ResnetModelForImageClassification(PreTrainedModel):
32
+ config_class = ResnetConfig
33
+
34
+ def __init__(self, config):
35
+ super().__init__(config)
36
+ block_layer = BLOCK_MAPPING[config.block_type]
37
+ self.model = ResNet(
38
+ block_layer,
39
+ config.layers,
40
+ num_classes=config.num_classes,
41
+ in_chans=config.input_channels,
42
+ cardinality=config.cardinality,
43
+ base_width=config.base_width,
44
+ stem_width=config.stem_width,
45
+ stem_type=config.stem_type,
46
+ avg_down=config.avg_down,
47
+ )
48
+
49
+ def forward(self, tensor, labels=None):
50
+ logits = self.model(tensor)
51
+ if labels is not None:
52
+ loss = torch.nn.cross_entropy(logits, labels)
53
+ return {"loss": loss, "logits": logits}
54
+ return {"logits": logits}
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4a945a1d4bcb0c80f4a944dd77dc32441d39c2c06fa67d650e9a9090fde8934b
3
+ size 102620157