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 selector. If not, see <http://www.gnu.org/licenses/>.
26 from torch import Tensor
27 from torch.autograd import Variable
31 ######################################################################
34 def __init__(self, problem_number, nb_batches, batch_size, cuda = False):
36 self.batch_size = batch_size
37 self.problem_number = problem_number
38 self.nb_batches = nb_batches
39 self.nb_samples = self.nb_batches * self.batch_size
46 for b in range(0, self.nb_batches):
47 target = torch.LongTensor(self.batch_size).bernoulli_(0.5)
48 input = svrt.generate_vignettes(problem_number, target)
49 input = input.float().view(input.size(0), 1, input.size(1), input.size(2))
52 target = target.cuda()
53 acc += input.sum() / input.numel()
54 acc_sq += input.pow(2).sum() / input.numel()
55 self.targets.append(target)
56 self.inputs.append(input)
58 mean = acc / self.nb_batches
59 std = sqrt(acc_sq / self.nb_batches - mean * mean)
60 for b in range(0, self.nb_batches):
61 self.inputs[b].sub_(mean).div_(std)
63 def get_batch(self, b):
64 return self.inputs[b], self.targets[b]
66 ######################################################################
68 class CompressedVignetteSet:
69 def __init__(self, problem_number, nb_batches, batch_size, cuda = False):
71 self.batch_size = batch_size
72 self.problem_number = problem_number
73 self.nb_batches = nb_batches
74 self.nb_samples = self.nb_batches * self.batch_size
76 self.input_storages = []
80 for b in range(0, self.nb_batches):
81 target = torch.LongTensor(self.batch_size).bernoulli_(0.5)
82 input = svrt.generate_vignettes(problem_number, target)
83 acc += input.float().sum() / input.numel()
84 acc_sq += input.float().pow(2).sum() / input.numel()
85 self.targets.append(target)
86 self.input_storages.append(svrt.compress(input.storage()))
88 self.mean = acc / self.nb_batches
89 self.std = sqrt(acc_sq / self.nb_batches - self.mean * self.mean)
91 def get_batch(self, b):
92 input = torch.ByteTensor(svrt.uncompress(self.input_storages[b])).float()
93 input = input.view(self.batch_size, 1, 128, 128).sub_(self.mean).div_(self.std)
94 target = self.targets[b]
98 target = target.cuda()
102 ######################################################################