comparison data_generation/transformations/thick.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/thick.py@5e00ed18ae32
children
comparison
equal deleted inserted replaced
166:17ae5a1a4dd1 167:1f5937e9e530
1 #!/usr/bin/python
2 # coding: utf-8
3
4 '''
5 Simple implementation of random thickness deformation using morphological
6 operation of scipy.
7 Only one morphological operation applied (dilation or erosion), the kernel is random
8 out of a list of 12 symmetric kernels. (only 5 to be chosen for erosion because it can
9 hurt the recognizability of the charater and 12 for dilation).
10
11 Author: Xavier Glorot
12
13 '''
14
15 import scipy.ndimage.morphology
16 import numpy as N
17
18
19 class Thick():
20 def __init__(self,complexity = 1):
21 #---------- private attributes
22 self.__nx__ = 32 #xdim of the images
23 self.__ny__ = 32 #ydim of the images
24 self.__erodemax__ = 5 #nb of index max of erode structuring elements
25 self.__dilatemax__ = 9 #nb of index max of dilation structuring elements
26 self.__structuring_elements__ = [N.asarray([[1,1]]),N.asarray([[1],[1]]),\
27 N.asarray([[1,1],[1,1]]),N.asarray([[0,1,0],[1,1,1],[0,1,0]]),\
28 N.asarray([[1,1,1],[1,1,1]]),N.asarray([[1,1],[1,1],[1,1]]),\
29 N.asarray([[1,1,1],[1,1,1],[1,1,1]]),\
30 N.asarray([[1,1,1,1],[1,1,1,1],[1,1,1,1]]),\
31 N.asarray([[1,1,1],[1,1,1],[1,1,1],[1,1,1]]),\
32 N.asarray([[0,0,1,0,0],[0,1,1,1,0],[1,1,1,1,1],[0,1,1,1,0],[0,0,1,0,0]]),\
33 N.asarray([[1,1,1,1],[1,1,1,1]]),N.asarray([[1,1],[1,1],[1,1],[1,1]])]
34 #------------------------------------------------
35
36 #---------- generation parameters
37 self.regenerate_parameters(complexity)
38 #------------------------------------------------
39
40 def _get_current_parameters(self):
41 return [self.thick_param]
42
43 def get_settings_names(self):
44 return ['thick_param']
45
46 def regenerate_parameters(self, complexity):
47 self.erodenb = N.ceil(complexity * self.__erodemax__)
48 self.dilatenb = N.ceil(complexity * self.__dilatemax__)
49 self.Perode = self.erodenb / (self.dilatenb + self.erodenb + 1.0)
50 self.Pdilate = self.dilatenb / (self.dilatenb + self.erodenb + 1.0)
51 assert (self.Perode + self.Pdilate <= 1) & (self.Perode + self.Pdilate >= 0)
52 assert (complexity >= 0) & (complexity <= 1)
53 P = N.random.uniform()
54 if P>1-(self.Pdilate+self.Perode):
55 if P>1-(self.Pdilate+self.Perode)+self.Perode:
56 self.meth = 1
57 self.nb=N.random.randint(self.dilatenb)
58 else:
59 self.meth = -1
60 self.nb=N.random.randint(self.erodenb)
61 else:
62 self.meth = 0
63 self.nb = -1
64 self.thick_param = self.meth*self.nb
65 return self._get_current_parameters()
66
67 def transform_1_image(self,image): #the real transformation method
68 if self.meth!=0:
69 maxi = float(N.max(image))
70 mini = float(N.min(image))
71
72 imagenorm=image/maxi
73
74 if self.meth==1:
75 trans=scipy.ndimage.morphology.grey_dilation\
76 (imagenorm,size=self.__structuring_elements__[self.nb].shape,structure=self.__structuring_elements__[self.nb])
77 else:
78 trans=scipy.ndimage.morphology.grey_erosion\
79 (imagenorm,size=self.__structuring_elements__[self.nb].shape,structure=self.__structuring_elements__[self.nb])
80
81 #------renormalizing
82 maxit = N.max(trans)
83 minit = N.min(trans)
84 trans= N.asarray((trans - (minit+mini)) / (maxit - (minit+mini)) * maxi,dtype=image.dtype)
85 #--------
86 return trans
87 else:
88 return image
89
90 def transform_image(self,image): #handling different format
91 if image.shape == (self.__nx__,self.__ny__):
92 return self.transform_1_image(image)
93 if image.ndim == 3:
94 newimage = copy.copy(image)
95 for i in range(image.shape[0]):
96 newimage[i,:,:] = self.transform_1_image(image[i,:,:])
97 return newimage
98 if image.ndim == 2 and image.shape != (self.__nx__,self.__ny__):
99 newimage = N.reshape(image,(image.shape[0],self.__nx__,self.__ny__))
100 for i in range(image.shape[0]):
101 newimage[i,:,:] = self.transform_1_image(newimage[i,:,:])
102 return N.reshape(newimage,image.shape)
103 if image.ndim == 1:
104 newimage = N.reshape(image,(self.__nx__,self.__ny__))
105 newimage = self.transform_1_image(newimage)
106 return N.reshape(newimage,image.shape)
107 assert False #should never go there
108
109
110
111
112 #test on NIST (you need pylearn and access to NIST to do that)
113
114 if __name__ == '__main__':
115
116 from pylearn.io import filetensor as ft
117 import copy
118 import pygame
119 import time
120 datapath = '/data/lisa/data/nist/by_class/'
121 f = open(datapath+'digits/digits_train_data.ft')
122 d = ft.read(f)
123
124 pygame.surfarray.use_arraytype('numpy')
125
126 pygame.display.init()
127 screen = pygame.display.set_mode((8*4*32,8*32),0,8)
128 anglcolorpalette=[(x,x,x) for x in xrange(0,256)]
129 screen.set_palette(anglcolorpalette)
130
131 MyThick = Thick()
132
133 #debut=time.time()
134 #MyThick.transform_image(d)
135 #fin=time.time()
136 #print '------------------------------------------------'
137 #print d.shape[0],' images transformed in :', fin-debut, ' seconds'
138 #print '------------------------------------------------'
139 #print (fin-debut)/d.shape[0]*1000000,' microseconds per image'
140 #print '------------------------------------------------'
141 #print MyThick.get_settings_names()
142 #print MyThick._get_current_parameters()
143 #print MyThick.regenerate_parameters(0)
144 #print MyThick.regenerate_parameters(0.5)
145 #print MyThick.regenerate_parameters(1)
146 for i in range(10000):
147 a=d[i,:]
148 b=N.asarray(N.reshape(a,(32,32))).T
149
150 new=pygame.surfarray.make_surface(b)
151 new=pygame.transform.scale2x(new)
152 new=pygame.transform.scale2x(new)
153 new=pygame.transform.scale2x(new)
154 new.set_palette(anglcolorpalette)
155 screen.blit(new,(0,0))
156
157 #max dilation
158 MyThick.meth=1
159 MyThick.nb=MyThick.__dilatemax__
160 c=MyThick.transform_image(a)
161 b=N.asarray(N.reshape(c,(32,32))).T
162
163 new=pygame.surfarray.make_surface(b)
164 new=pygame.transform.scale2x(new)
165 new=pygame.transform.scale2x(new)
166 new=pygame.transform.scale2x(new)
167 new.set_palette(anglcolorpalette)
168 screen.blit(new,(8*32,0))
169
170 #max erosion
171 MyThick.meth=-1
172 MyThick.nb=MyThick.__erodemax__
173 c=MyThick.transform_image(a)
174 b=N.asarray(N.reshape(c,(32,32))).T
175
176 new=pygame.surfarray.make_surface(b)
177 new=pygame.transform.scale2x(new)
178 new=pygame.transform.scale2x(new)
179 new=pygame.transform.scale2x(new)
180 new.set_palette(anglcolorpalette)
181 screen.blit(new,(8*2*32,0))
182
183 #random
184 print MyThick.get_settings_names(), MyThick.regenerate_parameters(1)
185 c=MyThick.transform_image(a)
186 b=N.asarray(N.reshape(c,(32,32))).T
187
188 new=pygame.surfarray.make_surface(b)
189 new=pygame.transform.scale2x(new)
190 new=pygame.transform.scale2x(new)
191 new=pygame.transform.scale2x(new)
192 new.set_palette(anglcolorpalette)
193 screen.blit(new,(8*3*32,0))
194
195 pygame.display.update()
196 raw_input('Press Enter')
197
198 pygame.display.quit()