def fromFile(filename, encoding='utf-8'): """Load data from a pickle or JSON file. Parameters ---------- encoding : str The encoding to use when reading a JSON file. This parameter will be ignored for any other file type. """ filename = pathToString(filename) if filename.endswith('.psydat'): with open(filename, 'rb') as f: contents = pickle.load(f) # if loading an experiment file make sure we don't save further # copies using __del__ if hasattr(contents, 'abort'): contents.abort() elif filename.endswith('.json'): with codecs.open(filename, 'r', encoding=encoding) as f: contents = json_tricks.load(f) # Restore RNG if we load a TrialHandler2 object. # We also need to remove the 'temporary' ._rng_state attribute that # was saved with it. from psychopy.data import TrialHandler2 if isinstance(contents, TrialHandler2): contents._rng = np.random.RandomState(seed=contents.seed) contents._rng.set_state(contents._rng_state) del contents._rng_state else: msg = "Don't know how to handle this file type, aborting." raise ValueError(msg) return contents
def _loadAll(self): """Fetches the calibrations for this monitor from disk, storing them as self.calibs """ if constants.PY3: ext = ".json" else: ext = ".calib" # the name of the actual file: thisFileName = os.path.join(monitorFolder, self.name + ext) if not os.path.exists(thisFileName): self.calibNames = [] else: if ext==".json": with open(thisFileName, 'r') as thisFile: self.calibs = json_tricks.load(thisFile, ignore_comments=False, encoding='utf-8', preserve_order=False) else: with open(thisFileName, 'rb') as thisFile: self.calibs = pickle.load(thisFile) self.calibNames = sorted(self.calibs) if not constants.PY3: # saving for future (not needed if we are IN future!) # save JSON copies of our calibrations self._saveJSON()
def fromFile(filename): """Load data from a pickle or JSON file. """ if filename.endswith('.psydat'): with open(filename, 'rb') as f: contents = pickle.load(f) # if loading an experiment file make sure we don't save further # copies using __del__ if hasattr(contents, 'abort'): contents.abort() elif filename.endswith('.json'): with open(filename, 'r') as f: contents = json_tricks.load(f) # Restore RNG if we load a TrialHandler2 object. # We also need to remove the 'temporary' ._rng_state attribute that # was saved with it. from psychopy.data import TrialHandler2 if isinstance(contents, TrialHandler2): contents._rng = np.random.RandomState(seed=contents.seed) contents._rng.set_state(contents._rng_state) del contents._rng_state else: msg = "Don't know how to handle this file type, aborting." raise ValueError(msg) return contents
def read_json(file_path): """ Read in a json file and return a dictionary representation """ try: with open(file_path, 'r') as f: config = json_tricks.load(f) except ValueError: print(' '+'!'*58) print(' Woops! Looks the JSON syntax is not valid in:') print(' {}'.format(file_path)) print(' Note: commonly this is a result of having a trailing comma \n in the file') print(' '+'!'*58) raise return config
def _load_coco_person_detection_results(self): """Load coco person detection results.""" num_joints = self.ann_info['num_joints'] all_boxes = None with open(self.bbox_file, 'r') as f: all_boxes = json.load(f) if not all_boxes: raise ValueError('=> Load %s fail!' % self.bbox_file) print(f'=> Total boxes: {len(all_boxes)}') kpt_db = [] num_boxes = 0 for n_img in range(0, len(all_boxes)): det_res = all_boxes[n_img] if det_res['category_id'] != 1: continue img_name = self._image_path_from_index(det_res['image_id']) box = det_res['bbox'] score = det_res['score'] if score < self.image_thr: continue num_boxes = num_boxes + 1 center, scale = self._box2cs(box) joints_3d = np.zeros((num_joints, 3), dtype=np.float) joints_3d_visible = np.ones((num_joints, 3), dtype=np.float) kpt_db.append({ 'image_file': img_name, 'center': center, 'scale': scale, 'bbox_score': score, 'dataset': 'coco', 'rotation': 0, 'imgnum': 0, 'joints_3d': joints_3d, 'joints_3d_visible': joints_3d_visible }) print(f'=> Total boxes after fliter ' f'low score@{self.image_thr}: {num_boxes}') return kpt_db
def _load_coco_person_detection_results(self): """Load coco person detection results.""" num_joints = self.ann_info['num_joints'] all_boxes = None with open(self.bbox_file, 'r') as f: all_boxes = json.load(f) if not all_boxes: raise ValueError('=> Load %s fail!' % self.bbox_file) print(f'=> Total boxes: {len(all_boxes)}') kpt_db = [] bbox_id = 0 for det_res in all_boxes: if det_res['category_id'] != 1: continue # print(self.img_prefix) # print(self.id2name) image_file = os.path.join(self.img_prefix, det_res['image_id']) box = det_res['bbox'] score = det_res['score'] if score < self.det_bbox_thr: continue center, scale = self._xywh2cs(*box[:4]) joints_3d = np.zeros((num_joints, 3), dtype=np.float32) joints_3d_visible = np.ones((num_joints, 3), dtype=np.float32) kpt_db.append({ 'image_file': image_file, 'center': center, 'scale': scale, 'rotation': 0, 'bbox': box[:4], 'bbox_score': score, 'dataset': self.dataset_name, 'joints_3d': joints_3d, 'joints_3d_visible': joints_3d_visible, 'bbox_id': bbox_id }) bbox_id = bbox_id + 1 print(f'=> Total boxes after filter ' f'low score@{self.det_bbox_thr}: {bbox_id}') return kpt_db
def _load_posetrack_person_detection_results(self): all_boxes = None with open(self.bbox_file, 'r') as f: all_boxes = json.load(f) if not all_boxes: logger.error('=> Load %s fail!' % self.bbox_file) return None logger.info('=> Total boxes: {}'.format(len(all_boxes))) kpt_db = [] num_boxes = 0 for n_img in range(0, len(all_boxes)): det_res = all_boxes[n_img] if det_res['category_id'] != 1: continue img_name = det_res['image_name'] box = det_res['bbox'] score = det_res['score'] nframes = det_res['nframes'] frame_id = det_res['frame_id'] if score < self.image_thre: continue num_boxes = num_boxes + 1 center, scale = self._box2cs(box) joints_3d = np.zeros((self.num_joints, 3), dtype=np.float) joints_3d_vis = np.ones((self.num_joints, 3), dtype=np.float) kpt_db.append({ 'image': self.img_dir + img_name, 'center': center, 'scale': scale, 'score': score, 'joints_3d': joints_3d, 'joints_3d_vis': joints_3d_vis, 'nframes': nframes, 'frame_id': frame_id, }) logger.info('=> Total boxes after fliter low score@{}: {}'.format( self.image_thre, num_boxes)) return kpt_db
def _get_cam(self, seq): cam_file = osp.join(self.dataset_root, seq, 'calibration_{:s}.json'.format(seq)) with open(cam_file) as cfile: calib = json.load(cfile) M = np.array([[1.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]]) cameras = {} for cam in calib['cameras']: if (cam['panel'], cam['node']) in self.cam_list: sel_cam = {} sel_cam['K'] = np.array(cam['K']) sel_cam['distCoef'] = np.array(cam['distCoef']) sel_cam['R'] = np.array(cam['R']).dot(M) sel_cam['t'] = np.array(cam['t']).reshape((3, 1)) cameras[(cam['panel'], cam['node'])] = sel_cam return cameras
def main(mdfn, existing=None, outfn=None): units = extract_units(mdfn) if existing: with open(existing) as f: # Can't count on having json_tricks. used = json.load(f) units = {k: v for k, v in units.items() if k in used} if not outfn: outfn = existing.replace('.json', '_units.json') outtext = json.dumps(units, indent=2, sort_keys=True, separators=(', ', ': ')) if outfn: with open(outfn, 'w') as outf: outf.write(outtext + "\n") else: print(outtext) return units
def load(string: Optional[str] = None, *, fp: Optional[Any] = None, ignore_comments: bool = True, **json_tricks_kwargs) -> Any: """ Load the string or from file, and convert it to a complex data structure. At least one of string or fp has to be not none. Parameters ---------- string : str JSON string to parse. Can be set to none if fp is used. fp : str File path to load JSON from. Can be set to none if string is used. ignore_comments : bool Remove comments (starting with ``#`` or ``//``). Default is true. Returns ------- any The loaded object. """ assert string is not None or fp is not None # see encoders for explanation hooks = [ json_tricks.pathlib_hook, json_tricks.pandas_hook, json_tricks.json_numpy_obj_hook, json_tricks.decoders.EnumInstanceHook(), json_tricks.json_date_time_hook, json_tricks.json_complex_hook, json_tricks.json_set_hook, json_tricks.numeric_types_hook, _json_tricks_serializable_object_decode, _json_tricks_func_or_cls_decode, _json_tricks_any_object_decode ] # to bypass a deprecation warning in json-tricks json_tricks_kwargs['ignore_comments'] = ignore_comments if string is not None: if isinstance(string, IOBase): raise TypeError(f'Expect a string, found a {string}. If you intend to use a file, use `nni.load(fp=file)`') return json_tricks.loads(string, obj_pairs_hooks=hooks, **json_tricks_kwargs) else: return json_tricks.load(fp, obj_pairs_hooks=hooks, **json_tricks_kwargs)
def test_hand_gesture_demo(): # build the pose model from a config file and a checkpoint file pose_model = init_pose_model( 'configs/hand/gesture_sview_rgbd_vid/mtut/nvgesture/' 'i3d_nvgesture_bbox_112x112_fps15.py', None, device='cpu') dataset_info = pose_model.cfg.data['test'].get('dataset_info', None) video_files = [ 'tests/data/nvgesture/sk_color.avi', 'tests/data/nvgesture/sk_depth.avi' ] with open('tests/data/nvgesture/bboxes.json', 'r') as f: bbox = next(iter(json.load(f).values())) pred_label, _ = inference_gesture_model(pose_model, video_files, bbox, dataset_info)
def _report_metric(self, res_file, metrics): """Keypoint evaluation. Report PCK, AUC or EPE. """ info_str = [] with open(res_file, 'r') as fin: preds = json.load(fin) assert len(preds) == len(self.db) outputs = [] gts = [] for pred, item in zip(preds, self.db): outputs.append(pred['keypoints']) gts.append(item['joints_3d']) outputs = np.array(outputs)[:, :, :-1] gts = np.array(gts)[:, :, :-1] if 'PCK' in metrics: hit = 0 exist = 0 for pred, item in zip(preds, self.db): bbox = np.array(item['bbox']) threshold = np.max(bbox[2:]) * 0.2 h, _, e = keypoint_pck_accuracy( np.array(pred['keypoints'])[None, :, :-1], np.array(item['joints_3d'])[None, :, :-1], 1, np.array([[threshold, threshold]])) hit += len(h[h > 0]) exist += e pck = hit / exist info_str.append(('PCK', pck)) if 'AUC' in metrics: info_str.append(('AUC', keypoint_auc(outputs, gts, 30))) if 'EPE' in metrics: info_str.append(('EPE', keypoint_epe(outputs, gts))) return info_str
def _report_metric(self, res_file, metrics, pck_thr=0.3): """Keypoint evaluation. Args: res_file (str): Json file stored prediction results. metrics (str | list[str]): Metric to be performed. Options: 'PCK', 'NME'. pck_thr (float): PCK threshold, default: 0.3. Returns: dict: Evaluation results for evaluation metric. """ info_str = [] with open(res_file, 'r') as fin: preds = json.load(fin) assert len(preds) == len(self.db) outputs = [] gts = [] masks = [] for pred, item in zip(preds, self.db): outputs.append(np.array(pred['keypoints'])[:, :-1]) gts.append(np.array(item['joints_3d'])[:, :-1]) masks.append((np.array(item['joints_3d_visible'])[:, 0]) > 0) outputs = np.array(outputs) gts = np.array(gts) masks = np.array(masks) normalize_factor = self._get_normalize_factor(gts) if 'PCK' in metrics: _, pck, _ = keypoint_pck_accuracy(outputs, gts, masks, pck_thr, normalize_factor) info_str.append(('PCK', pck)) if 'NME' in metrics: info_str.append( ('NME', keypoint_nme(outputs, gts, masks, normalize_factor))) return info_str
def gen_temporal_dir(self): self.seqs.sort(key=lambda x: (int(x[:2]), x[3:4], x[5:6])) for seq in self.seqs: if seq == 'annot': continue image_path = os.path.join(self.data_dir, seq) imgs = os.listdir(image_path + '/images/') imgs.sort(key=lambda x: (int(x[x.index('_', 6) + 1:x.index('.')]))) img_num = len(imgs) label_path = os.path.join(self.label_dir, seq) lables = json.load(open(label_path + '/annot/' + seq + '.json')) for i in range(img_num): self.temporal_dir.append( os.path.join(image_path + '/images', imgs[i])) self.labels[self.size] = lables[str(i)] self.size += 1 self.show() print('total numbers of image sequence is ' + str(len(self.temporal_dir)))
def load_from_json(self, fname): # load the model import_data = json_tricks.load(open(fname)) import_clf = ModifiedNB() import_clf.class_count_ = import_data['class_count_'] import_clf.class_log_prior_ = import_data['class_log_prior_'] import_clf.classes_ = import_data['classes_'] import_clf.feature_count_ = import_data['feature_count_'] import_clf.feature_log_prob_ = import_data['feature_log_prob_'] self.clf = import_clf # load the fps dict vectoriser v_fps = DictVectorizer() dv = import_data['fps_vectoriser'] v_fps.vocabulary_ = {int(k): v for k, v in dv['vocabulary_'].items()} v_fps.feature_names_ = dv['feature_names_'] self.v_fps = v_fps # load the continous variables binariser try: binariser = import_data['binariser'] kbd = KBinsDiscretizer(n_bins=10, encode='onehot', strategy='quantile') kbd.n_bins = binariser['n_bins'] kbd.n_bins_ = binariser['n_bins_'] kbd.bin_edges_ = np.asarray( [np.asarray(x) for x in binariser['bin_edges_']]) encoder = OneHotEncoder() encoder.categories = binariser['categories'] encoder._legacy_mode = False kbd._encoder = encoder self.kbd = kbd except Exception as e: pass # extra parameters self.trained = True self.con_desc_list = import_data['con_desc_list'] self.fp_type = import_data['fp_type'] self.fp_radius = import_data['fp_radius'] self.informative_cvb = import_data['informative_cvb']
def _load_coco_person_detection_results(self): all_boxes = None with open(self.bbox_file, 'r') as f: all_boxes = json.load(f) if not all_boxes: logger.error('=> Load %s fail!' % self.bbox_file) return None logger.info('=> Total boxes: {}'.format(len(all_boxes))) kpt_db = [] num_boxes = 0 for n_img in range(0, len(all_boxes)): det_res = all_boxes[n_img] if det_res['category_id'] != 1: continue img_name = self.image_path_from_index(det_res['image_id']) box = det_res['bbox'] score = det_res['score'] if score < self.image_thre: continue num_boxes = num_boxes + 1 center, scale = self._box2cs(box) print(f"Center:{center}, Scale: {scale}, Box: {box}") joints_3d = np.zeros((self.num_joints, 3), dtype=np.float) joints_3d_vis = np.ones((self.num_joints, 3), dtype=np.float) kpt_db.append({ 'image': img_name, 'center': center, 'scale': scale, 'score': score, 'joints_3d': joints_3d, 'joints_3d_vis': joints_3d_vis, }) logger.info('=> Total boxes after fliter low score@{}: {}'.format( self.image_thre, num_boxes)) return kpt_db
def _get_db(self): file_name = os.path.join(self.root, 'mpii', 'annot', self.subset + '.json') with open(file_name) as anno_file: anno = json.load(anno_file) gt_db = [] for a in anno: image_name = a['image'] c = np.array(a['center'], dtype=np.float) s = np.array([a['scale'], a['scale']], dtype=np.float) # Adjust center/scale slightly to avoid cropping limbs if c[0] != -1: c[1] = c[1] + 15 * s[1] s = s * 1.25 # MPII uses matlab format, index is based 1, # we should first convert to 0-based index c = c - 1 joints_vis = np.zeros((16, 3), dtype=np.float) if self.subset != 'test': joints = np.array(a['joints']) joints[:, 0:2] = joints[:, 0:2] - 1 vis = np.array(a['joints_vis']) joints_vis[:, 0] = vis[:] joints_vis[:, 1] = vis[:] gt_db.append({ 'image': image_name, 'center': c, 'scale': s, 'joints_2d': joints, 'joints_3d': np.zeros((16, 3)), 'joints_vis': joints_vis, 'source': 'mpii' }) return gt_db
def __init__(self, root, dataset, data_format, num_joints, get_rescore_data, transform=None, target_transform=None, bbox_file=None): from pycocotools.coco import COCO self.root = root self.dataset = dataset self.data_format = data_format self.coco = COCO(self._get_anno_file_name()) self.ids = list(self.coco.imgs.keys()) self.transform = transform self.target_transform = target_transform self.num_joints = num_joints self.bbox_preds = None self.get_rescore_data = get_rescore_data if bbox_file is not None: # 是否在结果评估中引入bbox的信息 with open(bbox_file) as f: data = json.load(f) coco_dt = self.coco.loadRes(data) coco_eval = COCOeval(self.coco, coco_dt, 'keypoints') coco_eval._prepare() self.bbox_preds = coco_eval._dts # 读取COCO Dataset里面的所有类别并为接下来的分类做准备 cats = [ cat['name'] for cat in self.coco.loadCats(self.coco.getCatIds()) ] self.classes = ['__background__'] + cats logger.info('=> classes: {}'.format(self.classes)) self.num_classes = len(self.classes) self._class_to_ind = dict(zip(self.classes, range(self.num_classes))) self._class_to_coco_ind = dict(zip(cats, self.coco.getCatIds())) self._coco_ind_to_class_ind = dict([(self._class_to_coco_ind[cls], self._class_to_ind[cls]) for cls in self.classes[1:]])
def _load_coco_person_detection_results(self): all_boxes = None with open(self.bbox_file, "r") as f: all_boxes = json.load(f) if not all_boxes: logger.error("=> Load %s fail!" % self.bbox_file) return None logger.info("=> Total boxes: {}".format(len(all_boxes))) kpt_db = [] num_boxes = 0 for n_img in range(0, len(all_boxes)): det_res = all_boxes[n_img] if det_res["category_id"] != 1: continue img_name = self.image_path_from_index(det_res["image_id"]) box = det_res["bbox"] score = det_res["score"] if score < self.image_thre: continue num_boxes = num_boxes + 1 center, scale = self._box2cs(box) joints_3d = np.zeros((self.num_joints, 3), dtype=np.float) joints_3d_vis = np.ones((self.num_joints, 3), dtype=np.float) kpt_db.append({ "image": img_name, "center": center, "scale": scale, "score": score, "joints_3d": joints_3d, "joints_3d_vis": joints_3d_vis, }) logger.info("=> Total boxes after fliter low score@{}: {}".format( self.image_thre, num_boxes)) return kpt_db
def _report_metric(self, res_file): """Keypoint evaluation. Report mean per joint position error (MPJPE) and mean per joint position error after rigid alignment (MPJPE-PA) """ with open(res_file, 'r') as fin: preds = json.load(fin) assert len(preds) == len(self.db) pred_joints_3d = [pred['keypoints'] for pred in preds] gt_joints_3d = [item['joints_3d'] for item in self.db] gt_joints_visible = [item['joints_3d_visible'] for item in self.db] pred_joints_3d = np.array(pred_joints_3d) gt_joints_3d = np.array(gt_joints_3d) gt_joints_visible = np.array(gt_joints_visible) # we only evaluate on 14 lsp joints joint_mapper = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 18] pred_joints_3d = pred_joints_3d[:, joint_mapper, :] pred_pelvis = (pred_joints_3d[:, 2] + pred_joints_3d[:, 3]) / 2 pred_joints_3d = pred_joints_3d - pred_pelvis[:, None, :] gt_joints_3d = gt_joints_3d[:, joint_mapper, :] gt_pelvis = (gt_joints_3d[:, 2] + gt_joints_3d[:, 3]) / 2 gt_joints_3d = gt_joints_3d - gt_pelvis[:, None, :] gt_joints_visible = gt_joints_visible[:, joint_mapper, 0] > 0 mpjpe = keypoint_mpjpe(pred_joints_3d, gt_joints_3d, gt_joints_visible) mpjpe_pa = keypoint_mpjpe( pred_joints_3d, gt_joints_3d, gt_joints_visible, alignment='procrustes') info_str = [] info_str.append(('MPJPE', mpjpe * 1000)) info_str.append(('MPJPE-PA', mpjpe_pa * 1000)) return info_str
def init_pending_tasks(self) -> List[Task]: origin_model = torch.load(self._origin_model_path) origin_masks = torch.load(self._origin_masks_path) with open(self._origin_config_list_path, "r") as f: origin_config_list = json_tricks.load(f) self.T = [] self.action = None self.observation = None self.warmup_episode = self.ddpg_params['warmup'] if 'warmup' in self.ddpg_params.keys() else int(self.total_episode / 4) config_list_copy = config_list_canonical(origin_model, origin_config_list) total_sparsity = config_list_copy[0]['total_sparsity'] max_sparsity_per_layer = config_list_copy[0].get('max_sparsity_per_layer', 1.) self.env = AMCEnv(origin_model, origin_config_list, self.dummy_input, total_sparsity, max_sparsity_per_layer, self.target) self.agent = DDPG(len(self.env.state_feature), 1, self.ddpg_params) self.agent.is_training = True task_result = TaskResult('origin', origin_model, origin_masks, origin_masks, None) return self.generate_tasks(task_result)
def get_best_result( self ) -> Optional[Tuple[int, Module, Dict[str, Dict[str, Tensor]], float, List[Dict]]]: """ Returns ------- Optional[Tuple[int, Module, Dict[str, Dict[str, Tensor]], float, List[Dict]]] If self._best_task_id is not None, return best task id, best compact model, masks on the compact model, score, config list used in this task. """ if self._best_task_id is not None: compact_model = torch.load( Path(self._log_dir_root, 'best_result', 'model.pth')) compact_model_masks = torch.load( Path(self._log_dir_root, 'best_result', 'masks.pth')) with Path(self._log_dir_root, 'best_result', 'config_list.json').open('r') as f: config_list = json_tricks.load(f) return self._best_task_id, compact_model, compact_model_masks, self._best_score, config_list return None
def visualize_variation(trial_count): with open(os.path.join(RESULTS_DIR, 'variation-results.json'), 'r') as f: results = json.load(f) print(results) variation_list = np.linspace(0, 5, 11) data = [] for i, variation in enumerate(variation_list): data.append([ result[0]['Acc. summary'][2] for result in results[trial_count * i:trial_count * (i + 1)] ]) plt.hold(True) plt.errorbar(variation_list, np.mean(data, axis=1), np.std(data, axis=1), fmt='r') data = [] for i, variation in enumerate(variation_list): data.append([ result[1]['Acc. summary'][2] for result in results[trial_count * i:trial_count * (i + 1)] ]) plt.errorbar(variation_list, np.mean(data, axis=1), np.std(data, axis=1), fmt='b') plt.legend(['Training', 'Testing']) plt.ylim((0.8, 1.0)) plt.ylabel('Accuracy') plt.xlabel('Variation factor') plt.show()
def load_parameters(self, filename): """ Loads the training parameters from a JSON file. Training parameters are: * weights of the neural networks (classifier and regressor) * names of the input and target fields Args: filename: The name of file from where to load the training parameters. Returns: None """ with open(filename, 'r') as infile: parameter = json_tricks.load(infile) estimator = parameter.pop("estimator", None) if estimator is None: raise ValueError("Found no coefficients for estimator!") self.estimator = self._create_model( MLPRegressor, estimator["params"], estimator["coefs"], ) # self._set_sklearn_coefs(self.estimator, estimator_coefs) scaler = parameter.pop("scaler", None) if scaler is None: raise ValueError("Found no coefficients for scaler!") # The data must be scaled before passed to the regressor. Always # the same scaling must be applied. self.scaler = self._create_model( MinMaxScaler, scaler["params"], scaler["coefs"], ) self.parameter.update(parameter)
def load_json_to_output(json_name, prefix='', batch_size=2, use_bbox_id=True): data = json.load(open(json_name, 'r')) outputs = [] num_data = len(data['images']) for i in range(0, num_data, batch_size): keypoints = np.stack([ np.array(data['annotations'][j]['keypoints'], dtype=np.float32).reshape((-1, 3)) for j in range(i, min(i + batch_size, num_data)) ]) box = np.zeros((batch_size, 6), dtype=np.float32) img_path = [ os.path.join(prefix, data['images'][j]['file_name']) for j in range(i, min(i + batch_size, num_data)) ] bbox_ids = [j for j in range(i, min(i + batch_size, num_data))] if use_bbox_id: output = (keypoints, box, img_path, None, bbox_ids) else: output = (keypoints, box, img_path, None) outputs.append(output) return outputs
def _report_metric(self, res_file, metrics): """Keypoint evaluation. Args: res_file (str): Json file stored prediction results. metrics (str | list[str]): Metric to be performed. Options: 'NME'. Returns: dict: Evaluation results for evaluation metric. """ info_str = [] with open(res_file, 'r') as fin: preds = json.load(fin) assert len(preds) == len(self.db) outputs = [] gts = [] masks = [] box_sizes = [] for pred, item in zip(preds, self.db): outputs.append(np.array(pred['keypoints'])[:, :-1]) gts.append(np.array(item['joints_3d'])[:, :-1]) masks.append((np.array(item['joints_3d_visible'])[:, 0]) > 0) box_sizes.append(item['box_size']) outputs = np.array(outputs) gts = np.array(gts) masks = np.array(masks) box_sizes = np.array(box_sizes).reshape([-1, 1]) if 'NME' in metrics: normalize_factor = self._get_normalize_factor(box_sizes) info_str.append( ('NME', keypoint_nme(outputs, gts, masks, normalize_factor))) return info_str
def __init__(self, cfg, root, image_set, is_train, transform=None): super().__init__(cfg, root, image_set, is_train, transform) self.num_joints = 15 self.dataset_joints = np.array([[ 'back', 'beak', 'belly', 'breast', 'crown', 'forehead', 'left eye', 'left leg', 'left wing', 'nape', 'right eye', 'right leg', 'right wing', 'tail', 'throat' ]]) self.flip_pairs = [[6, 10], [7, 11], [8, 12]] self.db = self._get_db() if is_train and cfg.DATASET.SELECT_DATA: self.db = self.select_data(self.db) file_name = os.path.join(self.root, 'annot', self.image_set + '.json') with open(file_name) as file: gt_file = json.load(file) self.jnt_visible = [] self.pos_gt_src = [] self.bbox_size = [] for elem in gt_file: self.jnt_visible.append(elem['joints_vis']) self.pos_gt_src.append(elem['joints']) self.bbox_size.append(elem['bbox_max_side']) self.jnt_visible = np.array(self.jnt_visible) self.jnt_visible = self.jnt_visible.swapaxes(0, 1) self.pos_gt_src = np.array(self.pos_gt_src) self.pos_gt_src = self.pos_gt_src.transpose((1, 2, 0)) self.bbox_size = np.array(self.bbox_size) logger.info('=> load {} samples'.format(len(self.db)))
def _get_db(self): """Load dataset.""" db = [] with open(self.ann_file, 'r') as f: samples = f.readlines() use_bbox = bool(self.bbox_file) if use_bbox: with open(self.bbox_file, 'r') as f: bboxes = json.load(f) for sample in samples: sample = sample.strip().split() sample = { item.split(':', 1)[0]: item.split(':', 1)[1] for item in sample } path = sample['path'][2:] for key in ('depth', 'color'): fname, start, end = sample[key].split(':') sample[key] = { 'path': os.path.join(path, fname + '.avi'), 'valid_frames': (eval(start), eval(end)) } sample['flow'] = { 'path': sample['color']['path'].replace('color', 'flow'), 'valid_frames': sample['color']['valid_frames'] } sample['rgb'] = sample['color'] sample['label'] = eval(sample['label']) - 1 if use_bbox: sample['bbox'] = bboxes[path] del sample['path'], sample['duo_left'], sample['color'] db.append(sample) return db
def load_exp_info(): """ Load experimental metadata from last run from disk. Returns ------- dict or False Returns the experimental metadata, or `False` if the input file was not found. """ base_dir = os.path.join(os.path.split(__file__)[0]) exp_info_file = os.path.join(base_dir, '.metadata.json') try: with open(exp_info_file, 'r') as f: exp_info = json_tricks.load(f) exp_info['Date'] = data.getDateStr(format='%Y-%m-%d_%H%M') except FileNotFoundError: exp_info = False return exp_info
def _load_coco_person_detection_results(self): try: with open(self.bbox_file, 'r') as f: all_boxes = json.load(f) except IOError: logger.error('=> Load %s fail!' % self.bbox_file) return None logger.info('=> Total boxes: {}'.format(len(all_boxes))) kpt_db = [] num_boxes = 0 for det_res in all_boxes: if det_res['category_id'] != 1: continue info = self.coco.loadImgs(det_res['image_id'])[0] img_name = info['coco_url'] box = det_res['bbox'] score = det_res['score'] if score < self.image_thre: continue num_boxes = num_boxes + 1 center, scale = self._box2cs(box) joints_3d = np.zeros((self.num_joints, 3), dtype=np.float) joints_3d_vis = np.ones((self.num_joints, 3), dtype=np.float) kpt_db.append({ 'image': img_name, 'center': center, 'scale': scale, 'score': score, 'joints_3d': joints_3d, 'joints_3d_vis': joints_3d_vis, }) logger.info('=> Total boxes after filtering low score@{}: {}'.format( self.image_thre, num_boxes)) return kpt_db
def main(fp): ''' takes a filepath to print out the parameters in it Args: fp: FilePath of the pickle or json file with params Returns: None, prints parameters in the shell ''' try: from helper import depickler import json_tricks as json #get the extension ext=fp.split('.')[-1] #depickle it if in pickle if ext=='pickle': a = depickler(fp) try: parameters = a['parameters'] except KeyError: print "key error" parameters = a['metadata'] #json load if json elif ext=='json': with open(fp) as j: parameters=json.load(j)['parameters'] #print it neatly using json dumps in a sorted fashion with nice indents print json.dumps(parameters, indent=4, sort_keys=True) except IOError as e: print 'no such file', e
def gen_temporal_dir(self): self.seqs.sort(key=lambda x: (int(x[:2]), x[3:4], x[5:6])) for seq in self.seqs: if seq == 'annot': continue image_path = os.path.join(self.data_dir, seq) imgs = os.listdir(image_path + '/images/') imgs.sort(key=lambda x: (int(x[x.index('_', 6) + 1:x.index('.')]))) img_num = len(imgs) label_path = os.path.join(self.label_dir, seq) lables = json.load(open(label_path + '/annot/' + seq + '.json')) # if self.train: # for i in range(img_num-self.temporal): # tmp1 = [] # tmp2 = [] # for k in range(i,self.temporal+i): # tmp1.append(os.path.join(os.path.join(image_path + '/images', imgs[k]))) # tmp2.append(lables[str(k)]) # self.temporal_dir.append(tmp1) # self.labels.append(tmp2) # else: for i in range(img_num // self.temporal): tmp1 = [] tmp2 = [] for k in range(self.temporal * i, self.temporal * (i + 1)): tmp1.append( os.path.join( os.path.join(image_path + '/images', imgs[k]))) tmp2.append(lables[str(k)]) self.temporal_dir.append(tmp1) self.labels.append(tmp2) self.joint_true = self.labels # self.show() print(len(self.temporal_dir)) print('total numbers of image sequence is ' + str(len(self.temporal_dir)))
def _report_metric(self, res_file): """Keypoint evaluation. Report Mean Acc of skeleton, contour and all joints. """ num_joints = self.ann_info['num_joints'] hit = np.zeros(num_joints, dtype=np.float32) exist = np.zeros(num_joints, dtype=np.float32) with open(res_file, 'r') as fin: preds = json.load(fin) assert len(preds) == len(self.db) for pred, item in zip(preds, self.db): h, e = self._evaluate_kernel(pred['keypoints'], item['joints_3d'], item['joints_3d_visible'], item['bbox']) hit += h exist += e pck = np.sum(hit) / np.sum(exist) info_str = [] info_str.append(('PCK', pck.item())) return info_str
def _read_tracking_results(self, source, discard=None): """Read the tracking results from the JSON file into an internal DataFrame. Args: source (Cockpit or str): ``Cockpit`` instance, or string containing the path to a .json log produced with ``Cockpit.write``, where information will be fetched from. discard (int, optional): Global step after which information should be discarded. Raises: ValueError: If `source` is neither a ``Cockpit```instance or string. """ if isinstance(source, Cockpit): data = source.get_output() elif isinstance(source, str): with open(source + ".json") as f: # defaultdict to be consistent with fetching from Cockpit data = defaultdict(dict, json_tricks.load(f)) else: raise ValueError( f"Source must be Cockpit or path to .json. Got {source}") # Read data into a DataFrame self.tracking_data = pd.DataFrame.from_dict(data, orient="index") # Change data type of index to numeric self.tracking_data.index = pd.to_numeric(self.tracking_data.index) # Sort by this index self.tracking_data = self.tracking_data.sort_index() # Rename index to 'iteration' and store it in seperate column self.tracking_data = self.tracking_data.rename_axis( "iteration").reset_index() if discard is not None: self.tracking_data = self.tracking_data[ self.tracking_data.index <= discard]
# In[11]: from json_tricks import dump, dumps, load, loads, strip_comments # In[12]: ff = '/mnt/c/Users/sleblanc/Research/ORACLES/aero_file_v2.txt' # In[13]: n = load(ff) # In[14]: n.keys() # In[15]: n['asy'].shape # In[13]:
class Container(): def __init__(self): return container = Container() ## IN_FILENAME_PICKLED = "parameters_and_constants.json" IN_FILENAME_INSILICO_EXPECTED = "kd-matrix-expected.csv" IN_FILENAME_INSILICO_TEMPLATE = "input_traces.csv" OUT_FILENAME_RESULTS_CSV = "summary_performance.csv" ## with open(IN_FILENAME_PICKLED, "rb") as f: constants_and_values = pickler.load(f) ## WORKAROUND for OrderedDict import of json_tricks if pickler.__name__ == "json_tricks": constants_and_values = dict(constants_and_values) import footprint footprint = footprint.Footprinter() _accepted_offset = constants_and_values["accepted_offset"] _factor_method = constants_and_values["factor_method"] _weight_smaller = constants_and_values["weight_smaller"] _weight_bigger = constants_and_values["weight_bigger"] _relative_mode = constants_and_values["relative_mode"] _from_bp = constants_and_values["from_bp"] _to_bp = constants_and_values["to_bp"]