RishiDarkDevil commited on
Commit
cf3cc27
1 Parent(s): d942758

Upload model

Browse files
Files changed (4) hide show
  1. config.json +13 -0
  2. configuration_resnet.py +11 -0
  3. model_resnet.py +47 -0
  4. pytorch_model.bin +3 -0
config.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ResnetModelForImageClassification"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration_resnet.ResnetFeatureExtractorConfig",
7
+ "AutoModelForImageClassification": "model_resnet.ResnetModelForImageClassification"
8
+ },
9
+ "model_type": "resnet",
10
+ "name": "resnet152",
11
+ "torch_dtype": "float32",
12
+ "transformers_version": "4.27.1"
13
+ }
configuration_resnet.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+
3
+ class ResnetFeatureExtractorConfig(PretrainedConfig):
4
+ model_type = "resnet"
5
+
6
+ def __init__(self, name = 'resnet152', **kwargs):
7
+ if name != 'resnet152':
8
+ raise ValueError(f"`name` must be 'resnet152', got {name}.")
9
+
10
+ self.name = name
11
+ super().__init__(**kwargs)
model_resnet.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PreTrainedModel
2
+ import torch
3
+ import torch.nn as nn
4
+ from torchvision import transforms
5
+ from transformers.models.mvp.modeling_mvp import CrossEntropyLoss
6
+
7
+ from .configuration_resnet import ResnetFeatureExtractorConfig
8
+
9
+ class ResnetFeatureExtractor(PreTrainedModel):
10
+ config_class = ResnetFeatureExtractorConfig
11
+
12
+ def __init__(self, config):
13
+ super().__init__(config)
14
+
15
+ if config.name == 'resnet152':
16
+ self.model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet152', pretrained=False)
17
+ self.model.fc = nn.Identity()
18
+ self.model.to(self.device)
19
+ self.preprocess = transforms.Compose([
20
+ transforms.Resize(256),
21
+ transforms.CenterCrop(224),
22
+ transforms.ToTensor(),
23
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
24
+ ])
25
+
26
+ def forward(self, images):
27
+ tensor = torch.stack([self.preprocess(image) for image in images]).to(self.device).float()
28
+ return self.model(tensor)
29
+
30
+ class ResnetModelForImageClassification(PreTrainedModel):
31
+ config_class = ResnetFeatureExtractorConfig
32
+
33
+ def __init__(self, config):
34
+ super().__init__(config)
35
+ if config.name == 'resnet152':
36
+ self.model = nn.Sequential(
37
+ nn.Linear(2048, 32),
38
+ nn.ReLU(),
39
+ nn.Linear(32, 2)
40
+ )
41
+
42
+ def forward(self, tensor, labels=None):
43
+ logits = self.model(tensor)
44
+ if labels is not None:
45
+ loss = CrossEntropyLoss()(logits, torch.tensor(labels))
46
+ return {"loss": loss, "logits": logits}
47
+ return {"logits": logits}
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0b809790a34c7cf9a393c984d6ccdf0049dd07b03a37469a40c1527593da1b43
3
+ size 264131