def __init__(self): ImageRoot = './data/coco2017/train2017' AnnoFile = './data/coco2017/annotations/person_keypoints_train2017_pose2seg.json' self.datainfos = CocoDatasetInfo(ImageRoot, AnnoFile, onlyperson=True, loadimg=True)
def __init__(self, batch_size, shuffle, num_workers): super().__init__(batch_size=batch_size, shuffle=shuffle, num_workers=num_workers) ImageRoot = './data/coco2017/train2017' AnnoFile = './data/coco2017/annotations/person_keypoints_train2017_pose2seg.json' self.datainfos = CocoDatasetInfo(ImageRoot, AnnoFile, onlyperson=True, loadimg=True) self.total_len = len(self.datainfos)
def test(model, dataset='cocoVal', logger=print): ImageRoot = './data/coco2017/val2017' AnnoFile = './data/person_keypoints_val2017_pose2seg.json' datainfos = CocoDatasetInfo(ImageRoot, AnnoFile, onlyperson=True, loadimg=True) model.eval() results_segm = [] imgIds = [] for i in tqdm(range(len(datainfos))): rawdata = datainfos[i] img = rawdata['data'] image_id = rawdata['id'] height, width = img.shape[0:2] gt_kpts = np.float32(rawdata['gt_keypoints']).transpose(0, 2, 1) # (N, 17, 3) gt_segms = rawdata['segms'] gt_masks = np.array([annToMask(segm, height, width) for segm in gt_segms]) output = model([img], [gt_kpts], [gt_masks]) for mask in output[0]: maskencode = maskUtils.encode(np.asfortranarray(mask)) maskencode['counts'] = maskencode['counts'].decode('ascii') results_segm.append({ "image_id": image_id, "category_id": 1, "score": 1.0, "segmentation": maskencode }) imgIds.append(image_id) def do_eval_coco(image_ids, coco, results, flag): from pycocotools.cocoeval import COCOeval assert flag in ['bbox', 'segm', 'keypoints'] # Evaluate coco_results = coco.loadRes(results) cocoEval = COCOeval(coco, coco_results, flag) cocoEval.params.imgIds = image_ids cocoEval.params.catIds = [1] cocoEval.evaluate() cocoEval.accumulate() cocoEval.summarize() return cocoEval cocoEval = do_eval_coco(imgIds, datainfos.COCO, results_segm, 'segm') logger('[POSE2SEG] AP|.5|.75| S| M| L| AR|.5|.75| S| M| L|') _str = '[segm_score] %s '%dataset for value in cocoEval.stats.tolist(): _str += '%.3f '%value logger(_str)
cocoEval.accumulate() cocoEval.summarize() return cocoEval cocoEval = do_eval_coco(imgIds, datainfos.COCO, results_segm, 'segm') logger('[POSE2SEG] AP|.5|.75| S| M| L| AR|.5|.75| S| M| L|') _str = '[segm_score] %s '%dataset for value in cocoEval.stats.tolist(): _str += '%.3f '%value logger(_str) if __name__=='__main__': model_path = './pose2seg_release.pkl' print('===========> loading model <===========') model = Pose2Seg().cuda() model.init(model_path) model.eval() ImageRoot = './data/images' AnnoFile = './data/person_keypoints_val2017_pose2seg.json' datainfos = CocoDatasetInfo(ImageRoot, AnnoFile, onlyperson=True, loadimg=True) model.eval() results_segm = [] imgIds = [] for i in tqdm(range(len(datainfos))): rawdata = datainfos[i] g1 = np.float32(rawdata['gt_keypoints']) g2 = np.float32(rawdata['gt_keypoints']).transpose(0, 2, 1)
def cluster(dataset='coco', cat_num=3, vis_threshold=0.4, minpoints=8, save_file='./modeling/templates2.json', visualize=False): # We try `cat_num` from 1 to 6 multiple times. we want to see # what the cluster centers look like when vary the numbers of # group. While the kmean method, which is heavily relay on the # initial status, gives nearly the same cluster centers when # `cat_num` = 3 each time. So we assume the coco dataset accurately # have 3 clusters.(a TODO is to visualize this dataset.) And # the visualization of the cluster centers seems to reasonable: # (1) a full body. (2) a full body without head (3) an upper body. # Note that (2) seems representing the backward of a person. if dataset == 'coco': datainfos = CocoDatasetInfo('./data/coco2017/train2017', './data/coco2017/annotations/person_keypoints_train2017_pose2seg.json', loadimg=False) connections = [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]] names = ["nose", "left_eye", "right_eye", "left_ear", "right_ear", "left_shoulder", "right_shoulder", "left_elbow", "right_elbow", "left_wrist", "right_wrist", "left_hip", "right_hip", "left_knee", "right_knee", "left_ankle", "right_ankle"] flip_map = {'left_eye': 'right_eye', 'left_ear': 'right_ear', 'left_shoulder': 'right_shoulder', 'left_elbow': 'right_elbow', 'left_wrist': 'right_wrist', 'left_hip': 'right_hip', 'left_knee': 'right_knee', 'left_ankle': 'right_ankle'} def flip_keypoints(keypoints, keypoint_flip_map, keypoint_coords, width): """Left/right flip keypoint_coords. keypoints and keypoint_flip_map are accessible from get_keypoints(). """ flipped_kps = keypoint_coords.copy() for lkp, rkp in keypoint_flip_map.items(): lid = keypoints.index(lkp) rid = keypoints.index(rkp) flipped_kps[:, :, lid] = keypoint_coords[:, :, rid] flipped_kps[:, :, rid] = keypoint_coords[:, :, lid] # Flip x coordinates flipped_kps[:, 0, :] = width - flipped_kps[:, 0, :] # Maintain COCO convention that if visibility == 0, then x, y = 0 inds = np.where(flipped_kps[:, 2, :] == 0) flipped_kps[inds[0], 0, inds[1]] = 0 return flipped_kps all_kpts = [] for idx in tqdm(range(len(datainfos))): rawdata = datainfos[idx] gt_boxes = rawdata['boxes'] gt_kpts = rawdata['gt_keypoints'].transpose(0, 2, 1) # (N, 17, 3) gt_ignores = rawdata['is_crowd'] normed_kpts = norm_kpt_by_box(gt_kpts, gt_boxes) normed_kpts_flipped = flip_keypoints(names, flip_map, normed_kpts.transpose(0, 2, 1), 1.0).transpose(0, 2, 1) normed_kpts = np.vstack((normed_kpts, normed_kpts_flipped)) for kpt in normed_kpts: if np.sum(kpt) == 0: continue elif np.sum(kpt[:, 2] > 0) < minpoints: continue else: all_kpts.append(kpt) all_kpts = np.array(all_kpts) print('data to be clustered:', all_kpts.shape) res = cluster_zixi(all_kpts, cat_num) save_dict = {} save_dict['connections'] = connections save_dict['names'] = names save_dict['flip_map'] = flip_map save_dict['vis_threshold'] = vis_threshold save_dict['minpoints'] = minpoints save_dict['templates'] = [item.tolist() for item in res[0]] if save_file is not None: with open(save_file, 'w') as result_file: json.dump(save_dict, result_file) if visualize: for center in res[0]: center = center.reshape(-1, 3) draw_skeleton(center, 200, 200, vis_threshold) print('cluster() done.') return res else: raise NotImplementedError
def test(model, dataset='cocoVal', logger=print): if dataset == 'OCHumanVal': ImageRoot = r'\\fs01\Algo\ML\Datasets\Pose2Seg\data\OCHuman\images' AnnoFile = r'\\fs01\Algo\ML\Datasets\Pose2Seg\data\OCHuman\annotations\ochuman_coco_format_val_range_0.00_1.00.json' elif dataset == 'OCHumanTest': ImageRoot = r'\\fs01\Algo\ML\Datasets\Pose2Seg\data\OCHuman\images' AnnoFile = r'\\fs01\Algo\ML\Datasets\Pose2Seg\data\OCHuman\annotations\ochuman_coco_format_test_range_0.00_1.00.json' elif dataset == 'cocoVal': ImageRoot = r'\\fs01\Algo\ML\Datasets\Pose2Seg\data\coco2017\val2017' AnnoFile = r'\\fs01\Algo\ML\Datasets\Pose2Seg\data\coco2017\annotations\person_keypoints_val2017_pose2seg.json' datainfos = CocoDatasetInfo(ImageRoot, AnnoFile, onlyperson=True, loadimg=True) model.eval() results_segm = [] imgIds = [] for i in tqdm(range(3, len(datainfos))): rawdata = datainfos[i] img = rawdata['data'] image_id = rawdata['id'] height, width = img.shape[0:2] gt_kpts = np.float32(rawdata['gt_keypoints']).transpose( 0, 2, 1) # (N, 17, 3) gt_segms = rawdata['segms'] gt_masks = np.array( [annToMask(segm, height, width) for segm in gt_segms]) output = model([img], [gt_kpts], [gt_masks]) img = img[..., ::-1] # plt.switch_backend("TkAgg") # plt.imshow(gt_masks[0,...]) # plt.show() # fig.savefig('C:\\Users\\erez\\Projects\\Pose2Seg\\demo.png', bbox_inches='tight') MASKS = np.zeros(output[0][0].shape) for id, mask in enumerate(output[0]): from skimage.color import label2rgb MASKS += (id + 1) * mask maskencode = maskUtils.encode(np.asfortranarray(mask)) maskencode['counts'] = maskencode['counts'].decode('ascii') results_segm.append({ "image_id": image_id, "category_id": 1, "score": 1.0, "segmentation": maskencode }) imgIds.append(image_id) image_label_overlay = label2rgb(MASKS, image=img, alpha=0.3, bg_label=0) plt.imshow(image_label_overlay) plt.show() def do_eval_coco(image_ids, coco, results, flag): from pycocotools.cocoeval import COCOeval assert flag in ['bbox', 'segm', 'keypoints'] # Evaluate coco_results = coco.loadRes(results) cocoEval = COCOeval(coco, coco_results, flag) cocoEval.params.imgIds = image_ids cocoEval.params.catIds = [1] cocoEval.evaluate() cocoEval.accumulate() cocoEval.summarize() return cocoEval cocoEval = do_eval_coco(imgIds, datainfos.COCO, results_segm, 'segm') logger('[POSE2SEG] AP|.5|.75| S| M| L| AR|.5|.75| S| M| L|') _str = '[segm_score] %s ' % dataset for value in cocoEval.stats.tolist(): _str += '%.3f ' % value logger(_str)
def __init__(self): ImageRoot = r'\\fs01\Algo\ML\Datasets\Pose2Seg\data\coco2017\train2017' AnnoFile = r'\\fs01\Algo\ML\Datasets\Pose2Seg\data\coco2017\annotations\person_keypoints_train2017_pose2seg.json' self.datainfos = CocoDatasetInfo(ImageRoot, AnnoFile, onlyperson=True, loadimg=True)