Mercurial > ift6266
annotate transformations/affine_transform.py @ 72:af2f9252dd14
forgot to import sys in testmod
author | Arnaud Bergeron <abergeron@gmail.com> |
---|---|
date | Wed, 10 Feb 2010 11:34:32 -0500 |
parents | d9d836d3c625 |
children | 859ebd7f8754 |
rev | line source |
---|---|
39
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/python |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
2 # coding: utf-8 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
3 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
4 ''' |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
5 Simple implementation of random affine transformations based on the Python |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
6 Imaging Module affine transformations. |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
7 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
8 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
9 Author: Razvan Pascanu |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
10 ''' |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
11 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
12 import numpy, Image |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
13 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
14 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
15 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
16 class AffineTransformation(): |
51
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
17 def __init__( self, complexity = .5): |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
18 self.shape = (32,32) |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
19 self.rng = numpy.random.RandomState() |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
20 self.complexity = complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
21 params = self.rng.uniform(size=6) -.5 |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
22 self.a = 1. + params[0]*.4*complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
23 self.b = 0. + params[1]*.4*complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
24 self.c = params[2]*8.*complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
25 self.d = 0. + params[3]*.4*complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
26 self.e = 1. + params[4]*.4*complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
27 self.f = params[5]*8.*complexity |
39
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
28 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
29 |
56
d9d836d3c625
Change in affine_transform to handle float images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
51
diff
changeset
|
30 def _get_current_parameters(self): |
d9d836d3c625
Change in affine_transform to handle float images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
51
diff
changeset
|
31 return [self.a, self.b, self.c, self.d, self.e, self.f] |
51
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
32 |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
33 def get_settings_names(self): |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
34 return ['a','b','c','d','e','f'] |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
35 |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
36 def regenerate_parameters(self, complexity): |
39
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
37 # generate random affine transformation |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
38 # a point (x',y') of the new image corresponds to (x,y) of the old |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
39 # image where : |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
40 # x' = params[0]*x + params[1]*y + params[2] |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
41 # y' = params[3]*x + params[4]*y _ params[5] |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
42 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
43 # the ranges are set manually as to look acceptable |
51
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
44 |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
45 self.complexity = complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
46 params = self.rng.uniform(size=6) -.5 |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
47 self.a = 1. + params[0]*.4*complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
48 self.b = 0. + params[1]*.4*complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
49 self.c = params[2]*8.*complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
50 self.d = 0. + params[3]*.4*complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
51 self.e = 1. + params[4]*.4*complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
52 self.f = params[5]*8.*complexity |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
53 return self._get_current_parameters() |
39
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
54 |
51
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
55 |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
56 |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
57 |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
58 def transform_image(self,NIST_image): |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
59 |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
60 im = Image.fromarray( \ |
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
61 numpy.asarray(\ |
56
d9d836d3c625
Change in affine_transform to handle float images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
51
diff
changeset
|
62 NIST_image.reshape(self.shape)*255.0, dtype='uint8')) |
51
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
63 nwim = im.transform( (32,32), Image.AFFINE, [self.a,self.b,self.c,self.d,self.e,self.f]) |
56
d9d836d3c625
Change in affine_transform to handle float images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
51
diff
changeset
|
64 return numpy.asarray(nwim)/255.0 |
39
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
65 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
66 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
67 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
68 if __name__ =='__main__': |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
69 print 'random test' |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
70 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
71 from pylearn.io import filetensor as ft |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
72 import pylab |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
73 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
74 datapath = '/data/lisa/data/nist/by_class/' |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
75 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
76 f = open(datapath+'digits/digits_train_data.ft') |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
77 d = ft.read(f) |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
78 f.close() |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
79 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
80 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
81 transformer = AffineTransformation() |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
82 id = numpy.random.randint(30) |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
83 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
84 pylab.figure() |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
85 pylab.imshow(d[id].reshape((32,32))) |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
86 pylab.figure() |
51
81b9567ec4ae
transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents:
39
diff
changeset
|
87 pylab.imshow(transformer.transform_image(d[id]).reshape((32,32))) |
39
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
88 |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
89 pylab.show() |
17caecc92544
affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
90 |