Mercurial > ift6266
comparison data_generation/transformations/DistorsionGauss.py @ 167:1f5937e9e530
More moves - transformations into data_generation, added "deep" folder
author | Dumitru Erhan <dumitru.erhan@gmail.com> |
---|---|
date | Fri, 26 Feb 2010 14:15:38 -0500 |
parents | transformations/DistorsionGauss.py@7640cb31cf1f |
children |
comparison
equal
deleted
inserted
replaced
166:17ae5a1a4dd1 | 167:1f5937e9e530 |
---|---|
1 #!/usr/bin/python | |
2 # coding: utf-8 | |
3 | |
4 ''' | |
5 Ajout d'une composante aleatoire dans chaque pixel de l'image. | |
6 C'est une distorsion gaussienne de moyenne 0 et d'écart type complexity/10 | |
7 | |
8 Il y a 30% d'effectuer le bruitage | |
9 | |
10 Sylvain Pannetier Lebeuf dans le cadre de IFT6266, hiver 2010 | |
11 | |
12 ''' | |
13 | |
14 import numpy | |
15 import random | |
16 | |
17 class DistorsionGauss(): | |
18 | |
19 def __init__(self,seed=3459): | |
20 self.ecart_type=0.1 #L'ecart type de la gaussienne | |
21 self.effectuer=1 #1=on effectue et 0=rien faire | |
22 self.seed=seed | |
23 | |
24 #Les deux generateurs sont de types differents, avoir la meme seed n'a pas d'influence | |
25 #numpy.random.seed(self.seed) | |
26 #random.seed(self.seed) | |
27 | |
28 def get_settings_names(self): | |
29 return ['effectuer'] | |
30 | |
31 def get_seed(self): | |
32 return self.seed | |
33 | |
34 def get_settings_names_determined_by_complexity(self,complexity): | |
35 return ['ecart_type'] | |
36 | |
37 def regenerate_parameters(self, complexity): | |
38 self.ecart_type=float(complexity)/10 | |
39 self.effectuer =numpy.random.binomial(1,0.3) ##### On a 30% de faire un bruit ##### | |
40 return self._get_current_parameters() | |
41 | |
42 def _get_current_parameters(self): | |
43 return [self.effectuer] | |
44 | |
45 def get_parameters_determined_by_complexity(self,complexity): | |
46 return [float(complexity)/10] | |
47 | |
48 def transform_image(self, image): | |
49 if self.effectuer == 0: | |
50 return image | |
51 | |
52 image=image.reshape(1024,1) | |
53 aleatoire=numpy.zeros((1024,1)).astype('float32') | |
54 for i in xrange(0,1024): | |
55 aleatoire[i]=float(random.gauss(0,self.ecart_type)) | |
56 image=image+aleatoire | |
57 | |
58 | |
59 #Ramener tout entre 0 et 1. Ancienne facon de normaliser. | |
60 #Resultats moins interessant je trouve. | |
61 ## if numpy.min(image) < 0: | |
62 ## image-=numpy.min(image) | |
63 ## if numpy.max(image) > 1: | |
64 ## image/=numpy.max(image) | |
65 | |
66 for i in xrange(0,1024): | |
67 image[i]=min(1,max(0,image[i])) | |
68 | |
69 return image.reshape(32,32) | |
70 | |
71 | |
72 #---TESTS--- | |
73 | |
74 def _load_image(): | |
75 f = open('/home/sylvain/Dropbox/Msc/IFT6266/donnees/lower_test_data.ft') #Le jeu de donnees est en local. | |
76 d = ft.read(f) | |
77 w=numpy.asarray(d[random.randint(0,100)]) | |
78 return (w/255.0).astype('float') | |
79 | |
80 def _test(complexite): | |
81 img=_load_image() | |
82 transfo = DistorsionGauss() | |
83 pylab.imshow(img.reshape((32,32))) | |
84 pylab.show() | |
85 print transfo.get_settings_names() | |
86 print transfo.regenerate_parameters(complexite) | |
87 | |
88 img_trans=transfo.transform_image(img) | |
89 | |
90 pylab.imshow(img_trans.reshape((32,32))) | |
91 pylab.show() | |
92 | |
93 | |
94 if __name__ == '__main__': | |
95 from pylearn.io import filetensor as ft | |
96 import pylab | |
97 for i in xrange(0,5): | |
98 _test(0.5) | |
99 | |
100 |