72880bab74b2041a013f3bb2048ca50d8e54488c
[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 from multiprocessing import Pool, cpu_count
26
27 from torch import Tensor
28 from torch.autograd import Variable
29
30 import svrt
31
32 ######################################################################
33
34 def generate_one_batch(s):
35     svrt.seed(s)
36     target = torch.LongTensor(self.batch_size).bernoulli_(0.5)
37     input = svrt.generate_vignettes(problem_number, target)
38     input = input.float().view(input.size(0), 1, input.size(1), input.size(2))
39     if self.cuda:
40         input = input.cuda()
41         target = target.cuda()
42     return [ input, target ]
43
44 class VignetteSet:
45
46     def __init__(self, problem_number, nb_batches, batch_size, cuda = False):
47         self.cuda = cuda
48         self.batch_size = batch_size
49         self.problem_number = problem_number
50         self.nb_batches = nb_batches
51         self.nb_samples = self.nb_batches * self.batch_size
52
53         seed_list = torch.LongTensor(self.nb_batches).random_().tolist()
54
55         # self.data = []
56         # for b in range(0, self.nb_batches):
57             # self.data.append(generate_one_batch(seed_list[b]))
58
59         self.data = Pool(cpu_count()).map(generate_one_batch, seed_list)
60
61         acc = 0.0
62         acc_sq = 0.0
63         for b in range(0, self.nb_batches):
64             input = self.data[b][0]
65             acc += input.sum() / input.numel()
66             acc_sq += input.pow(2).sum() /  input.numel()
67
68         mean = acc / self.nb_batches
69         std = sqrt(acc_sq / self.nb_batches - mean * mean)
70         for b in range(0, self.nb_batches):
71             self.data[b][0].sub_(mean).div_(std)
72
73     def get_batch(self, b):
74         return self.data[b]
75
76 ######################################################################
77
78 class CompressedVignetteSet:
79     def __init__(self, problem_number, nb_batches, batch_size, cuda = False):
80         self.cuda = cuda
81         self.batch_size = batch_size
82         self.problem_number = problem_number
83         self.nb_batches = nb_batches
84         self.nb_samples = self.nb_batches * self.batch_size
85         self.targets = []
86         self.input_storages = []
87
88         acc = 0.0
89         acc_sq = 0.0
90         for b in range(0, self.nb_batches):
91             target = torch.LongTensor(self.batch_size).bernoulli_(0.5)
92             input = svrt.generate_vignettes(problem_number, target)
93             acc += input.float().sum() / input.numel()
94             acc_sq += input.float().pow(2).sum() /  input.numel()
95             self.targets.append(target)
96             self.input_storages.append(svrt.compress(input.storage()))
97
98         self.mean = acc / self.nb_batches
99         self.std = sqrt(acc_sq / self.nb_batches - self.mean * self.mean)
100
101     def get_batch(self, b):
102         input = torch.ByteTensor(svrt.uncompress(self.input_storages[b])).float()
103         input = input.view(self.batch_size, 1, 128, 128).sub_(self.mean).div_(self.std)
104         target = self.targets[b]
105
106         if self.cuda:
107             input = input.cuda()
108             target = target.cuda()
109
110         return input, target
111
112 ######################################################################