Files changed (2) hide show
  1. eval_onnx.py +144 -104
  2. movenet_int8.onnx +2 -2
eval_onnx.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import onnxruntime as rt
2
  import numpy as np
3
  import json
@@ -14,7 +15,7 @@ MODEL_DIR = './movenet_int8.onnx' # Path to the MoveNet model
14
  IMG_SIZE = 192 # Image size used for processing
15
  FEATURE_MAP_SIZE = 48 # Feature map size used in the model
16
  CENTER_WEIGHT_ORIGIN_PATH = './center_weight_origin.npy' # Path to center weight origin file
17
- DATASET_PATH = 'your_dataset_path' # Base path for the dataset
18
  EVAL_LABLE_PATH = os.path.join(DATASET_PATH, "val2017.json") # Path to validation labels JSON file
19
  EVAL_IMG_PATH = os.path.join(DATASET_PATH, 'imgs') # Path to validation images
20
 
@@ -50,6 +51,7 @@ def getAccRight(dist, th = 5/IMG_SIZE):
50
  res = np.zeros(dist.shape[1], dtype=np.int64)
51
  for i in range(dist.shape[1]):
52
  res[i] = sum(dist[:,i]<th)
 
53
  return res
54
 
55
  def myAcc(output, target):
@@ -63,6 +65,7 @@ def myAcc(output, target):
63
  Returns:
64
  cate_acc: Categorical accuracy [7,] representing the count of correct predictions per keypoint
65
  '''
 
66
  # [h, ls, rs, lb, rb, lr, rr]
67
  # output[:,6:10] = output[:,6:10]+output[:,2:6]
68
  # output[:,10:14] = output[:,10:14]+output[:,6:10]
@@ -91,11 +94,15 @@ def maxPoint(heatmap, center=True):
91
  if len(heatmap.shape) == 3:
92
  batch_size,h,w = heatmap.shape
93
  c = 1
 
94
  elif len(heatmap.shape) == 4:
95
  # n,c,h,w
96
  batch_size,c,h,w = heatmap.shape
 
97
  if center:
98
- heatmap = heatmap*_center_weight
 
 
99
  heatmap = heatmap.reshape((batch_size,c, -1)) #64,c, cfg['feature_map_size']xcfg['feature_map_size']
100
  max_id = np.argmax(heatmap,2)#64,c, 1
101
  y = max_id//w
@@ -103,47 +110,47 @@ def maxPoint(heatmap, center=True):
103
  # bv
104
  return x,y
105
 
 
106
  def movenetDecode(data, kps_mask=None,mode='output', num_joints = 17,
107
  img_size=192, hm_th=0.1):
108
-
109
- '''
110
- Decode MoveNet output data to predicted keypoints.
111
-
112
- Args:
113
- data: MoveNet output data
114
- kps_mask: Keypoints mask
115
- mode: Mode of decoding ('output' or 'label')
116
- num_joints: Number of joints/keypoints
117
- img_size: Image size
118
- hm_th: Threshold for heatmap processing
119
-
120
- Returns:
121
- res: Decoded keypoints
122
- '''
123
-
124
  ##data [64, 7, 48, 48] [64, 1, 48, 48] [64, 14, 48, 48] [64, 14, 48, 48]
125
  #kps_mask [n, 7]
 
 
126
  if mode == 'output':
127
  batch_size = data[0].shape[0]
 
128
  heatmaps = data[0]
 
129
  heatmaps[heatmaps < hm_th] = 0
 
130
  centers = data[1]
 
 
131
  regs = data[2]
132
  offsets = data[3]
 
 
133
  cx,cy = maxPoint(centers)
 
134
  dim0 = np.arange(batch_size,dtype=np.int32).reshape(batch_size,1)
135
  dim1 = np.zeros((batch_size,1),dtype=np.int32)
 
136
  res = []
137
  for n in range(num_joints):
 
 
138
  reg_x_origin = (regs[dim0,dim1+n*2,cy,cx]+0.5).astype(np.int32)
139
  reg_y_origin = (regs[dim0,dim1+n*2+1,cy,cx]+0.5).astype(np.int32)
140
  reg_x = reg_x_origin+cx
141
  reg_y = reg_y_origin+cy
 
142
  ### for post process
143
  reg_x = np.reshape(reg_x, (reg_x.shape[0],1,1))
144
  reg_y = np.reshape(reg_y, (reg_y.shape[0],1,1))
145
  reg_x = reg_x.repeat(FEATURE_MAP_SIZE,1).repeat(FEATURE_MAP_SIZE,2)
146
  reg_y = reg_y.repeat(FEATURE_MAP_SIZE,1).repeat(FEATURE_MAP_SIZE,2)
 
147
  range_weight_x = np.reshape(_range_weight_x,(1,FEATURE_MAP_SIZE,FEATURE_MAP_SIZE)).repeat(reg_x.shape[0],0)
148
  range_weight_y = np.reshape(_range_weight_y,(1,FEATURE_MAP_SIZE,FEATURE_MAP_SIZE)).repeat(reg_x.shape[0],0)
149
  tmp_reg_x = (range_weight_x-reg_x)**2
@@ -152,10 +159,12 @@ def movenetDecode(data, kps_mask=None,mode='output', num_joints = 17,
152
  tmp_reg = heatmaps[:,n,...]/tmp_reg
153
  tmp_reg = tmp_reg[:,np.newaxis,:,:]
154
  reg_x,reg_y = maxPoint(tmp_reg, center=False)
 
155
  reg_x[reg_x>47] = 47
156
  reg_x[reg_x<0] = 0
157
  reg_y[reg_y>47] = 47
158
  reg_y[reg_y<0] = 0
 
159
  score = heatmaps[dim0,dim1+n,reg_y,reg_x]
160
  offset_x = offsets[dim0,dim1+n*2,reg_y,reg_x]#*img_size//4
161
  offset_y = offsets[dim0,dim1+n*2+1,reg_y,reg_x]#*img_size//4
@@ -163,57 +172,69 @@ def movenetDecode(data, kps_mask=None,mode='output', num_joints = 17,
163
  res_y = (reg_y+offset_y)/(img_size//4)
164
  res_x[score<hm_th] = -1
165
  res_y[score<hm_th] = -1
 
 
166
  res.extend([res_x, res_y])
 
 
167
  res = np.concatenate(res,axis=1) #bs*14
 
 
 
168
  elif mode == 'label':
169
  kps_mask = kps_mask.detach().cpu().numpy()
 
170
  data = data.detach().cpu().numpy()
171
  batch_size = data.shape[0]
 
172
  heatmaps = data[:,:17,:,:]
173
  centers = data[:,17:18,:,:]
174
  regs = data[:,18:52,:,:]
175
  offsets = data[:,52:,:,:]
 
176
  cx,cy = maxPoint(centers)
177
  dim0 = np.arange(batch_size,dtype=np.int32).reshape(batch_size,1)
178
  dim1 = np.zeros((batch_size,1),dtype=np.int32)
 
179
  res = []
180
  for n in range(num_joints):
 
181
  reg_x_origin = (regs[dim0,dim1+n*2,cy,cx]+0.5).astype(np.int32)
182
  reg_y_origin = (regs[dim0,dim1+n*2+1,cy,cx]+0.5).astype(np.int32)
183
  reg_x = reg_x_origin+cx
184
  reg_y = reg_y_origin+cy
 
 
185
  reg_x[reg_x>47] = 47
186
  reg_x[reg_x<0] = 0
187
  reg_y[reg_y>47] = 47
188
  reg_y[reg_y<0] = 0
 
189
  offset_x = offsets[dim0,dim1+n*2,reg_y,reg_x]#*img_size//4
190
  offset_y = offsets[dim0,dim1+n*2+1,reg_y,reg_x]#*img_size//4
 
191
  res_x = (reg_x+offset_x)/(img_size//4)
192
  res_y = (reg_y+offset_y)/(img_size//4)
 
 
193
  res_x[kps_mask[:,n]==0] = -1
194
  res_y[kps_mask[:,n]==0] = -1
195
  res.extend([res_x, res_y])
 
 
196
  res = np.concatenate(res,axis=1) #bs*14
 
197
  return res
198
 
 
199
  def label2heatmap(keypoints, other_keypoints, img_size):
200
- '''
201
- Convert labeled keypoints to heatmaps for keypoints.
202
-
203
- Args:
204
- keypoints: Target person's keypoints
205
- other_keypoints: Other people's keypoints
206
- img_size: Size of the image
207
-
208
- Returns:
209
- heatmaps: Heatmaps for keypoints
210
- sigma: Value used for heatmap generation
211
- '''
212
  #keypoints: target person
213
  #other_keypoints: other people's keypoints need to be add to the heatmap
214
  heatmaps = []
 
215
  keypoints_range = np.reshape(keypoints,(-1,3))
216
  keypoints_range = keypoints_range[keypoints_range[:,2]>0]
 
217
  min_x = np.min(keypoints_range[:,0])
218
  min_y = np.min(keypoints_range[:,1])
219
  max_x = np.max(keypoints_range[:,0])
@@ -226,40 +247,36 @@ def label2heatmap(keypoints, other_keypoints, img_size):
226
  sigma = 5
227
  else:
228
  sigma = 7
 
 
229
  for i in range(0,len(keypoints),3):
230
  if keypoints[i+2]==0:
231
  heatmaps.append(np.zeros((img_size//4, img_size//4)))
232
  continue
233
- x = int(keypoints[i]*img_size//4)
 
234
  y = int(keypoints[i+1]*img_size//4)
235
  if x==img_size//4:x=(img_size//4-1)
236
  if y==img_size//4:y=(img_size//4-1)
237
  if x>img_size//4 or x<0:x=-1
238
  if y>img_size//4 or y<0:y=-1
239
  heatmap = generate_heatmap(x, y, other_keypoints[i//3], (img_size//4, img_size//4),sigma)
 
240
  heatmaps.append(heatmap)
 
241
  heatmaps = np.array(heatmaps, dtype=np.float32)
242
  return heatmaps,sigma
243
 
244
- def generate_heatmap(x, y, other_keypoints, size, sigma):
245
- '''
246
- Generate a heatmap for a specific keypoint.
247
-
248
- Args:
249
- x, y: Absolute position of the keypoint
250
- other_keypoints: Position of other keypoints
251
- size: Size of the heatmap
252
- sigma: Value used for heatmap generation
253
 
254
- Returns:
255
- heatmap: Generated heatmap for the keypoint
256
- '''
257
  #x,y abs postion
258
  #other_keypoints positive position
259
  sigma+=6
260
  heatmap = np.zeros(size)
261
  if x<0 or y<0 or x>=size[0] or y>=size[1]:
262
  return heatmap
 
263
  tops = [[x,y]]
264
  if len(other_keypoints)>0:
265
  #add other people's keypoints
@@ -270,6 +287,8 @@ def generate_heatmap(x, y, other_keypoints, size, sigma):
270
  if y==size[1]:y=(size[1]-1)
271
  if x>size[0] or x<0 or y>size[1] or y<0: continue
272
  tops.append([x,y])
 
 
273
  for top in tops:
274
  #heatmap[top[1]][top[0]] = 1
275
  x,y = top
@@ -277,60 +296,55 @@ def generate_heatmap(x, y, other_keypoints, size, sigma):
277
  x1 = min(size[0],x+sigma//2)
278
  y0 = max(0,y-sigma//2)
279
  y1 = min(size[1],y+sigma//2)
 
 
280
  for map_y in range(y0, y1):
281
  for map_x in range(x0, x1):
282
  d2 = ((map_x - x) ** 2 + (map_y - y) ** 2)**0.5
 
283
  if d2<=sigma//2:
284
  heatmap[map_y, map_x] += math.exp(-d2/(sigma//2)*3)
285
  if heatmap[map_y, map_x] > 1:
 
286
  heatmap[map_y, map_x] = 1
 
287
  # heatmap[heatmap<0.1] = 0
288
  return heatmap
289
 
 
290
  def label2center(cx, cy, other_centers, img_size, sigma):
291
- '''
292
- Convert labeled keypoints to a center heatmap.
293
-
294
- Args:
295
- cx, cy: Center coordinates
296
- other_centers: Other people's centers
297
- img_size: Size of the image
298
- sigma: Value used for heatmap generation
299
-
300
- Returns:
301
- heatmaps: Heatmap representing the center
302
- '''
303
  heatmaps = []
 
304
  heatmap = generate_heatmap(cx, cy, other_centers, (img_size//4, img_size//4),sigma+2)
305
  heatmaps.append(heatmap)
 
306
  heatmaps = np.array(heatmaps, dtype=np.float32)
 
 
307
  return heatmaps
308
 
 
309
  def label2reg(keypoints, cx, cy, img_size):
310
- '''
311
- Convert labeled keypoints to regression maps.
312
-
313
- Args:
314
- keypoints: Labeled keypoints
315
- cx, cy: Center coordinates
316
- img_size: Size of the image
317
-
318
- Returns:
319
- heatmaps: Regression maps for keypoints
320
- '''
321
 
322
  heatmaps = np.zeros((len(keypoints)//3*2, img_size//4, img_size//4), dtype=np.float32)
 
323
  for i in range(len(keypoints)//3):
324
  if keypoints[i*3+2]==0:
325
  continue
 
326
  x = keypoints[i*3]*img_size//4
327
  y = keypoints[i*3+1]*img_size//4
328
  if x==img_size//4:x=(img_size//4-1)
329
  if y==img_size//4:y=(img_size//4-1)
330
  if x>img_size//4 or x<0 or y>img_size//4 or y<0:
331
  continue
 
332
  reg_x = x-cx
333
  reg_y = y-cy
 
 
 
 
334
  for j in range(cy-2,cy+3):
335
  if j<0 or j>img_size//4-1:
336
  continue
@@ -345,55 +359,57 @@ def label2reg(keypoints, cx, cy, img_size):
345
  heatmaps[i*2+1][j][k] = reg_y-(cy-j)#/(img_size//4)
346
  else:
347
  heatmaps[i*2+1][j][k] = reg_y+(cy-j)
 
348
  return heatmaps
349
 
 
350
  def label2offset(keypoints, cx, cy, regs, img_size):
351
- '''
352
- Convert labeled keypoints to offset maps.
353
-
354
- Args:
355
- keypoints: Labeled keypoints
356
- cx, cy: Center coordinates
357
- regs: Regression maps
358
- img_size: Size of the image
359
-
360
- Returns:
361
- heatmaps: Offset maps for keypoints
362
- '''
363
  heatmaps = np.zeros((len(keypoints)//3*2, img_size//4, img_size//4), dtype=np.float32)
 
364
  for i in range(len(keypoints)//3):
365
  if keypoints[i*3+2]==0:
366
  continue
 
367
  large_x = int(keypoints[i*3]*img_size)
368
  large_y = int(keypoints[i*3+1]*img_size)
 
 
369
  small_x = int(regs[i*2,cy,cx]+cx)
370
  small_y = int(regs[i*2+1,cy,cx]+cy)
 
 
371
  offset_x = large_x/4-small_x
372
  offset_y = large_y/4-small_y
 
373
  if small_x==img_size//4:small_x=(img_size//4-1)
374
  if small_y==img_size//4:small_y=(img_size//4-1)
375
  if small_x>img_size//4 or small_x<0 or small_y>img_size//4 or small_y<0:
376
  continue
 
377
  heatmaps[i*2][small_y][small_x] = offset_x#/(img_size//4)
378
  heatmaps[i*2+1][small_y][small_x] = offset_y#/(img_size//4)
 
 
379
  return heatmaps
380
 
 
381
  class TensorDataset(Dataset):
382
- '''
383
- Custom Dataset class for handling data loading and preprocessing
384
- '''
385
 
386
  def __init__(self, data_labels, img_dir, img_size, data_aug=None):
387
  self.data_labels = data_labels
388
  self.img_dir = img_dir
389
  self.data_aug = data_aug
390
  self.img_size = img_size
 
 
391
  self.interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA,
392
  cv2.INTER_NEAREST, cv2.INTER_LANCZOS4]
393
 
394
 
395
  def __getitem__(self, index):
 
396
  item = self.data_labels[index]
 
397
  """
398
  item = {
399
  "img_name":save_name,
@@ -405,50 +421,70 @@ class TensorDataset(Dataset):
405
  """
406
  # [name,h,w,keypoints...]
407
  img_path = os.path.join(self.img_dir, item["img_name"])
 
408
  img = cv2.imread(img_path, cv2.IMREAD_COLOR)
409
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 
410
  img = cv2.resize(img, (self.img_size, self.img_size),
411
  interpolation=random.choice(self.interp_methods))
 
 
412
  #### Data Augmentation
413
  if self.data_aug is not None:
414
  img, item = self.data_aug(img, item)
 
 
415
  img = img.astype(np.float32)
416
  img = np.transpose(img,axes=[2,0,1])
 
 
417
  keypoints = item["keypoints"]
418
  center = item['center']
419
  other_centers = item["other_centers"]
420
  other_keypoints = item["other_keypoints"]
 
 
421
  kps_mask = np.ones(len(keypoints)//3)
422
  for i in range(len(keypoints)//3):
 
423
  if keypoints[i*3+2]==0:
424
  kps_mask[i] = 0
 
 
 
425
  heatmaps,sigma = label2heatmap(keypoints, other_keypoints, self.img_size) #(17, 48, 48)
 
 
 
426
  cx = min(max(0,int(center[0]*self.img_size//4)),self.img_size//4-1)
427
  cy = min(max(0,int(center[1]*self.img_size//4)),self.img_size//4-1)
 
 
428
  centers = label2center(cx, cy, other_centers, self.img_size, sigma) #(1, 48, 48)
 
429
  regs = label2reg(keypoints, cx, cy, self.img_size) #(14, 48, 48)
 
 
430
  offsets = label2offset(keypoints, cx, cy, regs, self.img_size)#(14, 48, 48)
 
 
 
 
431
  labels = np.concatenate([heatmaps,centers,regs,offsets],axis=0)
432
  img = img / 127.5 - 1.0
433
  return img, labels, kps_mask, img_path
434
 
 
 
 
435
  def __len__(self):
436
  return len(self.data_labels)
437
 
438
  # Function to get data loader based on mode (e.g., evaluation)
439
  def getDataLoader(mode, input_data):
440
- '''
441
- Function to get data loader based on mode (e.g., evaluation).
442
-
443
- Args:
444
- mode: Mode of data loader (e.g., 'eval')
445
- input_data: Input data
446
-
447
- Returns:
448
- data_loader: DataLoader for specified mode
449
- '''
450
 
451
  if mode=="eval":
 
452
  val_loader = torch.utils.data.DataLoader(
453
  TensorDataset(input_data[0],
454
  EVAL_IMG_PATH,
@@ -458,33 +494,27 @@ def getDataLoader(mode, input_data):
458
  shuffle=False,
459
  num_workers=0,
460
  pin_memory=False)
 
461
  return val_loader
462
 
463
  # Class for managing data and obtaining evaluation data loader
464
  class Data():
465
- '''
466
- Class for managing data and obtaining evaluation data loader.
467
- '''
468
  def __init__(self):
469
  pass
470
 
471
  def getEvalDataloader(self):
472
  with open(EVAL_LABLE_PATH, 'r') as f:
473
  data_label_list = json.loads(f.readlines()[0])
 
474
  print("[INFO] Total images: ", len(data_label_list))
 
 
475
  input_data = [data_label_list]
476
  data_loader = getDataLoader("eval",
477
  input_data)
478
  return data_loader
479
-
480
  # Configs for onnx inference session
481
  def make_parser():
482
- '''
483
- Create parser for MoveNet ONNX runtime inference.
484
-
485
- Returns:
486
- parser: Argument parser for MoveNet inference
487
- '''
488
  parser = argparse.ArgumentParser("movenet onnxruntime inference")
489
  parser.add_argument(
490
  "--ipu",
@@ -514,20 +544,30 @@ if __name__ == '__main__':
514
  data_loader = data.getEvalDataloader()
515
  # Load MoveNet model using ONNX runtime
516
  model = rt.InferenceSession(MODEL_DIR, providers=providers, provider_options=provider_options)
517
-
518
  correct = 0
519
  total = 0
520
  # Loop through the data loader for evaluation
521
  for batch_idx, (imgs, labels, kps_mask, img_names) in enumerate(data_loader):
 
522
  if batch_idx%100 == 0:
523
  print('Finish ',batch_idx)
 
524
  imgs = imgs.detach().cpu().numpy()
525
- output = model.run(['1548','1607','1665','1723'],{'blob.1':imgs})
 
 
 
 
 
526
  pre = movenetDecode(output, kps_mask,mode='output',img_size=IMG_SIZE)
527
  gt = movenetDecode(labels, kps_mask,mode='label',img_size=IMG_SIZE)
 
 
528
  acc = myAcc(pre, gt)
 
529
  correct += sum(acc)
530
  total += len(acc)
531
  # Compute and print accuracy based on evaluated data
532
  acc = correct/total
533
- print('[Info] acc: {:.3f}% \n'.format(100. * acc))
 
1
+
2
  import onnxruntime as rt
3
  import numpy as np
4
  import json
 
15
  IMG_SIZE = 192 # Image size used for processing
16
  FEATURE_MAP_SIZE = 48 # Feature map size used in the model
17
  CENTER_WEIGHT_ORIGIN_PATH = './center_weight_origin.npy' # Path to center weight origin file
18
+ DATASET_PATH = '/group/dphi_algo_scratch_02/ziheng/datasets/coco/croped' # Base path for the dataset
19
  EVAL_LABLE_PATH = os.path.join(DATASET_PATH, "val2017.json") # Path to validation labels JSON file
20
  EVAL_IMG_PATH = os.path.join(DATASET_PATH, 'imgs') # Path to validation images
21
 
 
51
  res = np.zeros(dist.shape[1], dtype=np.int64)
52
  for i in range(dist.shape[1]):
53
  res[i] = sum(dist[:,i]<th)
54
+
55
  return res
56
 
57
  def myAcc(output, target):
 
65
  Returns:
66
  cate_acc: Categorical accuracy [7,] representing the count of correct predictions per keypoint
67
  '''
68
+
69
  # [h, ls, rs, lb, rb, lr, rr]
70
  # output[:,6:10] = output[:,6:10]+output[:,2:6]
71
  # output[:,10:14] = output[:,10:14]+output[:,6:10]
 
94
  if len(heatmap.shape) == 3:
95
  batch_size,h,w = heatmap.shape
96
  c = 1
97
+
98
  elif len(heatmap.shape) == 4:
99
  # n,c,h,w
100
  batch_size,c,h,w = heatmap.shape
101
+
102
  if center:
103
+ heatmap = heatmap*_center_weight#加权取最靠近中间的
104
+
105
+
106
  heatmap = heatmap.reshape((batch_size,c, -1)) #64,c, cfg['feature_map_size']xcfg['feature_map_size']
107
  max_id = np.argmax(heatmap,2)#64,c, 1
108
  y = max_id//w
 
110
  # bv
111
  return x,y
112
 
113
+ # Function for decoding MoveNet output data
114
  def movenetDecode(data, kps_mask=None,mode='output', num_joints = 17,
115
  img_size=192, hm_th=0.1):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  ##data [64, 7, 48, 48] [64, 1, 48, 48] [64, 14, 48, 48] [64, 14, 48, 48]
117
  #kps_mask [n, 7]
118
+
119
+
120
  if mode == 'output':
121
  batch_size = data[0].shape[0]
122
+
123
  heatmaps = data[0]
124
+
125
  heatmaps[heatmaps < hm_th] = 0
126
+
127
  centers = data[1]
128
+
129
+
130
  regs = data[2]
131
  offsets = data[3]
132
+
133
+
134
  cx,cy = maxPoint(centers)
135
+
136
  dim0 = np.arange(batch_size,dtype=np.int32).reshape(batch_size,1)
137
  dim1 = np.zeros((batch_size,1),dtype=np.int32)
138
+
139
  res = []
140
  for n in range(num_joints):
141
+ #nchw!!!!!!!!!!!!!!!!!
142
+
143
  reg_x_origin = (regs[dim0,dim1+n*2,cy,cx]+0.5).astype(np.int32)
144
  reg_y_origin = (regs[dim0,dim1+n*2+1,cy,cx]+0.5).astype(np.int32)
145
  reg_x = reg_x_origin+cx
146
  reg_y = reg_y_origin+cy
147
+
148
  ### for post process
149
  reg_x = np.reshape(reg_x, (reg_x.shape[0],1,1))
150
  reg_y = np.reshape(reg_y, (reg_y.shape[0],1,1))
151
  reg_x = reg_x.repeat(FEATURE_MAP_SIZE,1).repeat(FEATURE_MAP_SIZE,2)
152
  reg_y = reg_y.repeat(FEATURE_MAP_SIZE,1).repeat(FEATURE_MAP_SIZE,2)
153
+ #### 根据center得到关键点回归位置,然后加权heatmap
154
  range_weight_x = np.reshape(_range_weight_x,(1,FEATURE_MAP_SIZE,FEATURE_MAP_SIZE)).repeat(reg_x.shape[0],0)
155
  range_weight_y = np.reshape(_range_weight_y,(1,FEATURE_MAP_SIZE,FEATURE_MAP_SIZE)).repeat(reg_x.shape[0],0)
156
  tmp_reg_x = (range_weight_x-reg_x)**2
 
159
  tmp_reg = heatmaps[:,n,...]/tmp_reg
160
  tmp_reg = tmp_reg[:,np.newaxis,:,:]
161
  reg_x,reg_y = maxPoint(tmp_reg, center=False)
162
+
163
  reg_x[reg_x>47] = 47
164
  reg_x[reg_x<0] = 0
165
  reg_y[reg_y>47] = 47
166
  reg_y[reg_y<0] = 0
167
+
168
  score = heatmaps[dim0,dim1+n,reg_y,reg_x]
169
  offset_x = offsets[dim0,dim1+n*2,reg_y,reg_x]#*img_size//4
170
  offset_y = offsets[dim0,dim1+n*2+1,reg_y,reg_x]#*img_size//4
 
172
  res_y = (reg_y+offset_y)/(img_size//4)
173
  res_x[score<hm_th] = -1
174
  res_y[score<hm_th] = -1
175
+
176
+
177
  res.extend([res_x, res_y])
178
+ # b
179
+
180
  res = np.concatenate(res,axis=1) #bs*14
181
+
182
+
183
+
184
  elif mode == 'label':
185
  kps_mask = kps_mask.detach().cpu().numpy()
186
+
187
  data = data.detach().cpu().numpy()
188
  batch_size = data.shape[0]
189
+
190
  heatmaps = data[:,:17,:,:]
191
  centers = data[:,17:18,:,:]
192
  regs = data[:,18:52,:,:]
193
  offsets = data[:,52:,:,:]
194
+
195
  cx,cy = maxPoint(centers)
196
  dim0 = np.arange(batch_size,dtype=np.int32).reshape(batch_size,1)
197
  dim1 = np.zeros((batch_size,1),dtype=np.int32)
198
+
199
  res = []
200
  for n in range(num_joints):
201
+ #nchw!!!!!!!!!!!!!!!!!
202
  reg_x_origin = (regs[dim0,dim1+n*2,cy,cx]+0.5).astype(np.int32)
203
  reg_y_origin = (regs[dim0,dim1+n*2+1,cy,cx]+0.5).astype(np.int32)
204
  reg_x = reg_x_origin+cx
205
  reg_y = reg_y_origin+cy
206
+
207
+ # print(reg_x, reg_y)
208
  reg_x[reg_x>47] = 47
209
  reg_x[reg_x<0] = 0
210
  reg_y[reg_y>47] = 47
211
  reg_y[reg_y<0] = 0
212
+
213
  offset_x = offsets[dim0,dim1+n*2,reg_y,reg_x]#*img_size//4
214
  offset_y = offsets[dim0,dim1+n*2+1,reg_y,reg_x]#*img_size//4
215
+ # print(offset_x,offset_y)
216
  res_x = (reg_x+offset_x)/(img_size//4)
217
  res_y = (reg_y+offset_y)/(img_size//4)
218
+
219
+ #不存在的点设为-1 后续不参与acc计算
220
  res_x[kps_mask[:,n]==0] = -1
221
  res_y[kps_mask[:,n]==0] = -1
222
  res.extend([res_x, res_y])
223
+ # b
224
+
225
  res = np.concatenate(res,axis=1) #bs*14
226
+
227
  return res
228
 
229
+ # Function to convert labeled keypoints to heatmaps for keypoints
230
  def label2heatmap(keypoints, other_keypoints, img_size):
 
 
 
 
 
 
 
 
 
 
 
 
231
  #keypoints: target person
232
  #other_keypoints: other people's keypoints need to be add to the heatmap
233
  heatmaps = []
234
+
235
  keypoints_range = np.reshape(keypoints,(-1,3))
236
  keypoints_range = keypoints_range[keypoints_range[:,2]>0]
237
+ # print(keypoints_range)
238
  min_x = np.min(keypoints_range[:,0])
239
  min_y = np.min(keypoints_range[:,1])
240
  max_x = np.max(keypoints_range[:,0])
 
247
  sigma = 5
248
  else:
249
  sigma = 7
250
+
251
+
252
  for i in range(0,len(keypoints),3):
253
  if keypoints[i+2]==0:
254
  heatmaps.append(np.zeros((img_size//4, img_size//4)))
255
  continue
256
+
257
+ x = int(keypoints[i]*img_size//4) #取值应该是0-47
258
  y = int(keypoints[i+1]*img_size//4)
259
  if x==img_size//4:x=(img_size//4-1)
260
  if y==img_size//4:y=(img_size//4-1)
261
  if x>img_size//4 or x<0:x=-1
262
  if y>img_size//4 or y<0:y=-1
263
  heatmap = generate_heatmap(x, y, other_keypoints[i//3], (img_size//4, img_size//4),sigma)
264
+
265
  heatmaps.append(heatmap)
266
+
267
  heatmaps = np.array(heatmaps, dtype=np.float32)
268
  return heatmaps,sigma
269
 
 
 
 
 
 
 
 
 
 
270
 
271
+ # Function to generate a heatmap for a specific keypoint
272
+ def generate_heatmap(x, y, other_keypoints, size, sigma):
 
273
  #x,y abs postion
274
  #other_keypoints positive position
275
  sigma+=6
276
  heatmap = np.zeros(size)
277
  if x<0 or y<0 or x>=size[0] or y>=size[1]:
278
  return heatmap
279
+
280
  tops = [[x,y]]
281
  if len(other_keypoints)>0:
282
  #add other people's keypoints
 
287
  if y==size[1]:y=(size[1]-1)
288
  if x>size[0] or x<0 or y>size[1] or y<0: continue
289
  tops.append([x,y])
290
+
291
+
292
  for top in tops:
293
  #heatmap[top[1]][top[0]] = 1
294
  x,y = top
 
296
  x1 = min(size[0],x+sigma//2)
297
  y0 = max(0,y-sigma//2)
298
  y1 = min(size[1],y+sigma//2)
299
+
300
+
301
  for map_y in range(y0, y1):
302
  for map_x in range(x0, x1):
303
  d2 = ((map_x - x) ** 2 + (map_y - y) ** 2)**0.5
304
+
305
  if d2<=sigma//2:
306
  heatmap[map_y, map_x] += math.exp(-d2/(sigma//2)*3)
307
  if heatmap[map_y, map_x] > 1:
308
+ #不同关键点可能重合,这里累加
309
  heatmap[map_y, map_x] = 1
310
+
311
  # heatmap[heatmap<0.1] = 0
312
  return heatmap
313
 
314
+ # Function to convert labeled keypoints to a center heatmap
315
  def label2center(cx, cy, other_centers, img_size, sigma):
 
 
 
 
 
 
 
 
 
 
 
 
316
  heatmaps = []
317
+
318
  heatmap = generate_heatmap(cx, cy, other_centers, (img_size//4, img_size//4),sigma+2)
319
  heatmaps.append(heatmap)
320
+
321
  heatmaps = np.array(heatmaps, dtype=np.float32)
322
+
323
+
324
  return heatmaps
325
 
326
+ # Function to convert labeled keypoints to regression maps
327
  def label2reg(keypoints, cx, cy, img_size):
 
 
 
 
 
 
 
 
 
 
 
328
 
329
  heatmaps = np.zeros((len(keypoints)//3*2, img_size//4, img_size//4), dtype=np.float32)
330
+ # print(keypoints)
331
  for i in range(len(keypoints)//3):
332
  if keypoints[i*3+2]==0:
333
  continue
334
+
335
  x = keypoints[i*3]*img_size//4
336
  y = keypoints[i*3+1]*img_size//4
337
  if x==img_size//4:x=(img_size//4-1)
338
  if y==img_size//4:y=(img_size//4-1)
339
  if x>img_size//4 or x<0 or y>img_size//4 or y<0:
340
  continue
341
+
342
  reg_x = x-cx
343
  reg_y = y-cy
344
+
345
+
346
+
347
+
348
  for j in range(cy-2,cy+3):
349
  if j<0 or j>img_size//4-1:
350
  continue
 
359
  heatmaps[i*2+1][j][k] = reg_y-(cy-j)#/(img_size//4)
360
  else:
361
  heatmaps[i*2+1][j][k] = reg_y+(cy-j)
362
+
363
  return heatmaps
364
 
365
+ # Function to convert labeled keypoints to offset maps
366
  def label2offset(keypoints, cx, cy, regs, img_size):
 
 
 
 
 
 
 
 
 
 
 
 
367
  heatmaps = np.zeros((len(keypoints)//3*2, img_size//4, img_size//4), dtype=np.float32)
368
+
369
  for i in range(len(keypoints)//3):
370
  if keypoints[i*3+2]==0:
371
  continue
372
+
373
  large_x = int(keypoints[i*3]*img_size)
374
  large_y = int(keypoints[i*3+1]*img_size)
375
+
376
+
377
  small_x = int(regs[i*2,cy,cx]+cx)
378
  small_y = int(regs[i*2+1,cy,cx]+cy)
379
+
380
+
381
  offset_x = large_x/4-small_x
382
  offset_y = large_y/4-small_y
383
+
384
  if small_x==img_size//4:small_x=(img_size//4-1)
385
  if small_y==img_size//4:small_y=(img_size//4-1)
386
  if small_x>img_size//4 or small_x<0 or small_y>img_size//4 or small_y<0:
387
  continue
388
+
389
  heatmaps[i*2][small_y][small_x] = offset_x#/(img_size//4)
390
  heatmaps[i*2+1][small_y][small_x] = offset_y#/(img_size//4)
391
+
392
+
393
  return heatmaps
394
 
395
+ # Custom Dataset class for handling data loading and preprocessing
396
  class TensorDataset(Dataset):
 
 
 
397
 
398
  def __init__(self, data_labels, img_dir, img_size, data_aug=None):
399
  self.data_labels = data_labels
400
  self.img_dir = img_dir
401
  self.data_aug = data_aug
402
  self.img_size = img_size
403
+
404
+
405
  self.interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA,
406
  cv2.INTER_NEAREST, cv2.INTER_LANCZOS4]
407
 
408
 
409
  def __getitem__(self, index):
410
+
411
  item = self.data_labels[index]
412
+
413
  """
414
  item = {
415
  "img_name":save_name,
 
421
  """
422
  # [name,h,w,keypoints...]
423
  img_path = os.path.join(self.img_dir, item["img_name"])
424
+
425
  img = cv2.imread(img_path, cv2.IMREAD_COLOR)
426
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
427
+
428
  img = cv2.resize(img, (self.img_size, self.img_size),
429
  interpolation=random.choice(self.interp_methods))
430
+
431
+
432
  #### Data Augmentation
433
  if self.data_aug is not None:
434
  img, item = self.data_aug(img, item)
435
+
436
+
437
  img = img.astype(np.float32)
438
  img = np.transpose(img,axes=[2,0,1])
439
+
440
+
441
  keypoints = item["keypoints"]
442
  center = item['center']
443
  other_centers = item["other_centers"]
444
  other_keypoints = item["other_keypoints"]
445
+
446
+
447
  kps_mask = np.ones(len(keypoints)//3)
448
  for i in range(len(keypoints)//3):
449
+ ##0没有标注;1有标注不可见(被遮挡);2有标注可见
450
  if keypoints[i*3+2]==0:
451
  kps_mask[i] = 0
452
+
453
+
454
+
455
  heatmaps,sigma = label2heatmap(keypoints, other_keypoints, self.img_size) #(17, 48, 48)
456
+
457
+
458
+
459
  cx = min(max(0,int(center[0]*self.img_size//4)),self.img_size//4-1)
460
  cy = min(max(0,int(center[1]*self.img_size//4)),self.img_size//4-1)
461
+
462
+
463
  centers = label2center(cx, cy, other_centers, self.img_size, sigma) #(1, 48, 48)
464
+
465
  regs = label2reg(keypoints, cx, cy, self.img_size) #(14, 48, 48)
466
+
467
+
468
  offsets = label2offset(keypoints, cx, cy, regs, self.img_size)#(14, 48, 48)
469
+
470
+
471
+
472
+
473
  labels = np.concatenate([heatmaps,centers,regs,offsets],axis=0)
474
  img = img / 127.5 - 1.0
475
  return img, labels, kps_mask, img_path
476
 
477
+
478
+
479
+
480
  def __len__(self):
481
  return len(self.data_labels)
482
 
483
  # Function to get data loader based on mode (e.g., evaluation)
484
  def getDataLoader(mode, input_data):
 
 
 
 
 
 
 
 
 
 
485
 
486
  if mode=="eval":
487
+
488
  val_loader = torch.utils.data.DataLoader(
489
  TensorDataset(input_data[0],
490
  EVAL_IMG_PATH,
 
494
  shuffle=False,
495
  num_workers=0,
496
  pin_memory=False)
497
+
498
  return val_loader
499
 
500
  # Class for managing data and obtaining evaluation data loader
501
  class Data():
 
 
 
502
  def __init__(self):
503
  pass
504
 
505
  def getEvalDataloader(self):
506
  with open(EVAL_LABLE_PATH, 'r') as f:
507
  data_label_list = json.loads(f.readlines()[0])
508
+
509
  print("[INFO] Total images: ", len(data_label_list))
510
+
511
+
512
  input_data = [data_label_list]
513
  data_loader = getDataLoader("eval",
514
  input_data)
515
  return data_loader
 
516
  # Configs for onnx inference session
517
  def make_parser():
 
 
 
 
 
 
518
  parser = argparse.ArgumentParser("movenet onnxruntime inference")
519
  parser.add_argument(
520
  "--ipu",
 
544
  data_loader = data.getEvalDataloader()
545
  # Load MoveNet model using ONNX runtime
546
  model = rt.InferenceSession(MODEL_DIR, providers=providers, provider_options=provider_options)
547
+
548
  correct = 0
549
  total = 0
550
  # Loop through the data loader for evaluation
551
  for batch_idx, (imgs, labels, kps_mask, img_names) in enumerate(data_loader):
552
+
553
  if batch_idx%100 == 0:
554
  print('Finish ',batch_idx)
555
+
556
  imgs = imgs.detach().cpu().numpy()
557
+ imgs = imgs.transpose((0,2,3,1))
558
+ output = model.run(['1548_transpose','1607_transpose','1665_transpose','1723_transpose'],{'blob.1':imgs})
559
+ output[0] = output[0].transpose((0,3,1,2))
560
+ output[1] = output[1].transpose((0,3,1,2))
561
+ output[2] = output[2].transpose((0,3,1,2))
562
+ output[3] = output[3].transpose((0,3,1,2))
563
  pre = movenetDecode(output, kps_mask,mode='output',img_size=IMG_SIZE)
564
  gt = movenetDecode(labels, kps_mask,mode='label',img_size=IMG_SIZE)
565
+
566
+ #n
567
  acc = myAcc(pre, gt)
568
+
569
  correct += sum(acc)
570
  total += len(acc)
571
  # Compute and print accuracy based on evaluated data
572
  acc = correct/total
573
+ print('[Info] acc: {:.3f}% \n'.format(100. * acc))
movenet_int8.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:9e3243a492e8886d29d9dcdd2831da3c69eb18bc76185f2db565cfe8ddcd58d8
3
- size 7681846
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
3
+ size 0