Skip to content

Commit 0f5b709

Browse files
committed
Update
1 parent b3ba809 commit 0f5b709

11 files changed

+152
-37
lines changed

dataset_tools/transcrowdpose2coco.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
for path in file_path:
2828
data_t = read_crowd_pose(path)
2929
datas.extend(data_t)
30-
do_vis = True
30+
do_vis = False
3131
new_coco_data = []
3232
for i,data in enumerate(datas):
3333
sys.stdout.write(f"\r{i}/{len(datas)}")
@@ -52,7 +52,7 @@
5252
wmli.imwrite(save_path,img)
5353
new_coco_data.append([img_name,org_bboxes,kps])
5454

55-
coco_pt_path = '/home/wj/ai/mldata1/crowd_pose/CrowdPose/crowdpose_coco.pt',
55+
coco_pt_path = '/home/wj/ai/mldata1/crowd_pose/CrowdPose/crowdpose_coco.pt'
5656
with open(coco_pt_path,"wb") as f:
5757
pickle.dump(new_coco_data,f)
5858
exit(0)

dataset_tools/transpennaction2cocov2.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@
3030
else:
3131
datas = datas[eidx:bidx]
3232
datas = datas[::-1]
33-
do_vis = False
33+
do_vis = True
3434
if not os.path.exists(coco_pt_dir):
3535
os.makedirs(coco_pt_dir)
3636

3737
for i,data in enumerate(datas):
3838
sys.stdout.write(f"\r{i}/{len(datas)}")
3939
file,kps,bboxes = data
4040
bf_name = wmlu.base_name(file)
41-
coco_pt_path = f"/home/wj/ai/mldata1/penn_action/Penn_Action/coco_labels/{bf_name}.pt"
41+
'''coco_pt_path = f"/home/wj/ai/mldata1/penn_action/Penn_Action/coco_labels/{bf_name}.pt"
4242
if osp.exists(coco_pt_path):
43-
continue
43+
continue'''
4444
new_coco_data = []
45+
left_node = list(range(1,17,2))
4546
for i,kp,bbox in zip(count(),kps,bboxes):
4647
img_name = f"{bf_name}/{i+1:06d}.jpg"
4748
file_path = osp.join(img_dir_path,img_name)
@@ -59,10 +60,10 @@
5960
if do_vis:
6061
t_bboxes = odb.npchangexyorder(t_bboxes)
6162
img = odv.draw_bboxes(img,bboxes=t_bboxes,is_relative_coordinate=False)
62-
img = odv.draw_keypoints(img,kps,no_line=False,joints_pair=JOINTS_PAIR)
63+
img = odv.draw_keypoints(img,kps,no_line=False,joints_pair=JOINTS_PAIR,left_node=left_node)
6364
img = odv.draw_keypoints(img,coco_kps,no_line=True)
6465
save_path = osp.join(save_dir,img_name)
6566
wmli.imwrite(save_path,img)
6667
new_coco_data.append([img_name,org_bboxes,kps])
67-
with open(coco_pt_path,"wb") as f:
68-
pickle.dump(new_coco_data,f)
68+
#with open(coco_pt_path,"wb") as f:
69+
#pickle.dump(new_coco_data,f)

lib/core/function.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import time
1212
import logging
1313
import os
14-
14+
from datadef import *
1515
import numpy as np
1616
import torch
1717
import wtorch.utils as wtu
@@ -90,7 +90,7 @@ def train(config, train_loader, model, criterion, optimizer, epoch,
9090
batch_time.update(time.time() - end)
9191
end = time.time()
9292

93-
if True or i % config.PRINT_FREQ == 0:
93+
if i % config.PRINT_FREQ == 0:
9494
msg = 'Epoch: [{0}][{1}/{2}]\t' \
9595
'Time {batch_time.val:.3f}s ({batch_time.avg:.3f}s)\t' \
9696
'Speed {speed:.1f} samples/s\t' \
@@ -152,6 +152,7 @@ def validate(config, val_loader, val_dataset, model, criterion, output_dir,
152152
for i, (input, target, target_weight, meta) in enumerate(val_loader):
153153
# compute output
154154
outputs = model(input)
155+
#outputs = target.cuda()
155156
if isinstance(outputs, list):
156157
output = outputs[-1]
157158
else:
@@ -225,6 +226,7 @@ def validate(config, val_loader, val_dataset, model, criterion, output_dir,
225226
prefix = '{}_{}'.format(
226227
os.path.join(output_dir, 'val'), i
227228
)
229+
print(f"Save {prefix}")
228230
save_debug_images(config, input, meta, target, pred*4, output,
229231
prefix)
230232

lib/datadef.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ def get_ckpt_dir():
2424

2525
def get_log_dir(suffix=""):
2626
torch_loger_dir = get_data_dir("tmp/tbloger"+suffix)
27-
return torch_loger_dir
27+
return torch_loger_dir
28+
29+
30+
def is_debug(default_value = True):
31+
print(f"is_debug {default_value}")
32+
return default_value

lib/dataset/JointsDataset.py

+37-13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from utils.transforms import fliplr_joints
2424
import wtorch.dataset_toolkit as tdt
2525
import img_utils as wmli
26+
from datadef import *
2627

2728

2829
logger = logging.getLogger(__name__)
@@ -177,6 +178,9 @@ def __getitem__(self, idx):
177178
joints, joints_vis = fliplr_joints(
178179
joints, joints_vis, data_numpy.shape[1], self.flip_pairs)
179180
c[0] = data_numpy.shape[1] - c[0] - 1
181+
else:
182+
#return self.trans_data_type0(data_numpy,db_rec)
183+
pass
180184

181185
trans = get_affine_transform(c, s, r, self.image_size)
182186
input = cv2.warpAffine(
@@ -232,31 +236,51 @@ def trans_data_type0(self,data_numpy,db_rec):
232236
bbox = db_rec.get('clean_bbox',None)
233237
if bbox is None:
234238
bbox = odk.npget_bbox(joints)
235-
bbox = odb.npscale_bboxes(bbox,1.25)
239+
bbox = odb.npscale_bboxes(bbox,1.25,max_size=data_numpy.shape[:2][::-1])
236240
score = db_rec['score'] if 'score' in db_rec else 1
237241
c = db_rec['center']
238-
sf = self.scale_factor
239-
rf = self.rotation_factor
240-
s = np.clip(np.random.randn()*sf + 1, 1 - sf, 1 + sf)
241-
r = np.clip(np.random.randn()*rf, -rf*2, rf*2) \
242-
if random.random() <= 0.6 else 0
243242

244243
#s = 1.4
245244
#r = 45
246245
#self.save_vis_kps(data_numpy,joints,"a.jpg",bbox)
247-
data_numpy,joints,bbox = odk.rotate(r,data_numpy,joints,bbox,s)
248-
joints_vis[:,0] = joints[:,2]
249-
joints_vis[:,1] = joints[:,2]
250-
#self.save_vis_kps(data_numpy,joints,"b.jpg",bbox)
251-
if self.flip and random.random() <= 0.5:
252-
data_numpy,joints,joints_vis,bbox = odk.flip(data_numpy,joints,joints_vis,self.flip_pairs,
246+
if self.is_train:
247+
sf = self.scale_factor
248+
rf = self.rotation_factor
249+
s = np.clip(np.random.randn()*sf + 1, 1 - sf, 1 + sf)
250+
r = np.clip(np.random.randn()*rf, -rf*2, rf*2) \
251+
if random.random() <= 0.6 else 0
252+
data_numpy,joints,bbox = odk.rotate(r,data_numpy,joints,bbox,s)
253+
joints_vis[:,0] = joints[:,2]
254+
joints_vis[:,1] = joints[:,2]
255+
256+
#self.save_vis_kps(data_numpy,joints,"b.jpg",bbox)
257+
if self.flip and random.random() <= 0.5:
258+
data_numpy,joints,joints_vis,bbox = odk.flip(data_numpy,joints,joints_vis,self.flip_pairs,
253259
bbox=bbox)
260+
else:
261+
r = 0.0
254262
#self.save_vis_kps(data_numpy,joints,"b1.jpg",bbox)
255263
#bbox = odk.npget_bbox(joints)
256264
#bbox = odb.npscale_bboxes(bbox,1.4)
265+
org_img = data_numpy
257266
data_numpy,bbox = self.cut_and_resize(data_numpy,[bbox],size=self.image_size)
258267
data_numpy = data_numpy[0]
259268
bbox = bbox[0]
269+
img_a = wmli.sub_imagev2(org_img,bbox.astype(np.int32))
270+
img_a = wmli.resize_img(img_a,self.image_size)
271+
img_b = data_numpy
272+
wmli.imwrite("a.jpg",img_a)
273+
wmli.imwrite("b.jpg",img_b)
274+
275+
c = np.array([(bbox[0]+bbox[2])/2,(bbox[1]+bbox[3])/2],dtype=np.float32)
276+
scale = np.array(
277+
[
278+
(bbox[2]-bbox[0]) * 1.0 / self.pixel_std,
279+
(bbox[3]-bbox[1]) * 1.0 / self.pixel_std
280+
],
281+
dtype=np.float32
282+
)
283+
260284
joints = odk.cut2size(joints,bbox,self.image_size)
261285
#self.save_vis_kps(data_numpy,joints,"c.jpg",bbox)
262286

@@ -276,7 +300,7 @@ def trans_data_type0(self,data_numpy,db_rec):
276300
'joints': joints,
277301
'joints_vis': joints_vis,
278302
'center': c,
279-
'scale': db_rec['scale'],
303+
'scale': scale,
280304
'rotation': r,
281305
'score': score
282306
}

lib/dataset/coco_mpii.py

+75-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from __future__ import absolute_import
88
from __future__ import division
99
from __future__ import print_function
10-
10+
import copy
11+
from datadef import *
1112
from collections import defaultdict
1213
from collections import OrderedDict
1314
import logging
@@ -63,7 +64,7 @@ def __init__(self, cfg, root, image_set, is_train, transform=None):
6364
self.lpset_img_dir = "/home/wj/ai/mldata1/lspet/lspet/images"
6465
self.penn_anno_path = "/home/wj/ai/mldata1/penn_action/Penn_Action/coco_labels"
6566
self.penn_img_dir = "/home/wj/ai/mldata1/penn_action/Penn_Action/frames"
66-
self.crowd_pose_anno_path = '/home/wj/ai/mldata1/crowd_pose/CrowdPose/crowdpose_coco.pt',
67+
self.crowd_pose_anno_path = '/home/wj/ai/mldata1/crowd_pose/CrowdPose/crowdpose_coco.pt'
6768
self.crowd_pose_img_dir = '/home/wj/ai/mldata1/crowd_pose/images'
6869

6970
self.nms_thre = cfg.TEST.NMS_THRE
@@ -140,7 +141,7 @@ def _load_image_set_index(self):
140141
def _get_db(self):
141142
gt_db = []
142143
if self.is_train:
143-
'''if self.is_train or self.use_gt_bbox:
144+
if self.is_train or self.use_gt_bbox:
144145
# use ground truth bbox
145146
gt_db = self._load_coco_keypoint_annotations()
146147
else:
@@ -149,8 +150,10 @@ def _get_db(self):
149150

150151
gt_db.extend(self._load_mpii_keypoint_annotations())
151152
gt_db.extend(self._load_lpset_keypoint_annotations())
152-
gt_db.extend(self._load_penn_action_keypoint_annotations())'''
153+
gt_db.extend(self._load_penn_action_keypoint_annotations())
153154
gt_db.extend(self._load_crowd_pose_keypoint_annotations())
155+
'''if is_debug():
156+
gt_db.extend(self._load_penn_action_keypoint_annotations())'''
154157
else:
155158
if self.is_train or self.use_gt_bbox:
156159
# use ground truth bbox
@@ -212,7 +215,7 @@ def _load_penn_action_keypoint_annotations(self):
212215
with open(file,"rb") as f:
213216
datas = pickle.load(f)
214217
for data in datas:
215-
tmp_db = self._load_mpii_keypoint_annotation_kernal(data,img_dir=self.penn_img_dir)
218+
tmp_db = self._load_penn_action_keypoint_annotation_kernal(data,img_dir=self.penn_img_dir)
216219
gt_db.extend(tmp_db)
217220

218221
print(f"Total load {len(gt_db)} penn action data.")
@@ -319,8 +322,11 @@ def _load_mpii_keypoint_annotation_kernal(self, data,img_dir):
319322
joints_3d_vis = np.zeros((self.num_joints, 3), dtype=np.float)
320323
for ipt in range(self.num_joints):
321324
t_vis = kps[ipt,2]
325+
if t_vis<0.99 and ipt<5:
326+
t_vis = t_vis*0.5
322327
if t_vis > 1:
323328
t_vis = 1
329+
joints_3d[ipt,2] = t_vis
324330
joints_3d_vis[ipt, 0] = t_vis
325331
joints_3d_vis[ipt, 1] = t_vis
326332
joints_3d_vis[ipt, 2] = 0
@@ -350,6 +356,70 @@ def _load_mpii_keypoint_annotation_kernal(self, data,img_dir):
350356

351357
return rec
352358

359+
def _load_penn_action_keypoint_annotation_kernal(self, data,img_dir):
360+
"""
361+
coco ann: [u'segmentation', u'area', u'iscrowd', u'image_id', u'bbox', u'category_id', u'id']
362+
iscrowd:
363+
crowd instances are handled by marking their overlaps with all categories to -1
364+
and later excluded in training
365+
bbox:
366+
[x1, y1, w, h]
367+
:param index: coco image id
368+
:return: db entry
369+
"""
370+
img_name,bboxes,all_kps = data
371+
if bboxes.shape[0]==0:
372+
return []
373+
374+
rec = []
375+
for bbox,kps in zip(bboxes,all_kps):
376+
# ignore objs without keypoints annotation
377+
if np.max(kps[:,:2]) == 0:
378+
continue
379+
380+
joints_3d = kps
381+
joints_3d_vis = np.zeros((self.num_joints, 3), dtype=np.float)
382+
for ipt in range(self.num_joints):
383+
t_vis = kps[ipt,2]
384+
if t_vis<0.99 and ipt<5:
385+
t_vis = t_vis*0.5
386+
if t_vis > 1:
387+
t_vis = 1
388+
joints_3d[ipt,2] = t_vis
389+
joints_3d_vis[ipt, 0] = t_vis
390+
joints_3d_vis[ipt, 1] = t_vis
391+
joints_3d_vis[ipt, 2] = 0
392+
src_idxs = list(range(5,17))
393+
dst_idxs = [6,5,8,7,10,9,12,11,14,13,16,15]
394+
tmp_joints_3d = copy.deepcopy(joints_3d)
395+
tmp_joints_3d_vis = copy.deepcopy(joints_3d_vis)
396+
joints_3d[dst_idxs] = tmp_joints_3d[src_idxs]
397+
joints_3d_vis[dst_idxs] = tmp_joints_3d_vis[src_idxs]
398+
399+
clean_bbox = [bbox[0],bbox[1],bbox[2]-bbox[0],bbox[3]-bbox[1]]
400+
if clean_bbox[2]<2 or clean_bbox[3]<2:
401+
print(f"Skip {img_dir}/{img_name} {bbox}")
402+
continue
403+
total_kps_nr = np.sum((kps[...,2]>0.1).astype(np.int32))
404+
if total_kps_nr<3:
405+
print(f"Skip {img_dir}/{img_name} {total_kps_nr} {kps} {bbox}")
406+
continue
407+
kp_bbox = odk.npget_bbox(joints_3d)
408+
if kp_bbox is not None:
409+
bbox = odb.bbox_of_boxes([bbox,kp_bbox])
410+
center, scale = self._box2cs(clean_bbox)
411+
rec.append({
412+
'image': osp.join(img_dir,img_name),
413+
'center': center,
414+
'scale': scale,
415+
'joints_3d': joints_3d,
416+
'joints_3d_vis': joints_3d_vis,
417+
'clean_bbox':bbox,
418+
'filename': img_name,
419+
'imgnum': 0,
420+
})
421+
422+
return rec
353423
def _box2cs(self, box):
354424
x, y, w, h = box[:4]
355425
return self._xywh2cs(x, y, w, h)

lib/utils/utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ def create_logger(cfg, cfg_name, phase='train'):
3333
model = cfg.MODEL.NAME
3434
cfg_name = os.path.basename(cfg_name).split('.')[0]
3535

36-
final_output_dir = root_output_dir / dataset / model / cfg_name
36+
if phase == 'train':
37+
final_output_dir = root_output_dir / dataset / model / cfg_name
38+
else:
39+
final_output_dir = root_output_dir / dataset / model / (cfg_name+"_"+phase)
3740

3841
print('=> creating {}'.format(final_output_dir))
3942
final_output_dir.mkdir(parents=True, exist_ok=True)

lib/utils/vis.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def save_batch_image_with_joints(batch_image, batch_joints, batch_joints_vis,
5757
cv2.circle(ndarr, (int(joint_0), int(joint_1)), 2, color, 2)
5858
#cv2.putText(ndarr, f"{k}_{i}", (int(joint_0), int(joint_1)), cv2.FONT_HERSHEY_DUPLEX, 0.5, color, 1)
5959
k = k + 1
60-
cv2.imwrite(file_name, ndarr)
60+
cv2.imwrite(file_name, ndarr[...,::-1])
6161

6262

6363
def save_batch_heatmaps(batch_image, batch_heatmaps, file_name,
@@ -122,7 +122,7 @@ def save_batch_heatmaps(batch_image, batch_heatmaps, file_name,
122122

123123
grid_image[height_begin:height_end, 0:heatmap_width, :] = resized_image
124124

125-
cv2.imwrite(file_name, grid_image)
125+
cv2.imwrite(file_name, grid_image[...,::-1])
126126

127127

128128
def save_debug_images(config, input, meta, target, joints_pred, output,

tools/dist_train.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def main():
175175
final_output_dir, 'checkpoint.pth'
176176
)
177177

178-
if cfg.AUTO_RESUME and os.path.exists(checkpoint_file):
178+
if False and cfg.AUTO_RESUME and os.path.exists(checkpoint_file):
179179
logger.info("=> loading checkpoint '{}'".format(checkpoint_file))
180180
checkpoint = torch.load(checkpoint_file)
181181
begin_epoch = checkpoint['epoch']
@@ -192,7 +192,6 @@ def main():
192192
print(f"Load {ckpt_path}.")
193193
model.module.load_state_dict(torch.load(ckpt_path), strict=True)
194194

195-
196195

197196
lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(
198197
optimizer, cfg.TRAIN.LR_STEP, cfg.TRAIN.LR_FACTOR,

0 commit comments

Comments
 (0)