-
Notifications
You must be signed in to change notification settings - Fork 8
/
soc_generator.py
168 lines (141 loc) · 4.83 KB
/
soc_generator.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
# https://github.com/kimhc6028/relational-networks/blob/master/sort_of_clevr_generator.py
import cv2
import os
import numpy as np
import random
#import cPickle as pickle
import pickle
import argparse
parser = argparse.ArgumentParser("train")
parser.add_argument('--out', type=str, default='./data')
config, _ = parser.parse_known_args()
train_size = 9000
test_size = 1000
img_size = 75
size = 5
question_size = 11 ##6 for one-hot vector of color, 2 for question type, 3 for question subtype
"""Answer : [yes, no, rectangle, circle, r, g, b, o, k, y]"""
nb_questions = 10
dirs = config.out
colors = [
(0,0,255),##r
(0,255,0),##g
(255,0,0),##b
(0,156,255),##o
(128,128,128),##k
(0,255,255)##y
]
try:
os.makedirs(dirs)
except:
print('directory {} already exists'.format(dirs))
def center_generate(objects):
while True:
pas = True
center = np.random.randint(0+size, img_size - size, 2)
if len(objects) > 0:
for name,c,shape in objects:
if ((center - c) ** 2).sum() < ((size * 2) ** 2):
pas = False
if pas:
return center
def build_dataset():
objects = []
img = np.ones((img_size,img_size,3)) * 255
for color_id,color in enumerate(colors):
center = center_generate(objects)
if random.random()<0.5:
start = (center[0]-size, center[1]-size)
end = (center[0]+size, center[1]+size)
cv2.rectangle(img, start, end, color, -1)
objects.append((color_id,center,'r'))
else:
center_ = (center[0], center[1])
cv2.circle(img, center_, size, color, -1)
objects.append((color_id,center,'c'))
rel_questions = []
norel_questions = []
rel_answers = []
norel_answers = []
"""Non-relational questions"""
for _ in range(nb_questions):
question = np.zeros((question_size))
color = random.randint(0,5)
question[color] = 1
question[6] = 1
subtype = random.randint(0,2)
question[subtype+8] = 1
norel_questions.append(question)
"""Answer : [yes, no, rectangle, circle, r, g, b, o, k, y]"""
if subtype == 0:
"""query shape->rectangle/circle"""
if objects[color][2] == 'r':
answer = 2
else:
answer = 3
elif subtype == 1:
"""query horizontal position->yes/no"""
if objects[color][1][0] < img_size / 2:
answer = 0
else:
answer = 1
elif subtype == 2:
"""query vertical position->yes/no"""
if objects[color][1][1] < img_size / 2:
answer = 0
else:
answer = 1
norel_answers.append(answer)
"""Relational questions"""
for i in range(nb_questions):
question = np.zeros((question_size))
color = random.randint(0,5)
question[color] = 1
question[7] = 1
subtype = random.randint(0,2)
question[subtype+8] = 1
rel_questions.append(question)
if subtype == 0:
"""closest-to->rectangle/circle"""
my_obj = objects[color][1]
dist_list = [((my_obj - obj[1]) ** 2).sum() for obj in objects]
dist_list[dist_list.index(0)] = 99999
closest = dist_list.index(min(dist_list))
if objects[closest][2] == 'r':
answer = 2
else:
answer = 3
elif subtype == 1:
"""furthest-from->rectangle/circle"""
my_obj = objects[color][1]
dist_list = [((my_obj - obj[1]) ** 2).sum() for obj in objects]
furthest = dist_list.index(max(dist_list))
if objects[furthest][2] == 'r':
answer = 2
else:
answer = 3
elif subtype == 2:
"""count->1~6"""
my_obj = objects[color][2]
count = -1
for obj in objects:
if obj[2] == my_obj:
count +=1
answer = count+4
rel_answers.append(answer)
relations = (rel_questions, rel_answers)
norelations = (norel_questions, norel_answers)
img = img/255.
dataset = (img, relations, norelations)
return dataset
print('building test datasets...')
test_datasets = [build_dataset() for _ in range(test_size)]
print('building train datasets...')
train_datasets = [build_dataset() for _ in range(train_size)]
#img_count = 0
#cv2.imwrite(os.path.join(dirs,'{}.png'.format(img_count)), cv2.resize(train_datasets[0][0]*255, (512,512)))
print('saving datasets...')
filename = os.path.join(dirs,'sort-of-clevr.pickle')
with open(filename, 'wb') as f:
pickle.dump((train_datasets, test_datasets), f)
print('datasets saved at {}'.format(filename))