wip: draft transformer
Browse files- transformer/configuration.py +35 -0
- transformer/model.py +28 -0
transformer/configuration.py
CHANGED
@@ -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)
|
transformer/model.py
CHANGED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PreTrainedModel
|
2 |
+
from timm.models.resnet import BasicBlock, Bottleneck, ResNet
|
3 |
+
from .configuration_resnet import ResnetConfig
|
4 |
+
|
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)
|