annotate transformations/thick.py @ 11:dbc806d025a2

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