-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.py
executable file
·208 lines (177 loc) · 7.77 KB
/
generate.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
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
# svrt is the ``Synthetic Visual Reasoning Test'', an image
# generator for evaluating classification performance of machine
# learning systems, humans and primates.
#
# Copyright (c) 2017 Idiap Research Institute, http://www.idiap.ch/
# Written by Francois Fleuret <[email protected]>
#
# This file is part of svrt.
#
# svrt is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# svrt is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with svrt. If not, see <http://www.gnu.org/licenses/>.
import time
import argparse
import torch
import torchvision, os
from torch import optim
from torch import FloatTensor as Tensor
from torch.autograd import Variable
from torch import nn
from torch.nn import functional as fn
from torchvision import datasets, transforms, utils
import h5py
import svrt
import svrt.parse
import svrt.utils
######################################################################
# Parsing arguments
######################################################################
parser = argparse.ArgumentParser(
description='SVRT sample generator.',
formatter_class = argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('--nb_samples',
type = int,
default = 1000,
help='How many samples to generate in total')
parser.add_argument('--batch_size',
type = int,
default = 1000,
help='How many samples to generate at once')
parser.add_argument('--problem',
type = int,
default = 1,
help='Problem to generate samples from')
parser.add_argument('--data_dir',
type = str,
default = '',
help='Where to generate the samples')
parser.add_argument('--parsed_dir',
type = str,
help='Where to put parsed output strings')
parser.add_argument('--parsed_dir_classic',
type = str,
help='Where to put classic-style parsed output strings')
parser.add_argument('--symb_h5_raw_dir',
type = str,
help='Where to put HDF5 files containing raw tensors of'
' symbolic representations of stimuli')
parser.add_argument('--symb_h5_obf_dir',
type = str,
help='Where to put HDF5 files containing tensors of'
' obfuscated symbolic representations of stimuli')
######################################################################
args = parser.parse_args()
if not os.path.isdir(args.data_dir):
os.makedirs(args.data_dir)
if not os.path.isdir(args.data_dir):
# FileNotFoundError does not exist in Python 2, so this is a work-around
# where we define it as IOError.
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
raise FileNotFoundError('Cannot find ' + args.data_dir)
for class_label in [0, 1]:
dirname = 'problem_{:02d}/class_{:d}'.format(args.problem, class_label)
dirname = os.path.join(args.data_dir, dirname)
if not os.path.isdir(dirname):
os.makedirs(dirname)
if args.symb_h5_raw_dir:
if not os.path.isdir(args.symb_h5_raw_dir):
os.makedirs(args.symb_h5_raw_dir)
fname = os.path.join(args.symb_h5_raw_dir,
'problem_{:02d}.h5'.format(args.problem))
hf_raw = h5py.File(fname, 'w')
if args.symb_h5_obf_dir:
if not os.path.isdir(args.symb_h5_obf_dir):
os.makedirs(args.symb_h5_obf_dir)
fname = os.path.join(args.symb_h5_obf_dir,
'problem_{:02d}.h5'.format(args.problem))
hf_obf = h5py.File(fname, 'w')
for n in range(0, args.nb_samples, args.batch_size):
print('{}/{}'.format(n, args.nb_samples))
labels = torch.LongTensor(min(args.batch_size, args.nb_samples - n)).zero_()
labels.narrow(0, 0, labels.size(0)//2).fill_(1)
x, nb_shapes, shape_list, intershape_distance, is_containing = \
svrt.generate_vignettes_full(args.problem, labels)
# Save to H5
if args.symb_h5_raw_dir:
hf = hf_raw
for class_label in [0, 1]:
vg_is_class_member = labels == class_label
hf.create_dataset(
'class_{:d}/nb_shapes/{:d}'.format(class_label, n),
data=nb_shapes[vg_is_class_member])
hf.create_dataset(
'class_{:d}/shape_list/{:d}'.format(class_label, n),
data=shape_list[vg_is_class_member, :, :])
hf.create_dataset(
'class_{:d}/intershape_distance/{:d}'.format(class_label, n),
data=intershape_distance[vg_is_class_member, :, :])
hf.create_dataset(
'class_{:d}/is_containing/{:d}'.format(class_label, n),
data=is_containing[vg_is_class_member, :, :])
# Obfuscate shape construction order, and rotation/reflection state
nb_shapes, shape_list, intershape_distance, is_containing = \
svrt.utils.obfuscate_shape_construction(
nb_shapes, shape_list, intershape_distance, is_containing)
# Save to H5
if args.symb_h5_raw_dir:
hf = hf_obf
for class_label in [0, 1]:
vg_is_class_member = labels == class_label
hf.create_dataset(
'class_{:d}/nb_shapes/{:d}'.format(class_label, n),
data=nb_shapes[vg_is_class_member])
hf.create_dataset(
'class_{:d}/shape_list/{:d}'.format(class_label, n),
data=shape_list[vg_is_class_member, :, :])
hf.create_dataset(
'class_{:d}/intershape_distance/{:d}'.format(class_label, n),
data=intershape_distance[vg_is_class_member, :, :])
hf.create_dataset(
'class_{:d}/is_containing/{:d}'.format(class_label, n),
data=is_containing[vg_is_class_member, :, :])
x = x.float()
x.sub_(128).div_(64)
for k in range(x.size(0)):
subdir_fname = 'problem_{:02d}/class_{:d}/img_{:07d}.png'.format(
args.problem, labels[k], k + n)
filename = os.path.join(args.data_dir, subdir_fname)
torchvision.utils.save_image(x[k].view(1, x.size(1), x.size(2)), filename)
# Output parsed strings in classic sasquatch style
if args.parsed_dir_classic:
parsed_str = svrt.parse.parse_vignette_to_string_classic(
nb_shapes[k], shape_list[k], intershape_distance[k], is_containing[k])
fname = os.path.join(args.parsed_dir_classic,
subdir_fname[:-4] + '.txt')
if not os.path.isdir(os.path.split(fname)[0]):
os.makedirs(os.path.split(fname)[0])
with open(fname, "w") as f:
f.write(parsed_str)
# Output parsed strings in updated style, with rotation and reflection
if args.parsed_dir:
parsed_str = svrt.parse.parse_vignette_to_string(
nb_shapes[k], shape_list[k], intershape_distance[k], is_containing[k])
fname = os.path.join(args.parsed_dir,
subdir_fname[:-4] + '.txt')
if not os.path.isdir(os.path.split(fname)[0]):
os.makedirs(os.path.split(fname)[0])
with open(fname, "w") as f:
f.write(parsed_str)
# Close open files
if args.symb_h5_raw_dir:
hf_raw.close()
if args.symb_h5_obf_dir:
hf_obf.close()