-
Notifications
You must be signed in to change notification settings - Fork 13
/
goturndataloader.py
199 lines (159 loc) · 6.86 KB
/
goturndataloader.py
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
197
198
199
"""
File: goturndataloader.py
Author: Nrupatunga
Email: nrupatunga.s@byjus.com
Github: https://github.com/nrupatunga
Description: goturn dataloader
"""
import math
import sys
from multiprocessing import Manager
from pathlib import Path
import numpy as np
import torch
from torch.utils.data import DataLoader, Dataset
from tqdm import tqdm
from loguru import logger
try:
from goturn.dataloaders.alov import AlovDataset
from goturn.dataloaders.imagenet import ImageNetDataset
from goturn.dataloaders.sampler import sample_generator
from goturn.helper.image_io import resize
except ImportError:
logger.error('Please run $source settings.sh from root directory')
sys.exit(1)
class GoturnDataloader(Dataset):
"""Docstring for goturnDataloader. """
def __init__(self, imagenet_path, alov_path, mean_file=None, isTrain=True,
val_ratio=0.005, width=227, height=227,
images_p=None, targets_p=None, bboxes_p=None,
dbg=False):
"""Dataloader initialization for goturn tracker """
# ALOV
img_dir = Path(imagenet_path).joinpath('images')
ann_dir = Path(imagenet_path).joinpath('gt')
self._imagenetD = ImageNetDataset(str(img_dir), str(ann_dir),
isTrain=isTrain,
val_ratio=val_ratio)
# Imagenet
img_dir = Path(alov_path).joinpath('images')
ann_dir = Path(alov_path).joinpath('gt')
self._alovD = AlovDataset(str(img_dir), str(ann_dir),
isTrain=isTrain, val_ratio=val_ratio)
# sample generator
self._sample_gen = sample_generator(5, 15, -0.4, 0.4, dbg=dbg)
self._kGenExPerImage = 10
self._images = []
self._targets = []
self._bboxes = []
# from prev batch
self._images_p = images_p
self._targets_p = targets_p
self._bboxes_p = bboxes_p
self._width = width
self._height = height
if mean_file:
self._mean = np.load(mean_file)
else:
self._mean = np.array([104, 117, 123])
self._minDataLen = min(len(self._imagenetD), len(self._alovD))
self._maxDataLen = max(len(self._imagenetD), len(self._alovD))
self._batchSize = 50
def __len__(self):
''' length of the total dataset, is max of one of the dataset '''
return int((self._maxDataLen + self._minDataLen) * 1.25)
def __getitem__(self, idx):
"""Get the current idx data
@idx: Current index for the data
"""
imagenet_pair = self._imagenetD[idx % len(self._imagenetD)]
alov_pair = self._alovD[idx % len(self._alovD)]
return imagenet_pair, alov_pair
def collate(self, batch):
''' Custom data collation for alov and imagenet
@batch: batch of data
'''
self._images = []
self._targets = []
self._bboxes = []
count = 0
for i, batch_i in enumerate(batch):
for i, (img_prev, bbox_prev, img_cur, bbox_cur) in enumerate(batch_i):
if count == 5:
break
self._sample_gen.reset(bbox_cur, bbox_prev, img_cur,
img_prev)
self.__make_training_samples()
count = count + 1
num_prev_batch = len(self._images_p)
num_curr_batch = self._batchSize - num_prev_batch
last_idx = (self._kGenExPerImage + 1) * math.ceil(num_curr_batch / (self._kGenExPerImage + 1))
self._images_c = self._images[0:num_curr_batch]
self._targets_c = self._targets[0:num_curr_batch]
self._bboxes_c = self._bboxes[0:num_curr_batch]
batch_images, batch_targets, batch_boxes = self._images, self._targets, self._bboxes
if num_prev_batch != 0:
self._images = self._images_p[:]
self._targets = self._targets_p[:]
self._bboxes = self._bboxes_p[:]
self._images_p[:] = []
self._targets_p[:] = []
self._bboxes_p[:] = []
else:
self._images = []
self._targets = []
self._bboxes = []
self._images_p.extend(batch_images[num_curr_batch:last_idx])
self._targets_p.extend(batch_targets[num_curr_batch:last_idx])
self._bboxes_p.extend(batch_boxes[num_curr_batch:last_idx])
self._images.extend(self._images_c)
self._targets.extend(self._targets_c)
self._bboxes.extend(self._bboxes_c)
_images = []
_targets = []
_bboxes = []
for i, (im, tar, bbox) in enumerate(zip(self._images,
self._targets,
self._bboxes)):
im = resize(im, (self._width, self._height)) - self._mean
_images.append(np.transpose(im, axes=(2, 0, 1)))
tar = resize(tar, (self._width, self._height)) - self._mean
_targets.append(np.transpose(tar, axes=(2, 0, 1)))
_bboxes.append(np.array([bbox.x1, bbox.y1, bbox.x2,
bbox.y2]))
images = torch.from_numpy(np.stack(_images))
targets = torch.from_numpy(np.stack(_targets))
bboxes = torch.from_numpy(np.stack(_bboxes))
return images, targets, bboxes
def __make_training_samples(self):
"""
1. First decide the current search region, which is
kContextFactor(=2) * current bounding box.
2. Crop the valid search region and copy to the new padded image
3. Recenter the actual bounding box of the object to the new
padded image
4. Scale the bounding box for regression
"""
sample_gen = self._sample_gen
images = self._images
targets = self._targets
bboxes = self._bboxes
image, target, bbox_gt_scaled = sample_gen.make_true_sample()
images.append(image)
targets.append(target)
bboxes.append(bbox_gt_scaled)
# Generate more number of examples
images, targets, bbox_gt_scaled = sample_gen.make_training_samples(self._kGenExPerImage, images, targets, bboxes)
if __name__ == "__main__":
imagenet_path = '/media/nthere/datasets/ISLVRC2014_Det/'
alov_path = '/media/nthere/datasets/ALOV/'
manager = Manager()
objGoturn = GoturnDataloader(imagenet_path, alov_path,
images_p=manager.list(),
targets_p=manager.list(),
bboxes_p=manager.list(),
isTrain=True, dbg=False)
dataloader = DataLoader(objGoturn, batch_size=3, shuffle=True,
num_workers=6, collate_fn=objGoturn.collate)
for i, *data in tqdm(enumerate(dataloader), desc='Loading imagenet/alov', total=len(objGoturn) / 3, unit='files'):
pass