Added svrt.seed(long).
[pysvrt.git] / vignette_set.py
1
2 #  svrt is the ``Synthetic Visual Reasoning Test'', an image
3 #  generator for evaluating classification performance of machine
4 #  learning systems, humans and primates.
5 #
6 #  Copyright (c) 2017 Idiap Research Institute, http://www.idiap.ch/
7 #  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8 #
9 #  This file is part of svrt.
10 #
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.
14 #
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.
19 #
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/>.
22
23 import torch
24 from math import sqrt
25
26 from torch import Tensor
27 from torch.autograd import Variable
28
29 import svrt
30
31 ######################################################################
32
33 class VignetteSet:
34     def __init__(self, problem_number, nb_batches, batch_size, cuda = False):
35         self.cuda = cuda
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
40         self.targets = []
41         self.inputs = []
42
43         acc = 0.0
44         acc_sq = 0.0
45
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))
50             if self.cuda:
51                 input = input.cuda()
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)
57
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)
62
63     def get_batch(self, b):
64         return self.inputs[b], self.targets[b]
65
66 ######################################################################
67
68 class CompressedVignetteSet:
69     def __init__(self, problem_number, nb_batches, batch_size, cuda = False):
70         self.cuda = cuda
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
75         self.targets = []
76         self.input_storages = []
77
78         acc = 0.0
79         acc_sq = 0.0
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()))
87
88         self.mean = acc / self.nb_batches
89         self.std = sqrt(acc_sq / self.nb_batches - self.mean * self.mean)
90
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]
95
96         if self.cuda:
97             input = input.cuda()
98             target = target.cuda()
99
100         return input, target
101
102 ######################################################################