comparison transformations/testtransformations.py @ 144:c958941c1b9d

merge
author XavierMuller
date Tue, 23 Feb 2010 18:16:55 -0500
parents a507adba0ce3
children
comparison
equal deleted inserted replaced
143:f341a4efb44a 144:c958941c1b9d
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.7
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 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
34 95
35 96
36 97
37 nbmodule = len(MODULE_INSTANCES) 98 nbmodule = len(MODULE_INSTANCES)
38 99
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') 100 pygame.surfarray.use_arraytype('numpy')
46 101
47 #pygame.display.init() 102 #pygame.display.init()
48 screen = pygame.Surface((4*(nbmodule+1)/2*32,2*(4*32+20)),depth=32) 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)
49 105
50 anglcolorpalette=[(x,x,x) for x in xrange(0,256)] 106 anglcolorpalette=[(x,x,x) for x in xrange(0,256)]
51 #pygame.Surface.set_palette(anglcolorpalette) 107 #pygame.Surface.set_palette(anglcolorpalette)
52 #screen.set_palette(anglcolorpalette) 108 #screen.set_palette(anglcolorpalette)
53 109
54 pygame.font.init() 110 pygame.font.init()
55 111
56 for i in range(1000): 112 d = N.zeros((n,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 113
75 b=j.transform_image(b) 114 datapath = '/data/lisa/data/ocr_breuel/filetensor/unlv-corrected-2010-02-01-shuffled.ft'
76 c=N.asarray([b*255]*3).T 115 f = open(datapath)
77 116 d = ft.read(f)
78 new=pygame.surfarray.make_surface(c) 117 d = d[0:n,:]/255.0
79 new=pygame.transform.scale2x(new) 118 createimage('/u/glorotxa/transf/OCR',d)
80 new=pygame.transform.scale2x(new) 119
81 #new.set_palette(anglcolorpalette) 120
82 screen.blit(new,(offset,offset2)) 121
83 font = pygame.font.SysFont('liberationserif',18) 122 datapath = '/data/lisa/data/nist/by_class/'
84 text = font.render(j.__module__,0,(255,255,255),(0,0,0)) 123 f = open(datapath+'digits_reshuffled/digits_reshuffled_train_data.ft')
85 screen.blit(text,(offset,offset2+4*32)) 124 d = ft.read(f)
86 if ct == len(MODULE_INSTANCES)/2-1: 125 d = d[0:n,:]/255.0
87 offset = 0 126 createimage('/u/glorotxa/transf/NIST_digits',d)
88 offset2 = 4*32+20 127
89 else: 128
90 offset += 4*32 129
91 ct+=1 130 datapath = '/data/lisa/data/nist/by_class/'
92 pygame.image.save(screen,'/u/glorotxa/exemples/%s.PNG'%i) 131 f = open(datapath+'upper/upper_train_data.ft')
93 #raw_input('Press Enter') 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
94 155
95 #pygame.display.quit() 156 #pygame.display.quit()