Skip to content

Commit

Permalink
[MRG] Update image resize functinality (#326)
Browse files Browse the repository at this point in the history
* optimize image resizing with is_resize_needed function

* update-1

* fix
  • Loading branch information
satyakesav authored and haifeng-jin committed Nov 16, 2018
1 parent ce72d39 commit 407687e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
32 changes: 20 additions & 12 deletions autokeras/image/image_supervised.py
Expand Up @@ -132,15 +132,21 @@ def loss(self):
def fit(self, x, y, x_test=None, y_test=None, time_limit=None):
x = np.array(x)

if len(x.shape) != 0 and len(x[0].shape) == 3:
if self.verbose:
print("Preprocessing the images.")
if self.verbose:
print("Preprocessing the images.")

if x is not None and (len(x.shape) == 4 or len(x.shape) == 1 and len(x[0].shape) == 3):
self.resize_height, self.resize_width = compute_image_resize_params(x)

if self.resize_height is not None:
x = resize_image_data(x, self.resize_height, self.resize_width)
if x_test is not None:
x_test = resize_image_data(x_test, self.resize_height, self.resize_width)
if self.verbose:
print("Preprocessing finished.")
print("x is ", x.shape)

if self.resize_height is not None:
x_test = resize_image_data(x_test, self.resize_height, self.resize_width)

if self.verbose:
print("Preprocessing finished.")

y = np.array(y).flatten()
validate_xy(x, y)
Expand Down Expand Up @@ -206,7 +212,8 @@ def inverse_transform_y(self, output):

def evaluate(self, x_test, y_test):
"""Return the accuracy score between predict value and `y_test`."""
if len(x_test.shape) != 0 and len(x_test[0].shape) == 3:
print("x test is ", x_test.shape)
if self.resize_height is not None:
x_test = resize_image_data(x_test, self.resize_height, self.resize_width)
y_predict = self.predict(x_test)
return self.metric().evaluate(y_test, y_predict)
Expand All @@ -225,10 +232,11 @@ def final_fit(self, x_train, y_train, x_test, y_test, trainer_args=None, retrain
if trainer_args is None:
trainer_args = {'max_no_improvement_num': 30}

if len(x_train.shape) != 0 and len(x_train[0].shape) == 3:
if self.resize_height is not None:
x_train = resize_image_data(x_train, self.resize_height, self.resize_width)
if x_test is not None:
x_test = resize_image_data(x_test, self.resize_height, self.resize_width)

if self.resize_height is not None:
x_test = resize_image_data(x_test, self.resize_height, self.resize_width)

y_train = self.transform_y(y_train)
y_test = self.transform_y(y_test)
Expand Down Expand Up @@ -392,7 +400,7 @@ def inverse_transform_y(self, output):

def evaluate(self, x_test, y_test):
"""Return the accuracy score between predict value and `y_test`."""
if len(x_test.shape) != 0 and len(x_test.shape) == 3:
if self.resize_height is not None:
x_test = resize_image_data(x_test, self.resize_height, self.resize_width)
y_predict = self.predict(x_test)
return self.metric().evaluate(y_test, y_predict)
12 changes: 10 additions & 2 deletions autokeras/utils.py
Expand Up @@ -201,6 +201,9 @@ def compute_image_resize_params(data):
median height: Median height of all images in the data.
median width: Median width of all images in the data.
"""
if len(data.shape) == 1 and len(data[0].shape) != 3:
return None, None

median_height, median_width = numpy.median(numpy.array(list(map(lambda x: x.shape, data))), axis=0)[:2]

if median_height * median_width > Constant.MAX_IMAGE_SIZE:
Expand All @@ -211,7 +214,7 @@ def compute_image_resize_params(data):
return int(median_height), int(median_width)


def resize_image_data(data, height, weight):
def resize_image_data(data, height, width):
"""Resize images to provided height and width.
Resize all images in data to size h x w x c, where h is the height, w is the width and c is the number of channels.
Expand All @@ -225,13 +228,18 @@ def resize_image_data(data, height, weight):
Returns:
data: Resize data.
"""
if data is None:
return data

if len(data.shape) == 4 and data[0].shape[0] == height and data[0].shape[1] == width:
return data

output_data = []
for im in data:
if len(im.shape) != 3:
return data
output_data.append(resize(image=im,
output_shape=(height, weight, im.shape[-1]),
output_shape=(height, width, im.shape[-1]),
mode='edge',
preserve_range=True))

Expand Down

0 comments on commit 407687e

Please sign in to comment.