comparison data_generation/transformations/PermutPixel.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/PermutPixel.py@7640cb31cf1f
children
comparison
equal deleted inserted replaced
166:17ae5a1a4dd1 167:1f5937e9e530
1 #!/usr/bin/python
2 # coding: utf-8
3
4 '''
5 Un echange de pixels est effectue entre certain pixels choisit aleatoirement
6 et un de ses 4 voisins, tout aussi choisi aleatoirement.
7
8 Le nombre de pixels permutes est definit pas complexity*1024
9
10 Il y a proba 20% d'effectuer le bruitage
11
12 Sylvain Pannetier Lebeuf dans le cadre de IFT6266, hiver 2010
13
14 '''
15
16 import numpy
17 import random
18
19 class PermutPixel():
20
21 def __init__(self,seed=7152):
22 self.nombre=10 #Le nombre de pixels a permuter
23 self.proportion=0.3
24 self.effectuer=1 #1=on effectue, 0=rien faire
25 self.seed=seed
26
27 #Les deux generateurs sont de types differents, avoir la meme seed n'a pas d'influence
28 #numpy.random.seed(self.seed)
29 #random.seed(self.seed)
30
31 def get_seed(self):
32 return self.seed
33
34 def get_settings_names(self):
35 return ['effectuer']
36
37 def get_settings_names_determined_by_complexity(self,complexity):
38 return ['nombre']
39
40 def regenerate_parameters(self, complexity):
41 self.proportion=float(complexity)/3
42 self.nombre=int(256*self.proportion)*4 #Par multiple de 4 (256=1024/4)
43 self.echantillon=random.sample(xrange(0,1024),self.nombre) #Les pixels qui seront permutes
44 self.effectuer =numpy.random.binomial(1,0.2) ##### On a 20% de faire un bruit #####
45 return self._get_current_parameters()
46
47 def _get_current_parameters(self):
48 return [self.effectuer]
49
50 def get_parameters_determined_by_complexity(self, complexity):
51 return [int(complexity*256)*4]
52
53 def transform_image(self, image):
54 if self.effectuer==0:
55 return image
56
57 image=image.reshape(1024,1)
58 temp=0 #variable temporaire
59
60 for i in xrange(0,self.nombre,4): #Par bonds de 4
61 #gauche
62 if self.echantillon[i] > 0:
63 temp=image[self.echantillon[i]-1]
64 image[self.echantillon[i]-1]=image[self.echantillon[i]]
65 image[self.echantillon[i]]=temp
66 #droite
67 if self.echantillon[i+1] < 1023:
68 temp=image[self.echantillon[i+1]+1]
69 image[self.echantillon[i+1]+1]=image[self.echantillon[i+1]]
70 image[self.echantillon[i+1]]=temp
71 #haut
72 if self.echantillon[i+2] > 31:
73 temp=image[self.echantillon[i+2]-32]
74 image[self.echantillon[i+2]-32]=image[self.echantillon[i+2]]
75 image[self.echantillon[i+2]]=temp
76 #bas
77 if self.echantillon[i+3] < 992:
78 temp=image[self.echantillon[i+3]+32]
79 image[self.echantillon[i+3]+32]=image[self.echantillon[i+3]]
80 image[self.echantillon[i+3]]=temp
81
82
83 return image.reshape((32,32))
84
85
86 #---TESTS---
87
88 def _load_image():
89 f = open('/home/sylvain/Dropbox/Msc/IFT6266/donnees/lower_test_data.ft') #Le jeu de donnees est en local.
90 d = ft.read(f)
91 w=numpy.asarray(d[random.randint(0,100)])
92 return (w/255.0).astype('float')
93
94 def _test(complexite):
95 img=_load_image()
96 transfo = PermutPixel()
97 pylab.imshow(img.reshape((32,32)))
98 pylab.show()
99 print transfo.get_settings_names()
100 print transfo.regenerate_parameters(complexite)
101
102 img_trans=transfo.transform_image(img)
103
104 pylab.imshow(img_trans.reshape((32,32)))
105 pylab.show()
106
107
108 if __name__ == '__main__':
109 from pylearn.io import filetensor as ft
110 import pylab
111 for i in xrange(0,5):
112 _test(0.5)
113
114