16
16
from PIL import Image
17
17
from cStringIO import StringIO
18
18
19
+
19
20
def resize_image (img , target_size ):
20
21
"""
21
22
Resize an image so that the shorter edge has length target_size.
22
23
img: the input image to be resized.
23
24
target_size: the target resized image size.
24
25
"""
25
- percent = (target_size / float (min (img .size [0 ], img .size [1 ])))
26
- resized_size = int (round (img .size [0 ] * percent )), int (round (img .size [1 ] * percent ))
26
+ percent = (target_size / float (min (img .size [0 ], img .size [1 ])))
27
+ resized_size = int (round (img .size [0 ] * percent )), int (
28
+ round (img .size [1 ] * percent ))
27
29
img = img .resize (resized_size , Image .ANTIALIAS )
28
30
return img
29
31
32
+
30
33
def flip (im ):
31
34
"""
32
35
Return the flipped image.
@@ -38,6 +41,7 @@ def flip(im):
38
41
else :
39
42
return im [:, ::- 1 ]
40
43
44
+
41
45
def crop_img (im , inner_size , color = True , test = True ):
42
46
"""
43
47
Return cropped image.
@@ -50,20 +54,22 @@ def crop_img(im, inner_size, color=True, test=True):
50
54
If True, crop the center of images.
51
55
"""
52
56
if color :
53
- height , width = max (inner_size , im .shape [1 ]), max (inner_size , im .shape [2 ])
57
+ height , width = max (inner_size , im .shape [1 ]), max (inner_size ,
58
+ im .shape [2 ])
54
59
padded_im = np .zeros ((3 , height , width ))
55
60
startY = (height - im .shape [1 ]) / 2
56
61
startX = (width - im .shape [2 ]) / 2
57
62
endY , endX = startY + im .shape [1 ], startX + im .shape [2 ]
58
- padded_im [:, startY : endY , startX : endX ] = im
63
+ padded_im [:, startY :endY , startX :endX ] = im
59
64
else :
60
65
im = im .astype ('float32' )
61
- height , width = max (inner_size , im .shape [0 ]), max (inner_size , im .shape [1 ])
66
+ height , width = max (inner_size , im .shape [0 ]), max (inner_size ,
67
+ im .shape [1 ])
62
68
padded_im = np .zeros ((height , width ))
63
69
startY = (height - im .shape [0 ]) / 2
64
70
startX = (width - im .shape [1 ]) / 2
65
71
endY , endX = startY + im .shape [0 ], startX + im .shape [1 ]
66
- padded_im [startY : endY , startX : endX ] = im
72
+ padded_im [startY :endY , startX :endX ] = im
67
73
if test :
68
74
startY = (height - inner_size ) / 2
69
75
startX = (width - inner_size ) / 2
@@ -72,19 +78,21 @@ def crop_img(im, inner_size, color=True, test=True):
72
78
startX = np .random .randint (0 , width - inner_size + 1 )
73
79
endY , endX = startY + inner_size , startX + inner_size
74
80
if color :
75
- pic = padded_im [:, startY : endY , startX : endX ]
81
+ pic = padded_im [:, startY :endY , startX :endX ]
76
82
else :
77
- pic = padded_im [startY : endY , startX : endX ]
83
+ pic = padded_im [startY :endY , startX :endX ]
78
84
if (not test ) and (np .random .randint (2 ) == 0 ):
79
85
pic = flip (pic )
80
86
return pic
81
87
88
+
82
89
def decode_jpeg (jpeg_string ):
83
90
np_array = np .array (Image .open (StringIO (jpeg_string )))
84
91
if len (np_array .shape ) == 3 :
85
92
np_array = np .transpose (np_array , (2 , 0 , 1 ))
86
93
return np_array
87
94
95
+
88
96
def preprocess_img (im , img_mean , crop_size , is_train , color = True ):
89
97
"""
90
98
Does data augmentation for images.
@@ -99,6 +107,7 @@ def preprocess_img(im, img_mean, crop_size, is_train, color=True):
99
107
pic -= img_mean
100
108
return pic .flatten ()
101
109
110
+
102
111
def load_meta (meta_path , mean_img_size , crop_size , color = True ):
103
112
"""
104
113
Return the loaded meta file.
@@ -109,17 +118,18 @@ def load_meta(meta_path, mean_img_size, crop_size, color=True):
109
118
mean = np .load (meta_path )['data_mean' ]
110
119
border = (mean_img_size - crop_size ) / 2
111
120
if color :
112
- assert (mean_img_size * mean_img_size * 3 == mean .shape [0 ])
121
+ assert (mean_img_size * mean_img_size * 3 == mean .shape [0 ])
113
122
mean = mean .reshape (3 , mean_img_size , mean_img_size )
114
- mean = mean [:, border : border + crop_size ,
115
- border : border + crop_size ].astype ('float32' )
123
+ mean = mean [:, border :border + crop_size , border : border +
124
+ crop_size ].astype ('float32' )
116
125
else :
117
- assert (mean_img_size * mean_img_size == mean .shape [0 ])
126
+ assert (mean_img_size * mean_img_size == mean .shape [0 ])
118
127
mean = mean .reshape (mean_img_size , mean_img_size )
119
- mean = mean [border : border + crop_size ,
120
- border : border + crop_size ].astype ('float32' )
128
+ mean = mean [border :border + crop_size , border : border +
129
+ crop_size ].astype ('float32' )
121
130
return mean
122
131
132
+
123
133
def load_image (img_path , is_color = True ):
124
134
"""
125
135
Load image and return.
@@ -130,6 +140,7 @@ def load_image(img_path, is_color=True):
130
140
img .load ()
131
141
return img
132
142
143
+
133
144
def oversample (img , crop_dims ):
134
145
"""
135
146
image : iterable of (H x W x K) ndarrays
@@ -152,50 +163,53 @@ def oversample(img, crop_dims):
152
163
for j in w_indices :
153
164
crops_ix [curr ] = (i , j , i + crop_dims [0 ], j + crop_dims [1 ])
154
165
curr += 1
155
- crops_ix [4 ] = np .tile (im_center , (1 , 2 )) + np .concatenate ([
156
- - crop_dims / 2.0 ,
157
- crop_dims / 2.0
158
- ])
166
+ crops_ix [4 ] = np .tile (im_center , (1 , 2 )) + np .concatenate (
167
+ [- crop_dims / 2.0 , crop_dims / 2.0 ])
159
168
crops_ix = np .tile (crops_ix , (2 , 1 ))
160
169
161
170
# Extract crops
162
- crops = np .empty ((10 * len (img ), crop_dims [0 ], crop_dims [1 ],
163
- im_shape [- 1 ]), dtype = np .float32 )
171
+ crops = np .empty (
172
+ (10 * len (img ), crop_dims [0 ], crop_dims [1 ], im_shape [- 1 ]),
173
+ dtype = np .float32 )
164
174
ix = 0
165
175
for im in img :
166
176
for crop in crops_ix :
167
177
crops [ix ] = im [crop [0 ]:crop [2 ], crop [1 ]:crop [3 ], :]
168
178
ix += 1
169
- crops [ix - 5 :ix ] = crops [ix - 5 :ix , :, ::- 1 , :] # flip for mirrors
179
+ crops [ix - 5 :ix ] = crops [ix - 5 :ix , :, ::- 1 , :] # flip for mirrors
170
180
return crops
171
181
182
+
172
183
class ImageTransformer :
173
- def __init__ (self , transpose = None ,
174
- channel_swap = None , mean = None , is_color = True ):
184
+ def __init__ (self ,
185
+ transpose = None ,
186
+ channel_swap = None ,
187
+ mean = None ,
188
+ is_color = True ):
175
189
self .transpose = transpose
176
190
self .channel_swap = None
177
191
self .mean = None
178
- self .is_color = is_color
192
+ self .is_color = is_color
179
193
180
- def set_transpose (self , order ):
194
+ def set_transpose (self , order ):
181
195
if self .is_color :
182
- assert 3 == len (order )
196
+ assert 3 == len (order )
183
197
self .transpose = order
184
198
185
- def set_channel_swap (self , order ):
199
+ def set_channel_swap (self , order ):
186
200
if self .is_color :
187
- assert 3 == len (order )
201
+ assert 3 == len (order )
188
202
self .channel_swap = order
189
203
190
204
def set_mean (self , mean ):
191
205
# mean value, may be one value per channel
192
206
if mean .ndim == 1 :
193
- mean = mean [:, np .newaxis , np .newaxis ]
194
- else :
207
+ mean = mean [:, np .newaxis , np .newaxis ]
208
+ else :
195
209
# elementwise mean
196
210
if self .is_color :
197
211
assert len (mean .shape ) == 3
198
- self .mean = mean
212
+ self .mean = mean
199
213
200
214
def transformer (self , data ):
201
215
if self .transpose is not None :
0 commit comments