fengguo21 commited on
Commit
4eb4df0
·
verified ·
1 Parent(s): 8ed8e39

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. model_index.json +8 -0
  2. modeling_resnet.py +100 -0
model_index.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "ResnetModelForImageClassification",
3
+ "_diffusers_version": "0.21.4",
4
+ "_module": "modeling_resnet",
5
+ "architectures": [
6
+ "ResnetModelForImageClassification"
7
+ ]
8
+ }
modeling_resnet.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2024 The HuggingFace Inc. team.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """ ResNet model configuration and model."""
16
+
17
+ from transformers import PretrainedConfig, PreTrainedModel
18
+ from typing import List
19
+ import torch
20
+ from timm.models.resnet import BasicBlock, Bottleneck, ResNet
21
+
22
+ BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck}
23
+
24
+ class ResnetConfig(PretrainedConfig):
25
+ model_type = "resnetfengguo"
26
+
27
+ def __init__(
28
+ self,
29
+ block_type="bottleneck",
30
+ layers: list[int] = [3, 4, 6, 3],
31
+ num_classes: int = 1000,
32
+ input_channels: int = 3,
33
+ cardinality: int = 1,
34
+ base_width: int = 64,
35
+ stem_width: int = 64,
36
+ stem_type: str = "",
37
+ avg_down: bool = False,
38
+ **kwargs,
39
+ ):
40
+ if block_type not in ["basic", "bottleneck"]:
41
+ raise ValueError(f"`block_type` must be 'basic' or bottleneck', got {block_type}.")
42
+ if stem_type not in ["", "deep", "deep-tiered"]:
43
+ raise ValueError(f"`stem_type` must be '', 'deep' or 'deep-tiered', got {stem_type}.")
44
+
45
+ self.block_type = block_type
46
+ self.layers = layers
47
+ self.num_classes = num_classes
48
+ self.input_channels = input_channels
49
+ self.cardinality = cardinality
50
+ self.base_width = base_width
51
+ self.stem_width = stem_width
52
+ self.stem_type = stem_type
53
+ self.avg_down = avg_down
54
+ super().__init__(**kwargs)
55
+
56
+ class ResnetModel(PreTrainedModel):
57
+ config_class = ResnetConfig
58
+
59
+ def __init__(self, config):
60
+ super().__init__(config)
61
+ block_layer = BLOCK_MAPPING[config.block_type]
62
+ self.model = ResNet(
63
+ block_layer,
64
+ config.layers,
65
+ num_classes=config.num_classes,
66
+ in_chans=config.input_channels,
67
+ cardinality=config.cardinality,
68
+ base_width=config.base_width,
69
+ stem_width=config.stem_width,
70
+ stem_type=config.stem_type,
71
+ avg_down=config.avg_down,
72
+ )
73
+
74
+ def forward(self, tensor):
75
+ return self.model.forward_features(tensor)
76
+
77
+ class ResnetModelForImageClassification(PreTrainedModel):
78
+ config_class = ResnetConfig
79
+
80
+ def __init__(self, config):
81
+ super().__init__(config)
82
+ block_layer = BLOCK_MAPPING[config.block_type]
83
+ self.model = ResNet(
84
+ block_layer,
85
+ config.layers,
86
+ num_classes=config.num_classes,
87
+ in_chans=config.input_channels,
88
+ cardinality=config.cardinality,
89
+ base_width=config.base_width,
90
+ stem_width=config.stem_width,
91
+ stem_type=config.stem_type,
92
+ avg_down=config.avg_down,
93
+ )
94
+
95
+ def forward(self, tensor, labels=None):
96
+ logits = self.model(tensor)
97
+ if labels is not None:
98
+ loss = torch.nn.functional.cross_entropy(logits, labels)
99
+ return {"loss": loss, "logits": logits}
100
+ return {"logits": logits}