comparison transformations/testtransformations.py @ 113:291d749452df

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