Mercurial > ift6266
comparison transformations/DistorsionGauss.py @ 47:3bc75139654a
Ajout d'un petit bruit gaussien sur chaque pixel de l'image.
author | SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca> |
---|---|
date | Thu, 04 Feb 2010 10:32:07 -0500 |
parents | |
children | aee278ebc827 |
comparison
equal
deleted
inserted
replaced
46:48a21d19b8eb | 47:3bc75139654a |
---|---|
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 Sylvain Pannetier Lebeuf dans le cadre de IFT6266, hiver 2010 | |
9 | |
10 ''' | |
11 | |
12 import numpy | |
13 import random | |
14 | |
15 class DistorsionGauss(): | |
16 | |
17 def __init__(self): | |
18 self.ecart_type=0.1 #L'ecart type de la gaussienne | |
19 | |
20 def get_settings_names(self): | |
21 return ['ecart_type'] | |
22 | |
23 def regenerate_parameters(self, complexity): | |
24 self.ecart_type=float(complexity)/10 | |
25 return self._get_current_parameters() | |
26 | |
27 def _get_current_parameters(self): | |
28 return [] | |
29 | |
30 def get_parameters_determined_by_complexity(self, complexity): | |
31 return [float(complexity)/10] | |
32 | |
33 def transform_image(self, image): | |
34 image=image.reshape(1024,1) | |
35 aleatoire=numpy.zeros((1024,1)).astype('float32') | |
36 for i in xrange(0,1024): | |
37 aleatoire[i]=float(random.gauss(0,self.ecart_type)) | |
38 image=image+aleatoire | |
39 | |
40 #Ramener tout entre 0 et 1 | |
41 if numpy.min(image) < 0: | |
42 image-=numpy.min(image) | |
43 if numpy.max(image) > 1: | |
44 image/=numpy.max(image) | |
45 | |
46 return image.reshape(32,32) | |
47 | |
48 | |
49 #---TESTS--- | |
50 | |
51 def _load_image(): | |
52 f = open('/home/sylvain/Dropbox/Msc/IFT6266/donnees/lower_test_data.ft') #Le jeu de donnees est en local. | |
53 d = ft.read(f) | |
54 w=numpy.asarray(d[random.randint(0,100)]) | |
55 return (w/255.0).astype('float') | |
56 | |
57 def _test(complexite): | |
58 img=_load_image() | |
59 transfo = DistorsionGauss() | |
60 pylab.imshow(img.reshape((32,32))) | |
61 pylab.show() | |
62 print transfo.get_settings_names() | |
63 print transfo.regenerate_parameters(complexite) | |
64 | |
65 img_trans=transfo.transform_image(img) | |
66 | |
67 pylab.imshow(img_trans.reshape((32,32))) | |
68 pylab.show() | |
69 | |
70 | |
71 if __name__ == '__main__': | |
72 from pylearn.io import filetensor as ft | |
73 import pylab | |
74 for i in xrange(0,5): | |
75 _test(1) | |
76 | |
77 |