whyen-wang commited on
Commit
59be662
·
1 Parent(s): 6c22bea

update model

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. __init__.py +0 -0
  3. configuration_resnet.py +35 -0
  4. modeling_resnet.py +55 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__/
__init__.py ADDED
File without changes
configuration_resnet.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+ from typing import List
3
+
4
+
5
+ class ResnetConfig(PretrainedConfig):
6
+ model_type = "resnet"
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)
modeling_resnet.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import PreTrainedModel
3
+ from timm.models.resnet import BasicBlock, Bottleneck, ResNet
4
+ from .configuration_resnet import ResnetConfig
5
+
6
+
7
+ BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck}
8
+
9
+
10
+ class ResnetModel(PreTrainedModel):
11
+ config_class = ResnetConfig
12
+
13
+ def __init__(self, config):
14
+ super().__init__(config)
15
+ block_layer = BLOCK_MAPPING[config.block_type]
16
+ self.model = ResNet(
17
+ block_layer,
18
+ config.layers,
19
+ num_classes=config.num_classes,
20
+ in_chans=config.input_channels,
21
+ cardinality=config.cardinality,
22
+ base_width=config.base_width,
23
+ stem_width=config.stem_width,
24
+ stem_type=config.stem_type,
25
+ avg_down=config.avg_down,
26
+ )
27
+
28
+ def forward(self, tensor):
29
+ return self.model.forward_features(tensor)
30
+
31
+
32
+ class ResnetModelForImageClassification(PreTrainedModel):
33
+ config_class = ResnetConfig
34
+
35
+ def __init__(self, config):
36
+ super().__init__(config)
37
+ block_layer = BLOCK_MAPPING[config.block_type]
38
+ self.model = ResNet(
39
+ block_layer,
40
+ config.layers,
41
+ num_classes=config.num_classes,
42
+ in_chans=config.input_channels,
43
+ cardinality=config.cardinality,
44
+ base_width=config.base_width,
45
+ stem_width=config.stem_width,
46
+ stem_type=config.stem_type,
47
+ avg_down=config.avg_down,
48
+ )
49
+
50
+ def forward(self, tensor, labels=None):
51
+ logits = self.model(tensor)
52
+ if labels is not None:
53
+ loss = torch.nn.cross_entropy(logits, labels)
54
+ return {"loss": loss, "logits": logits}
55
+ return {"logits": logits}