diff transformations/thick.py @ 13:a25474d4d34f

Changed parameter generation in order to fit with the new specificities
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Thu, 28 Jan 2010 11:28:46 -0500
parents dbc806d025a2
children ebf61603489b
line wrap: on
line diff
--- a/transformations/thick.py	Wed Jan 27 21:05:58 2010 -0500
+++ b/transformations/thick.py	Thu Jan 28 11:28:46 2010 -0500
@@ -34,19 +34,14 @@
         #------------------------------------------------
         
         #---------- generation parameters
-        self.erodenb = N.ceil(complexity * self.__erodemax__)
-        self.dilatenb = N.ceil(complexity * self.__dilatemax__)
-        self.Perode = self.erodenb / (self.dilatenb + self.erodenb + 1.0)
-        self.Pdilate = self.dilatenb / (self.dilatenb + self.erodenb + 1.0)
-        assert (self.Perode + self.Pdilate <= 1) & (self.Perode + self.Pdilate >= 0)
-        assert (complexity >= 0) & (complexity <= 1)
+        self.regenerate_parameters(complexity)
         #------------------------------------------------
     
     def _get_current_parameters(self):
-        return [self.erodenb, self.dilatenb, self.Perode, self.Pdilate]
+        return [self.meth,self.nb]
     
     def get_settings_names(self):
-        return ['erodenb','dilatenb','Perode','Pdilate']
+        return ['meth','nb']
     
     def regenerate_parameters(self, complexity):
         self.erodenb = N.ceil(complexity * self.__erodemax__)
@@ -55,59 +50,52 @@
         self.Pdilate = self.dilatenb / (self.dilatenb + self.erodenb + 1.0)
         assert (self.Perode + self.Pdilate <= 1) & (self.Perode + self.Pdilate >= 0)
         assert (complexity >= 0) & (complexity <= 1)
+        P = N.random.uniform()
+        if P>1-(self.Pdilate+self.Perode):
+            if P>1-(self.Pdilate+self.Perode)+self.Perode:
+                self.meth = 1
+                self.nb=N.random.randint(self.dilatenb)
+            else:
+                self.meth = -1
+                self.nb=N.random.randint(self.erodenb)
+        else:
+            self.meth = 0
+            self.nb = -1
         return self._get_current_parameters()
     
-    def transform_1_image(self,image,genparam_save = None):
-        P = N.random.uniform()
-        
-        if P>1-(self.Pdilate+self.Perode):
+    def transform_1_image(self,image):
+        if self.meth!=0:
             maxi = float(N.max(image))
             mini = float(N.min(image))
             
             if maxi>1.0:
                 image=image/maxi
             
-            if P>1-(self.Pdilate+self.Perode)+self.Perode:
-                nb=N.random.randint(self.dilatenb)
+            if self.meth==1:
                 trans=scipy.ndimage.morphology.grey_dilation\
-                        (image,size=self.__structuring_elements__[nb].shape,structure=self.__structuring_elements__[nb])
-                meth = 'dilate'
+                    (image,size=self.__structuring_elements__[self.nb].shape,structure=self.__structuring_elements__[self.nb])
             else:
-                nb=N.random.randint(self.erodenb)
                 trans=scipy.ndimage.morphology.grey_erosion\
-                        (image,size=self.__structuring_elements__[nb].shape,structure=self.__structuring_elements__[nb])
-                meth = 'erode'
+                    (image,size=self.__structuring_elements__[self.nb].shape,structure=self.__structuring_elements__[self.nb])
             
             #------renormalizing
             maxit = N.max(trans)
             minit = N.min(trans)
             trans= numpy.asarray((trans - (minit+mini)) / (maxit - (minit+mini)) * maxi,dtype=image.dtype)
             #--------
-            if genparam_save is not None:
-                genparam_save.update({'Thick':{'meth':meth,'nb':nb}})
             return trans
         else:
-            meth = 'nothing'
-            nb = 0
-            if genparam_save is not None:
-                genparam_save.update({'Thick':{'meth':meth,'nb':nb}})
             return image
     
-    def transform_image(self,image,genparam_save = None):
+    def transform_image(self,image):
         if image.ndim == 2:
             newimage = N.reshape(image,(image.shape[0],self.__nx__,self.__ny__))
             for i in range(image.shape[0]):
-                if genparam_save is not None:
-                    newimage[i,:,:] = self.transform_1_image(newimage[i,:,:],genparam_save[i])
-                else:
-                    newimage[i,:,:] = self.transform_1_image(newimage[i,:,:])
+                newimage[i,:,:] = self.transform_1_image(newimage[i,:,:])
             return N.reshape(newimage,image.shape)
         else:
             newimage = N.reshape(image,(self.__nx__,self.__ny__))
-            if genparam_save is not None:
-                newimage = self.transform_1_image(newimage,genparam_save)
-            else:
-                newimage = self.transform_1_image(newimage)
+            newimage = self.transform_1_image(newimage)
             return N.reshape(newimage,image.shape)
 
 
@@ -134,14 +122,14 @@
     
     MyThick = Thick()
     
-    #debut=time.time()
-    #MyThick.transform_image(d)
-    #fin=time.time()
-    #print '------------------------------------------------'
-    #print d.shape[0],' images transformed in :', fin-debut, ' seconds'
-    #print '------------------------------------------------'
-    #print (fin-debut)/d.shape[0]*1000000,' microseconds per image'
-    #print '------------------------------------------------'
+    debut=time.time()
+    MyThick.transform_image(d)
+    fin=time.time()
+    print '------------------------------------------------'
+    print d.shape[0],' images transformed in :', fin-debut, ' seconds'
+    print '------------------------------------------------'
+    print (fin-debut)/d.shape[0]*1000000,' microseconds per image'
+    print '------------------------------------------------'
     #print MyThick.get_settings_names()
     #print MyThick._get_current_parameters()
     #print MyThick.regenerate_parameters(0)
@@ -158,8 +146,8 @@
         new.set_palette(anglcolorpalette)
         screen.blit(new,(0,0))
         
-        dd={}
-        c=MyThick.transform_image(a,dd)
+        print MyThick.get_settings_names(), MyThick.regenerate_parameters(1)
+        c=MyThick.transform_image(a)
         b=N.asarray(N.reshape(c,(32,32))).T
         
         new=pygame.surfarray.make_surface(b)
@@ -170,7 +158,6 @@
         screen.blit(new,(8*32,0))
         
         pygame.display.update()
-        print dd
         raw_input('Press Enter')
     
     pygame.display.quit()
\ No newline at end of file