Mercurial > ift6266
comparison data_generation/transformations/contrast.py @ 167:1f5937e9e530
More moves - transformations into data_generation, added "deep" folder
author | Dumitru Erhan <dumitru.erhan@gmail.com> |
---|---|
date | Fri, 26 Feb 2010 14:15:38 -0500 |
parents | transformations/contrast.py@7ef8aac2cdb5 |
children |
comparison
equal
deleted
inserted
replaced
166:17ae5a1a4dd1 | 167:1f5937e9e530 |
---|---|
1 #!/usr/bin/python | |
2 # coding: utf-8 | |
3 | |
4 ''' | |
5 Simple implementation of random contrast. This always switch half the time the polarity. | |
6 then it decides of a random contrast dependant of the complexity, the mean of the maximum and minimum | |
7 pixel value stays 0 (to avoid import bias change between exemples). | |
8 | |
9 Author: Xavier Glorot | |
10 ''' | |
11 | |
12 import numpy as N | |
13 import copy | |
14 | |
15 | |
16 class Contrast(): | |
17 def __init__(self,complexity = 1): | |
18 #---------- private attributes | |
19 self.__nx__ = 32 #xdim of the images | |
20 self.__ny__ = 32 #ydim of the images | |
21 self.__Pinvert__ = 0.5 #probability to switch polarity | |
22 self.__mincontrast__ = 0.15 | |
23 self.__resolution__ = 256 | |
24 self.__rangecontrastres__ = self.__resolution__ - N.int(self.__mincontrast__*self.__resolution__) | |
25 #------------------------------------------------ | |
26 | |
27 #---------- generation parameters | |
28 self.regenerate_parameters(complexity) | |
29 #------------------------------------------------ | |
30 | |
31 def _get_current_parameters(self): | |
32 return [self.invert,self.contrast] | |
33 | |
34 def get_settings_names(self): | |
35 return ['invert','contrast'] | |
36 | |
37 def regenerate_parameters(self, complexity): | |
38 self.invert = (N.random.uniform() < self.__Pinvert__) | |
39 self.contrast = self.__resolution__ - N.random.randint(1 + self.__rangecontrastres__ * complexity) | |
40 return self._get_current_parameters() | |
41 | |
42 def transform_1_image(self,image): #the real transformation method | |
43 maxi = image.max() | |
44 mini = image.min() | |
45 if self.invert: | |
46 newimage = 1 - (self.__resolution__- self.contrast) / (2 * float(self.__resolution__)) -\ | |
47 (image - mini) / float(maxi - mini) * self.contrast / float(self.__resolution__) | |
48 else: | |
49 newimage = (self.__resolution__- self.contrast) / (2 * float(self.__resolution__)) +\ | |
50 (image - mini) / float(maxi - mini) * self.contrast / float(self.__resolution__) | |
51 if image.dtype == 'uint8': | |
52 return N.asarray(newimage*255,dtype='uint8') | |
53 else: | |
54 return N.asarray(newimage,dtype=image.dtype) | |
55 | |
56 def transform_image(self,image): #handling different format | |
57 if image.shape == (self.__nx__,self.__ny__): | |
58 return self.transform_1_image(image) | |
59 if image.ndim == 3: | |
60 newimage = copy.copy(image) | |
61 for i in range(image.shape[0]): | |
62 newimage[i,:,:] = self.transform_1_image(image[i,:,:]) | |
63 return newimage | |
64 if image.ndim == 2 and image.shape != (self.__nx__,self.__ny__): | |
65 newimage = N.reshape(image,(image.shape[0],self.__nx__,self.__ny__)) | |
66 for i in range(image.shape[0]): | |
67 newimage[i,:,:] = self.transform_1_image(newimage[i,:,:]) | |
68 return N.reshape(newimage,image.shape) | |
69 if image.ndim == 1: | |
70 newimage = N.reshape(image,(self.__nx__,self.__ny__)) | |
71 newimage = self.transform_1_image(newimage) | |
72 return N.reshape(newimage,image.shape) | |
73 assert False #should never go there | |
74 | |
75 | |
76 | |
77 | |
78 #test on NIST (you need pylearn and access to NIST to do that) | |
79 | |
80 if __name__ == '__main__': | |
81 | |
82 from pylearn.io import filetensor as ft | |
83 import copy | |
84 import pygame | |
85 import time | |
86 datapath = '/data/lisa/data/nist/by_class/' | |
87 f = open(datapath+'digits/digits_train_data.ft') | |
88 d = ft.read(f) | |
89 | |
90 pygame.surfarray.use_arraytype('numpy') | |
91 | |
92 pygame.display.init() | |
93 screen = pygame.display.set_mode((8*2*32,8*32),0,8) | |
94 anglcolorpalette=[(x,x,x) for x in xrange(0,256)] | |
95 screen.set_palette(anglcolorpalette) | |
96 | |
97 MyContrast = Contrast() | |
98 | |
99 debut=time.time() | |
100 MyContrast.transform_image(d) | |
101 fin=time.time() | |
102 print '------------------------------------------------' | |
103 print d.shape[0],' images transformed in :', fin-debut, ' seconds' | |
104 print '------------------------------------------------' | |
105 print (fin-debut)/d.shape[0]*1000000,' microseconds per image' | |
106 print '------------------------------------------------' | |
107 print MyContrast.get_settings_names() | |
108 print MyContrast._get_current_parameters() | |
109 print MyContrast.regenerate_parameters(0) | |
110 print MyContrast.regenerate_parameters(0.5) | |
111 print MyContrast.regenerate_parameters(1) | |
112 for i in range(10000): | |
113 a=d[i,:] | |
114 b=N.asarray(N.reshape(a,(32,32))).T | |
115 | |
116 new=pygame.surfarray.make_surface(b) | |
117 new=pygame.transform.scale2x(new) | |
118 new=pygame.transform.scale2x(new) | |
119 new=pygame.transform.scale2x(new) | |
120 new.set_palette(anglcolorpalette) | |
121 screen.blit(new,(0,0)) | |
122 | |
123 print MyContrast.get_settings_names(), MyContrast.regenerate_parameters(1) | |
124 c=MyContrast.transform_image(a) | |
125 b=N.asarray(N.reshape(c,(32,32))).T | |
126 | |
127 new=pygame.surfarray.make_surface(b) | |
128 new=pygame.transform.scale2x(new) | |
129 new=pygame.transform.scale2x(new) | |
130 new=pygame.transform.scale2x(new) | |
131 new.set_palette(anglcolorpalette) | |
132 screen.blit(new,(8*32,0)) | |
133 | |
134 pygame.display.update() | |
135 raw_input('Press Enter') | |
136 | |
137 pygame.display.quit() |