def __getitem__(self, batch_idx): X_batch = np.zeros((self.batch_size, ) + self.target_size) y_batch = np.zeros((self.batch_size, len(self.classes))) filenames = [] labels = [] aux = [] current_index = batch_idx * self.batch_size for i in range(self.batch_size): filenames.append(self.filenames[current_index]) labels.append(self.labels[current_index]) if hasattr(self, "auxiliary"): aux.append(self.auxiliary[current_index]) current_index += 1 color_mode = "rgb" if self.target_size[2] == 3 else "grayscale" target_size = (self.target_size[0], self.target_size[1]) X_batch = np.array([ self.transformer.random_transform( img_to_array( load_img(fn, color_mode=color_mode, target_size=target_size))) for fn in filenames ]) y_batch = to_categorical(labels, num_classes=len(self.classes)) if hasattr(self, "auxiliary"): return X_batch, [y_batch, aux] else: return X_batch, y_batch
def test_image_data_generator(all_test_images): for test_images in all_test_images: img_list = [] for im in test_images: img_list.append(utils.img_to_array(im)[None, ...]) image_data_generator.ImageDataGenerator( featurewise_center=True, samplewise_center=True, featurewise_std_normalization=True, samplewise_std_normalization=True, zca_whitening=True, rotation_range=90., width_shift_range=0.1, height_shift_range=0.1, shear_range=0.5, zoom_range=0.2, channel_shift_range=0., brightness_range=(1, 5), fill_mode='nearest', cval=0.5, horizontal_flip=True, vertical_flip=True, interpolation_order=1 )
def test_batch_standardize(all_test_images): # ImageDataGenerator.standardize should work on batches for test_images in all_test_images: img_list = [] for im in test_images: img_list.append(utils.img_to_array(im)[None, ...]) images = np.vstack(img_list) generator = image_data_generator.ImageDataGenerator( featurewise_center=True, samplewise_center=True, featurewise_std_normalization=True, samplewise_std_normalization=True, zca_whitening=True, rotation_range=90., width_shift_range=0.1, height_shift_range=0.1, shear_range=0.5, zoom_range=0.2, channel_shift_range=0., brightness_range=(1, 5), fill_mode='nearest', cval=0.5, horizontal_flip=True, vertical_flip=True) generator.fit(images, augment=True) transformed = np.copy(images) for i, im in enumerate(transformed): transformed[i] = generator.random_transform(im) transformed = generator.standardize(transformed)
def resize_to_max(self, im, n): ''' Resize self.original so its longest side has n pixels (maintain proportion) ''' w, h = im.size size = (n, int(n * h / w)) if w > h else (int(n * w / h), n) return img_to_array(im.resize(size))
def run(self): model = load_model("./models/" + self.model_selected.get()) for i, img_path in enumerate(self.img_paths): img_array = img_to_array(load_img(img_path, color_mode="grayscale")) / 255 pred = predict(img_array, model, self.model_threshold_slider.get()) save_img(self.destination_path_field.get() + "/" + str(i) + ".png", pred)
def run(self): model = load_model("./models/" + self.model_selected.get()) for img_path in self.img_paths: img_array = img_to_array(load_img(img_path, color_mode="grayscale")) / 255 pred = predict(img_array, model, self.model_threshold_slider.get()) save_img( self.destination_path_field.get() + "/" + os.path.splitext(img_path.split("/")[-1])[0] + ".png", pred)
def test_image_data_generator_with_validation_split(all_test_images): for test_images in all_test_images: img_list = [] for im in test_images: img_list.append(utils.img_to_array(im)[None, ...]) images = np.vstack(img_list) labels = np.concatenate([ np.zeros((int(len(images) / 2), )), np.ones((int(len(images) / 2), )) ]) generator = image_data_generator.ImageDataGenerator( validation_split=0.5) # training and validation sets would have different # number of classes, because labels are sorted with pytest.raises(ValueError, match='Training and validation subsets ' 'have different number of classes after ' 'the split.*'): generator.flow(images, labels, shuffle=False, batch_size=10, subset='validation') labels = np.concatenate([ np.zeros((int(len(images) / 4), )), np.ones((int(len(images) / 4), )), np.zeros((int(len(images) / 4), )), np.ones((int(len(images) / 4), )) ]) seq = generator.flow(images, labels, shuffle=False, batch_size=10, subset='validation') x, y = seq[0] assert 2 == len(np.unique(y)) seq = generator.flow(images, labels, shuffle=False, batch_size=10, subset='training') x2, y2 = seq[0] assert 2 == len(np.unique(y2)) with pytest.raises(ValueError): generator.flow(images, np.arange(images.shape[0]), shuffle=False, batch_size=3, subset='foo')
def augmentation(x): ''' Here the actual data augmentation takes place''' x = img_to_array(x) x = x.reshape((1,) + x.shape) i=0 for batch in datagenerator.flow(x, batch_size=1, save_to_dir='preview', #Target directory/folder save_prefix='images', #Name of augmented images save_format='jpeg'): #Extension of target images i += 1 if i > 5: #No of target images break
def test_fit_rescale(all_test_images): rescale = 1. / 255 for test_images in all_test_images: img_list = [] for im in test_images: img_list.append(utils.img_to_array(im)[None, ...]) images = np.vstack(img_list) # featurewise_center test generator = image_data_generator.ImageDataGenerator( rescale=rescale, featurewise_center=True, dtype='float64') generator.fit(images) batch = generator.flow(images, batch_size=8).next() assert abs(np.mean(batch)) < 1e-6 # featurewise_std_normalization test generator = image_data_generator.ImageDataGenerator( rescale=rescale, featurewise_center=True, featurewise_std_normalization=True, dtype='float64') generator.fit(images) batch = generator.flow(images, batch_size=8).next() assert abs(np.mean(batch)) < 1e-6 assert abs(1 - np.std(batch)) < 1e-5 # zca_whitening test generator = image_data_generator.ImageDataGenerator( rescale=rescale, featurewise_center=True, zca_whitening=True, dtype='float64') generator.fit(images) batch = generator.flow(images, batch_size=8).next() batch = np.reshape(batch, (batch.shape[0], batch.shape[1] * batch.shape[2] * batch.shape[3])) # Y * Y_T = n * I, where Y = W * X identity = np.dot(batch, batch.T) / batch.shape[0] assert ((np.abs(identity) - np.identity(identity.shape[0])) < 1e-6).all()
def imagenet_generator(self): if len(self.image_paths) == 0: raise ValueError("Why do you not have image paths?") indices = np.arange(len(self.image_paths)) while True: _index = 0 # if shuffle, shuffle indices if self.shuffle: np.random.shuffle(indices) # another loop keep track of generations for current epoch while _index < len(self.image_paths) and \ _index < self.steps_per_epoch * self.batch: # images and labels (classes) for the current batch images_to_yield = [] labels_to_yield = [] # indices for current batch indices_to_yield = indices[_index: _index + self.batch] # assemble batch of images _curr_img_paths = self.image_paths[indices_to_yield] for _cip in _curr_img_paths: x = img_to_array(load_img(_cip, target_size=self.img_size)) # preprocess Imagenet data # https://github.com/keras-team/keras-applications/blob/master/keras_applications/mobilenet.py#L75-L84 pre_processed_img = iu.preprocess_input( np.asarray(x), mode="tf") images_to_yield.append(pre_processed_img) # assemble batch of labels if there are any classes if self.no_classes > 0: for _cip in _curr_img_paths: pl = path_leaf(_cip)[:-5] labels_to_yield.append(self.cls_dict[pl]) _index += self.batch yield (np.asarray(images_to_yield), np.asarray(labels_to_yield))
def test_image_data_generator_flow(all_test_images, tmpdir): for test_images in all_test_images: img_list = [] for im in test_images: img_list.append(utils.img_to_array(im)[None, ...]) images = np.vstack(img_list) dsize = images.shape[0] generator = image_data_generator.ImageDataGenerator( featurewise_center=True, samplewise_center=True, featurewise_std_normalization=True, samplewise_std_normalization=True, zca_whitening=True, rotation_range=90., width_shift_range=0.1, height_shift_range=0.1, shear_range=0.5, zoom_range=0.2, channel_shift_range=0., brightness_range=(1, 5), fill_mode='nearest', cval=0.5, horizontal_flip=True, vertical_flip=True, interpolation_order=1) generator.flow(images, np.arange(images.shape[0]), shuffle=False, save_to_dir=str(tmpdir), batch_size=3) generator.flow(images, np.arange(images.shape[0]), shuffle=False, sample_weight=np.arange(images.shape[0]) + 1, save_to_dir=str(tmpdir), batch_size=3) # Test with `shuffle=True` generator.flow(images, np.arange(images.shape[0]), shuffle=True, save_to_dir=str(tmpdir), batch_size=3, seed=42) # Test without y generator.flow(images, None, shuffle=True, save_to_dir=str(tmpdir), batch_size=3) # Test with a single miscellaneous input data array x_misc1 = np.random.random(dsize) generator.flow((images, x_misc1), np.arange(dsize), shuffle=False, batch_size=2) # Test with two miscellaneous inputs x_misc2 = np.random.random((dsize, 3, 3)) generator.flow((images, [x_misc1, x_misc2]), np.arange(dsize), shuffle=False, batch_size=2) # Test cases with `y = None` generator.flow(images, None, batch_size=3) generator.flow((images, x_misc1), None, batch_size=3, shuffle=False) generator.flow((images, [x_misc1, x_misc2]), None, batch_size=3, shuffle=False) generator = image_data_generator.ImageDataGenerator( validation_split=0.2) generator.flow(images, batch_size=3) # Test some failure cases: x_misc_err = np.random.random((dsize + 1, 3, 3)) with pytest.raises(ValueError) as e_info: generator.flow((images, x_misc_err), np.arange(dsize), batch_size=3) assert str(e_info.value).find('All of the arrays in') != -1 with pytest.raises(ValueError) as e_info: generator.flow((images, x_misc1), np.arange(dsize + 1), batch_size=3) assert str( e_info.value).find('`x` (images tensor) and `y` (labels) ') != -1 # Test `flow` behavior as Sequence generator.flow(images, np.arange(images.shape[0]), shuffle=False, save_to_dir=str(tmpdir), batch_size=3) # Test with `shuffle=True` generator.flow(images, np.arange(images.shape[0]), shuffle=True, save_to_dir=str(tmpdir), batch_size=3, seed=123) # test order_interpolation labels = np.array([[2, 2, 0, 2, 2], [1, 3, 2, 3, 1], [2, 1, 0, 1, 2], [3, 1, 0, 2, 0], [3, 1, 3, 2, 1]]) label_generator = image_data_generator.ImageDataGenerator( rotation_range=90., interpolation_order=0) label_generator.flow(x=labels[np.newaxis, ..., np.newaxis], seed=123)
def _get_batches_of_transformed_samples(self, index_array): """Gets a batch of transformed samples. # Arguments index_array: Array of sample indices to include in batch. # Returns A batch of transformed samples. """ y = self.classes[index_array] # creo una matrice indice - classe batch = np.concatenate( [np.expand_dims(index_array, 1), np.expand_dims(y, 1)], axis=1) batch = pd.DataFrame(batch, columns=['idx', 'class']) batch = batch.groupby('class') # Per ogni classe genero K sample # batch totale 128 try: batch = batch.apply( lambda _x: _x.sample(18).reset_index(drop=True)) except ValueError: batch = batch.apply( lambda _x: _x.sample(18, replace=True).reset_index(drop=True)) print("This batch is garbage") index_array = np.array(batch['idx']) index_array = np.random.permutation(index_array) batch_x = np.zeros((len(index_array), ) + self.image_shape, dtype=self.dtype) # build batch of image data # self.filepaths is dynamic, is better to call it once outside the loop filepaths = self.filepaths for i, j in enumerate(index_array): img = load_img(filepaths[j], color_mode=self.color_mode, target_size=self.target_size, interpolation=self.interpolation) x = img_to_array(img, data_format=self.data_format) # Pillow images should be closed after `load_img`, # but not PIL images. if hasattr(img, 'close'): img.close() if self.image_data_generator: params = self.image_data_generator.get_random_transform( x.shape) x = self.image_data_generator.apply_transform(x, params) x = self.image_data_generator.standardize(x) batch_x[i] = x # optionally save augmented images to disk for debugging purposes if self.save_to_dir: for i, j in enumerate(index_array): img = array_to_img(batch_x[i], self.data_format, scale=True) fname = '{prefix}_{index}_{hash}.{format}'.format( prefix=self.save_prefix, index=j, hash=np.random.randint(1e7), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) # build batch of labels if self.class_mode == 'input': batch_y = batch_x.copy() elif self.class_mode in {'binary', 'sparse'}: batch_y = np.empty(len(batch_x), dtype=self.dtype) for i, n_observation in enumerate(index_array): batch_y[i] = self.classes[n_observation] elif self.class_mode == 'categorical': batch_y = np.zeros((len(batch_x), len(self.class_indices)), dtype=self.dtype) for i, n_observation in enumerate(index_array): batch_y[i, self.classes[n_observation]] = 1. elif self.class_mode == 'multi_output': batch_y = [output[index_array] for output in self.labels] elif self.class_mode == 'raw': batch_y = self.labels[index_array] else: return batch_x if self.sample_weight is None: return [batch_x, batch_x, batch_x, np.argmax(batch_y, axis=1)],\ [np.zeros((len(batch_x), 4096), dtype='float16'), batch_y] else: return batch_x, batch_y, self.sample_weight[index_array]
def sample(self, X=None, sample_size=None, standardize=True): """ Retrived fix-sized image tersors Parameters ---------- X : 2D-array. Default is None Expanded sub-index array of the dataframe. If None, X = np.arange(n_samples)[:, np.newaxis]. sample_size : int. Default is None. The number of samples to be retrieved. If None, sample_size = X.shape[0] standardize : bool. Default is True. Whether to transform the image tersor data. If False, return direct results of `img_to_array`. """ if X is None: X = np.arange(self.dataframe.shape[0])[:, np.newaxis] if not sample_size: sample_size = X.shape[0] retrieved_X = np.zeros((sample_size, ) + self.image_shape, dtype=self.dtype) filepaths = self.filepaths indices = np.squeeze(X) sample_index = self.rng_.choice(indices, size=sample_size, replace=False) for i, j in enumerate(sample_index): img = load_img(filepaths[j], color_mode=self.color_mode, target_size=self.target_size, interpolation=self.interpolation) x = img_to_array(img, data_format=self.data_format) if hasattr(img, 'close'): img.close() if not standardize: retrieved_X[i] = x continue params = self.get_random_transform(x.shape) x = self.apply_transform(x, params) x = self.standardize(x) retrieved_X[i] = x if self.save_to_dir: for i, j in enumerate(sample_index): img = array_to_img(retrieved_X[i], self.data_format, scale=True) fname = '{prefix}_{index}_{hash}.{format}'.format( prefix=self.save_prefix, index=j, hash=np.random.randint(1e7), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) # retrieve labels if self.class_mode == 'input': retrieved_y = retrieved_X.copy() elif self.class_mode in {'binary', 'sparse'}: retrieved_y = np.empty(sample_size, dtype=self.dtype) for i, n_observation in enumerate(sample_index): retrieved_y[i] = self.classes[n_observation] elif self.class_mode == 'categorical': retrieved_y = np.zeros((sample_size, len(self.class_indices)), dtype=self.dtype) for i, n_observation in enumerate(sample_index): retrieved_y[i, self.classes[n_observation]] = 1. elif self.class_mode == 'multi_output': retrieved_y = [output[sample_index] for output in self.labels] elif self.class_mode == 'raw': retrieved_y = self.labels[sample_index] else: return retrieved_X return retrieved_X, retrieved_y
def image_to_array(self, img): pil_img = Image.open(BytesIO(img)).convert('RGB').resize((160,120), Image.BILINEAR) return img_to_array(pil_img)
def visualize_camap(model, fold, test_imgs): last_conv_layer = model.get_layer('block5_conv3') iterateBE0 = get_iterates(0, 0, model, last_conv_layer) iterateBE1 = get_iterates(0, 1, model, last_conv_layer) iterateBE2 = get_iterates(0, 2, model, last_conv_layer) iterateICM0 = get_iterates(1, 0, model, last_conv_layer) iterateICM1 = get_iterates(1, 1, model, last_conv_layer) iterateICM2 = get_iterates(1, 2, model, last_conv_layer) iterateTE0 = get_iterates(2, 0, model, last_conv_layer) iterateTE1 = get_iterates(2, 1, model, last_conv_layer) iterateTE2 = get_iterates(2, 2, model, last_conv_layer) CAM_DICT = { "BE0": iterateBE0, "BE1": iterateBE1, "BE2": iterateBE2, "ICM0": iterateICM0, "ICM1": iterateICM1, "ICM2": iterateICM2, "TE0": iterateTE0, "TE1": iterateTE1, "TE2": iterateTE2 } for img_name in test_imgs: print(img_name) img_arr = load_img(join(args.img_path, img_name), color_mode='rgb', target_size=(PATCH_SIZE, PATCH_SIZE)) img_arr = img_to_array(img_arr) x = np.expand_dims(img_arr, axis=0) # x = preprocess_input(x) img = imread(join(args.img_path, img_name)) img = np.uint8(255 * resize(img, (img_arr.shape[1], img_arr.shape[0]))) preds = model.predict(x) for i, grade in enumerate(['BE', 'ICM', 'TE']): iterate = CAM_DICT["{}{}".format(grade, np.argmax(preds[i]))] pooled_grads_value, conv_layer_output_value = iterate([x]) pooled_grads_value_resized = pooled_grads_value * np.ones( conv_layer_output_value.shape) temp = np.multiply(conv_layer_output_value, pooled_grads_value_resized) heatmap = np.mean(temp, axis=-1) heatmap = np.maximum(heatmap, 0) heatmap /= (np.max(heatmap) + K.epsilon()) heatmap = resize(heatmap, (img_arr.shape[1], img_arr.shape[0])) heatmap = np.uint8(255 * heatmap) plt.figure() plt.imshow(heatmap, cmap=plt.get_cmap('jet'), alpha=0.4) plt.imshow(gray2rgb(img), alpha=0.6) cur_ax = plt.gca() cur_ax.axes.get_xaxis().set_visible(False) cur_ax.axes.get_yaxis().set_visible(False) plt.savefig(join( CAMAP_PATH, img_name[:-4] + 'fold{}'.format(fold) + grade + img_name[-4:]), bbox_inches='tight', pad_inches=0) plt.close()
def test_array_to_img_and_img_to_array(): height, width = 10, 8 # Test the data format # Test RGB 3D x = np.random.random((3, height, width)) img = utils.array_to_img(x, data_format='channels_first') assert img.size == (width, height) x = utils.img_to_array(img, data_format='channels_first') assert x.shape == (3, height, width) # Test RGBA 3D x = np.random.random((4, height, width)) img = utils.array_to_img(x, data_format='channels_first') assert img.size == (width, height) x = utils.img_to_array(img, data_format='channels_first') assert x.shape == (4, height, width) # Test 2D x = np.random.random((1, height, width)) img = utils.array_to_img(x, data_format='channels_first') assert img.size == (width, height) x = utils.img_to_array(img, data_format='channels_first') assert x.shape == (1, height, width) # grayscale 32-bit signed integer x = np.array( np.random.randint(-2147483648, 2147483647, (1, height, width)), dtype=np.int32 ) img = utils.array_to_img(x, data_format='channels_first') assert img.size == (width, height) x = utils.img_to_array(img, data_format='channels_first') assert x.shape == (1, height, width) # Test tf data format # Test RGB 3D x = np.random.random((height, width, 3)) img = utils.array_to_img(x, data_format='channels_last') assert img.size == (width, height) x = utils.img_to_array(img, data_format='channels_last') assert x.shape == (height, width, 3) # Test RGBA 3D x = np.random.random((height, width, 4)) img = utils.array_to_img(x, data_format='channels_last') assert img.size == (width, height) x = utils.img_to_array(img, data_format='channels_last') assert x.shape == (height, width, 4) # Test 2D x = np.random.random((height, width, 1)) img = utils.array_to_img(x, data_format='channels_last') assert img.size == (width, height) x = utils.img_to_array(img, data_format='channels_last') assert x.shape == (height, width, 1) # grayscale 16-bit signed integer x = np.array( np.random.randint(-2147483648, 2147483647, (height, width, 1)), dtype=np.int16 ) img = utils.array_to_img(x, data_format='channels_last') assert img.size == (width, height) x = utils.img_to_array(img, data_format='channels_last') assert x.shape == (height, width, 1) # grayscale 32-bit signed integer x = np.array( np.random.randint(-2147483648, 2147483647, (height, width, 1)), dtype=np.int32 ) img = utils.array_to_img(x, data_format='channels_last') assert img.size == (width, height) x = utils.img_to_array(img, data_format='channels_last') assert x.shape == (height, width, 1) # Test invalid use case with pytest.raises(ValueError): x = np.random.random((height, width)) # not 3D img = utils.array_to_img(x, data_format='channels_first') with pytest.raises(ValueError): x = np.random.random((height, width, 3)) # unknown data_format img = utils.array_to_img(x, data_format='channels') with pytest.raises(ValueError): # neither RGB, RGBA, or gray-scale x = np.random.random((height, width, 5)) img = utils.array_to_img(x, data_format='channels_last') with pytest.raises(ValueError): x = np.random.random((height, width, 3)) # unknown data_format img = utils.img_to_array(x, data_format='channels') with pytest.raises(ValueError): # neither RGB, RGBA, or gray-scale x = np.random.random((height, width, 5, 3)) img = utils.img_to_array(x, data_format='channels_last')
def _get_batches_of_transformed_samples(self, index_array): """Gets a batch of transformed samples. # Arguments index_array: Array of sample indices to include in batch. # Returns A batch of transformed samples. """ index_array = self.X[index_array] batch_x = np.zeros((len(index_array), ) + self.image_shape, dtype=self.dtype) # build batch of image data # self.filepaths is dynamic, is better to call it once outside the loop filepaths = self.filepaths for i, j in enumerate(index_array): img = load_img(filepaths[j], color_mode=self.color_mode, target_size=self.target_size, interpolation=self.interpolation) x = img_to_array(img, data_format=self.data_format) # Pillow images should be closed after `load_img`, # but not PIL images. if hasattr(img, 'close'): img.close() if self.image_data_generator: params = self.image_data_generator.get_random_transform( x.shape) x = self.image_data_generator.apply_transform(x, params) x = self.image_data_generator.standardize(x) batch_x[i] = x # optionally save augmented images to disk for debugging purposes if self.save_to_dir: for i, j in enumerate(index_array): img = array_to_img(batch_x[i], self.data_format, scale=True) fname = '{prefix}_{index}_{hash}.{format}'.format( prefix=self.save_prefix, index=j, hash=np.random.randint(1e7), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) # build batch of labels if self.class_mode == 'input': batch_y = batch_x.copy() elif self.class_mode in {'binary', 'sparse'}: batch_y = np.empty(len(batch_x), dtype=self.dtype) for i, n_observation in enumerate(index_array): batch_y[i] = self.classes[n_observation] elif self.class_mode == 'categorical': batch_y = np.zeros((len(batch_x), len(self.class_indices)), dtype=self.dtype) for i, n_observation in enumerate(index_array): batch_y[i, self.classes[n_observation]] = 1. elif self.class_mode == 'multi_output': batch_y = [output[index_array] for output in self.labels] elif self.class_mode == 'raw': batch_y = self.labels[index_array] else: return batch_x if self.sample_weight is None: return batch_x, batch_y else: return batch_x, batch_y, self.sample_weight[index_array]
def _get_batches_of_transformed_samples(self, index_array): """Gets a batch of transformed samples. # Arguments index_array: Array of sample indices to include in batch. # Returns A batch of transformed samples in one-hot-encoded format. """ additional_bg_class = 0 if self.include_background: additional_bg_class = 1 batch_x = np.zeros( (len(index_array), ) + (self.target_size[0], self.target_size[1], self.num_classes + additional_bg_class), dtype=self.dtype) # build batch of image data # self.filepaths is dynamic, is better to call it once outside the loop filepaths = self.filepaths known_label_keys = self.dropped_labels_memory.keys() known_label_drops = {} unset_label_drops = [] for i, j in enumerate(index_array): if j in known_label_keys: known_label_drops[i] = j else: unset_label_drops.append(i) one_hot_map = np.zeros((self.target_size[0], self.target_size[1], self.num_classes + additional_bg_class), dtype=np.float32) # Iterate over all classes params = None reserved_pixels = None for k in range(self.num_classes): filepath = os.path.join(self.directory, self._mask_classes[k], filepaths[j]) img = load_img(filepath, color_mode=self.color_mode, target_size=self.target_size, interpolation=self.interpolation) x = img_to_array(img, data_format=self.data_format) # Pillow images should be closed after `load_img`, # but not PIL images. if hasattr(img, 'close'): img.close() if self.image_data_generator: # Params need to be set once for every image (not for every mask) if params is None: params = self.image_data_generator.get_random_transform( x.shape) x = self.image_data_generator.apply_transform(x, params) x = self.image_data_generator.standardize(x) if reserved_pixels is None: reserved_pixels = np.zeros(x.shape) x = np.where(reserved_pixels, 0, x) reserved_pixels += x one_hot_map += self.get_one_hot_map( x, k, background=self.background_color, additional_bg_class=additional_bg_class) # If one_hot_map has a max value >1 whe have overlapping classes -> prohibited one_hot_map = one_hot_map.numpy() if one_hot_map.max() > 1: raise ValueError( 'Mask mismatch: classes are not mutually exclusive (multiple class definitions for ' 'one pixel).') if self.include_background: # Background class is everywhere, where the one-hot encoding has only zeros one_hot_map = tf.where( tf.repeat(tf.reshape( tf.math.count_nonzero( one_hot_map == tf.zeros(self.num_classes + 1), axis=2), [img.height, img.width, 1]), self.num_classes + 1, axis=2) == self.num_classes + 1, tf.one_hot( tf.constant(self.num_classes, shape=one_hot_map.shape[:2]), self.num_classes + 1), one_hot_map) batch_x[i] = one_hot_map # optionally save augmented images to disk for debugging purposes if self.save_to_dir: for i, j in enumerate(index_array): img = array_to_img(batch_x[i], self.data_format, scale=True) fname = '{prefix}_{index}_{hash}.{format}'.format( prefix=self.save_prefix, index=j, hash=np.random.randint(1e7), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) # Only where labels are not already known if len(self.heterogeneously_labeled_masks) > 0: batch_x[unset_label_drops] = self.remove_masks( np.take(batch_x, unset_label_drops, axis=0)) # Known labels for item in known_label_drops.items(): index_in_batch = item[0] index_in_memory = item[1] memory_binary_mask = self.dropped_labels_memory.get( index_in_memory) batch_x[index_in_batch, :, :, :][ memory_binary_mask] = DELETED_MASK_IDENTIFIER # Extend Memory of known deletion masks. delete_mask = np.where(batch_x == DELETED_MASK_IDENTIFIER, True, False) for i in range(len(index_array)): self.dropped_labels_memory[index_array[i]] = delete_mask[ i, :, :, :] return batch_x
#!/usr/bin/python3 from keras.models import load_model from keras_preprocessing.image.utils import load_img, img_to_array import pandas as pd import numpy as np import os ROW_TO_PREDICT = 300 df = pd.read_csv('../data_set/data.csv', header=None, names=['x_col', 'y_col', 'z_col'])[['x_col', 'y_col']] model = load_model('best_model.h5') path = os.path.join('../data_set/', df.iloc[ROW_TO_PREDICT]['x_col']) print(path) image = img_to_array(load_img(path, target_size=(32, 32))) image = np.expand_dims(image, 0) prediction = model.predict(image) print(prediction)
def test_load_img(tmpdir): filename_rgb = str(tmpdir / 'rgb_utils.png') filename_rgba = str(tmpdir / 'rgba_utils.png') filename_grayscale_8bit = str(tmpdir / 'grayscale_8bit_utils.png') filename_grayscale_16bit = str(tmpdir / 'grayscale_16bit_utils.tiff') filename_grayscale_32bit = str(tmpdir / 'grayscale_32bit_utils.tiff') original_rgb_array = np.array(255 * np.random.rand(100, 100, 3), dtype=np.uint8) original_rgb = utils.array_to_img(original_rgb_array, scale=False) original_rgb.save(filename_rgb) original_rgba_array = np.array(255 * np.random.rand(100, 100, 4), dtype=np.uint8) original_rgba = utils.array_to_img(original_rgba_array, scale=False) original_rgba.save(filename_rgba) original_grayscale_8bit_array = np.array(255 * np.random.rand(100, 100, 1), dtype=np.uint8) original_grayscale_8bit = utils.array_to_img(original_grayscale_8bit_array, scale=False) original_grayscale_8bit.save(filename_grayscale_8bit) original_grayscale_16bit_array = np.array( np.random.randint(-2147483648, 2147483647, (100, 100, 1)), dtype=np.int16 ) original_grayscale_16bit = utils.array_to_img(original_grayscale_16bit_array, scale=False, dtype='int16') original_grayscale_16bit.save(filename_grayscale_16bit) original_grayscale_32bit_array = np.array( np.random.randint(-2147483648, 2147483647, (100, 100, 1)), dtype=np.int32 ) original_grayscale_32bit = utils.array_to_img(original_grayscale_32bit_array, scale=False, dtype='int32') original_grayscale_32bit.save(filename_grayscale_32bit) # Test that loaded image is exactly equal to original. loaded_im = utils.load_img(filename_rgb) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_rgb_array.shape assert np.all(loaded_im_array == original_rgb_array) loaded_im = utils.load_img(filename_rgba, color_mode='rgba') loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_rgba_array.shape assert np.all(loaded_im_array == original_rgba_array) loaded_im = utils.load_img(filename_rgb, color_mode='grayscale') loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (original_rgb_array.shape[0], original_rgb_array.shape[1], 1) loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale') loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_grayscale_8bit_array.shape assert np.all(loaded_im_array == original_grayscale_8bit_array) loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale') loaded_im_array = utils.img_to_array(loaded_im, dtype='int16') assert loaded_im_array.shape == original_grayscale_16bit_array.shape assert np.all(loaded_im_array == original_grayscale_16bit_array) # test casting int16 image to float32 loaded_im_array = utils.img_to_array(loaded_im) assert np.allclose(loaded_im_array, original_grayscale_16bit_array) loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale') loaded_im_array = utils.img_to_array(loaded_im, dtype='int32') assert loaded_im_array.shape == original_grayscale_32bit_array.shape assert np.all(loaded_im_array == original_grayscale_32bit_array) # test casting int32 image to float32 loaded_im_array = utils.img_to_array(loaded_im) assert np.allclose(loaded_im_array, original_grayscale_32bit_array) # Test that nothing is changed when target size is equal to original. loaded_im = utils.load_img(filename_rgb, target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_rgb_array.shape assert np.all(loaded_im_array == original_rgb_array) loaded_im = utils.load_img(filename_rgba, color_mode='rgba', target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_rgba_array.shape assert np.all(loaded_im_array == original_rgba_array) loaded_im = utils.load_img(filename_rgb, color_mode='grayscale', target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (original_rgba_array.shape[0], original_rgba_array.shape[1], 1) loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale', target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_grayscale_8bit_array.shape assert np.all(loaded_im_array == original_grayscale_8bit_array) loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale', target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im, dtype='int16') assert loaded_im_array.shape == original_grayscale_16bit_array.shape assert np.all(loaded_im_array == original_grayscale_16bit_array) loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale', target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im, dtype='int32') assert loaded_im_array.shape == original_grayscale_32bit_array.shape assert np.all(loaded_im_array == original_grayscale_32bit_array) # Test down-sampling with bilinear interpolation. loaded_im = utils.load_img(filename_rgb, target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (25, 25, 3) loaded_im = utils.load_img(filename_rgba, color_mode='rgba', target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (25, 25, 4) loaded_im = utils.load_img(filename_rgb, color_mode='grayscale', target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (25, 25, 1) loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale', target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (25, 25, 1) loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale', target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im, dtype='int16') assert loaded_im_array.shape == (25, 25, 1) loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale', target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im, dtype='int32') assert loaded_im_array.shape == (25, 25, 1) # Test down-sampling with nearest neighbor interpolation. loaded_im_nearest = utils.load_img(filename_rgb, target_size=(25, 25), interpolation="nearest") loaded_im_array_nearest = utils.img_to_array(loaded_im_nearest) assert loaded_im_array_nearest.shape == (25, 25, 3) assert np.any(loaded_im_array_nearest != loaded_im_array) loaded_im_nearest = utils.load_img(filename_rgba, color_mode='rgba', target_size=(25, 25), interpolation="nearest") loaded_im_array_nearest = utils.img_to_array(loaded_im_nearest) assert loaded_im_array_nearest.shape == (25, 25, 4) assert np.any(loaded_im_array_nearest != loaded_im_array) loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale', target_size=(25, 25), interpolation="nearest") loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (25, 25, 1) loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale', target_size=(25, 25), interpolation="nearest") loaded_im_array = utils.img_to_array(loaded_im, dtype='int16') assert loaded_im_array.shape == (25, 25, 1) loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale', target_size=(25, 25), interpolation="nearest") loaded_im_array = utils.img_to_array(loaded_im, dtype='int32') assert loaded_im_array.shape == (25, 25, 1) # Check that exception is raised if interpolation not supported. loaded_im = utils.load_img(filename_rgb, interpolation="unsupported") with pytest.raises(ValueError): loaded_im = utils.load_img(filename_rgb, target_size=(25, 25), interpolation="unsupported")
def resize_to_height(self, im, height): '''Resize `im` into an image with height `height` and proportional width''' w, h = im.size size = (int(w / h * height), height) return img_to_array(im.resize(size))
def test_numpy_array_iterator(image_data_generator, all_test_images, tmpdir): for test_images in all_test_images: img_list = [] for im in test_images: img_list.append(utils.img_to_array(im)[None, ...]) images = np.vstack(img_list) dsize = images.shape[0] iterator = numpy_array_iterator.NumpyArrayIterator( images, np.arange(images.shape[0]), image_data_generator, shuffle=False, save_to_dir=str(tmpdir), batch_size=3) x, y = next(iterator) assert x.shape == images[:3].shape assert list(y) == [0, 1, 2] # Test with sample weights iterator = numpy_array_iterator.NumpyArrayIterator( images, np.arange(images.shape[0]), image_data_generator, shuffle=False, sample_weight=np.arange(images.shape[0]) + 1, save_to_dir=str(tmpdir), batch_size=3) x, y, w = iterator.next() assert x.shape == images[:3].shape assert list(y) == [0, 1, 2] assert list(w) == [1, 2, 3] # Test with `shuffle=True` iterator = numpy_array_iterator.NumpyArrayIterator( images, np.arange(images.shape[0]), image_data_generator, shuffle=True, save_to_dir=str(tmpdir), batch_size=3, seed=42) x, y = iterator.next() assert x.shape == images[:3].shape # Check that the sequence is shuffled. assert list(y) != [0, 1, 2] # Test without y iterator = numpy_array_iterator.NumpyArrayIterator( images, None, image_data_generator, shuffle=True, save_to_dir=str(tmpdir), batch_size=3) x = iterator.next() assert type(x) is np.ndarray assert x.shape == images[:3].shape # Test with a single miscellaneous input data array x_misc1 = np.random.random(dsize) iterator = numpy_array_iterator.NumpyArrayIterator( (images, x_misc1), np.arange(dsize), image_data_generator, shuffle=False, batch_size=2) for i, (x, y) in enumerate(iterator): assert x[0].shape == images[:2].shape assert (x[1] == x_misc1[(i * 2):((i + 1) * 2)]).all() if i == 2: break # Test with two miscellaneous inputs x_misc2 = np.random.random((dsize, 3, 3)) iterator = numpy_array_iterator.NumpyArrayIterator( (images, [x_misc1, x_misc2]), np.arange(dsize), image_data_generator, shuffle=False, batch_size=2) for i, (x, y) in enumerate(iterator): assert x[0].shape == images[:2].shape assert (x[1] == x_misc1[(i * 2):((i + 1) * 2)]).all() assert (x[2] == x_misc2[(i * 2):((i + 1) * 2)]).all() if i == 2: break # Test cases with `y = None` iterator = numpy_array_iterator.NumpyArrayIterator( images, None, image_data_generator, batch_size=3) x = iterator.next() assert type(x) is np.ndarray assert x.shape == images[:3].shape iterator = numpy_array_iterator.NumpyArrayIterator( (images, x_misc1), None, image_data_generator, batch_size=3, shuffle=False) x = iterator.next() assert type(x) is list assert x[0].shape == images[:3].shape assert (x[1] == x_misc1[:3]).all() iterator = numpy_array_iterator.NumpyArrayIterator( (images, [x_misc1, x_misc2]), None, image_data_generator, batch_size=3, shuffle=False) x = iterator.next() assert type(x) is list assert x[0].shape == images[:3].shape assert (x[1] == x_misc1[:3]).all() assert (x[2] == x_misc2[:3]).all() # Test with validation split generator = ImageDataGenerator(validation_split=0.2) iterator = numpy_array_iterator.NumpyArrayIterator(images, None, generator, batch_size=3) x = iterator.next() assert isinstance(x, np.ndarray) assert x.shape == images[:3].shape # Test some failure cases: x_misc_err = np.random.random((dsize + 1, 3, 3)) with pytest.raises(ValueError) as e_info: numpy_array_iterator.NumpyArrayIterator((images, x_misc_err), np.arange(dsize), generator, batch_size=3) assert str(e_info.value).find('All of the arrays in') != -1 with pytest.raises(ValueError) as e_info: numpy_array_iterator.NumpyArrayIterator((images, x_misc1), np.arange(dsize + 1), generator, batch_size=3) assert str( e_info.value).find('`x` (images tensor) and `y` (labels) ') != -1 # Test `flow` behavior as Sequence seq = numpy_array_iterator.NumpyArrayIterator(images, np.arange( images.shape[0]), generator, shuffle=False, save_to_dir=str(tmpdir), batch_size=3) assert len(seq) == images.shape[0] // 3 + 1 x, y = seq[0] assert x.shape == images[:3].shape assert list(y) == [0, 1, 2] # Test with `shuffle=True` seq = numpy_array_iterator.NumpyArrayIterator(images, np.arange( images.shape[0]), generator, shuffle=True, save_to_dir=str(tmpdir), batch_size=3, seed=123) x, y = seq[0] # Check that the sequence is shuffled. assert list(y) != [0, 1, 2] # `on_epoch_end` should reshuffle the sequence. seq.on_epoch_end() x2, y2 = seq[0] assert list(y) != list(y2) # test order_interpolation labels = np.array([[2, 2, 0, 2, 2], [1, 3, 2, 3, 1], [2, 1, 0, 1, 2], [3, 1, 0, 2, 0], [3, 1, 3, 2, 1]]) label_generator = ImageDataGenerator(rotation_range=90., interpolation_order=0) labels_gen = numpy_array_iterator.NumpyArrayIterator(labels[np.newaxis, ..., np.newaxis], None, label_generator, seed=123) assert (np.unique(labels) == np.unique(next(labels_gen))).all()
def test_load_img(tmpdir): filename_rgb = str(tmpdir / 'rgb_utils.png') filename_rgba = str(tmpdir / 'rgba_utils.png') filename_grayscale_8bit = str(tmpdir / 'grayscale_8bit_utils.png') filename_grayscale_16bit = str(tmpdir / 'grayscale_16bit_utils.tiff') filename_grayscale_32bit = str(tmpdir / 'grayscale_32bit_utils.tiff') original_rgb_array = np.array(255 * np.random.rand(100, 100, 3), dtype=np.uint8) original_rgb = utils.array_to_img(original_rgb_array, scale=False) original_rgb.save(filename_rgb) original_rgba_array = np.array(255 * np.random.rand(100, 100, 4), dtype=np.uint8) original_rgba = utils.array_to_img(original_rgba_array, scale=False) original_rgba.save(filename_rgba) original_grayscale_8bit_array = np.array(255 * np.random.rand(100, 100, 1), dtype=np.uint8) original_grayscale_8bit = utils.array_to_img(original_grayscale_8bit_array, scale=False) original_grayscale_8bit.save(filename_grayscale_8bit) original_grayscale_16bit_array = np.array(np.random.randint( -2147483648, 2147483647, (100, 100, 1)), dtype=np.int16) original_grayscale_16bit = utils.array_to_img( original_grayscale_16bit_array, scale=False, dtype='int16') original_grayscale_16bit.save(filename_grayscale_16bit) original_grayscale_32bit_array = np.array(np.random.randint( -2147483648, 2147483647, (100, 100, 1)), dtype=np.int32) original_grayscale_32bit = utils.array_to_img( original_grayscale_32bit_array, scale=False, dtype='int32') original_grayscale_32bit.save(filename_grayscale_32bit) # Test that loaded image is exactly equal to original. loaded_im = utils.load_img(filename_rgb) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_rgb_array.shape assert np.all(loaded_im_array == original_rgb_array) loaded_im = utils.load_img(filename_rgba, color_mode='rgba') loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_rgba_array.shape assert np.all(loaded_im_array == original_rgba_array) loaded_im = utils.load_img(filename_rgb, color_mode='grayscale') loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (original_rgb_array.shape[0], original_rgb_array.shape[1], 1) loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale') loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_grayscale_8bit_array.shape assert np.all(loaded_im_array == original_grayscale_8bit_array) loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale') loaded_im_array = utils.img_to_array(loaded_im, dtype='int16') assert loaded_im_array.shape == original_grayscale_16bit_array.shape assert np.all(loaded_im_array == original_grayscale_16bit_array) # test casting int16 image to float32 loaded_im_array = utils.img_to_array(loaded_im) assert np.allclose(loaded_im_array, original_grayscale_16bit_array) loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale') loaded_im_array = utils.img_to_array(loaded_im, dtype='int32') assert loaded_im_array.shape == original_grayscale_32bit_array.shape assert np.all(loaded_im_array == original_grayscale_32bit_array) # test casting int32 image to float32 loaded_im_array = utils.img_to_array(loaded_im) assert np.allclose(loaded_im_array, original_grayscale_32bit_array) # Test that nothing is changed when target size is equal to original. loaded_im = utils.load_img(filename_rgb, target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_rgb_array.shape assert np.all(loaded_im_array == original_rgb_array) loaded_im = utils.load_img(filename_rgba, color_mode='rgba', target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_rgba_array.shape assert np.all(loaded_im_array == original_rgba_array) loaded_im = utils.load_img(filename_rgb, color_mode='grayscale', target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (original_rgba_array.shape[0], original_rgba_array.shape[1], 1) loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale', target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == original_grayscale_8bit_array.shape assert np.all(loaded_im_array == original_grayscale_8bit_array) loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale', target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im, dtype='int16') assert loaded_im_array.shape == original_grayscale_16bit_array.shape assert np.all(loaded_im_array == original_grayscale_16bit_array) loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale', target_size=(100, 100)) loaded_im_array = utils.img_to_array(loaded_im, dtype='int32') assert loaded_im_array.shape == original_grayscale_32bit_array.shape assert np.all(loaded_im_array == original_grayscale_32bit_array) # Test down-sampling with bilinear interpolation. loaded_im = utils.load_img(filename_rgb, target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (25, 25, 3) loaded_im = utils.load_img(filename_rgba, color_mode='rgba', target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (25, 25, 4) loaded_im = utils.load_img(filename_rgb, color_mode='grayscale', target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (25, 25, 1) loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale', target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (25, 25, 1) loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale', target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im, dtype='int16') assert loaded_im_array.shape == (25, 25, 1) loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale', target_size=(25, 25)) loaded_im_array = utils.img_to_array(loaded_im, dtype='int32') assert loaded_im_array.shape == (25, 25, 1) # Test down-sampling with nearest neighbor interpolation. loaded_im_nearest = utils.load_img(filename_rgb, target_size=(25, 25), interpolation="nearest") loaded_im_array_nearest = utils.img_to_array(loaded_im_nearest) assert loaded_im_array_nearest.shape == (25, 25, 3) assert np.any(loaded_im_array_nearest != loaded_im_array) loaded_im_nearest = utils.load_img(filename_rgba, color_mode='rgba', target_size=(25, 25), interpolation="nearest") loaded_im_array_nearest = utils.img_to_array(loaded_im_nearest) assert loaded_im_array_nearest.shape == (25, 25, 4) assert np.any(loaded_im_array_nearest != loaded_im_array) loaded_im = utils.load_img(filename_grayscale_8bit, color_mode='grayscale', target_size=(25, 25), interpolation="nearest") loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (25, 25, 1) loaded_im = utils.load_img(filename_grayscale_16bit, color_mode='grayscale', target_size=(25, 25), interpolation="nearest") loaded_im_array = utils.img_to_array(loaded_im, dtype='int16') assert loaded_im_array.shape == (25, 25, 1) loaded_im = utils.load_img(filename_grayscale_32bit, color_mode='grayscale', target_size=(25, 25), interpolation="nearest") loaded_im_array = utils.img_to_array(loaded_im, dtype='int32') assert loaded_im_array.shape == (25, 25, 1) # Check that exception is raised if interpolation not supported. loaded_im = utils.load_img(filename_rgb, interpolation="unsupported") with pytest.raises(ValueError): loaded_im = utils.load_img(filename_rgb, target_size=(25, 25), interpolation="unsupported") # Check that the aspect ratio of a square is the same filename_red_square = str(tmpdir / 'red_square_utils.png') A = np.zeros((50, 100, 3), dtype=np.uint8) # rectangle image 100x50 A[20:30, 45:55, 0] = 255 # red square 10x10 red_square_array = np.array(A) red_square = utils.array_to_img(red_square_array, scale=False) red_square.save(filename_red_square) loaded_im = utils.load_img(filename_red_square, target_size=(25, 25), keep_aspect_ratio=True) loaded_im_array = utils.img_to_array(loaded_im) assert loaded_im_array.shape == (25, 25, 3) red_channel_arr = loaded_im_array[:, :, 0].astype(np.bool) square_width = np.sum(np.sum(red_channel_arr, axis=0)) square_height = np.sum(np.sum(red_channel_arr, axis=1)) aspect_ratio_result = square_width / square_height # original square had 1:1 ratio assert aspect_ratio_result == pytest.approx(1.0)