Mercurial > ift6266
comparison data_generation/transformations/affine_transform.py @ 167:1f5937e9e530
More moves - transformations into data_generation, added "deep" folder
author | Dumitru Erhan <dumitru.erhan@gmail.com> |
---|---|
date | Fri, 26 Feb 2010 14:15:38 -0500 |
parents | transformations/affine_transform.py@ce56e8ca960d |
children |
comparison
equal
deleted
inserted
replaced
166:17ae5a1a4dd1 | 167:1f5937e9e530 |
---|---|
1 #!/usr/bin/python | |
2 # coding: utf-8 | |
3 | |
4 ''' | |
5 Simple implementation of random affine transformations based on the Python | |
6 Imaging Module affine transformations. | |
7 | |
8 | |
9 Author: Razvan Pascanu | |
10 ''' | |
11 | |
12 import numpy, Image | |
13 | |
14 | |
15 | |
16 class AffineTransformation(): | |
17 def __init__( self, complexity = .5): | |
18 self.shape = (32,32) | |
19 self.complexity = complexity | |
20 params = numpy.random.uniform(size=6) -.5 | |
21 self.a = 1. + params[0]*.6*complexity | |
22 self.b = 0. + params[1]*.6*complexity | |
23 self.c = params[2]*8.*complexity | |
24 self.d = 0. + params[3]*.6*complexity | |
25 self.e = 1. + params[4]*.6*complexity | |
26 self.f = params[5]*8.*complexity | |
27 | |
28 | |
29 def _get_current_parameters(self): | |
30 return [self.a, self.b, self.c, self.d, self.e, self.f] | |
31 | |
32 def get_settings_names(self): | |
33 return ['a','b','c','d','e','f'] | |
34 | |
35 def regenerate_parameters(self, complexity): | |
36 # generate random affine transformation | |
37 # a point (x',y') of the new image corresponds to (x,y) of the old | |
38 # image where : | |
39 # x' = params[0]*x + params[1]*y + params[2] | |
40 # y' = params[3]*x + params[4]*y _ params[5] | |
41 | |
42 # the ranges are set manually as to look acceptable | |
43 | |
44 self.complexity = complexity | |
45 params = numpy.random.uniform(size=6) -.5 | |
46 self.a = 1. + params[0]*.8*complexity | |
47 self.b = 0. + params[1]*.8*complexity | |
48 self.c = params[2]*9.*complexity | |
49 self.d = 0. + params[3]*.8*complexity | |
50 self.e = 1. + params[4]*.8*complexity | |
51 self.f = params[5]*9.*complexity | |
52 return self._get_current_parameters() | |
53 | |
54 | |
55 | |
56 | |
57 def transform_image(self,NIST_image): | |
58 | |
59 im = Image.fromarray( \ | |
60 numpy.asarray(\ | |
61 NIST_image.reshape(self.shape)*255.0, dtype='uint8')) | |
62 nwim = im.transform( (32,32), Image.AFFINE, [self.a,self.b,self.c,self.d,self.e,self.f]) | |
63 return numpy.asarray(numpy.asarray(nwim)/255.0,dtype='float32') | |
64 | |
65 | |
66 | |
67 if __name__ =='__main__': | |
68 print 'random test' | |
69 | |
70 from pylearn.io import filetensor as ft | |
71 import pylab | |
72 | |
73 datapath = '/data/lisa/data/nist/by_class/' | |
74 | |
75 f = open(datapath+'digits/digits_train_data.ft') | |
76 d = ft.read(f) | |
77 f.close() | |
78 | |
79 | |
80 transformer = AffineTransformation() | |
81 id = numpy.random.randint(30) | |
82 | |
83 pylab.figure() | |
84 pylab.imshow(d[id].reshape((32,32))) | |
85 pylab.figure() | |
86 pylab.imshow(transformer.transform_image(d[id]).reshape((32,32))) | |
87 | |
88 pylab.show() | |
89 |