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 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 _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. """ # build batch of image data # self.filepaths is dynamic, is better to call it once outside the loop filepaths = self.filepaths # build batch of image data batch_x = np.array([ load_img(filepaths[x], color_mode=self.color_mode, target_size=self.target_size, interpolation=self.interpolation) for x in index_array ]) # transform the image data batch_x = np.array( [self.image_data_generator.transform_image(x) for x in batch_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 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 load(self, size=256, keep_proportions=True): '''Load a dataset into RAM at a constant size''' X = [] for i in glob2.glob(os.path.join(self.out_dir, '**')): if not any([i.endswith(j) for j in self.image_extensions]): continue if os.path.basename(i).startswith('._'): continue im = load_img(i) if keep_proportions: X.append(self.resize_to_square(im, size)) else: X.append(im.resize(size, size)) return np.array(X)
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))
#!/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)
horizontal_flip=True, vertical_flip=True) def check_dir(directory): ''' Checks if the target directory exists or not. If it does not exist it will create one''' if not os.path.exists(directory): os.makedirs(directory) 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 x = load_img("F:/PV/canon eos 1500d/20180121213556_IMG_2535 (4).JPG") #Loading input image check_dir('preview') #Target directory augmentation(x)
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)
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 test_load_img_returns_image(tmpdir): path = write_sample_image(tmpdir) im = utils.load_img(path) assert isinstance(im, PIL.Image.Image)
def test_image_file_handlers_close(tmpdir): path = write_sample_image(tmpdir) max_open_files, _ = resource.getrlimit(resource.RLIMIT_NOFILE) for i in range(max_open_files + 1): utils.load_img(path)
def read_np_picture(path, target_size=None, scale=1): # img = PIL.Image.open(filename) img = load_img(path, target_size=target_size) img_np = np.asarray(img, dtype='uint8') img_np = img_np * scale return img_np
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 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 _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
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 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 get_images(paths: list, WH): """Get resized images by <WH>""" return np.array( [np.array(load_img(file_name, target_size=WH)) for file_name in paths])