comparison data_generation/pipeline/testtransformations.py @ 168:5e0e5f1860ec

Pipeline code shuffle
author Dumitru Erhan <dumitru.erhan@gmail.com>
date Fri, 26 Feb 2010 14:23:47 -0500
parents data_generation/transformations/testtransformations.py@1f5937e9e530
children 0de14b2034c6
comparison
equal deleted inserted replaced
167:1f5937e9e530 168:5e0e5f1860ec
1 #!/usr/bin/env python
2
3
4
5 from pylearn.io import filetensor as ft
6 import copy
7 import pygame
8 import time
9 import numpy as N
10
11 from ttf2jpg import ttf2jpg
12
13 #from gimpfu import *
14
15
16 from PoivreSel import PoivreSel
17 from thick import Thick
18 from BruitGauss import BruitGauss
19 from DistorsionGauss import DistorsionGauss
20 from PermutPixel import PermutPixel
21 from gimp_script import GIMP1
22 from Rature import Rature
23 from contrast import Contrast
24 from local_elastic_distortions import LocalElasticDistorter
25 from slant import Slant
26 from Occlusion import Occlusion
27 from add_background_image import AddBackground
28 from affine_transform import AffineTransformation
29
30 ###---------------------order of transformation module
31 MODULE_INSTANCES = [Slant(),Thick(),AffineTransformation(),LocalElasticDistorter(),GIMP1(),Rature(),Occlusion(), PermutPixel(),DistorsionGauss(),AddBackground(), PoivreSel(), BruitGauss(), Contrast()]
32
33 ###---------------------complexity associated to each of them
34 complexity = 0.7
35 #complexity = [0.5]*len(MODULE_INSTANCES)
36 #complexity = [0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.]
37 n=100
38
39 def createimage(path,d):
40 for i in range(n):
41 screen.fill(0)
42 a=d[i,:]
43 off1=4*32
44 off2=0
45 for u in range(n):
46 b=N.asarray(N.reshape(a,(32,32)))
47 c=N.asarray([N.reshape(a*255.0,(32,32))]*3).T
48 new=pygame.surfarray.make_surface(c)
49 new=pygame.transform.scale2x(new)
50 new=pygame.transform.scale2x(new)
51 #new.set_palette(anglcolorpalette)
52 screen.blit(new,(0,0))
53 exemple.blit(new,(0,0))
54
55 offset = 4*32
56 offset2 = 0
57 ct = 0
58 ctmp = N.random.rand()*complexity
59 print u
60 for j in MODULE_INSTANCES:
61 #max dilation
62 #ctmp = N.random.rand()*complexity[ct]
63 ctmp = N.random.rand()*complexity
64 #print j.get_settings_names(), j.regenerate_parameters(ctmp)
65 th=j.regenerate_parameters(ctmp)
66
67 b=j.transform_image(b)
68 c=N.asarray([b*255]*3).T
69 new=pygame.surfarray.make_surface(c)
70 new=pygame.transform.scale2x(new)
71 new=pygame.transform.scale2x(new)
72 if u==0:
73 #new.set_palette(anglcolorpalette)
74 screen.blit(new,(offset,offset2))
75 font = pygame.font.SysFont('liberationserif',18)
76 text = font.render('%s '%(int(ctmp*100.0)/100.0) + j.__module__,0,(255,255,255),(0,0,0))
77 #if j.__module__ == 'Rature':
78 # text = font.render('%s,%s'%(th[-1],int(ctmp*100.0)/100.0) + j.__module__,0,(255,255,255),(0,0,0))
79 screen.blit(text,(offset,offset2+4*32))
80 if ct == len(MODULE_INSTANCES)/2-1:
81 offset = 0
82 offset2 = 4*32+20
83 else:
84 offset += 4*32
85 ct+=1
86 exemple.blit(new,(off1,off2))
87 if off1 != 9*4*32:
88 off1+=4*32
89 else:
90 off1=0
91 off2+=4*32
92 pygame.image.save(exemple,path+'/perimages/%s.PNG'%i)
93 pygame.image.save(screen,path+'/exemples/%s.PNG'%i)
94
95
96
97
98 nbmodule = len(MODULE_INSTANCES)
99
100 pygame.surfarray.use_arraytype('numpy')
101
102 #pygame.display.init()
103 screen = pygame.Surface((4*(nbmodule+1)/2*32,2*(4*32+20)),depth=32)
104 exemple = pygame.Surface((N.ceil(N.sqrt(n))*4*32,N.ceil(N.sqrt(n))*4*32),depth=32)
105
106 anglcolorpalette=[(x,x,x) for x in xrange(0,256)]
107 #pygame.Surface.set_palette(anglcolorpalette)
108 #screen.set_palette(anglcolorpalette)
109
110 pygame.font.init()
111
112 d = N.zeros((n,1024))
113
114 datapath = '/data/lisa/data/ocr_breuel/filetensor/unlv-corrected-2010-02-01-shuffled.ft'
115 f = open(datapath)
116 d = ft.read(f)
117 d = d[0:n,:]/255.0
118 createimage('/u/glorotxa/transf/OCR',d)
119
120
121
122 datapath = '/data/lisa/data/nist/by_class/'
123 f = open(datapath+'digits_reshuffled/digits_reshuffled_train_data.ft')
124 d = ft.read(f)
125 d = d[0:n,:]/255.0
126 createimage('/u/glorotxa/transf/NIST_digits',d)
127
128
129
130 datapath = '/data/lisa/data/nist/by_class/'
131 f = open(datapath+'upper/upper_train_data.ft')
132 d = ft.read(f)
133 d = d[0:n,:]/255.0
134 createimage('/u/glorotxa/transf/NIST_upper',d)
135
136 from Facade import *
137
138 for i in range(n):
139 d[i,:]=N.asarray(N.reshape(generateCaptcha(0.8,0),(1,1024))/255.0,dtype='float32')
140
141 createimage('/u/glorotxa/transf/capcha',d)
142
143
144 for i in range(n):
145 myttf2jpg = ttf2jpg()
146 d[i,:]=N.reshape(myttf2jpg.generate_image()[0],(1,1024))
147 createimage('/u/glorotxa/transf/fonts',d)
148
149 datapath = '/data/lisa/data/nist/by_class/'
150 f = open(datapath+'lower/lower_train_data.ft')
151 d = ft.read(f)
152 d = d[0:n,:]/255.0
153 createimage('/u/glorotxa/transf/NIST_lower',d)
154
155
156 #pygame.display.quit()