-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhrnet_multi_MTL_D.py
224 lines (196 loc) · 11.4 KB
/
hrnet_multi_MTL_D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import colorsys
import copy
import time
import os
import cv2
import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image
from torch import nn
from nets.hrnet_MTL_D import HRnet
from utils.utils import cvtColor, preprocess_input, resize_image, show_config
class HRnet_Segmentation(object):
_defaults = {
"model_path" : 'logs/best_epoch_weights.pth',
"num_classes" : [7, 2],
"backbone" : "hrnetv2_w32",
"input_shape" : [480, 480],
"mix_type" : 0,
"cuda" : True,
}
def __init__(self, **kwargs):
self.__dict__.update(self._defaults)
for name, value in kwargs.items():
setattr(self, name, value)
if self.num_classes[0] <= 21:
self.colors = [ (0, 0, 0), (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128), (0, 128, 128),
(128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0), (192, 128, 0), (64, 0, 128), (192, 0, 128),
(64, 128, 128), (192, 128, 128), (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128),
(128, 64, 12)]
else:
hsv_tuples = [(x / self.num_classes, 1., 1.) for x in range(self.num_classes)]
self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
self.colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors))
self.generate()
show_config(**self._defaults)
def generate(self, onnx=False):
self.net = HRnet(num_classes=self.num_classes, backbone=self.backbone, pretrained=False)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.net.load_state_dict(torch.load(self.model_path, map_location=device))
self.net = self.net.eval()
print('{} model, and classes loaded.'.format(self.model_path))
if not onnx:
if self.cuda:
self.net = nn.DataParallel(self.net)
self.net = self.net.cuda()
def detect_image(self, image, cross_e, cross_d, count=False, name_classes=None):
image = cvtColor(image)
old_img = copy.deepcopy(image)
orininal_h = np.array(image).shape[0]
orininal_w = np.array(image).shape[1]
image_data, nw, nh = resize_image(image, (self.input_shape[1],self.input_shape[0]))
image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, np.float32)), (2, 0, 1)), 0)
with torch.no_grad():
images = torch.from_numpy(image_data)
if self.cuda:
images = images.cuda()
pr_e = self.net(images, cross_e.narrow(0, 7, 1), cross_d.narrow(0, 7, 1))[0][0]
pr_d = self.net(images, cross_e.narrow(0, 7, 1), cross_d.narrow(0, 7, 1))[1][0]
pr_e = F.softmax(pr_e.permute(1,2,0),dim = -1).cpu().numpy()
pr_d = F.softmax(pr_d.permute(1,2,0),dim = -1).cpu().numpy()
pr_e = pr_e[int((self.input_shape[0] - nh) // 2) : int((self.input_shape[0] - nh) // 2 + nh), \
int((self.input_shape[1] - nw) // 2) : int((self.input_shape[1] - nw) // 2 + nw)]
pr_d = pr_d[int((self.input_shape[0] - nh) // 2) : int((self.input_shape[0] - nh) // 2 + nh), \
int((self.input_shape[1] - nw) // 2) : int((self.input_shape[1] - nw) // 2 + nw)]
pr_e = cv2.resize(pr_e, (orininal_w, orininal_h), interpolation = cv2.INTER_LINEAR)
pr_d = cv2.resize(pr_d, (orininal_w, orininal_h), interpolation = cv2.INTER_LINEAR)
pr_e = pr_e.argmax(axis=-1)
pr_d = pr_d.argmax(axis=-1)
if count:
classes_nums_e = np.zeros([self.num_classes[0]])
classes_nums_d = np.zeros([self.num_classes[1]])
total_points_num = orininal_h * orininal_w
print('-' * 63)
print("|%25s | %15s | %15s|"%("Key", "Value", "Ratio"))
print('-' * 63)
for i in range(self.num_classes[0]):
num = np.sum(pr_e == i)
ratio = num / total_points_num * 100
if num > 0:
print("|%25s | %15s | %14.2f%%|"%(str(name_classes[0][i]), str(num), ratio))
print('-' * 63)
classes_nums_e[i] = num
print("classes_nums_e:", classes_nums_e)
for i in range(self.num_classes[1]):
num = np.sum(pr_d == i)
ratio = num / total_points_num * 100
if num > 0:
print("|%25s | %15s | %14.2f%%|"%(str(name_classes[1][i]), str(num), ratio))
print('-' * 63)
classes_nums_d[i] = num
print("classes_nums_d:", classes_nums_d)
if self.mix_type == 0:
seg_img_e = np.reshape(np.array(self.colors, np.uint8)[np.reshape(pr_e, [-1])], [orininal_h, orininal_w, -1])
seg_img_d = np.reshape(np.array(self.colors, np.uint8)[np.reshape(pr_d, [-1])], [orininal_h, orininal_w, -1])
image_e = Image.fromarray(np.uint8(seg_img_e))
image_d = Image.fromarray(np.uint8(seg_img_d))
image_e = Image.blend(old_img, image_e, 0.7)
image_d = Image.blend(old_img, image_d, 0.7)
elif self.mix_type == 1:
seg_img_e = np.reshape(np.array(self.colors, np.uint8)[np.reshape(pr_e, [-1])], [orininal_h, orininal_w, -1])
seg_img_d = np.reshape(np.array(self.colors, np.uint8)[np.reshape(pr_d, [-1])], [orininal_h, orininal_w, -1])
image_e = Image.fromarray(np.uint8(seg_img_e))
image_d = Image.fromarray(np.uint8(seg_img_d))
elif self.mix_type == 2:
seg_img_e = np.reshape(np.array(self.colors, np.uint8)[np.reshape(pr_e, [-1])], [orininal_h, orininal_w, -1])
seg_img_d = np.reshape(np.array(self.colors, np.uint8)[np.reshape(pr_d, [-1])], [orininal_h, orininal_w, -1])
image_e = Image.fromarray(np.uint8(seg_img_e))
image_d = Image.fromarray(np.uint8(seg_img_d))
return image_e, image_d
def get_FPS(self, image, test_interval):
image = cvtColor(image)
image_data, nw, nh = resize_image(image, (self.input_shape[1],self.input_shape[0]))
image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, np.float32)), (2, 0, 1)), 0)
with torch.no_grad():
images = torch.from_numpy(image_data)
if self.cuda:
images = images.cuda()
pr_e = self.net(images)[0][0]
pr_d = self.net(images)[1][0]
pr_e = F.softmax(pr_e.permute(1,2,0),dim = -1).cpu().numpy().argmax(axis=-1)
pr_d = F.softmax(pr_d.permute(1,2,0),dim = -1).cpu().numpy().argmax(axis=-1)
pr_e = pr_e[int((self.input_shape[0] - nh) // 2) : int((self.input_shape[0] - nh) // 2 + nh), \
int((self.input_shape[1] - nw) // 2) : int((self.input_shape[1] - nw) // 2 + nw)]
pr_d = pr_d[int((self.input_shape[0] - nh) // 2) : int((self.input_shape[0] - nh) // 2 + nh), \
int((self.input_shape[1] - nw) // 2) : int((self.input_shape[1] - nw) // 2 + nw)]
t1 = time.time()
for _ in range(test_interval):
with torch.no_grad():
pr_e = self.net(images)[0][0]
pr_d = self.net(images)[1][0]
pr_e = F.softmax(pr_e.permute(1,2,0),dim = -1).cpu().numpy().argmax(axis=-1)
pr_d = F.softmax(pr_d.permute(1,2,0),dim = -1).cpu().numpy().argmax(axis=-1)
pr_e = pr_e[int((self.input_shape[0] - nh) // 2) : int((self.input_shape[0] - nh) // 2 + nh), \
int((self.input_shape[1] - nw) // 2) : int((self.input_shape[1] - nw) // 2 + nw)]
pr_d = pr_d[int((self.input_shape[0] - nh) // 2) : int((self.input_shape[0] - nh) // 2 + nh), \
int((self.input_shape[1] - nw) // 2) : int((self.input_shape[1] - nw) // 2 + nw)]
t2 = time.time()
tact_time = (t2 - t1) / test_interval
return tact_time
def convert_to_onnx(self, simplify, model_path):
import onnx
self.generate(onnx=True)
im = torch.zeros(1, 3, *self.input_shape).to('cpu')
input_layer_names = ["images"]
output_layer_names = ["output"]
print(f'Starting export with onnx {onnx.__version__}.')
torch.onnx.export(self.net,
im,
f = model_path,
verbose = False,
opset_version = 12,
training = torch.onnx.TrainingMode.EVAL,
do_constant_folding = True,
input_names = input_layer_names,
output_names = output_layer_names,
dynamic_axes = None)
model_onnx = onnx.load(model_path) # load onnx model
onnx.checker.check_model(model_onnx) # check onnx model
if simplify:
import onnxsim
print(f'Simplifying with onnx-simplifier {onnxsim.__version__}.')
model_onnx, check = onnxsim.simplify(
model_onnx,
dynamic_input_shape=False,
input_shapes=None)
assert check, 'assert check failed'
onnx.save(model_onnx, model_path)
print('Onnx model save as {}'.format(model_path))
def get_miou_png(self, image, cross_e, cross_d):
image = cvtColor(image)
orininal_h = np.array(image).shape[0]
orininal_w = np.array(image).shape[1]
image_data, nw, nh = resize_image(image, (self.input_shape[1],self.input_shape[0]))
image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, np.float32)), (2, 0, 1)), 0)
with torch.no_grad():
images = torch.from_numpy(image_data)
if self.cuda:
images = images.cuda()
cross_e = cross_e.cuda()
cross_d = cross_d.cuda()
pr_e = self.net(images, cross_e.narrow(0, 7, 1), cross_d.narrow(0, 7, 1))[0][0]
pr_d = self.net(images, cross_e.narrow(0, 7, 1), cross_d.narrow(0, 7, 1))[1][0]
pr_e = F.softmax(pr_e.permute(1,2,0),dim = -1).cpu().numpy()
pr_d = F.softmax(pr_d.permute(1,2,0),dim = -1).cpu().numpy()
pr_e = pr_e[int((self.input_shape[0] - nh) // 2) : int((self.input_shape[0] - nh) // 2 + nh), \
int((self.input_shape[1] - nw) // 2) : int((self.input_shape[1] - nw) // 2 + nw)]
pr_d = pr_d[int((self.input_shape[0] - nh) // 2) : int((self.input_shape[0] - nh) // 2 + nh), \
int((self.input_shape[1] - nw) // 2) : int((self.input_shape[1] - nw) // 2 + nw)]
pr_e = cv2.resize(pr_e, (orininal_w, orininal_h), interpolation = cv2.INTER_LINEAR)
pr_d = cv2.resize(pr_d, (orininal_w, orininal_h), interpolation = cv2.INTER_LINEAR)
pr_e = pr_e.argmax(axis=-1)
pr_d = pr_d.argmax(axis=-1)
image_e = Image.fromarray(np.uint8(pr_e))
image_d = Image.fromarray(np.uint8(pr_d))
return image_e, image_d