-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathdataset.py
131 lines (109 loc) · 4.29 KB
/
dataset.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
import os
import numpy as np
import torch as t
import torchvision as tv
from torch.utils.data import Dataset
import cv2
import numpy as np
import types
from pycocotools.coco import COCO
class COCODataset(Dataset):
"""
COCO dataset class.
"""
def __init__(self, data_dir='COCO', json_file='instances_train2017.json',
name='train2017', img_size=416,
transform=None, min_size=1):
"""
COCO dataset initialization. Annotation data are read into memory by COCO API.
Args:
data_dir (str): dataset root directory
json_file (str): COCO json file name
name (str): COCO data name (e.g. 'train2017' or 'val2017')
img_size (int): target image size after pre-processing
min_size (int): bounding boxes smaller than this are ignored
"""
self.data_dir = data_dir
self.json_file = json_file
self.coco = COCO(self.data_dir+'annotations/'+self.json_file)
self.ids = self.coco.getImgIds()
self.class_ids = sorted(self.coco.getCatIds())
self.name = name
self.max_labels = 50
self.img_size = img_size
self.min_size = min_size
self.transform = transform
def __len__(self):
return len(self.ids)
def __getitem__(self, index):
id_ = self.ids[index]
anno_ids = self.coco.getAnnIds(imgIds=[int(id_)], iscrowd=None)
annotations = self.coco.loadAnns(anno_ids)
# 读取图像,做预处理
img_file = os.path.join(self.data_dir, self.name,
'{:012}'.format(id_) + '.jpg')
img = cv2.imread(img_file)
assert img is not None
height, width, channels = img.shape
# 预处理,将bbox原始标注[xmin,ymin,w,h]转化为[xmin,ymin,xmax,ymax,label]
target = []
for anno in annotations:
if 'bbox' in anno and anno['area'] > 0:
xmin = np.max((0, anno['bbox'][0]))
ymin = np.max((0, anno['bbox'][1]))
xmax = np.min((width - 1, xmin + np.max((0, anno['bbox'][2] - 1))))
ymax = np.min((height - 1, ymin + np.max((0, anno['bbox'][3] - 1))))
if xmax > xmin and ymax > ymin:
label_ind = anno['category_id']
cls_id = self.class_ids.index(label_ind)
xmin /= width
ymin /= height
xmax /= width
ymax /= height
target.append([xmin, ymin, xmax, ymax, cls_id])
else:
print('No bbox !!!')
if len(target) == 0:
target = np.zeros([1, 5])
else:
target = np.array(target)
img, boxes, labels = self.transform(img, target[:, :4], target[:, 4])
# to rgb
img = img[:, :, (2, 1, 0)]
target = np.hstack((boxes, np.expand_dims(labels, axis=1)))
return t.from_numpy(img).permute(2, 0, 1), target
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img, boxes=None, labels=None):
for t in self.transforms:
img, boxes, labels = t(img, boxes, labels)
return img, boxes, labels
class Normalize(object):
def __init__(self, mean=None, std=None):
self.mean = np.array(mean, dtype=np.float32)
self.std = np.array(std, dtype=np.float32)
def __call__(self, image, boxes=None, labels=None):
image = image.astype(np.float32)
image /= 255.
image -= self.mean
image /= self.std
return image, boxes, labels
class Resize(object):
def __init__(self, size=300):
self.size = size
def __call__(self, image, boxes=None, labels=None):
image = cv2.resize(image, (self.size,
self.size))
return image, boxes, labels
class Augmentation(object):
def __init__(self, size=512, mean=(0.406, 0.456, 0.485), std=(0.225, 0.224, 0.229)):
self.mean = mean
self.size = size
self.std = std
self.augment = Compose([
Resize(self.size),
Normalize(self.mean, self.std)
])
def __call__(self, img, boxes, labels):
return self.augment(img, boxes, labels)