annotate data_generation/transformations/affine_transform.py @ 266:1e4e60ddadb1

Merge. Ah, et dans le dernier commit, j'avais oublié de mentionner que j'ai ajouté du code pour gérer l'isolation de différents clones pour rouler des expériences et modifier le code en même temps.
author fsavard
date Fri, 19 Mar 2010 10:56:16 -0400
parents 1f5937e9e530
children
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.complexity = complexity
154
ce56e8ca960d Checked all modules to work with only numpy.random and random and to be deterministic after numpy.random.seed() and random.seed()
boulanni <nicolas_boulanger@hotmail.com>
parents: 124
diff changeset
20 params = numpy.random.uniform(size=6) -.5
124
b852dddf43a6 reduced affine transform coefficient
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 120
diff changeset
21 self.a = 1. + params[0]*.6*complexity
b852dddf43a6 reduced affine transform coefficient
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 120
diff changeset
22 self.b = 0. + params[1]*.6*complexity
b852dddf43a6 reduced affine transform coefficient
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 120
diff changeset
23 self.c = params[2]*8.*complexity
b852dddf43a6 reduced affine transform coefficient
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 120
diff changeset
24 self.d = 0. + params[3]*.6*complexity
b852dddf43a6 reduced affine transform coefficient
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 120
diff changeset
25 self.e = 1. + params[4]*.6*complexity
b852dddf43a6 reduced affine transform coefficient
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 120
diff changeset
26 self.f = params[5]*8.*complexity
39
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
27
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
28
56
d9d836d3c625 Change in affine_transform to handle float images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 51
diff changeset
29 def _get_current_parameters(self):
d9d836d3c625 Change in affine_transform to handle float images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 51
diff changeset
30 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
31
81b9567ec4ae transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents: 39
diff changeset
32 def get_settings_names(self):
81b9567ec4ae transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents: 39
diff changeset
33 return ['a','b','c','d','e','f']
81b9567ec4ae transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents: 39
diff changeset
34
81b9567ec4ae transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents: 39
diff changeset
35 def regenerate_parameters(self, complexity):
39
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
36 # generate random affine transformation
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
37 # 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
38 # image where :
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
39 # x' = params[0]*x + params[1]*y + params[2]
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
40 # y' = params[3]*x + params[4]*y _ params[5]
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
41
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
42 # 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
43
81b9567ec4ae transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents: 39
diff changeset
44 self.complexity = complexity
154
ce56e8ca960d Checked all modules to work with only numpy.random and random and to be deterministic after numpy.random.seed() and random.seed()
boulanni <nicolas_boulanger@hotmail.com>
parents: 124
diff changeset
45 params = numpy.random.uniform(size=6) -.5
120
5e00ed18ae32 changed some transformation paramters to better fit with visualization
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 73
diff changeset
46 self.a = 1. + params[0]*.8*complexity
5e00ed18ae32 changed some transformation paramters to better fit with visualization
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 73
diff changeset
47 self.b = 0. + params[1]*.8*complexity
5e00ed18ae32 changed some transformation paramters to better fit with visualization
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 73
diff changeset
48 self.c = params[2]*9.*complexity
5e00ed18ae32 changed some transformation paramters to better fit with visualization
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 73
diff changeset
49 self.d = 0. + params[3]*.8*complexity
5e00ed18ae32 changed some transformation paramters to better fit with visualization
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 73
diff changeset
50 self.e = 1. + params[4]*.8*complexity
5e00ed18ae32 changed some transformation paramters to better fit with visualization
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 73
diff changeset
51 self.f = params[5]*9.*complexity
51
81b9567ec4ae transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents: 39
diff changeset
52 return self._get_current_parameters()
39
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
53
51
81b9567ec4ae transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents: 39
diff changeset
54
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 def transform_image(self,NIST_image):
81b9567ec4ae transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents: 39
diff changeset
58
81b9567ec4ae transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents: 39
diff changeset
59 im = Image.fromarray( \
81b9567ec4ae transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents: 39
diff changeset
60 numpy.asarray(\
56
d9d836d3c625 Change in affine_transform to handle float images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 51
diff changeset
61 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
62 nwim = im.transform( (32,32), Image.AFFINE, [self.a,self.b,self.c,self.d,self.e,self.f])
73
859ebd7f8754 fixed float32 return on affine_transform
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 56
diff changeset
63 return numpy.asarray(numpy.asarray(nwim)/255.0,dtype='float32')
39
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
64
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 if __name__ =='__main__':
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
68 print 'random test'
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
69
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
70 from pylearn.io import filetensor as ft
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
71 import pylab
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
72
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
73 datapath = '/data/lisa/data/nist/by_class/'
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
74
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
75 f = open(datapath+'digits/digits_train_data.ft')
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
76 d = ft.read(f)
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
77 f.close()
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
78
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 transformer = AffineTransformation()
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
81 id = numpy.random.randint(30)
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
82
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
83 pylab.figure()
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
84 pylab.imshow(d[id].reshape((32,32)))
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
85 pylab.figure()
51
81b9567ec4ae transformation ready for pipeline
Razvan Pascanu <r.pascanu@gmail.com>
parents: 39
diff changeset
86 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
87
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
88 pylab.show()
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
89