Mercurial > ift6266
comparison transformations/add_background_image.py @ 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 | 53ee1097c02c |
children | 8aadb0f59a64 |
comparison
equal
deleted
inserted
replaced
82:c32a968851f5 | 83:f75f5acad4eb |
---|---|
10 import sys, os, random | 10 import sys, os, random |
11 import cPickle | 11 import cPickle |
12 import Image, numpy | 12 import Image, numpy |
13 | 13 |
14 class AddBackground(): | 14 class AddBackground(): |
15 def __init__(self, threshold = 128): | 15 def __init__(self, threshold = 128, complexity = 1): |
16 self.h = 32 | 16 self.h = 32 |
17 self.w = 32 | 17 self.w = 32 |
18 self.threshold = threshold; | 18 self.threshold = 1; |
19 try: #in order to load locally if it is available | 19 try: #in order to load locally if it is available |
20 self.bg_image_file = '/Tmp/image_net/' | 20 self.bg_image_file = '/Tmp/image_net/' |
21 f=open(self.bg_image_file+'filelist.pkl') | 21 f=open(self.bg_image_file+'filelist.pkl') |
22 except: | 22 except: |
23 self.bg_image_file = '/data/lisa/data/ift6266h10/image_net/' | 23 self.bg_image_file = '/data/lisa/data/ift6266h10/image_net/' |
24 f=open(self.bg_image_file+'filelist.pkl') | 24 f=open(self.bg_image_file+'filelist.pkl') |
25 self.image_files = cPickle.load(f) | 25 self.image_files = cPickle.load(f) |
26 f.close() | 26 f.close() |
27 | 27 self.regenerate_parameters(complexity) |
28 | |
29 def get_current_parameters(self): | |
30 return [self.contrast] | |
28 # get threshold value | 31 # get threshold value |
29 def get_settings_names(self): | 32 def get_settings_names(self): |
30 return [str(self.threshold)] | 33 return ['contrast'] |
31 | 34 |
32 # no need, except for testmod.py | 35 # no need, except for testmod.py |
33 def regenerate_parameters(self, complexity): | 36 def regenerate_parameters(self, complexity): |
34 value = random.gauss(0, 0.5*complexity) | 37 self.contrast = 1-numpy.random.rand()*complexity |
35 return [value] | 38 return [self.contrast] |
36 | 39 |
37 # load an image | 40 # load an image |
38 def load_image(self,filename): | 41 def load_image(self,filename): |
39 image = Image.open(filename).convert('L') | 42 image = Image.open(filename).convert('L') |
40 image = numpy.asarray(image) | 43 image = numpy.asarray(image) |
55 i_w, i_h = image.shape | 58 i_w, i_h = image.shape |
56 x, y = random.randint(0, i_w - self.w), random.randint(0, i_h - self.h) | 59 x, y = random.randint(0, i_w - self.w), random.randint(0, i_h - self.h) |
57 return image[x:x + self.w, y:y + self.h] | 60 return image[x:x + self.w, y:y + self.h] |
58 | 61 |
59 # select a random background image from "bg_image_file" and crops it | 62 # select a random background image from "bg_image_file" and crops it |
60 def rand_bg_image(self): | 63 def rand_bg_image(self,maximage): |
61 i = random.randint(0, len(self.image_files) - 1) | 64 i = random.randint(0, len(self.image_files) - 1) |
62 | 65 |
63 image = self.load_image(self.bg_image_file + self.image_files[i]) | 66 image = self.load_image(self.bg_image_file + self.image_files[i]) |
64 self.bg_image = self.rand_crop(image) | 67 self.bg_image = self.rand_crop(image) |
68 maxbg = self.bg_image.max() | |
69 self.bg_image = self.bg_image / maxbg * ( max(maximage - self.contrast/2.0,0.1) ) | |
65 | 70 |
66 # set "bg_image" as background to "image", based on a pixels threshold | 71 # set "bg_image" as background to "image", based on a pixels threshold |
67 def set_bg(self,image): | 72 def set_bg(self,image): |
68 b = (image < self.threshold / 255.0).astype(numpy.float32) | 73 tensor = numpy.asarray([self.bg_image,image],dtype='float32') |
69 return b * self.bg_image + ( 1 - b) * image | 74 return tensor.max(0) |
70 | 75 |
71 # transform an image file and return an array | 76 # transform an image file and return an array |
72 def transform_image_from_file(self, filename): | 77 def transform_image_from_file(self, filename): |
73 self.rand_bg_image() | 78 self.rand_bg_image() |
74 image = self.load_image(filename) | 79 image = self.load_image(filename) |
75 image = self.set_bg(image) | 80 image = self.set_bg(image) |
76 return image | 81 return image |
77 | 82 |
78 # standard array to array transform | 83 # standard array to array transform |
79 def transform_image(self, image): | 84 def transform_image(self, image): |
80 self.rand_bg_image() | 85 self.rand_bg_image(image.max()) |
81 image = self.set_bg(image) | 86 image = self.set_bg(image) |
82 return image | 87 return image |
83 | 88 |
84 # test method | 89 # test method |
85 def test(self,filename): | 90 def test(self,filename): |