Skip to content

Commit 8c18df4

Browse files
Merge pull request #1 from jena-shreyas/main
added support for loading annotations on-the-go
2 parents 13c9e23 + ca3976b commit 8c18df4

File tree

3 files changed

+81
-15
lines changed

3 files changed

+81
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
checkpoint/
2+
Data-Generated/
23
*.pyc
34
/*.log
45
*ptm

iRPE/DETR-with-iRPE/datasets/coco.py

+76-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import torch
1111
import torch.utils.data
1212
import torchvision
13+
from pycocotools.coco import COCO
1314
from pycocotools import mask as coco_mask
1415
from PIL import Image
1516
from io import BytesIO
@@ -18,6 +19,8 @@
1819

1920
import datasets.transforms as T
2021

22+
from torchvision.datasets import VisionDataset
23+
from typing import Any, Callable, Optional, Tuple, List
2124

2225
ZIPS = dict()
2326

@@ -59,34 +62,92 @@ def my_Image_open(root, fname):
5962
iob = BytesIO(my_open(root, fname))
6063
return Image.open(iob)
6164

65+
class CocoDetectionOptim(VisionDataset):
66+
"""`MS Coco Detection <https://cocodataset.org/#detection-2016>`_ Dataset.
67+
68+
Args:
69+
root (string): Root directory where images are downloaded to.
70+
annot (string): Path to json ** annotations directory **.
71+
transform (callable, optional): A function/transform that takes in an PIL image
72+
and returns a transformed version. E.g, ``transforms.ToTensor``
73+
target_transform (callable, optional): A function/transform that takes in the
74+
target and transforms it.
75+
transforms (callable, optional): A function/transform that takes input sample and its target as entry
76+
and returns a transformed version.
77+
"""
78+
79+
def __init__(
80+
self,
81+
root: str,
82+
# annFile: str,
83+
annot: str,
84+
transform: Optional[Callable] = None,
85+
target_transform: Optional[Callable] = None,
86+
transforms: Optional[Callable] = None,
87+
) -> None:
88+
super().__init__(root, transforms, transform, target_transform)
89+
# from pycocotools.coco import COCO
90+
91+
# self.coco = COCO(annFile)
92+
self.ann_paths = [os.path.join(annot, f) for f in os.listdir(annot) if f.endswith('.json')]
93+
# self.ids = list(sorted(self.coco.imgs.keys()))
94+
95+
def _load_image(self, index: int) -> Image.Image:
96+
coco = COCO(self.ann_paths[index])
97+
id = list(coco.imgs.keys())[0]
98+
path = coco.loadImgs(id)[0]["file_name"]
99+
return Image.open(os.path.join(self.root, path)).convert("RGB")
100+
101+
def _load_target(self, index: int) -> List[Any]:
102+
coco = COCO(self.ann_paths[index])
103+
id = list(coco.imgs.keys())[0]
104+
return coco.loadAnns(coco.getAnnIds(id))
105+
106+
def __getitem__(self, index: int) -> Tuple[Any, Any]:
107+
# id = self.ids[index]
108+
image = self._load_image(index)
109+
target = self._load_target(index)
62110

63-
class CocoDetection(torchvision.datasets.CocoDetection):
64-
def __init__(self, img_folder, ann_file, transforms, return_masks):
65-
super(CocoDetection, self).__init__(img_folder, ann_file)
111+
if self.transforms is not None:
112+
image, target = self.transforms(image, target)
113+
114+
return image, target
115+
116+
def __len__(self) -> int:
117+
return len(self.ann_paths)
118+
119+
120+
class CocoDetection(CocoDetectionOptim):
121+
def __init__(self, img_folder, ann_folder, transforms, return_masks):
122+
super(CocoDetection, self).__init__(img_folder, ann_folder)
66123
self._transforms = transforms
67124
self.prepare = ConvertCocoPolysToMask(return_masks)
68125

69126
def __getitem__(self, idx):
70-
id = self.ids[idx]
71-
img = self._load_image(id)
72-
target = self._load_target(id)
127+
img = self._load_image(idx)
128+
target = self._load_target(idx)
73129

74130
if self.transforms is not None:
75131
img, target = self.transforms(img, target)
76132

77-
image_id = self.ids[idx]
133+
coco = COCO(self.ann_paths[idx])
134+
image_id = list(coco.imgs.keys())[0]
78135
target = {'image_id': image_id, 'annotations': target}
79136
img, target = self.prepare(img, target)
80137
if self._transforms is not None:
81138
img, target = self._transforms(img, target)
82139
return img, target
83140

84-
def _load_image(self, id: int) -> Image.Image:
85-
path = self.coco.loadImgs(id)[0]["file_name"]
141+
def _load_image(self, index: int) -> Image.Image:
142+
coco = COCO(self.ann_paths[index])
143+
id = list(coco.imgs.keys())[0]
144+
path = coco.loadImgs(id)[0]["file_name"]
86145
return my_Image_open(self.root, path).convert('RGB')
87146

88-
def _load_target(self, id) -> List[Any]:
89-
return self.coco.loadAnns(self.coco.getAnnIds(id))
147+
def _load_target(self, index) -> List[Any]:
148+
coco = COCO(self.ann_paths[index])
149+
id = list(coco.imgs.keys())[0]
150+
return coco.loadAnns(coco.getAnnIds(id))
90151

91152

92153
def convert_coco_poly_to_mask(segmentations, height, width):
@@ -207,10 +268,10 @@ def build(image_set, args):
207268
root = Path(args.coco_path)
208269
assert root.exists(), f'provided COCO path {root} does not exist'
209270
PATHS = {
210-
"train": (root / "train", root / "annotations" / f'train_annotations.json'),
211-
"val": (root / "val", root / "annotations" / f'val_annotations.json'),
271+
"train": (root / "train", root / "annotations"),
272+
"val": (root / "val", root / "annotations"),
212273
}
213274

214-
img_folder, ann_file = PATHS[image_set]
215-
dataset = CocoDetection(img_folder, ann_file, transforms=make_coco_transforms(image_set), return_masks=args.masks)
275+
img_folder, ann_folder = PATHS[image_set]
276+
dataset = CocoDetection(img_folder, ann_folder, transforms=make_coco_transforms(image_set), return_masks=args.masks)
216277
return dataset

iRPE/DETR-with-iRPE/datasets/dummy.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import os
2+
paths = [os.path.join(os.getcwd(), f) for f in os.listdir('.') if f.endswith('.py')]
3+
for path in paths:
4+
print(path)

0 commit comments

Comments
 (0)