-
Notifications
You must be signed in to change notification settings - Fork 12
/
dataset.py
172 lines (147 loc) · 7.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
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
import json
import torch
import numpy as np
from torchvision import transforms
from torch.utils import data
class Dataset(data.Dataset):
def __init__(self, dataset, image_size, datasource, numpy_file_path, features_path=None):
self.data = np.load(numpy_file_path)
print('loading numpy data:', numpy_file_path)
if features_path is not None:
self.precomputed_features = torch.load(features_path, map_location='cpu')
print('loading', features_path)
self.dataset = dataset
self.image_size = image_size
self.ims = self.data['ims']
self.datasource = datasource
self.preprocess = transforms.Compose([
transforms.ToPILImage(mode='RGB'),
transforms.Resize(self.image_size),
transforms.CenterCrop(self.image_size),
transforms.ToTensor()
])
self.label_description = {
"left": "to the left of",
"right": "to the right of",
"behind": "behind",
"front": "in front of",
"above": "above",
"below": "below"
}
if dataset in ['clevr', 'igibson']:
with open('./data/attributes.json', 'r') as f:
data_json = json.load(f)
self.colors_to_idx = data_json[dataset]['colors']
self.shapes_to_idx = data_json[dataset]['shapes']
self.materials_to_idx = data_json[dataset]['materials']
self.sizes_to_idx = data_json[dataset]['sizes']
self.relations_to_idx = data_json[dataset]['relations']
self.colors = list(data_json[dataset]['colors'].keys())
self.shapes = list(data_json[dataset]['shapes'].keys())
self.materials = list(data_json[dataset]['materials'].keys())
self.sizes = list(data_json[dataset]['sizes'].keys())
self.relations = list(data_json[dataset]['relations'].keys())
else:
if dataset == 'visual_genome':
selected_objects = ['man', 'person', 'woman', 'bus', 'people', 'door', 'car', 'boy', 'girl', 'lady']
objects = {object_name: i for i, object_name in enumerate(selected_objects)}
elif dataset == 'blocks':
selected_objects = ['red', 'green', 'blue', 'yellow']
objects = {object_name: i for i, object_name in enumerate(selected_objects)}
else:
raise NotImplementedError
relations = {'below': 0, 'above': 1}
self.objects = {value: key for key, value in objects.items()}
self.relations = {value: key for key, value in relations.items()}
self.labels = self.data['labels']
self.size = self.labels.shape[0]
print('image data size', self.ims.shape)
print('label data size', self.labels.shape)
print('resize to', (self.image_size, self.image_size))
def __len__(self):
return self.size
def __getitem__(self, index):
im = self.preprocess(self.ims[index])
im = im.permute((1, 2, 0))
label = self.labels[index]
if self.datasource == 'default':
im_corrupt = im + 0.3 * torch.randn(self.image_size, self.image_size, 3)
elif self.datasource == 'random':
im_corrupt = 0.5 + 0.5 * torch.randn(self.image_size, self.image_size, 3)
else:
raise NotImplementedError
return im_corrupt, im, label, self._convert_caption(label)
def collate_fn(self, batches):
ims_corrupt = torch.stack([x[0].float() for x in batches], dim=0)
ims = torch.stack([x[1] for x in batches], dim=0)
labels = [self._convert_label(x[2]) for x in batches]
captions = [self._convert_caption(x[2]) for x in batches]
return ims_corrupt, ims, labels, captions
def collate_fn_clip_all(self, batches):
ims_corrupt = torch.stack([x[0].float() for x in batches], dim=0)
ims = torch.stack([x[1] for x in batches], dim=0)
labels = torch.cat([self._convert_to_sentence(x[2]) for x in batches], dim=0)
captions = [self._convert_caption(x[2]) for x in batches]
return ims_corrupt, ims, labels, captions
def _convert_to_sentence(self, label):
if self.dataset == 'blocks':
obj1 = self.objects[label[0]]
relation = self.relations[label[1]]
obj2 = self.objects[label[2]]
return torch.as_tensor(self.precomputed_features[f'{obj1} {relation} {obj2}'])
else:
text_label = []
for i in range(2):
shape, size, color, material, pos = label[i * 5:i * 5 + 5]
text_label.append(' '.join([self.sizes[size], self.colors[color],
self.materials[material], self.shapes[shape]]))
relation = self.relations[label[-1]]
if relation == 'none':
sentence = f'{text_label[0]}'
return self.precomputed_features[sentence]
else:
sentence = f'{text_label[0]} {self.label_description[relation]} {text_label[1]}'
return self.precomputed_features[sentence]
def _convert_label(self, label):
"""
convert label to return
(first object text description, second object text description, relation, 0, 1/2)
where 0 encodes the position of first object, 1 represents the position of second objects if exists.
"""
if self.dataset == 'visual_genome' or self.dataset == 'blocks':
obj1 = self.objects[label[0]]
obj2 = self.objects[label[2]]
return self.precomputed_features[obj1], self.precomputed_features[obj2], label[1], 0, 1
else:
text_label = []
positions = []
for i in range(2):
shape, size, color, material, pos = label[i*5:i*5+5]
text_label.append(' '.join([self.sizes[size], self.colors[color],
self.materials[material], self.shapes[shape]]))
positions.append(pos)
relation = self.relations[label[-1]]
# single object
if relation == 'none':
return self.precomputed_features[text_label[0]], \
self.precomputed_features[''], \
label[-1], positions[0], positions[1]
else:
return self.precomputed_features[text_label[0]], \
self.precomputed_features[text_label[1]], \
label[-1], positions[0], positions[1]
def _convert_caption(self, label):
if self.dataset == 'blocks':
return f'{self.objects[label[0]]} {self.relations[label[1]]} {self.objects[label[2]]}'
else:
text_label = []
for i in range(2):
shape, size, color, material, pos = label[i * 5:i * 5 + 5]
obj = ' '.join([self.sizes[size], self.colors[color],
self.materials[material], self.shapes[shape]]).strip()
text_label.append(obj)
relation = self.relations[label[-1]]
if relation == 'none':
return text_label[0]
else:
return f'{text_label[0]} {self.label_description[relation]} {text_label[1]}'