-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoco.py
165 lines (142 loc) · 6.03 KB
/
coco.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
import os
import torch
import torch.utils.data
import torchvision
import torchvision.transforms as transforms
from PIL import Image
from pycocotools.coco import COCO
class CocoDataset(torch.utils.data.Dataset):
def __init__(self, root, annotation, size, indoor_only=False):
self.root = root
# self.transforms = torchvision.transforms.Compose([
# torchvision.transforms.ToTensor(),
# ])
self.inp_size = size
if "val" not in annotation:
print("train")
self.transforms = transforms.Compose([
transforms.Resize(self.inp_size),
transforms.RandomCrop(self.inp_size),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10), # Include for resnet
transforms.ColorJitter(brightness=0.3, contrast=0.3, saturation=0.3, hue=0.1),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
else:
self.transforms = transforms.Compose([
transforms.Resize(self.inp_size),
transforms.CenterCrop(self.inp_size),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
self.disp_transforms = transforms.Compose([
transforms.Resize(self.inp_size),
transforms.RandomCrop(self.inp_size),
transforms.ToTensor()
])
self.coco = COCO(annotation)
self.ids = list(sorted(self.coco.imgs.keys()))
self.category_map = {cat["id"]:cat["name"] for cat in self.coco.dataset["categories"]}
self.indoor_only = indoor_only
def __getitem__(self, index):
# Own coco file
coco = self.coco
# Image ID
img_id = self.ids[index]
# List: get annotation id from coco
if not self.indoor_only:
ann_ids = coco.getAnnIds(imgIds=img_id)
else:
ann_ids = coco.getAnnIds(imgIds=img_id, catIds=coco.getCatIds(supNms=["indoor", "kitchen"]))
# print(ann_ids)
# Dictionary: target coco_annotation file for an image
coco_annotation = coco.loadAnns(ann_ids)
# print()
# path for input image
path = coco.loadImgs(img_id)[0]['file_name']
# open the input image
img = Image.open(os.path.join(self.root, path))
img = img.convert('RGB')
img_disp = Image.open(os.path.join(self.root, path))
img_disp = img_disp.convert('RGB')
# number of objects in the image
num_objs = len(coco_annotation)
# Bounding boxes for objects
# In coco format, bbox = [xmin, ymin, width, height]
# In pytorch, the input should be [xmin, ymin, xmax, ymax]
boxes = []
category_ids = []
for i in range(num_objs):
xmin = coco_annotation[i]['bbox'][0]
ymin = coco_annotation[i]['bbox'][1]
xmax = xmin + coco_annotation[i]['bbox'][2]
ymax = ymin + coco_annotation[i]['bbox'][3]
boxes.append([xmin, ymin, xmax, ymax])
category_ids.append(coco_annotation[i]['category_id'])
# print(category_ids)
boxes = torch.as_tensor(boxes, dtype=torch.float32)
labels = torch.as_tensor(category_ids)
# Tensorise img_id
img_id = torch.tensor([img_id])
# Size of bbox (Rectangular)
areas = []
for i in range(num_objs):
areas.append(coco_annotation[i]['area'])
areas = torch.as_tensor(areas, dtype=torch.float32)
# Iscrowd
iscrowd = torch.zeros((num_objs,), dtype=torch.int64)
# print(len(category_ids))
# Choose box to black out
if len(category_ids) != 0 :
box_id = int(torch.rand(1) * len(category_ids))
boxes = boxes[box_id]
labels = labels[box_id] - 1 # Shift labels to 0-index
#Make labels one-hot
if not self.indoor_only:
one_hot = torch.zeros(90)
else:
one_hot = torch.zeros(14)
one_hot[labels] = 1.
labels = one_hot
# White out region of image corresponding to box
img = self.white_out_image(img, boxes)
img_disp = self.white_out_image(img, boxes)
else :
if not self.indoor_only:
one_hot = torch.zeros(90)
else:
one_hot = torch.zeros(14)
labels = one_hot
# Annotation is in dictionary format
my_annotation = {}
my_annotation["boxes"] = boxes
my_annotation["labels"] = labels
# my_annotation["image_id"] = img_id
# my_annotation["area"] = areas
# my_annotation["iscrowd"] = iscrowd
if self.transforms is not None:
img = self.transforms(img)
return img, my_annotation['labels'], self.disp_transforms(img_disp)
def __len__(self):
return len(self.ids)
def white_out_image(self, img, box) :
img = torchvision.transforms.ToTensor()(img)
# print("Im back in the white")
# print(torch.max(img), torch.min(img))
img[:, int(box[1]) : int(box[3]), int(box[0]) : int(box[2])] = 1.0
# print(torch.max(img), torch.min(img))
img = torchvision.transforms.ToPILImage()(img)
return img
def class_to_label(self, ids) :
labels_list = []
# print(self.category_map)
for id in ids :
labels_list.append(self.category_map[id+1])
return labels_list