Skip to content

Commit f18d98f

Browse files
authored
fileio from petrel (#2182)
1 parent c6f1e12 commit f18d98f

File tree

6 files changed

+52
-46
lines changed

6 files changed

+52
-46
lines changed

mmpose/datasets/datasets/base/base_coco_style_dataset.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import numpy as np
99
from mmengine.dataset import BaseDataset, force_full_init
10-
from mmengine.fileio import load
11-
from mmengine.utils import check_file_exist, is_list_of
10+
from mmengine.fileio import exists, get_local_path, load
11+
from mmengine.utils import is_list_of
1212
from xtcocotools.coco import COCO
1313

1414
from mmpose.registry import DATASETS
@@ -195,18 +195,19 @@ def load_data_list(self) -> List[dict]:
195195
def _load_annotations(self) -> Tuple[List[dict], List[dict]]:
196196
"""Load data from annotations in COCO format."""
197197

198-
check_file_exist(self.ann_file)
198+
assert exists(self.ann_file), 'Annotation file does not exist'
199199

200-
coco = COCO(self.ann_file)
200+
with get_local_path(self.ann_file) as local_path:
201+
self.coco = COCO(local_path)
201202
# set the metainfo about categories, which is a list of dict
202203
# and each dict contains the 'id', 'name', etc. about this category
203-
self._metainfo['CLASSES'] = coco.loadCats(coco.getCatIds())
204+
self._metainfo['CLASSES'] = self.coco.loadCats(self.coco.getCatIds())
204205

205206
instance_list = []
206207
image_list = []
207208

208-
for img_id in coco.getImgIds():
209-
img = coco.loadImgs(img_id)[0]
209+
for img_id in self.coco.getImgIds():
210+
img = self.coco.loadImgs(img_id)[0]
210211
img.update({
211212
'img_id':
212213
img_id,
@@ -215,8 +216,8 @@ def _load_annotations(self) -> Tuple[List[dict], List[dict]]:
215216
})
216217
image_list.append(img)
217218

218-
ann_ids = coco.getAnnIds(imgIds=img_id)
219-
for ann in coco.loadAnns(ann_ids):
219+
ann_ids = self.coco.getAnnIds(imgIds=img_id)
220+
for ann in self.coco.loadAnns(ann_ids):
220221

221222
instance_info = self.parse_data_info(
222223
dict(raw_ann_info=ann, raw_img_info=img))
@@ -380,18 +381,18 @@ def _get_bottomup_data_infos(self, instance_list: List[Dict],
380381
def _load_detection_results(self) -> List[dict]:
381382
"""Load data from detection results with dummy keypoint annotations."""
382383

383-
check_file_exist(self.ann_file)
384-
check_file_exist(self.bbox_file)
385-
384+
assert exists(self.ann_file), 'Annotation file does not exist'
385+
assert exists(self.bbox_file), 'Bbox file does not exist'
386386
# load detection results
387387
det_results = load(self.bbox_file)
388388
assert is_list_of(det_results, dict)
389389

390390
# load coco annotations to build image id-to-name index
391-
coco = COCO(self.ann_file)
391+
with get_local_path(self.ann_file) as local_path:
392+
self.coco = COCO(local_path)
392393
# set the metainfo about categories, which is a list of dict
393394
# and each dict contains the 'id', 'name', etc. about this category
394-
self._metainfo['CLASSES'] = coco.loadCats(coco.getCatIds())
395+
self._metainfo['CLASSES'] = self.coco.loadCats(self.coco.getCatIds())
395396

396397
num_keypoints = self.metainfo['num_keypoints']
397398
data_list = []
@@ -401,7 +402,7 @@ def _load_detection_results(self) -> List[dict]:
401402
if det['category_id'] != 1:
402403
continue
403404

404-
img = coco.loadImgs(det['image_id'])[0]
405+
img = self.coco.loadImgs(det['image_id'])[0]
405406

406407
img_path = osp.join(self.data_prefix['img'], img['file_name'])
407408
bbox_xywh = np.array(

mmpose/datasets/datasets/body/mpii_dataset.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Callable, List, Optional, Sequence, Tuple, Union
55

66
import numpy as np
7-
from mmengine.utils import check_file_exist
7+
from mmengine.fileio import exists, get_local_path
88
from scipy.io import loadmat
99

1010
from mmpose.registry import DATASETS
@@ -137,14 +137,16 @@ def __init__(self,
137137
def _load_annotations(self) -> Tuple[List[dict], List[dict]]:
138138
"""Load data from annotations in MPII format."""
139139

140-
check_file_exist(self.ann_file)
141-
with open(self.ann_file) as anno_file:
142-
anns = json.load(anno_file)
140+
assert exists(self.ann_file), 'Annotation file does not exist'
141+
with get_local_path(self.ann_file) as local_path:
142+
with open(local_path) as anno_file:
143+
self.anns = json.load(anno_file)
143144

144145
if self.headbox_file:
145-
check_file_exist(self.headbox_file)
146-
headbox_dict = loadmat(self.headbox_file)
147-
headboxes_src = np.transpose(headbox_dict['headboxes_src'],
146+
assert exists(self.headbox_file), 'Headbox file does not exist'
147+
with get_local_path(self.headbox_file) as local_path:
148+
self.headbox_dict = loadmat(local_path)
149+
headboxes_src = np.transpose(self.headbox_dict['headboxes_src'],
148150
[2, 0, 1])
149151
SC_BIAS = 0.6
150152

@@ -156,7 +158,7 @@ def _load_annotations(self) -> Tuple[List[dict], List[dict]]:
156158
# mpii bbox scales are normalized with factor 200.
157159
pixel_std = 200.
158160

159-
for idx, ann in enumerate(anns):
161+
for idx, ann in enumerate(self.anns):
160162
center = np.array(ann['center'], dtype=np.float32)
161163
scale = np.array([ann['scale'], ann['scale']],
162164
dtype=np.float32) * pixel_std

mmpose/datasets/datasets/body/mpii_trb_dataset.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import List, Tuple
55

66
import numpy as np
7-
from mmengine.utils import check_file_exist
7+
from mmengine.fileio import exists, get_local_path
88

99
from mmpose.registry import DATASETS
1010
from mmpose.structures.bbox import bbox_cs2xyxy
@@ -106,11 +106,12 @@ class MpiiTrbDataset(BaseCocoStyleDataset):
106106
def _load_annotations(self) -> Tuple[List[dict], List[dict]]:
107107
"""Load data from annotations in MPII-TRB format."""
108108

109-
check_file_exist(self.ann_file)
110-
with open(self.ann_file) as anno_file:
111-
data = json.load(anno_file)
109+
assert exists(self.ann_file), 'Annotation file does not exist'
110+
with get_local_path(self.ann_file) as local_path:
111+
with open(local_path) as anno_file:
112+
self.data = json.load(anno_file)
112113

113-
imgid2info = {img['id']: img for img in data['images']}
114+
imgid2info = {img['id']: img for img in self.data['images']}
114115

115116
instance_list = []
116117
image_list = []
@@ -119,7 +120,7 @@ def _load_annotations(self) -> Tuple[List[dict], List[dict]]:
119120
# mpii-trb bbox scales are normalized with factor 200.
120121
pixel_std = 200.
121122

122-
for ann in data['annotations']:
123+
for ann in self.data['annotations']:
123124
img_id = ann['image_id']
124125

125126
# center, scale in shape [1, 2] and bbox in [1, 4]

mmpose/datasets/datasets/body/posetrack18_video_dataset.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from typing import Callable, List, Optional, Sequence, Union
44

55
import numpy as np
6-
from mmengine.fileio import load
7-
from mmengine.utils import check_file_exist, is_list_of
6+
from mmengine.fileio import exists, get_local_path, load
7+
from mmengine.utils import is_list_of
88
from xtcocotools.coco import COCO
99

1010
from mmpose.registry import DATASETS
@@ -287,22 +287,22 @@ def parse_data_info(self, raw_data_info: dict) -> Optional[dict]:
287287

288288
def _load_detection_results(self) -> List[dict]:
289289
"""Load data from detection results with dummy keypoint annotations."""
290-
291-
check_file_exist(self.ann_file)
292-
check_file_exist(self.bbox_file)
290+
assert exists(self.ann_file), 'Annotation file does not exist'
291+
assert exists(self.bbox_file), 'Bbox file does not exist'
293292

294293
# load detection results
295294
det_results = load(self.bbox_file)
296295
assert is_list_of(det_results, dict)
297296

298297
# load coco annotations to build image id-to-name index
299-
coco = COCO(self.ann_file)
298+
with get_local_path(self.ann_file) as local_path:
299+
self.coco = COCO(local_path)
300300

301301
# mapping image name to id
302302
name2id = {}
303303
# mapping image id to name
304304
id2name = {}
305-
for img_id, image in coco.imgs.items():
305+
for img_id, image in self.coco.imgs.items():
306306
file_name = image['file_name']
307307
id2name[img_id] = file_name
308308
name2id[file_name] = img_id
@@ -333,7 +333,7 @@ def _load_detection_results(self) -> List[dict]:
333333
img_id = name2id[det['image_name']]
334334
else:
335335
img_id = det['image_id']
336-
img_ann = coco.loadImgs(img_id)[0]
336+
img_ann = self.coco.loadImgs(img_id)[0]
337337
nframes = int(img_ann['nframes'])
338338

339339
# deal with multiple image paths

mmpose/datasets/datasets/hand/coco_wholebody_hand_dataset.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import List, Tuple
44

55
import numpy as np
6-
from mmengine.utils import check_file_exist
6+
from mmengine.fileio import exists, get_local_path
77
from xtcocotools.coco import COCO
88

99
from mmpose.registry import DATASETS
@@ -87,15 +87,16 @@ class CocoWholeBodyHandDataset(BaseCocoStyleDataset):
8787
def _load_annotations(self) -> Tuple[List[dict], List[dict]]:
8888
"""Load data from annotations in COCO format."""
8989

90-
check_file_exist(self.ann_file)
90+
assert exists(self.ann_file), 'Annotation file does not exist'
9191

92-
coco = COCO(self.ann_file)
92+
with get_local_path(self.ann_file) as local_path:
93+
self.coco = COCO(local_path)
9394
instance_list = []
9495
image_list = []
9596
id = 0
9697

97-
for img_id in coco.getImgIds():
98-
img = coco.loadImgs(img_id)[0]
98+
for img_id in self.coco.getImgIds():
99+
img = self.coco.loadImgs(img_id)[0]
99100

100101
img.update({
101102
'img_id':
@@ -105,8 +106,8 @@ def _load_annotations(self) -> Tuple[List[dict], List[dict]]:
105106
})
106107
image_list.append(img)
107108

108-
ann_ids = coco.getAnnIds(imgIds=img_id, iscrowd=False)
109-
anns = coco.loadAnns(ann_ids)
109+
ann_ids = self.coco.getAnnIds(imgIds=img_id, iscrowd=False)
110+
anns = self.coco.loadAnns(ann_ids)
110111
for ann in anns:
111112
for type in ['left', 'right']:
112113
# filter invalid hand annotations, there might be two

mmpose/evaluation/metrics/coco_metric.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99
from mmengine.evaluator import BaseMetric
10-
from mmengine.fileio import dump, load
10+
from mmengine.fileio import dump, get_local_path, load
1111
from mmengine.logging import MMLogger
1212
from xtcocotools.coco import COCO
1313
from xtcocotools.cocoeval import COCOeval
@@ -102,7 +102,8 @@ def __init__(self,
102102
# initialize coco helper with the annotation json file
103103
# if ann_file is not specified, initialize with the converted dataset
104104
if ann_file is not None:
105-
self.coco = COCO(ann_file)
105+
with get_local_path(ann_file) as local_path:
106+
self.coco = COCO(local_path)
106107
else:
107108
self.coco = None
108109

0 commit comments

Comments
 (0)