From 407687eed2edb749b79ec2ba764b836eecf3f313 Mon Sep 17 00:00:00 2001 From: Satya Kesav Date: Fri, 16 Nov 2018 12:44:56 -0600 Subject: [PATCH] [MRG] Update image resize functinality (#326) * optimize image resizing with is_resize_needed function * update-1 * fix --- autokeras/image/image_supervised.py | 32 ++++++++++++++++++----------- autokeras/utils.py | 12 +++++++++-- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/autokeras/image/image_supervised.py b/autokeras/image/image_supervised.py index 041bba67c..6414d84ea 100644 --- a/autokeras/image/image_supervised.py +++ b/autokeras/image/image_supervised.py @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/autokeras/utils.py b/autokeras/utils.py index e73b24240..1324913a1 100644 --- a/autokeras/utils.py +++ b/autokeras/utils.py @@ -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: @@ -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. @@ -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))