-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
196 lines (168 loc) · 7.76 KB
/
dataset.py
File metadata and controls
196 lines (168 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
import torch
import torchvision.transforms.v2
from torch.utils.data.dataset import Dataset
import xml.etree.ElementTree as ET
from torchvision import tv_tensors
from torchvision.io import read_image
import os
import urllib.request
import tarfile
# Download URLS for VOC datasets
# Where to store the downloaded and extracted data
BASE_DIR = os.path.join(os.getcwd(), "data")
os.makedirs(BASE_DIR, exist_ok=True)
def download_and_extract(url, BASE_DIR):
filename = url.split("/")[-1]
file_path = os.path.join(BASE_DIR, filename)
if not os.path.exists(file_path):
print(f"Downloading {filename}...")
urllib.request.urlretrieve(url, file_path)
print(f"Downloaded {filename}")
else:
print(f"{filename} already exists, skipping download.")
print(f"Extracting {filename}...")
with tarfile.open(file_path) as tar:
tar.extractall(path=BASE_DIR)
print(f"Extracted to {BASE_DIR}")
def move_voc(source_subdir, target_name):
"""Move VOCdevkit/VOC2007 -> VOC2007 or VOC2007-test etc."""
src_path = os.path.join(BASE_DIR, "VOCdevkit", source_subdir)
tgt_path = os.path.join(BASE_DIR, target_name)
if os.path.exists(tgt_path):
print(f"{target_name} already exists, skipping move.")
else:
os.rename(src_path, tgt_path)
print(f"Moved {source_subdir} → {target_name}")
def load_images_and_anns(im_sets, label2idx, ann_fname, split):
r"""
Method to get the xml files and for each file
get all the objects and their ground truth detection
information for the dataset
:param im_sets: Sets of images to consider
:param label2idx: Class Name to index mapping for dataset
:param ann_fname: txt file containing image names{trainval.txt/test.txt}
:param split: train/test
:return:
"""
im_infos = []
for im_set in im_sets:
im_names = []
# Fetch all image names in txt file for this imageset
for line in open(os.path.join(
im_set, 'ImageSets', 'Main', '{}.txt'.format(ann_fname))):
im_names.append(line.strip())
# Set annotation and image path
ann_dir = os.path.join(im_set, 'Annotations')
im_dir = os.path.join(im_set, 'JPEGImages')
for im_name in im_names:
ann_file = os.path.join(ann_dir, '{}.xml'.format(im_name))
im_info = {}
ann_info = ET.parse(ann_file)
root = ann_info.getroot()
size = root.find('size')
width = int(size.find('width').text)
height = int(size.find('height').text)
im_info['img_id'] = os.path.basename(ann_file).split('.xml')[0]
im_info['filename'] = os.path.join(
im_dir, '{}.jpg'.format(im_info['img_id'])
)
im_info['width'] = width
im_info['height'] = height
detections = []
for obj in ann_info.findall('object'):
det = {}
label = label2idx[obj.find('name').text]
difficult = int(obj.find('difficult').text)
bbox_info = obj.find('bndbox')
bbox = [
int(bbox_info.find('xmin').text) - 1,
int(bbox_info.find('ymin').text) - 1,
int(bbox_info.find('xmax').text) - 1,
int(bbox_info.find('ymax').text) - 1
]
det['label'] = label
det['bbox'] = bbox
det['difficult'] = difficult
detections.append(det)
im_info['detections'] = detections
# Because we are using 25 as num_queries,
# so we ignore all images in VOC with greater
# than 25 target objects.
# This is okay, since this just means we are
# ignoring a small number of images(15 to be precise)
if len(detections) <= 25:
im_infos.append(im_info)
print('Total {} images found'.format(len(im_infos)))
return im_infos
class VOCDataset(Dataset):
def __init__(self, split, im_sets, im_size=640):
self.split = split
# Imagesets for this dataset instance (VOC2007/VOC2007+VOC2012/VOC2007-test)
self.im_sets = im_sets
self.fname = 'trainval' if self.split == 'train' else 'test'
self.im_size = im_size
self.im_mean = [123.0, 117.0, 104.0]
self.imagenet_mean = [0.485, 0.456, 0.406]
self.imagenet_std = [0.229, 0.224, 0.225]
# Train and test transformations
self.transforms = {
'train': torchvision.transforms.v2.Compose([
torchvision.transforms.v2.RandomHorizontalFlip(p=0.5),
torchvision.transforms.v2.RandomZoomOut(fill=self.im_mean),
torchvision.transforms.v2.RandomIoUCrop(),
torchvision.transforms.v2.RandomPhotometricDistort(),
torchvision.transforms.v2.Resize(size=(self.im_size, self.im_size)),
torchvision.transforms.v2.SanitizeBoundingBoxes(
labels_getter=lambda transform_input:
(transform_input[1]["labels"], transform_input[1]["difficult"])),
torchvision.transforms.v2.ToPureTensor(),
torchvision.transforms.v2.ToDtype(torch.float32, scale=True),
torchvision.transforms.v2.Normalize(mean=self.imagenet_mean,
std=self.imagenet_std)
]),
'test': torchvision.transforms.v2.Compose([
torchvision.transforms.v2.Resize(size=(self.im_size, self.im_size)),
torchvision.transforms.v2.ToPureTensor(),
torchvision.transforms.v2.ToDtype(torch.float32, scale=True),
torchvision.transforms.v2.Normalize(mean=self.imagenet_mean,
std=self.imagenet_std)
]),
}
classes = [
'person', 'bird', 'cat', 'cow', 'dog', 'horse', 'sheep',
'aeroplane', 'bicycle', 'boat', 'bus', 'car', 'motorbike', 'train',
'bottle', 'chair', 'diningtable', 'pottedplant', 'sofa', 'tvmonitor'
]
classes = sorted(classes)
# We need to add background class as well with 0 index
classes = ['background'] + classes
self.label2idx = {classes[idx]: idx for idx in range(len(classes))}
self.idx2label = {idx: classes[idx] for idx in range(len(classes))}
print(self.idx2label)
self.images_info = load_images_and_anns(self.im_sets,
self.label2idx,
self.fname,
self.split)
def __len__(self):
return len(self.images_info)
def __getitem__(self, index):
im_info = self.images_info[index]
im = read_image(im_info['filename'])
# Get annotations for this image
targets = {}
targets['boxes'] = tv_tensors.BoundingBoxes(
[detection['bbox'] for detection in im_info['detections']],
format='XYXY', canvas_size=im.shape[-2:])
targets['labels'] = torch.as_tensor(
[detection['label'] for detection in im_info['detections']])
targets['difficult'] = torch.as_tensor(
[detection['difficult']for detection in im_info['detections']])
# Transform the image and targets
transformed_info = self.transforms[self.split](im, targets)
im_tensor, targets = transformed_info
h, w = im_tensor.shape[-2:]
# Boxes returned are in x1y1x2y2 format normalized from 0-1
wh_tensor = torch.as_tensor([[w, h, w, h]]).expand_as(targets['boxes'])
targets['boxes'] = targets['boxes'] / wh_tensor
return im_tensor, targets, im_info['filename']