diff transformations/affine_transform.py @ 51:81b9567ec4ae

transformation ready for pipeline
author Razvan Pascanu <r.pascanu@gmail.com>
date Thu, 04 Feb 2010 15:38:10 -0500
parents 17caecc92544
children d9d836d3c625
line wrap: on
line diff
--- a/transformations/affine_transform.py	Thu Feb 04 14:13:57 2010 -0500
+++ b/transformations/affine_transform.py	Thu Feb 04 15:38:10 2010 -0500
@@ -14,15 +14,26 @@
 
 
 class AffineTransformation():
-    def __init__( self, shape = (32,32), seed = None):
-        self.shape = shape
-        self.rng = numpy.random.RandomState(seed)
+    def __init__( self, complexity = .5):
+        self.shape = (32,32)
+        self.rng = numpy.random.RandomState()
+        self.complexity = complexity
+        params = self.rng.uniform(size=6) -.5
+        self.a = 1. + params[0]*.4*complexity
+        self.b = 0. + params[1]*.4*complexity
+        self.c = params[2]*8.*complexity
+        self.d = 0. + params[3]*.4*complexity
+        self.e = 1. + params[4]*.4*complexity
+        self.f = params[5]*8.*complexity
 
-    def transform(self,NIST_image):
     
-        im = Image.fromarray( \
-                numpy.asarray(\
-                       NIST_image.reshape(self.shape), dtype='uint8'))
+    def _get_current_paramteres(self):
+        return [self.a, self.b, self.c, self.d, self.e, self.g]
+
+    def get_settings_names(self):
+        return ['a','b','c','d','e','f']
+
+    def regenerate_parameters(self, complexity):
         # generate random affine transformation
         # a point (x',y') of the new image corresponds to (x,y) of the old
         # image where : 
@@ -30,16 +41,26 @@
         #   y' = params[3]*x + params[4]*y _ params[5]
 
         # the ranges are set manually as to look acceptable
-        params = self.rng.uniform(size = 6) -.5
-        params[2] *= 8.
-        params[5] *= 8.
-        params[0] = 1. + params[0]*0.4
-        params[3] = 0. + params[3]*0.4
-        params[1] = 0  + params[1]*0.4
-        params[4] = 1  + params[4]*0.4
+ 
+        self.complexity = complexity
+        params = self.rng.uniform(size=6) -.5
+        self.a = 1. + params[0]*.4*complexity
+        self.b = 0. + params[1]*.4*complexity
+        self.c = params[2]*8.*complexity
+        self.d = 0. + params[3]*.4*complexity
+        self.e = 1. + params[4]*.4*complexity
+        self.f = params[5]*8.*complexity
+        return self._get_current_parameters()
 
-        print params
-        nwim = im.transform( (32,32), Image.AFFINE, params)
+      
+
+
+    def transform_image(self,NIST_image):
+    
+        im = Image.fromarray( \
+                numpy.asarray(\
+                       NIST_image.reshape(self.shape), dtype='uint8'))
+        nwim = im.transform( (32,32), Image.AFFINE, [self.a,self.b,self.c,self.d,self.e,self.f])
         return numpy.asarray(nwim)
 
 
@@ -63,7 +84,7 @@
     pylab.figure()
     pylab.imshow(d[id].reshape((32,32)))
     pylab.figure()
-    pylab.imshow(transformer.transform(d[id]).reshape((32,32)))
+    pylab.imshow(transformer.transform_image(d[id]).reshape((32,32)))
 
     pylab.show()