annotate transformations/affine_transform.py @ 41:fdb0e0870fb4

Beaucoup de modifications à pipeline.py pour généraliser et un début de visualisation, et créé un wrapper (run_pipeline.py) pour appeler avec GIMP. - Modifications à pipeline.py - Wrappé la boucle du pipeline dans une classe - Isolé le problème de itérer sur les batches et les complexités dans des itérateurs - Permet d'avoir des ordres compliqués de batch (plusieurs sources), de complexités - Maintenant regenerate_parameters() est appelé pour chaque image. - Command line arguments avec getopt(). On pourra rajouter des options ainsi. - run_pipeline.py - Le but est de permettre de passer des arguments. Pas facile (pas trouvé comment de façon simple) avec la command line pour appeler GIMP en mode batch. C'est un hack ici. - Le but ultime est de permettre de lancer les jobs sur les clusters avec dbidispatch en précisant les options (diff. pour chaque job) sur la ligne de commande.
author fsavard
date Wed, 03 Feb 2010 17:08:27 -0500
parents 17caecc92544
children 81b9567ec4ae
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():
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
17 def __init__( self, shape = (32,32), seed = None):
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
18 self.shape = shape
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
19 self.rng = numpy.random.RandomState(seed)
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
20
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
21 def transform(self,NIST_image):
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
22
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
23 im = Image.fromarray( \
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
24 numpy.asarray(\
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
25 NIST_image.reshape(self.shape), dtype='uint8'))
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
26 # generate random affine transformation
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
27 # 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
28 # image where :
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
29 # x' = params[0]*x + params[1]*y + params[2]
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
30 # y' = params[3]*x + params[4]*y _ params[5]
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
31
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
32 # the ranges are set manually as to look acceptable
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
33 params = self.rng.uniform(size = 6) -.5
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
34 params[2] *= 8.
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
35 params[5] *= 8.
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
36 params[0] = 1. + params[0]*0.4
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
37 params[3] = 0. + params[3]*0.4
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
38 params[1] = 0 + params[1]*0.4
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
39 params[4] = 1 + params[4]*0.4
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
40
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
41 print params
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
42 nwim = im.transform( (32,32), Image.AFFINE, params)
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
43 return numpy.asarray(nwim)
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
44
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
45
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
46
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
47 if __name__ =='__main__':
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
48 print 'random test'
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
49
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
50 from pylearn.io import filetensor as ft
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
51 import pylab
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
52
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
53 datapath = '/data/lisa/data/nist/by_class/'
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
54
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
55 f = open(datapath+'digits/digits_train_data.ft')
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
56 d = ft.read(f)
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
57 f.close()
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
58
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
59
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
60 transformer = AffineTransformation()
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
61 id = numpy.random.randint(30)
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
62
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
63 pylab.figure()
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
64 pylab.imshow(d[id].reshape((32,32)))
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
65 pylab.figure()
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
66 pylab.imshow(transformer.transform(d[id]).reshape((32,32)))
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 pylab.show()
17caecc92544 affine transformation using PIL
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff changeset
69