BucketOfFish commited on
Commit
8269eca
·
1 Parent(s): 394e168

Upload model

Browse files
Files changed (4) hide show
  1. config.json +26 -0
  2. custom_configuration.py +42 -0
  3. custom_model.py +68 -0
  4. model.safetensors +3 -0
config.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ResnetModelForImageClassification"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "custom_configuration.ResnetConfig",
7
+ "AutoModelForImageClassification": "custom_model.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.36.2"
26
+ }
custom_configuration.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+ from typing import List
3
+
4
+
5
+ class ResnetConfig(PretrainedConfig):
6
+ model_type = "resnet" # not necessary unless you want to register model with auto classes
7
+
8
+ def __init__(
9
+ self,
10
+ block_type="bottleneck",
11
+ layers: List[int] = [3, 4, 6, 3],
12
+ num_classes: int = 1000,
13
+ input_channels: int = 3,
14
+ cardinality: int = 1,
15
+ base_width: int = 64,
16
+ stem_width: int = 64,
17
+ stem_type: str = "",
18
+ avg_down: bool = False,
19
+ **kwargs,
20
+ ):
21
+ if block_type not in ["basic", "bottleneck"]:
22
+ raise ValueError(f"`block_type` must be 'basic' or bottleneck', got {block_type}.")
23
+ if stem_type not in ["", "deep", "deep-tiered"]:
24
+ raise ValueError(f"`stem_type` must be '', 'deep' or 'deep-tiered', got {stem_type}.")
25
+
26
+ self.block_type = block_type
27
+ self.layers = layers
28
+ self.num_classes = num_classes
29
+ self.input_channels = input_channels
30
+ self.cardinality = cardinality
31
+ self.base_width = base_width
32
+ self.stem_width = stem_width
33
+ self.stem_type = stem_type
34
+ self.avg_down = avg_down
35
+ super().__init__(**kwargs)
36
+
37
+
38
+ if __name__ == "__main__":
39
+ resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True)
40
+ # resnet50d_config.save_pretrained("resnet50d_config")
41
+ # resnet50d_config = ResnetConfig.from_pretrained("resnet50d_config")
42
+ # resnet50d_config.push_to_hub("resnet50d_config")
custom_model.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import timm
2
+ import torch
3
+ from transformers import PreTrainedModel
4
+ from timm.models.resnet import BasicBlock, Bottleneck, ResNet
5
+
6
+ import custom_configuration
7
+
8
+
9
+ BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck}
10
+
11
+
12
+ class ResnetModel(PreTrainedModel):
13
+ config_class = custom_configuration.ResnetConfig # not necessary unless you want to register model with auto classes
14
+
15
+ def __init__(self, config):
16
+ super().__init__(config)
17
+ block_layer = BLOCK_MAPPING[config.block_type]
18
+ self.model = ResNet(
19
+ block_layer,
20
+ config.layers,
21
+ num_classes=config.num_classes,
22
+ in_chans=config.input_channels,
23
+ cardinality=config.cardinality,
24
+ base_width=config.base_width,
25
+ stem_width=config.stem_width,
26
+ stem_type=config.stem_type,
27
+ avg_down=config.avg_down,
28
+ )
29
+
30
+ def forward(self, tensor):
31
+ return self.model.forward_features(tensor)
32
+
33
+
34
+ class ResnetModelForImageClassification(PreTrainedModel):
35
+ config_class = custom_configuration.ResnetConfig # not necessary unless you want to register model with auto classes
36
+
37
+ def __init__(self, config):
38
+ super().__init__(config)
39
+ block_layer = BLOCK_MAPPING[config.block_type]
40
+ self.model = ResNet(
41
+ block_layer,
42
+ config.layers,
43
+ num_classes=config.num_classes,
44
+ in_chans=config.input_channels,
45
+ cardinality=config.cardinality,
46
+ base_width=config.base_width,
47
+ stem_width=config.stem_width,
48
+ stem_type=config.stem_type,
49
+ avg_down=config.avg_down,
50
+ )
51
+
52
+ def forward(self, tensor, labels=None):
53
+ logits = self.model(tensor)
54
+ if labels is not None:
55
+ loss = torch.nn.cross_entropy(logits, labels)
56
+ return {"loss": loss, "logits": logits} # this form, with a loss key, is usable by the Trainer class
57
+ return {"logits": logits}
58
+
59
+
60
+ if __name__ == "__main__":
61
+ resnet50d_config = custom_configuration.ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True)
62
+ resnet50d = ResnetModelForImageClassification(resnet50d_config)
63
+ # resnet50d.save_pretrained("resnet50d")
64
+ # resnet50d.push_to_hub("resnet50d")
65
+
66
+ # transfer weights from pretrained model
67
+ pretrained_model = timm.create_model("resnet50d", pretrained=True)
68
+ resnet50d.model.load_state_dict(pretrained_model.state_dict())
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:78472643c5bb8b9614a3a08d815c1520408058f47e07b26a0e99918c1f7e3176
3
+ size 102550264