Skip to content

Commit b421d08

Browse files
committedFeb 8, 2022
Update.
1 parent 9a2c76f commit b421d08

File tree

7 files changed

+131
-145
lines changed

7 files changed

+131
-145
lines changed
 

‎keypoints_onnxdemo/keypoints/get_keypoints.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def npnormalize(x,mean,std):
9595
return x
9696

9797
class KPDetection:
98+
#SIZE = (288,384)
99+
SIZE = (192,256)
98100
def __init__(self) -> None:
99101
self.init_model()
100102
self.person_det = PersonDetection()
@@ -104,12 +106,15 @@ def init_model(self):
104106
onnx_path = osp.join(curdir_path,"keypoints.torch")
105107
print(f"Load {onnx_path}")
106108
self.model = torch.jit.load(onnx_path)
109+
print(self.model)
107110

108111
@staticmethod
109112
def preprocess(img):
110-
img = to_tensor(img)
111-
img = npnormalize(img,mean=[0.485, 0.456, 0.406],
112-
std=[0.229, 0.224, 0.225])
113+
#img = to_tensor(img)
114+
#img = npnormalize(img,mean=[0.485, 0.456, 0.406],
115+
# std=[0.229, 0.224, 0.225])
116+
img = img.astype(np.float32)
117+
img = np.transpose(img,[2,0,1])
113118
return img
114119

115120

@@ -124,7 +129,7 @@ def cut_and_resize(img,bboxes,size=(288,384)):
124129
cur_img,bbox = KPDetection.resize_img(cur_img,bbox,size)
125130
else:
126131
cur_img = np.zeros([size[1],size[0],3],dtype=np.float32)
127-
cv2.imwrite(f"{i}.jpg",cur_img)
132+
cv2.imwrite(f"{i}.jpg",cur_img[...,::-1])
128133
res.append(cur_img)
129134
res_bboxes.append(bbox)
130135
return res,np.array(res_bboxes,dtype=np.float32)
@@ -141,24 +146,22 @@ def resize_img(img,bbox,target_size,pad_color=(127,127,127)):
141146
if img.shape[1]>ratio*img.shape[0]:
142147
nw = target_size[0]
143148
nh = int(target_size[0]*img.shape[0]/img.shape[1])
144-
#bbox_h = bbox_w/ratio
149+
bbox_h = bbox_w/ratio
145150
else:
146151
nh = target_size[1]
147152
nw = int(target_size[1]*img.shape[1]/img.shape[0])
148-
#bbox_w = bbox_h*ratio
153+
bbox_w = bbox_h*ratio
149154

150155
img = cv2.resize(img,(nw,nh),interpolation=cv2.INTER_LINEAR)
151156
xoffset = (target_size[0]-nw)//2
152157
yoffset = (target_size[1]-nh)//2
153-
xoffset = 0
154-
yoffset = 0
155158
res[yoffset:yoffset+nh,xoffset:xoffset+nw] = img
156159
bbox = np.array([bbox_cx-bbox_w/2,bbox_cy-bbox_h/2,bbox_cx+bbox_w/2,bbox_cy+bbox_h/2],dtype=np.float32)
157160
return res,bbox
158161

159162

160163
@staticmethod
161-
def get_offset_and_scalar(bboxes,size=(288,384)):
164+
def get_offset_and_scalar(bboxes,size):
162165
offset = bboxes[...,:2]
163166
offset = np.expand_dims(offset,axis=1)
164167
bboxes_size = bboxes[...,2:]-bboxes[...,:2]
@@ -252,7 +255,7 @@ def get_kps_by_bboxes(self,img,bboxes):
252255
'''
253256
#print(bboxes)
254257
#cv2.imwrite("/home/wj/ai/mldata/0day/x1/a.jpg",img)
255-
imgs,bboxes = self.cut_and_resize(img,bboxes.astype(np.int32))
258+
imgs,bboxes = self.cut_and_resize(img,bboxes.astype(np.int32),KPDetection.SIZE)
256259
bboxes = np.array(bboxes)
257260
imgs = [self.preprocess(x) for x in imgs]
258261
imgs = np.ascontiguousarray(np.array(imgs))
@@ -266,7 +269,7 @@ def get_kps_by_bboxes(self,img,bboxes):
266269
print(f"ERROR")
267270
return np.zeros([imgs.shape[0],17,3],dtype=np.float32)
268271
output = self.get_final_preds(output)
269-
offset,scalar = self.get_offset_and_scalar(bboxes)
272+
offset,scalar = self.get_offset_and_scalar(bboxes,KPDetection.SIZE)
270273
output[...,:2] = output[...,:2]*scalar+offset
271274
return output
272275

‎keypoints_onnxdemo/keypoints_demo.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
1010
pdir_path = osp.dirname(osp.dirname(__file__))
1111

12-
os.environ['CUDA_VISIBLE_DEVICES'] = "1"
12+
os.environ['CUDA_VISIBLE_DEVICES'] = "2"
1313

1414
class Model:
1515
def __init__(self):
@@ -24,7 +24,7 @@ def __call__(self, img):
2424
return img
2525

2626
if __name__ == "__main__":
27-
vd = VideoDemo(Model(),save_path="tmp0.mp4",buffer_size=1,show_video=False,max_frame_cn=1000)
27+
vd = VideoDemo(Model(),save_path="tmp1.mp4",buffer_size=1,show_video=False,max_frame_cn=10)
2828
#video_path = "/home/wj/ai/mldata/boeoffice/test_data/test3.webm"
2929
print(f"DATE: 2021-11-29")
3030
'''video_path = "/home/wj/ai/mldata/global_traj/tennis1.mp4"

‎lib/core/function.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,11 @@ def validate(config, val_loader, val_dataset, model, criterion, output_dir,
216216

217217
if i % config.PRINT_FREQ == 0:
218218
msg = 'Test: [{0}/{1}]\t' \
219+
'idx {idx}' \
219220
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' \
220221
'Loss {loss.val:.4f} ({loss.avg:.4f})\t' \
221222
'Accuracy {acc.val:.3f} ({acc.avg:.3f})'.format(
222-
i, len(val_loader), batch_time=batch_time,
223+
i, len(val_loader), idx=idx,batch_time=batch_time,
223224
loss=losses, acc=acc)
224225
logger.info(msg)
225226

@@ -269,7 +270,7 @@ def validate(config, val_loader, val_dataset, model, criterion, output_dir,
269270
global_steps
270271
)
271272
writer_dict['valid_global_steps'] = global_steps + 1
272-
273+
print('idx=',idx)
273274
return perf_indicator
274275

275276

‎lib/models/pose_whrnet.py

+42-70
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,44 @@
1414
import torch
1515
import torch.nn as nn
1616
from wtorch.utils import *
17-
from wtorch.nn import *
18-
from einops import rearrange
1917

2018

2119
BN_MOMENTUM = 0.1
2220
logger = logging.getLogger(__name__)
2321

2422

25-
def conv3x3(in_planes, out_planes, stride=1,bias=False):
23+
def conv3x3(in_planes, out_planes, stride=1):
2624
"""3x3 convolution with padding"""
2725
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
28-
padding=1, bias=bias)
26+
padding=1, bias=False)
2927

30-
def get_norm(planes,momentum=None,type="layer_norm"):
31-
if type == "bn":
32-
return nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
33-
elif type == "layer_norm":
34-
return LayerNorm(planes)
35-
36-
def get_activation_fn(*args,**kwargs):
37-
return nn.GELU()
3828

3929
class BasicBlock(nn.Module):
4030
expansion = 1
4131

4232
def __init__(self, inplanes, planes, stride=1, downsample=None):
4333
super(BasicBlock, self).__init__()
4434
self.conv1 = conv3x3(inplanes, planes, stride)
45-
self.bn1 = get_norm(planes, momentum=BN_MOMENTUM)
46-
self.relu = get_activation_fn(inplace=True)
47-
self.conv2 = conv3x3(planes, planes,bias=True)
35+
self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
36+
self.relu = nn.ReLU(inplace=True)
37+
self.conv2 = conv3x3(planes, planes)
38+
self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
4839
self.downsample = downsample
4940
self.stride = stride
50-
if self.stride == 1:
51-
self.drop_path = nn.Dropout2d(p=0.08)
52-
else:
53-
self.drop_path = None
5441

5542
def forward(self, x):
5643
residual = x
5744

5845
out = self.conv1(x)
5946
out = self.bn1(out)
47+
out = self.relu(out)
6048

6149
out = self.conv2(out)
50+
out = self.bn2(out)
6251

6352
if self.downsample is not None:
6453
residual = self.downsample(x)
65-
66-
if self.drop_path is not None:
67-
out = self.drop_path(out)
54+
6855
out += residual
6956
out = self.relu(out)
7057

@@ -77,30 +64,37 @@ class Bottleneck(nn.Module):
7764
def __init__(self, inplanes, planes, stride=1, downsample=None):
7865
super(Bottleneck, self).__init__()
7966
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
67+
self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
8068
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
8169
padding=1, bias=False)
82-
self.bn2 = get_norm(planes, momentum=BN_MOMENTUM)
70+
self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
8371
self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1,
84-
bias=True)
85-
self.relu = get_activation_fn(inplace=True)
72+
bias=False)
73+
self.bn3 = nn.BatchNorm2d(planes * self.expansion,
74+
momentum=BN_MOMENTUM)
75+
self.relu = nn.ReLU(inplace=True)
8676
self.downsample = downsample
8777
self.stride = stride
8878

8979
def forward(self, x):
9080
residual = x
9181

9282
out = self.conv1(x)
83+
out = self.bn1(out)
9384
out = self.relu(out)
9485

9586
out = self.conv2(out)
9687
out = self.bn2(out)
88+
out = self.relu(out)
9789

9890
out = self.conv3(out)
91+
out = self.bn3(out)
9992

10093
if self.downsample is not None:
10194
residual = self.downsample(x)
10295

10396
out += residual
97+
out = self.relu(out)
10498

10599
return out
106100

@@ -121,7 +115,7 @@ def __init__(self, num_branches, blocks, num_blocks, num_inchannels,
121115
self.branches = self._make_branches(
122116
num_branches, blocks, num_blocks, num_channels)
123117
self.fuse_layers = self._make_fuse_layers()
124-
self.relu = get_activation_fn(True)
118+
self.relu = nn.ReLU(True)
125119

126120
def _check_branches(self, num_branches, blocks, num_blocks,
127121
num_inchannels, num_channels):
@@ -154,7 +148,7 @@ def _make_one_branch(self, branch_index, block, num_blocks, num_channels,
154148
num_channels[branch_index] * block.expansion,
155149
kernel_size=1, stride=stride, bias=False
156150
),
157-
get_norm(
151+
nn.BatchNorm2d(
158152
num_channels[branch_index] * block.expansion,
159153
momentum=BN_MOMENTUM
160154
),
@@ -209,7 +203,7 @@ def _make_fuse_layers(self):
209203
num_inchannels[i],
210204
1, 1, 0, bias=False
211205
),
212-
get_norm(num_inchannels[i]),
206+
nn.BatchNorm2d(num_inchannels[i]),
213207
nn.Upsample(scale_factor=2**(j-i), mode='nearest')
214208
)
215209
)
@@ -227,7 +221,7 @@ def _make_fuse_layers(self):
227221
num_outchannels_conv3x3,
228222
3, 2, 1, bias=False
229223
),
230-
get_norm(num_outchannels_conv3x3)
224+
nn.BatchNorm2d(num_outchannels_conv3x3)
231225
)
232226
)
233227
else:
@@ -239,8 +233,8 @@ def _make_fuse_layers(self):
239233
num_outchannels_conv3x3,
240234
3, 2, 1, bias=False
241235
),
242-
get_norm(num_outchannels_conv3x3),
243-
get_activation_fn(True)
236+
nn.BatchNorm2d(num_outchannels_conv3x3),
237+
nn.ReLU(True)
244238
)
245239
)
246240
fuse_layer.append(nn.Sequential(*conv3x3s))
@@ -277,31 +271,6 @@ def forward(self, x):
277271
'BOTTLENECK': Bottleneck
278272
}
279273

280-
class FCBlock(nn.Module):
281-
def __init__(self,channels,width,height):
282-
super().__init__()
283-
channels1 = width*height
284-
self.fc0 = nn.Linear(channels1,channels1,bias=False)
285-
self.norm0 = nn.LayerNorm(channels1)
286-
self.fc1 = nn.Linear(channels,channels,bias=False)
287-
self.norm1 = nn.LayerNorm(channels)
288-
self.relu = get_activation_fn()
289-
290-
def forward(self,x):
291-
residual = x
292-
shape = x.shape
293-
x = rearrange(x,'b c h w -> b c (h w)')
294-
x = self.fc0(x)
295-
x = self.norm0(x)
296-
x = rearrange(x,'b c s -> b s c')
297-
x = self.fc1(x)
298-
x = self.norm1(x)
299-
x = rearrange(x,'b s c -> b c s')
300-
x = torch.reshape(x,shape)
301-
x = x+residual
302-
x = self.relu(x)
303-
return x
304-
305274

306275
class PoseHighResolutionNet(nn.Module):
307276

@@ -311,10 +280,13 @@ def __init__(self, cfg, **kwargs):
311280
super(PoseHighResolutionNet, self).__init__()
312281

313282
# stem net
314-
self.conv1 = nn.Conv2d(3, 64, kernel_size=4, stride=4,
283+
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1,
315284
bias=False)
316-
self.bn1 = get_norm(64, momentum=BN_MOMENTUM)
317-
self.relu = get_activation_fn(inplace=True)
285+
self.bn1 = nn.BatchNorm2d(64, momentum=BN_MOMENTUM)
286+
self.conv2 = nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1,
287+
bias=False)
288+
self.bn2 = nn.BatchNorm2d(64, momentum=BN_MOMENTUM)
289+
self.relu = nn.ReLU(inplace=True)
318290
self.layer1 = self._make_layer(Bottleneck, 64, 4)
319291

320292
self.stage2_cfg = extra['STAGE2']
@@ -349,8 +321,6 @@ def __init__(self, cfg, **kwargs):
349321
self.stage4, pre_stage_channels = self._make_stage(
350322
self.stage4_cfg, num_channels, multi_scale_output=False)
351323

352-
self.fc_block0 = FCBlock(pre_stage_channels[0],48,64)
353-
self.fc_block1 = FCBlock(pre_stage_channels[0],48,64)
354324
self.final_layer = nn.Conv2d(
355325
in_channels=pre_stage_channels[0],
356326
out_channels=cfg['MODEL']['NUM_JOINTS'],
@@ -378,8 +348,8 @@ def _make_transition_layer(
378348
num_channels_cur_layer[i],
379349
3, 1, 1, bias=False
380350
),
381-
get_norm(num_channels_cur_layer[i]),
382-
get_activation_fn(inplace=True)
351+
nn.BatchNorm2d(num_channels_cur_layer[i]),
352+
nn.ReLU(inplace=True)
383353
)
384354
)
385355
else:
@@ -395,8 +365,8 @@ def _make_transition_layer(
395365
nn.Conv2d(
396366
inchannels, outchannels, 3, 2, 1, bias=False
397367
),
398-
get_norm(outchannels),
399-
get_activation_fn(inplace=True)
368+
nn.BatchNorm2d(outchannels),
369+
nn.ReLU(inplace=True)
400370
)
401371
)
402372
transition_layers.append(nn.Sequential(*conv3x3s))
@@ -411,7 +381,7 @@ def _make_layer(self, block, planes, blocks, stride=1):
411381
self.inplanes, planes * block.expansion,
412382
kernel_size=1, stride=stride, bias=False
413383
),
414-
get_norm(planes * block.expansion, momentum=BN_MOMENTUM),
384+
nn.BatchNorm2d(planes * block.expansion, momentum=BN_MOMENTUM),
415385
)
416386

417387
layers = []
@@ -468,6 +438,9 @@ def forward(self, x):
468438
x = self.conv1(x)
469439
x = self.bn1(x)
470440
x = self.relu(x)
441+
x = self.conv2(x)
442+
x = self.bn2(x)
443+
x = self.relu(x)
471444
x = self.layer1(x)
472445

473446
x_list = []
@@ -493,9 +466,8 @@ def forward(self, x):
493466
else:
494467
x_list.append(y_list[i])
495468
y_list = self.stage4(x_list)
496-
x = self.fc_block0(y_list[0])
497-
x = self.fc_block1(x)
498-
x = self.final_layer(x)
469+
470+
x = self.final_layer(y_list[0])
499471

500472
return x
501473

@@ -508,7 +480,7 @@ def init_weights(self, pretrained=''):
508480
for name, _ in m.named_parameters():
509481
if name in ['bias']:
510482
nn.init.constant_(m.bias, 0)
511-
elif isinstance(m, LayerNorm):
483+
elif isinstance(m, nn.BatchNorm2d):
512484
nn.init.constant_(m.weight, 1)
513485
nn.init.constant_(m.bias, 0)
514486
elif isinstance(m, nn.ConvTranspose2d):

0 commit comments

Comments
 (0)
Please sign in to comment.