2 # svrt is the ``Synthetic Visual Reasoning Test'', an image
3 # generator for evaluating classification performance of machine
4 # learning systems, humans and primates.
6 # Copyright (c) 2017 Idiap Research Institute, http://www.idiap.ch/
7 # Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 # This file is part of svrt.
11 # svrt is free software: you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License version 3 as
13 # published by the Free Software Foundation.
15 # svrt is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with svrt. If not, see <http://www.gnu.org/licenses/>.
25 from torch import multiprocessing
27 from torch import Tensor
28 from torch.autograd import Variable
32 ######################################################################
34 def generate_one_batch(s):
35 problem_number, batch_size, random_seed = s
36 svrt.seed(random_seed)
37 target = torch.LongTensor(batch_size).bernoulli_(0.5)
38 input = svrt.generate_vignettes(problem_number, target)
39 input = input.float().view(input.size(0), 1, input.size(1), input.size(2))
40 return [ input, target ]
44 def __init__(self, problem_number, nb_samples, batch_size, cuda = False):
46 if nb_samples%batch_size > 0:
47 print('nb_samples must be a multiple of batch_size')
51 self.batch_size = batch_size
52 self.problem_number = problem_number
53 self.nb_batches = nb_samples // batch_size
54 self.nb_samples = self.nb_batches * self.batch_size
56 seeds = torch.LongTensor(self.nb_batches).random_()
58 for b in range(0, self.nb_batches):
59 mp_args.append( [ problem_number, batch_size, seeds[b] ])
62 for b in range(0, self.nb_batches):
63 self.data.append(generate_one_batch(mp_args[b]))
65 # Weird thing going on with the multi-processing, waiting for more info
67 # pool = multiprocessing.Pool(multiprocessing.cpu_count())
68 # self.data = pool.map(generate_one_batch, mp_args)
72 for b in range(0, self.nb_batches):
73 input = self.data[b][0]
74 acc += input.sum() / input.numel()
75 acc_sq += input.pow(2).sum() / input.numel()
77 mean = acc / self.nb_batches
78 std = sqrt(acc_sq / self.nb_batches - mean * mean)
79 for b in range(0, self.nb_batches):
80 self.data[b][0].sub_(mean).div_(std)
82 self.data[b][0] = self.data[b][0].cuda()
83 self.data[b][1] = self.data[b][1].cuda()
85 def get_batch(self, b):
88 ######################################################################
90 class CompressedVignetteSet:
91 def __init__(self, problem_number, nb_samples, batch_size, cuda = False):
93 if nb_samples%batch_size > 0:
94 print('nb_samples must be a multiple of batch_size')
98 self.batch_size = batch_size
99 self.problem_number = problem_number
100 self.nb_batches = nb_samples // batch_size
101 self.nb_samples = self.nb_batches * self.batch_size
103 self.input_storages = []
107 for b in range(0, self.nb_batches):
108 target = torch.LongTensor(self.batch_size).bernoulli_(0.5)
109 input = svrt.generate_vignettes(problem_number, target)
110 acc += input.float().sum() / input.numel()
111 acc_sq += input.float().pow(2).sum() / input.numel()
112 self.targets.append(target)
113 self.input_storages.append(svrt.compress(input.storage()))
115 self.mean = acc / self.nb_batches
116 self.std = sqrt(acc_sq / self.nb_batches - self.mean * self.mean)
118 def get_batch(self, b):
119 input = torch.ByteTensor(svrt.uncompress(self.input_storages[b])).float()
120 input = input.view(self.batch_size, 1, 128, 128).sub_(self.mean).div_(self.std)
121 target = self.targets[b]
125 target = target.cuda()
129 ######################################################################