changeset 83:f75f5acad4eb

Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Wed, 10 Feb 2010 17:37:00 -0500
parents c32a968851f5
children 104e0b8a1c70
files transformations/add_background_image.py
diffstat 1 files changed, 16 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/transformations/add_background_image.py	Wed Feb 10 15:09:24 2010 -0500
+++ b/transformations/add_background_image.py	Wed Feb 10 17:37:00 2010 -0500
@@ -12,10 +12,10 @@
 import Image, numpy           
 
 class AddBackground():
-    def __init__(self, threshold = 128):
+    def __init__(self, threshold = 128, complexity = 1):
         self.h = 32
         self.w = 32
-        self.threshold = threshold;
+        self.threshold = 1;
         try: #in order to load locally if it is available
             self.bg_image_file = '/Tmp/image_net/'
             f=open(self.bg_image_file+'filelist.pkl')
@@ -24,15 +24,18 @@
             f=open(self.bg_image_file+'filelist.pkl')
         self.image_files = cPickle.load(f)
         f.close()
-
+        self.regenerate_parameters(complexity)
+    
+    def get_current_parameters(self):
+        return [self.contrast]
     # get threshold value
     def get_settings_names(self):
-        return [str(self.threshold)]
-
+        return ['contrast']
+    
     # no need, except for testmod.py
     def regenerate_parameters(self, complexity):
-        value = random.gauss(0, 0.5*complexity)
-        return [value]
+        self.contrast = 1-numpy.random.rand()*complexity
+        return [self.contrast]
 
     # load an image
     def load_image(self,filename):
@@ -57,16 +60,18 @@
         return image[x:x + self.w, y:y + self.h]
 
     # select a random background image from "bg_image_file" and crops it
-    def rand_bg_image(self):
+    def rand_bg_image(self,maximage):
         i = random.randint(0, len(self.image_files) - 1)
 
         image = self.load_image(self.bg_image_file + self.image_files[i])
         self.bg_image = self.rand_crop(image)
+        maxbg = self.bg_image.max()
+        self.bg_image = self.bg_image / maxbg * ( max(maximage - self.contrast/2.0,0.1) ) 
 
     # set "bg_image" as background to "image", based on a pixels threshold
     def set_bg(self,image):
-        b = (image < self.threshold / 255.0).astype(numpy.float32)
-        return b * self.bg_image + ( 1 - b) * image
+        tensor = numpy.asarray([self.bg_image,image],dtype='float32')
+        return tensor.max(0)
 
     # transform an image file and return an array
     def transform_image_from_file(self, filename):
@@ -77,7 +82,7 @@
 
     # standard array to array transform
     def transform_image(self, image):
-        self.rand_bg_image()
+        self.rand_bg_image(image.max())
         image = self.set_bg(image)
         return image