Monet Joe commited on
Commit
3b9e533
1 Parent(s): c96a08b

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +142 -144
model.py CHANGED
@@ -1,144 +1,142 @@
1
- import torch
2
- import torch.nn as nn
3
- import torchvision.models as models
4
- from modelscope.msdatasets import MsDataset
5
- from utils import MODEL_DIR
6
-
7
-
8
- def get_backbone(ver, backbone_list):
9
- for bb in backbone_list:
10
- if ver == bb["ver"]:
11
- return bb
12
-
13
- print("Backbone name not found, using default option - alexnet.")
14
- return backbone_list[0]
15
-
16
-
17
- def model_info(m_ver):
18
- backbone_list = MsDataset.load(
19
- "monetjoe/cv_backbones", subset_name="ImageNet1k_v1", split="train"
20
- )
21
- backbone = get_backbone(m_ver, backbone_list)
22
- m_type = str(backbone["type"])
23
- input_size = int(backbone["input_size"])
24
- return m_type, input_size
25
-
26
-
27
- def Classifier(cls_num: int, output_size: int, linear_output: bool):
28
- q = (1.0 * output_size / cls_num) ** 0.25
29
- l1 = int(q * cls_num)
30
- l2 = int(q * l1)
31
- l3 = int(q * l2)
32
-
33
- if linear_output:
34
- return torch.nn.Sequential(
35
- nn.Dropout(),
36
- nn.Linear(output_size, l3),
37
- nn.ReLU(inplace=True),
38
- nn.Dropout(),
39
- nn.Linear(l3, l2),
40
- nn.ReLU(inplace=True),
41
- nn.Dropout(),
42
- nn.Linear(l2, l1),
43
- nn.ReLU(inplace=True),
44
- nn.Linear(l1, cls_num),
45
- )
46
-
47
- else:
48
- return torch.nn.Sequential(
49
- nn.Dropout(),
50
- nn.Conv2d(output_size, l3, kernel_size=(1, 1), stride=(1, 1)),
51
- nn.ReLU(inplace=True),
52
- nn.AdaptiveAvgPool2d(output_size=(1, 1)),
53
- nn.Flatten(),
54
- nn.Linear(l3, l2),
55
- nn.ReLU(inplace=True),
56
- nn.Dropout(),
57
- nn.Linear(l2, l1),
58
- nn.ReLU(inplace=True),
59
- nn.Linear(l1, cls_num),
60
- )
61
-
62
-
63
- class EvalNet:
64
- model = None
65
- m_type = "squeezenet"
66
- input_size = 224
67
- output_size = 512
68
-
69
- def __init__(self, log_name: str, cls_num=4):
70
- saved_model_path = f"{MODEL_DIR}/{log_name}/save.pt"
71
- m_ver = "_".join(log_name.split("_")[:-1])
72
- self.m_type, self.input_size = model_info(m_ver)
73
-
74
- if not hasattr(models, m_ver):
75
- print("Unsupported model.")
76
- exit()
77
-
78
- self.model = eval("models.%s()" % m_ver)
79
- linear_output = self._set_outsize()
80
- self._set_classifier(cls_num, linear_output)
81
- checkpoint = torch.load(saved_model_path, map_location="cpu")
82
- if torch.cuda.is_available():
83
- checkpoint = torch.load(saved_model_path)
84
-
85
- self.model.load_state_dict(checkpoint, False)
86
- self.model.eval()
87
-
88
- def _set_outsize(self, debug_mode=False):
89
- for name, module in self.model.named_modules():
90
- if (
91
- str(name).__contains__("classifier")
92
- or str(name).__eq__("fc")
93
- or str(name).__contains__("head")
94
- ):
95
- if isinstance(module, torch.nn.Linear):
96
- self.output_size = module.in_features
97
- if debug_mode:
98
- print(
99
- f"{name}(Linear): {self.output_size} -> {module.out_features}"
100
- )
101
- return True
102
-
103
- if isinstance(module, torch.nn.Conv2d):
104
- self.output_size = module.in_channels
105
- if debug_mode:
106
- print(
107
- f"{name}(Conv2d): {self.output_size} -> {module.out_channels}"
108
- )
109
- return False
110
-
111
- return False
112
-
113
- def _set_classifier(self, cls_num, linear_output):
114
- if self.m_type == "convnext":
115
- del self.model.classifier[2]
116
- self.model.classifier = nn.Sequential(
117
- *list(self.model.classifier)
118
- + list(Classifier(cls_num, self.output_size, linear_output))
119
- )
120
- return
121
-
122
- if hasattr(self.model, "classifier"):
123
- self.model.classifier = Classifier(cls_num, self.output_size, linear_output)
124
- return
125
-
126
- elif hasattr(self.model, "fc"):
127
- self.model.fc = Classifier(cls_num, self.output_size, linear_output)
128
- return
129
-
130
- elif hasattr(self.model, "head"):
131
- self.model.head = Classifier(cls_num, self.output_size, linear_output)
132
- return
133
-
134
- self.model.heads.head = Classifier(cls_num, self.output_size, linear_output)
135
-
136
- def forward(self, x):
137
- if torch.cuda.is_available():
138
- x = x.cuda()
139
- self.model = self.model.cuda()
140
-
141
- if self.m_type == "googlenet" and self.training:
142
- return self.model(x)[0]
143
- else:
144
- return self.model(x)
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torchvision.models as models
4
+ from modelscope.msdatasets import MsDataset
5
+ from utils import MODEL_DIR
6
+
7
+
8
+ def get_backbone(ver, backbone_list):
9
+ for bb in backbone_list:
10
+ if ver == bb["ver"]:
11
+ return bb
12
+
13
+ print("Backbone name not found, using default option - alexnet.")
14
+ return backbone_list[0]
15
+
16
+
17
+ def model_info(m_ver):
18
+ backbone_list = MsDataset.load("monetjoe/cv_backbones", split="train")
19
+ backbone = get_backbone(m_ver, backbone_list)
20
+ m_type = str(backbone["type"])
21
+ input_size = int(backbone["input_size"])
22
+ return m_type, input_size
23
+
24
+
25
+ def Classifier(cls_num: int, output_size: int, linear_output: bool):
26
+ q = (1.0 * output_size / cls_num) ** 0.25
27
+ l1 = int(q * cls_num)
28
+ l2 = int(q * l1)
29
+ l3 = int(q * l2)
30
+
31
+ if linear_output:
32
+ return torch.nn.Sequential(
33
+ nn.Dropout(),
34
+ nn.Linear(output_size, l3),
35
+ nn.ReLU(inplace=True),
36
+ nn.Dropout(),
37
+ nn.Linear(l3, l2),
38
+ nn.ReLU(inplace=True),
39
+ nn.Dropout(),
40
+ nn.Linear(l2, l1),
41
+ nn.ReLU(inplace=True),
42
+ nn.Linear(l1, cls_num),
43
+ )
44
+
45
+ else:
46
+ return torch.nn.Sequential(
47
+ nn.Dropout(),
48
+ nn.Conv2d(output_size, l3, kernel_size=(1, 1), stride=(1, 1)),
49
+ nn.ReLU(inplace=True),
50
+ nn.AdaptiveAvgPool2d(output_size=(1, 1)),
51
+ nn.Flatten(),
52
+ nn.Linear(l3, l2),
53
+ nn.ReLU(inplace=True),
54
+ nn.Dropout(),
55
+ nn.Linear(l2, l1),
56
+ nn.ReLU(inplace=True),
57
+ nn.Linear(l1, cls_num),
58
+ )
59
+
60
+
61
+ class EvalNet:
62
+ model = None
63
+ m_type = "squeezenet"
64
+ input_size = 224
65
+ output_size = 512
66
+
67
+ def __init__(self, log_name: str, cls_num=4):
68
+ saved_model_path = f"{MODEL_DIR}/{log_name}/save.pt"
69
+ m_ver = "_".join(log_name.split("_")[:-1])
70
+ self.m_type, self.input_size = model_info(m_ver)
71
+
72
+ if not hasattr(models, m_ver):
73
+ print("Unsupported model.")
74
+ exit()
75
+
76
+ self.model = eval("models.%s()" % m_ver)
77
+ linear_output = self._set_outsize()
78
+ self._set_classifier(cls_num, linear_output)
79
+ checkpoint = torch.load(saved_model_path, map_location="cpu")
80
+ if torch.cuda.is_available():
81
+ checkpoint = torch.load(saved_model_path)
82
+
83
+ self.model.load_state_dict(checkpoint, False)
84
+ self.model.eval()
85
+
86
+ def _set_outsize(self, debug_mode=False):
87
+ for name, module in self.model.named_modules():
88
+ if (
89
+ str(name).__contains__("classifier")
90
+ or str(name).__eq__("fc")
91
+ or str(name).__contains__("head")
92
+ ):
93
+ if isinstance(module, torch.nn.Linear):
94
+ self.output_size = module.in_features
95
+ if debug_mode:
96
+ print(
97
+ f"{name}(Linear): {self.output_size} -> {module.out_features}"
98
+ )
99
+ return True
100
+
101
+ if isinstance(module, torch.nn.Conv2d):
102
+ self.output_size = module.in_channels
103
+ if debug_mode:
104
+ print(
105
+ f"{name}(Conv2d): {self.output_size} -> {module.out_channels}"
106
+ )
107
+ return False
108
+
109
+ return False
110
+
111
+ def _set_classifier(self, cls_num, linear_output):
112
+ if self.m_type == "convnext":
113
+ del self.model.classifier[2]
114
+ self.model.classifier = nn.Sequential(
115
+ *list(self.model.classifier)
116
+ + list(Classifier(cls_num, self.output_size, linear_output))
117
+ )
118
+ return
119
+
120
+ if hasattr(self.model, "classifier"):
121
+ self.model.classifier = Classifier(cls_num, self.output_size, linear_output)
122
+ return
123
+
124
+ elif hasattr(self.model, "fc"):
125
+ self.model.fc = Classifier(cls_num, self.output_size, linear_output)
126
+ return
127
+
128
+ elif hasattr(self.model, "head"):
129
+ self.model.head = Classifier(cls_num, self.output_size, linear_output)
130
+ return
131
+
132
+ self.model.heads.head = Classifier(cls_num, self.output_size, linear_output)
133
+
134
+ def forward(self, x):
135
+ if torch.cuda.is_available():
136
+ x = x.cuda()
137
+ self.model = self.model.cuda()
138
+
139
+ if self.m_type == "googlenet" and self.training:
140
+ return self.model(x)[0]
141
+ else:
142
+ return self.model(x)