Spaces:
Running
Running
Monet Joe
commited on
Commit
•
613d22e
1
Parent(s):
3b68b62
Update model.py
Browse files
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 |
-
|
20 |
-
)
|
21 |
-
|
22 |
-
m_type
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
nn.
|
36 |
-
nn.
|
37 |
-
nn.
|
38 |
-
nn.
|
39 |
-
nn.
|
40 |
-
nn.
|
41 |
-
nn.
|
42 |
-
nn.Linear(
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
nn.
|
50 |
-
nn.
|
51 |
-
nn.
|
52 |
-
nn.
|
53 |
-
nn.
|
54 |
-
nn.
|
55 |
-
nn.
|
56 |
-
nn.
|
57 |
-
nn.Linear(
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
self.
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
str(name).__contains__("
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
return self.model(x)
|
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)
|
|
|
|