Mercurial > ift6266
annotate transformations/thick.py @ 13:a25474d4d34f
Changed parameter generation in order to fit with the new specificities
author | Xavier Glorot <glorotxa@iro.umontreal.ca> |
---|---|
date | Thu, 28 Jan 2010 11:28:46 -0500 |
parents | dbc806d025a2 |
children | ebf61603489b |
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 |
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): |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
41 return [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
|
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): |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
44 return ['meth','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
|
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) |
dbc806d025a2
Added 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 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
|
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 |
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
|
64 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
|
65 |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
66 def transform_1_image(self,image): |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 |
dbc806d025a2
Added 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 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
|
72 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
|
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\ |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
76 (image,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\ |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
79 (image,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) |
dbc806d025a2
Added 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 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 |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
90 def transform_image(self,image): |
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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 |
dbc806d025a2
Added 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 |
dbc806d025a2
Added 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 |
dbc806d025a2
Added 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 |
dbc806d025a2
Added 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 #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
|
105 |
dbc806d025a2
Added 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 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
|
107 |
dbc806d025a2
Added 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 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 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
|
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 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
|
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 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
|
119 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
|
120 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
|
121 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
|
122 |
dbc806d025a2
Added 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 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
|
124 |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
125 debut=time.time() |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
126 MyThick.transform_image(d) |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
127 fin=time.time() |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
128 print '------------------------------------------------' |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
129 print d.shape[0],' images transformed in :', fin-debut, ' seconds' |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
130 print '------------------------------------------------' |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
131 print (fin-debut)/d.shape[0]*1000000,' microseconds per image' |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
132 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
|
133 #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
|
134 #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
|
135 #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
|
136 #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
|
137 #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
|
138 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
|
139 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
|
140 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
|
141 |
dbc806d025a2
Added 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 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 |
13
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
149 print MyThick.get_settings_names(), MyThick.regenerate_parameters(1) |
a25474d4d34f
Changed parameter generation in order to fit with the new specificities
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
11
diff
changeset
|
150 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
|
151 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
|
152 |
dbc806d025a2
Added 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.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
|
154 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
|
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.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
|
158 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
|
159 |
dbc806d025a2
Added 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 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
|
161 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
|
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 pygame.display.quit() |