Mercurial > ift6266
annotate data_generation/transformations/thick.py @ 266:1e4e60ddadb1
Merge. Ah, et dans le dernier commit, j'avais oublié de mentionner que j'ai ajouté du code pour gérer l'isolation de différents clones pour rouler des expériences et modifier le code en même temps.
author | fsavard |
---|---|
date | Fri, 19 Mar 2010 10:56:16 -0400 |
parents | 1f5937e9e530 |
children |
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 |
14
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
8 out of a list of 12 symmetric kernels. (only 5 to be chosen for erosion because it can |
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
9 hurt the recognizability of the charater and 12 for dilation). |
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
|
10 |
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 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
|
12 |
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 |
14
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
22 self.__nx__ = 32 #xdim of the images |
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
23 self.__ny__ = 32 #ydim of the images |
120
5e00ed18ae32
changed some transformation paramters to better fit with visualization
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
26
diff
changeset
|
24 self.__erodemax__ = 5 #nb of index max of erode structuring elements |
23
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
25 self.__dilatemax__ = 9 #nb of index max of dilation structuring elements |
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 |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
37 self.regenerate_parameters(complexity) |
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
|
38 #------------------------------------------------ |
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 |
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 def _get_current_parameters(self): |
19
db10ee2a07fb
merged 2 thick parameter in one
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
17
diff
changeset
|
41 return [self.thick_param] |
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
|
42 |
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 def get_settings_names(self): |
19
db10ee2a07fb
merged 2 thick parameter in one
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
17
diff
changeset
|
44 return ['thick_param'] |
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
|
45 |
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 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
|
47 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
|
48 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
|
49 self.Perode = self.erodenb / (self.dilatenb + self.erodenb + 1.0) |
23
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
50 self.Pdilate = self.dilatenb / (self.dilatenb + self.erodenb + 1.0) |
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
|
51 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
|
52 assert (complexity >= 0) & (complexity <= 1) |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
53 P = N.random.uniform() |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
54 if P>1-(self.Pdilate+self.Perode): |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
55 if P>1-(self.Pdilate+self.Perode)+self.Perode: |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
56 self.meth = 1 |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
57 self.nb=N.random.randint(self.dilatenb) |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
58 else: |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
59 self.meth = -1 |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
60 self.nb=N.random.randint(self.erodenb) |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
61 else: |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
62 self.meth = 0 |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
63 self.nb = -1 |
19
db10ee2a07fb
merged 2 thick parameter in one
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
17
diff
changeset
|
64 self.thick_param = self.meth*self.nb |
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
|
65 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
|
66 |
14
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
67 def transform_1_image(self,image): #the real transformation method |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
68 if self.meth!=0: |
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
|
69 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
|
70 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
|
71 |
26
47e7202d4f19
Array format handling bug fix for thick.py
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
23
diff
changeset
|
72 imagenorm=image/maxi |
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
|
73 |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
74 if self.meth==1: |
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
|
75 trans=scipy.ndimage.morphology.grey_dilation\ |
26
47e7202d4f19
Array format handling bug fix for thick.py
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
23
diff
changeset
|
76 (imagenorm,size=self.__structuring_elements__[self.nb].shape,structure=self.__structuring_elements__[self.nb]) |
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
|
77 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
|
78 trans=scipy.ndimage.morphology.grey_erosion\ |
26
47e7202d4f19
Array format handling bug fix for thick.py
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
23
diff
changeset
|
79 (imagenorm,size=self.__structuring_elements__[self.nb].shape,structure=self.__structuring_elements__[self.nb]) |
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
|
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) |
17
c91d9f70206d
Changed a call to numpy without having imported it as numpy
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
14
diff
changeset
|
84 trans= N.asarray((trans - (minit+mini)) / (maxit - (minit+mini)) * maxi,dtype=image.dtype) |
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
|
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 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
|
87 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
|
88 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
|
89 |
14
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
90 def transform_image(self,image): #handling different format |
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
91 if image.shape == (self.__nx__,self.__ny__): |
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
92 return self.transform_1_image(image) |
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
93 if image.ndim == 3: |
26
47e7202d4f19
Array format handling bug fix for thick.py
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
23
diff
changeset
|
94 newimage = copy.copy(image) |
14
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
95 for i in range(image.shape[0]): |
26
47e7202d4f19
Array format handling bug fix for thick.py
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
23
diff
changeset
|
96 newimage[i,:,:] = self.transform_1_image(image[i,:,:]) |
47e7202d4f19
Array format handling bug fix for thick.py
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
23
diff
changeset
|
97 return newimage |
14
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
98 if image.ndim == 2 and image.shape != (self.__nx__,self.__ny__): |
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
|
99 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
|
100 for i in range(image.shape[0]): |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
101 newimage[i,:,:] = self.transform_1_image(newimage[i,:,:]) |
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
|
102 return N.reshape(newimage,image.shape) |
14
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
103 if image.ndim == 1: |
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
|
104 newimage = N.reshape(image,(self.__nx__,self.__ny__)) |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
105 newimage = self.transform_1_image(newimage) |
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
|
106 return N.reshape(newimage,image.shape) |
14
ebf61603489b
Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
13
diff
changeset
|
107 assert False #should never go there |
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
|
108 |
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 |
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 |
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 |
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 #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
|
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 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
|
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 from pylearn.io import filetensor as ft |
17
c91d9f70206d
Changed a call to numpy without having imported it as numpy
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
14
diff
changeset
|
117 import copy |
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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 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
|
123 |
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 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
|
125 |
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 pygame.display.init() |
23
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
127 screen = pygame.display.set_mode((8*4*32,8*32),0,8) |
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
|
128 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
|
129 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
|
130 |
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 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
|
132 |
23
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
133 #debut=time.time() |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
134 #MyThick.transform_image(d) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
135 #fin=time.time() |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
136 #print '------------------------------------------------' |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
137 #print d.shape[0],' images transformed in :', fin-debut, ' seconds' |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
138 #print '------------------------------------------------' |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
139 #print (fin-debut)/d.shape[0]*1000000,' microseconds per image' |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
140 #print '------------------------------------------------' |
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
|
141 #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
|
142 #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
|
143 #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
|
144 #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
|
145 #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
|
146 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
|
147 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
|
148 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
|
149 |
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 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 |
23
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
157 #max dilation |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
158 MyThick.meth=1 |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
159 MyThick.nb=MyThick.__dilatemax__ |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
160 c=MyThick.transform_image(a) |
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
|
161 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
|
162 |
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 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
|
164 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
|
165 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
|
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.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
|
168 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
|
169 |
23
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
170 #max erosion |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
171 MyThick.meth=-1 |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
172 MyThick.nb=MyThick.__erodemax__ |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
173 c=MyThick.transform_image(a) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
174 b=N.asarray(N.reshape(c,(32,32))).T |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
175 |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
176 new=pygame.surfarray.make_surface(b) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
177 new=pygame.transform.scale2x(new) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
178 new=pygame.transform.scale2x(new) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
179 new=pygame.transform.scale2x(new) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
180 new.set_palette(anglcolorpalette) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
181 screen.blit(new,(8*2*32,0)) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
182 |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
183 #random |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
184 print MyThick.get_settings_names(), MyThick.regenerate_parameters(1) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
185 c=MyThick.transform_image(a) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
186 b=N.asarray(N.reshape(c,(32,32))).T |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
187 |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
188 new=pygame.surfarray.make_surface(b) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
189 new=pygame.transform.scale2x(new) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
190 new=pygame.transform.scale2x(new) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
191 new=pygame.transform.scale2x(new) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
192 new.set_palette(anglcolorpalette) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
193 screen.blit(new,(8*3*32,0)) |
442789c94b27
Changed thick maximum erosion and dilation transformations
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
19
diff
changeset
|
194 |
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
|
195 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
|
196 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
|
197 |
17
c91d9f70206d
Changed a call to numpy without having imported it as numpy
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
14
diff
changeset
|
198 pygame.display.quit() |