Mercurial > ift6266
annotate data_generation/transformations/add_background_image.py @ 613:5e481b224117
fix the reading of PNIST dataset following Dumi compression of the data.
author | Frederic Bastien <nouiz@nouiz.org> |
---|---|
date | Thu, 06 Jan 2011 13:57:05 -0500 |
parents | 1f5937e9e530 |
children |
rev | line source |
---|---|
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
1 #!/usr/bin/python |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
2 # -*- coding: iso-8859-1 -*- |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
3 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
4 ''' |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
5 Implementation of random background adding to a specific image |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
6 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
7 Author: Guillaume Sicard |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
8 ''' |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
9 |
68
ee6a788557b6
Change in add_background_image to load a pickled list of file, not to do a listdir
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
65
diff
changeset
|
10 import sys, os, random |
ee6a788557b6
Change in add_background_image to load a pickled list of file, not to do a listdir
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
65
diff
changeset
|
11 import cPickle |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
12 import Image, numpy |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
13 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
14 class AddBackground(): |
83
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
15 def __init__(self, threshold = 128, complexity = 1): |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
16 self.h = 32 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
17 self.w = 32 |
83
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
18 self.threshold = 1; |
79
53ee1097c02c
added a local data load if available (on maggie it will) for background images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
70
diff
changeset
|
19 try: #in order to load locally if it is available |
53ee1097c02c
added a local data load if available (on maggie it will) for background images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
70
diff
changeset
|
20 self.bg_image_file = '/Tmp/image_net/' |
53ee1097c02c
added a local data load if available (on maggie it will) for background images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
70
diff
changeset
|
21 f=open(self.bg_image_file+'filelist.pkl') |
53ee1097c02c
added a local data load if available (on maggie it will) for background images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
70
diff
changeset
|
22 except: |
53ee1097c02c
added a local data load if available (on maggie it will) for background images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
70
diff
changeset
|
23 self.bg_image_file = '/data/lisa/data/ift6266h10/image_net/' |
53ee1097c02c
added a local data load if available (on maggie it will) for background images
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
70
diff
changeset
|
24 f=open(self.bg_image_file+'filelist.pkl') |
68
ee6a788557b6
Change in add_background_image to load a pickled list of file, not to do a listdir
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
65
diff
changeset
|
25 self.image_files = cPickle.load(f) |
69
6d87c0df2b0e
Forgot a close in add_background_image
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
68
diff
changeset
|
26 f.close() |
83
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
27 self.regenerate_parameters(complexity) |
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
28 |
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
29 def get_current_parameters(self): |
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
30 return [self.contrast] |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
31 # get threshold value |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
32 def get_settings_names(self): |
83
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
33 return ['contrast'] |
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
34 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
35 # no need, except for testmod.py |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
36 def regenerate_parameters(self, complexity): |
83
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
37 self.contrast = 1-numpy.random.rand()*complexity |
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
38 return [self.contrast] |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
39 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
40 # load an image |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
41 def load_image(self,filename): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
42 image = Image.open(filename).convert('L') |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
43 image = numpy.asarray(image) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
44 image = (image / 255.0).astype(numpy.float32) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
45 return image |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
46 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
47 # save an image |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
48 def save_image(self,array, filename): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
49 image = (array * 255.0).astype('int') |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
50 image = Image.fromarray(image) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
51 if (filename != ''): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
52 image.save(filename) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
53 else: |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
54 image.show() |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
55 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
56 # make a random 32x32 crop of an image |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
57 def rand_crop(self,image): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
58 i_w, i_h = image.shape |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
59 x, y = random.randint(0, i_w - self.w), random.randint(0, i_h - self.h) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
60 return image[x:x + self.w, y:y + self.h] |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
61 |
70
be24db3a4d6e
bug fix in reading pkl image list file
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
69
diff
changeset
|
62 # select a random background image from "bg_image_file" and crops it |
83
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
63 def rand_bg_image(self,maximage): |
65
ab70fbca513c
Change path to AddBackground and add a image_file attribute (not to do a listdir at each image)
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
32
diff
changeset
|
64 i = random.randint(0, len(self.image_files) - 1) |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
65 |
70
be24db3a4d6e
bug fix in reading pkl image list file
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
69
diff
changeset
|
66 image = self.load_image(self.bg_image_file + self.image_files[i]) |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
67 self.bg_image = self.rand_crop(image) |
83
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
68 maxbg = self.bg_image.max() |
85
8aadb0f59a64
changed contrast definition for add_background_image
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
83
diff
changeset
|
69 self.bg_image = self.bg_image / maxbg * ( max(maximage - self.contrast,0.0) ) |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
70 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
71 # set "bg_image" as background to "image", based on a pixels threshold |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
72 def set_bg(self,image): |
83
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
73 tensor = numpy.asarray([self.bg_image,image],dtype='float32') |
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
74 return tensor.max(0) |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
75 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
76 # transform an image file and return an array |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
77 def transform_image_from_file(self, filename): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
78 self.rand_bg_image() |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
79 image = self.load_image(filename) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
80 image = self.set_bg(image) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
81 return image |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
82 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
83 # standard array to array transform |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
84 def transform_image(self, image): |
83
f75f5acad4eb
Changed behavior of add_background in order to have a contrast generation parameter and doing the max without using a treshold mask
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
79
diff
changeset
|
85 self.rand_bg_image(image.max()) |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
86 image = self.set_bg(image) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
87 return image |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
88 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
89 # test method |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
90 def test(self,filename): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
91 import time |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
92 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
93 sys.stdout.write('Starting addBackground test : loading image') |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
94 sys.stdout.flush() |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
95 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
96 image = self.load_image(filename) |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
97 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
98 t = 0 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
99 n = 500 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
100 for i in range(n): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
101 t0 = time.time() |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
102 image2 = self.transform_image(image) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
103 t = ( i * t + (time.time() - t0) ) / (i + 1) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
104 sys.stdout.write('.') |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
105 sys.stdout.flush() |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
106 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
107 print "Done!\nAverage time : " + str(1000 * t) + " ms" |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
108 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
109 if __name__ == '__main__': |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
110 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
111 myAddBackground = AddBackground() |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
112 myAddBackground.test('./images/0-LiberationSans-Italic.ttf.jpg') |