def augment(movie, mask): #MOVIE is a 3d array that represents each slice of the movie stacked on top of each other # transform every horizontal slice datagen_xy = ImageDataGenerator(zoom_range=[.75, 1.1], rotation_range=90, shear_range=10, horizontal_flip=True, vertical_flip=True) xy_transform = datagen_xy.get_random_transform(movie.shape[1:3]) for i in range(movie.shape[0]): movie[i, :, :, :] = datagen_xy.apply_transform(movie[i, :, :, :], xy_transform) #transform the mask to match mask = datagen_xy.apply_transform(mask, xy_transform) return movie, mask
def dataset_augmentation(directory_from, directory_to): if not os.path.exists(directory_to): os.mkdir(directory_to) datagen = ImageDataGenerator() filenames = os.listdir(directory_from) #create 24 rotated images for one image angle = 45 zoom = 0.75 imgs = [] for i, fileNb in enumerate(filenames): if fileNb != ".DS_Store": full_name = directory_from + fileNb img = mpimg.imread(full_name) io.imsave(directory_to + fileNb, img) img_asarray = img_to_array(img) out = datagen.apply_transform(x=img_asarray, transform_parameters={ 'theta': angle, 'zx': zoom, 'zy': zoom }) j = 101 + i fileNb2 = "satImage_%03d" % j + ".png" io.imsave(directory_to + fileNb2, out) sys.stdout.write("\rImage {}/{} is being loaded".format( i + 1, len(filenames))) sys.stdout.flush()
def dataset_augmentation(directory_name, seed): data_aug_path = "Data_aug" if not os.path.exists(data_aug_path): os.mkdir(data_aug_path"Data_aug") datagen = ImageDataGenerator() filenames = os.listdir(directory_name) #create 24 rotated images for one image angls = 45 zoom = 0.75 imgs = [] for i, fileNb in enumerate(filenames): full_name = directory_name+fileNb img=mpimg.imread(full_name) io.imsave(data_aug_path+fileNb, img) img_asarray = img_to_array(img) out = datagen.apply_transform(x=img_asarray, transform_parameters={'theta':angle, 'zx':zoom, 'zy':zoom}) fileNb2 = fileNb+i print(out.shape) io.imsave(data_aug_path+fileNb2, out) sys.stdout.write("\rImage {}/{} is being loaded".format(i+1,len(filenames))) sys.stdout.flush()
def augmentation(self, flow, normalize=True): """ Take an existing flow of data and augment it with consistent random transformations uniformly accross all frames in the video. Also supports normalization /in [0,1] :param flow: the iterator of video data :param normalize: do you want to scale the data to [0,1] using a linear map """ image_datagen = ImageDataGenerator(**self.data_gen_args) for video in flow: # for every frame in this video generate the same transformation # and yield it all back in sequence order trans = image_datagen.get_random_transform(video[2].shape) augmentedVideo = np.zeros(video[2].shape) for i in range(video[2].shape[0]): augmentedVideo[i] = image_datagen.apply_transform( video[2][i], trans) # now is a good time to transform the video onto 0-1 # we need to do this to get convergence when we train i.e. homogenise features if normalize: augmentedVideo[i] = augmentedVideo[i] / 255 yield video[:-1] + (augmentedVideo, )
def tta(image, model, model_output='regression'): datagen = ImageDataGenerator() all_images = np.expand_dims(image, 0) hori_image = np.expand_dims(datagen.apply_transform(x=image, transform_parameters={"flip_horizontal": True}), axis=0) vert_image = np.expand_dims(datagen.apply_transform(x=image, transform_parameters={"flip_vertical": True}), axis=0) rotated_image = np.expand_dims(datagen.apply_transform(x=image, transform_parameters={"theta": 15}), axis=0) all_images = np.append(all_images, hori_image, axis=0) all_images = np.append(all_images, vert_image, axis=0) all_images = np.append(all_images, rotated_image, axis=0) prediction = model.predict(all_images) if model_output is 'regression': return np.mean(prediction) else: prediction = np.sum(prediction, axis=0) return np.argmax(prediction)
def augment(movie, mask): #MOVIE is a 3d array that represents each slice of the movie stacked on top of each other #first, transform every vertical slice # datagen_zy = ImageDataGenerator(zoom_range = .15) # z_transform = datagen_zy.get_random_transform(movie.shape[0:2]) # z_transform["zx"] = 0 # for i in range(movie.shape[2]): # movie[:, :, i, :] = datagen_zy.apply_transform(movie[:, :, i, :], z_transform) #now transform every horizontal slice datagen_xy = ImageDataGenerator(zoom_range = [.6, 1], rotation_range = 90, shear_range = 12, horizontal_flip = True, vertical_flip = True) xy_transform = datagen_xy.get_random_transform(movie.shape[1:3]) for i in range(movie.shape[0]): movie[i, :, :, :] = datagen_xy.apply_transform(movie[i, :, :, :], xy_transform) #transform the mask to match mask = datagen_xy.apply_transform(mask, xy_transform) return movie, mask
class Generator(VideoFramesGenerator): '''Generate groups of contiguous frames from a dataset. The generated data has inputs [l_input, ab_and_mask_input].''' def __init__(self, **kwargs): augment = kwargs.pop('augment', False) super().__init__(contiguous_count=0, **kwargs) self.augmentation = ImageDataGenerator() if augment else None self.random = None def flow_from_directory(self, root, batch_size=32, target_size=None, seed=None): contiguous_frames = self.get_contiguous_frames( dataset.get_all_scenes(root, names_as_int=False)) print('Dataset {} has {} contiguous subscenes'.format( root, len(contiguous_frames))) self.random = random.Random(seed) while True: yield self.load_batch(self.random.choices(contiguous_frames, k=batch_size), target_size=target_size) def augment(self, x): return self.augmentation.apply_transform( x, { 'theta': self.random.uniform(-15, 15), 'tx': self.random.uniform(-4, 4), 'ty': self.random.uniform(-4, 4), 'shear': self.random.uniform(-20, 20), 'zx': self.random.uniform(.7, 1), 'zy': self.random.uniform(.7, 1), 'flip_horizontal': self.random.choices((False, True)), }) def load_batch(self, start_frames, target_size): assert self.contiguous_count == 0 x_batch = [[], []] y_batch = [] for scene, frame in start_frames: l, ab = dataset.read_frame_lab(scene, frame, target_size) if self.augmentation: x = np.dstack((l, ab)) x = self.augment(x) l, ab = x[:, :, :1], x[:, :, 1:] x_batch[0].append(ab_and_mask_matrix(ab, .00016)) x_batch[1].append(l) y_batch.append(ab) return [np.array(model_input) for model_input in x_batch], np.array(y_batch) def load_sample(self, scene, start_frame, target_size): pass # unused
def batch_generator(self, handle): """ Extract a random batch of images, apply transformations and return both, together with the corresponding transformation parameters Arguments: handle -- hdf5 dataset handle """ # extract batch_size random indices from the dataset for SGD total_size = handle.shape[0] while True: indices = np.random.permutation( range(total_size))[0:self.batch_size] indices = np.sort(indices) # define batch_size x and y translations as integers in # xrange and yrange x_trans = np.random.randint(*self.xrange, size=self.batch_size) y_trans = np.random.randint(*self.yrange, size=self.batch_size) # define batch_size rotations in trange z_rotat = np.random.uniform(*self.trange, size=self.batch_size) # group them in tbins z_rotat = np.digitize( z_rotat, np.linspace(*self.trange, self.tbins + 1, endpoint=True)) # group each x, y , z transformation in a list of # batch_size dictionaries trans_list = [{ 'theta': t, 'tx': x * self.lscale, 'ty': y * self.lscale } for t, x, y in zip(z_rotat, x_trans, y_trans)] # apply transformations to a batch (hdf5 dataset accepts lists for # slicing, not np.arrays) datagen = ImageDataGenerator() Xbatch_trans = np.array([ datagen.apply_transform(img, tran) for img, tran in zip(handle[list(indices)], trans_list) ]) Xbatch = handle[list(indices)] # turning classes into categorical values # classes need to be positive Ybatch_tx = to_categorical(x_trans - self.xrange[0], sum(np.absolute(self.xrange))) # classes need to be positive Ybatch_ty = to_categorical(y_trans - self.yrange[0], sum(np.absolute(self.yrange))) # classes need to start from 0 Ybatch_rot = to_categorical(z_rotat - 1, self.tbins) yield ({ 'input0': Xbatch, 'input1': Xbatch_trans }, { 'output0': Ybatch_rot, 'output1': Ybatch_tx, 'output2': Ybatch_ty })
def img_aug(self, img): data_gen = ImageDataGenerator() dic_parameter = { 'flip_horizontal': random.choice([True, False]), 'flip_vertical': random.choice([True, False]), 'theta': random.choice([0, 0, 0, 90, 180, 270]) } img_aug = data_gen.apply_transform(img, transform_parameters=dic_parameter) return img_aug
def mirror_image_and_shape(img, shape): imgAugmentator = ImageDataGenerator() mirrored_img = imgAugmentator.apply_transform( img, transform_parameters={'flip_horizontal': True}) img_width = img.shape[1] mirrored_shape = swap_mirrored_shape((shape * np.array([-1, 1])) + np.array([img_width, 0])) return mirrored_img, mirrored_shape
def aug(img , p, total): print ("Image Found: " + p) ext = p[p.rfind('.'):] aug = ImageDataGenerator() x_size = cv2.resize(img,(1000,1000)) x = img_to_array (x_size) #rotations for types in range(0,6): p = 'C:\\Users\\Asus\\Project\\augdata\\'+ str(total+types).zfill(3) + ext if types == 0: rot1 = aug.apply_transform(x,{'theta':10}) cv2.imwrite(p,rot1) elif types == 1: rot2 = aug.apply_transform(x,{'theta':-10}) cv2.imwrite(p,rot2) # flips elif types == 2: flip1 = aug.apply_transform(x,{'shear':20}) cv2.imwrite(p,flip1) elif types == 3: flip2 = aug.apply_transform(x,{'flip_horizontal':1}) cv2.imwrite(p,flip2) # brightness elif types == 4: dark = aug.apply_transform(x,{'brightness':0.5}) cv2.imwrite(p,dark) elif types == 5: light = aug.apply_transform(x,{'brightness':1.5}) cv2.imwrite(p,light)
def TTA_prediction(model, X_pred): datagen = ImageDataGenerator() assert X_pred.shape == (n_test, ori_n_h, ori_n_v, ori_n_c) Y_pred = np.zeros((n_test, ori_n_h, ori_n_v, ori_n_c)) for idx in tqdm(range(n_test)): X_pred_indiv = X_pred[idx, :, :, :] Y_pred_indiv = np.zeros((8, ori_n_h, ori_n_v, ori_n_c)) for theta in [0., 90., 180., 270.]: for is_flip_horizontal in [True, False]: img = datagen.apply_transform( X_pred_indiv, { 'theta': theta, 'flip_horizontal': is_flip_horizontal }) mask = model.predict(img, verbose=1) Y_pred_indiv[idx, :, :, :] = datagen.apply_transform( mask, { 'theta': np.abs(360. - theta), 'flip_horizontal': is_flip_horizontal }) Y_pred[idx, :, :, :] = np.mean(Y_pred_indiv, axis=0) print Y_pred
def rotate_image(img, rot_ang_deg=0.): # Using Keras data augmenting functions datagen = ImageDataGenerator() transform_parameters = {'theta' : rot_ang_deg} # Padding: Keras requires 3D tensor for 2D image padded_img = np.zeros((img.shape[0], img.shape[1], 1)) padded_img[:,:,0] = img.copy() # Rotate Image rot_img = datagen.apply_transform(padded_img, transform_parameters) return rot_img[:,:,0]
def random_rotation(x, y, rg, row_index=1, col_index=2, channel_index=0, fill_mode='nearest', cval=0.): theta = np.pi / 180 * np.random.uniform(-rg, rg) rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0], [np.sin(theta), np.cos(theta), 0], [0, 0, 1]]) h, w = x.shape[row_index], x.shape[col_index] #transform_matrix = transform_matrix_offset_center(rotation_matrix, h, w) datagen = ImageDataGenerator() dictimg = {'theta':theta} x = datagen.apply_transform(x, dictimg) y = datagen.apply_transform(y, dictimg) return x, y
def DataAugmentation(faceResized, label, fileName): x, y = [], [] # data augmentation through ImageDataGenerator img_gen = ImageDataGenerator() # transformation types for theta_input in [-15, -10, -5, 0, 5, 10, 15]: for flip_horizontal_input in [False, True]: for flip_vertical_input in [False, True]: for channel_shift_intencity_input in [-100, 0, 100]: faceTransform = img_gen.apply_transform( faceResized, { 'theta': theta_input, 'flip_horizontal': flip_horizontal_input, 'flip_vertical': flip_vertical_input, 'channel_shift_intencity': channel_shift_intencity_input }) cv2.imwrite( '{}_{}_{}_{}_{}.jpg'.format( fileName, theta_input, flip_horizontal_input, channel_shift_intencity_input), faceTransform) x.append(faceTransform) y.append(label) # data augmetnation through OpenCV # augmented data: mirror (vertical flip) faceMirror = cv2.flip(faceResized, 1) cv2.imwrite('{}_Mirror.jpg'.format(fileName), faceMirror) x.append(faceMirror) y.append(label) # augmented data: Gaussian Blur faceBlur = gaussian_filter(faceResized, sigma=0.5) cv2.imwrite('{}_Blur.jpg'.format(fileName), faceBlur) x.append(faceBlur) y.append(label) # augmented data: mirror and Gaussian Blur faceBlurMirror = gaussian_filter(faceMirror, sigma=0.5) cv2.imwrite('{}_BlurMirror.jpg'.format(fileName), faceBlurMirror) x.append(faceBlurMirror) y.append(label) return x, y
def augment_images(raw_images, files, mult_factor): gen = ImageDataGenerator() for idx, image in enumerate(raw_images): for mult in range(mult_factor): img_fname = files[idx].split('/')[4] img_fname = '../../Data/AugmentedImages/' + \ img_fname.split('.')[0] + '_' + str(multi) + '.jpg' theta_tfx = np.random.choice(range(270)) transformed_raw_image = gen.apply_transform(image, {'theta': theta_fx}) new_image = Image.fromarray(transformed_raw_image, 'RGB') new_image = new_image.resize((1024, 1024), Image.ANTIALIAS) new_image.save(img_fname) transformed_raw_image = None new_image = None
def custom_image_reader(filepath, target_mode=None, target_size=None, dim_ordering=K.image_dim_ordering(), **kwargs): img = PIL.Image(filepath, mode="r") #rot_img = rotate_image(img, rot_ang_deg) # Using Keras data augmenting functions datagen = ImageDataGenerator() transform_parameters = {'theta' : rot_ang_deg} # Padding: Keras requires 3D tensor for 2D image padded_img = np.zeros((img.shape[0], img.shape[1], 1)) padded_img[:,:,0] = img.copy() # Rotate Image rot_img = datagen.apply_transform(padded_img, transform_parameters) return rot_img
def transform_img(img): if self.transform: augGen = ImageDataGenerator() thetaRand = np.random.uniform(0, 90) flipV = bool(np.random.randint(2)) flipH = bool(np.random.randint(2)) params = { 'theta': thetaRand, 'flip_horizontal': flipH, 'flip_vertical': flipV } img = np.expand_dims(img, -1) img = augGen.apply_transform(img, params) img = np.squeeze(img) return img
def transform(inputs, outputs, ntimes=8, args=None): datagen = ImageDataGenerator(**args) input_gen = [] output_gen = [] for i in range(ntimes): for j in range(len(inputs)): inp = inputs[j] out = outputs[j] trans = datagen.get_random_transform(inp.shape) inp = datagen.apply_transform(inp, trans) out = datagen.apply_transform(out, trans) input_gen.append(inp) output_gen.append(out) input_gen = np.array(input_gen) output_gen = np.array(output_gen) return input_gen, output_gen
def batch_generator(self, handle): """ Extract a random batch of images, apply transformations and return both, together with the corresponding transformation parameters #Arguments handle: hdf5 dataset """ # extract batch_size random indices from the dataset for SGD total_size = handle.shape[0] while True: indices = np.random.permutation( range(total_size))[0:self.batch_size] indices = np.sort(indices) # define batch_size x and y translations as # integers in xrange and yrange x_trans = np.random.randint(*self.xrange, size=self.batch_size) y_trans = np.random.randint(*self.yrange, size=self.batch_size) # define batch_size rotations in trange z_rotat = np.random.uniform(*self.trange, size=self.batch_size) # group each x, y , z transformation in a list # of batch_size dictionaries trans_list = [{ 'theta': t, 'tx': x * self.lscale, 'ty': y * self.lscale } for t, x, y in zip(z_rotat, x_trans, y_trans)] # apply transformations to a batch # (hdf5 dataset accepts lists for slicing, not np.arrays) datagen = ImageDataGenerator() Xbatch_trans = np.array([ datagen.apply_transform(img, tran) for img, tran in zip(handle[list(indices)], trans_list) ]) Xbatch = handle[list(indices)] yield ({ 'input0': Xbatch, 'input1': Xbatch_trans }, { 'output0': z_rotat, 'output1': x_trans, 'output2': y_trans })
def random_zoom(x, y, zoom_range, row_index=1, col_index=2, channel_index=0, fill_mode='nearest', cval=0.): if len(zoom_range) != 2: raise Exception('zoom_range should be a tuple or list of two floats. ' 'Received arg: ', zoom_range) if zoom_range[0] == 1 and zoom_range[1] == 1: zx, zy = 1, 1 else: zx, zy = np.random.uniform(zoom_range[0], zoom_range[1], 2) zoom_matrix = np.array([[zx, 0, 0], [0, zy, 0], [0, 0, 1]]) #h, w = x.shape[row_index], x.shape[col_index] #transform_matrix = transform_matrix_offset_center(zoom_matrix, h, w) dictimg = {'zx':zy,'zy':zy} datagen = ImageDataGenerator() x = datagen.apply_transform(x, dictimg) y = datagen.apply_transform(y, dictimg) return x, y
def data_augmentation(directory_name, seed): datagen = ImageDataGenerator() filenames = os.listdir(directory_name) #create 24 rotated images for one image angls = np.arange(0, 360, 15) zooms = np.array([ 1., 0.85, 0.8, 0.75, 0.8, 0.85, 1., 0.85, 0.8, 0.75, 0.8, 0.85, 1., 0.85, 0.8, 0.75, 0.8, 0.85, 1., 0.85, 0.8, 0.75, 0.8, 0.85 ]) imgs = [] for i, fileNb in enumerate(filenames): full_name = directory_name + fileNb img = mpimg.imread(full_name) imgr = img_to_array(img) for j, angle in enumerate(angls): zoom = zooms[j] img2 = datagen.apply_transform(x=imgr, transform_parameters={ 'theta': angle, 'zx': zoom, 'zy': zoom }) imgs.append(img2) sys.stdout.write("\rImage {}/{} is being loaded".format( i + 1, len(filenames))) sys.stdout.flush() print(' ... Shuffle data ...') imgs1 = np.asarray(imgs) np.random.seed(seed) rand = np.random.randint(imgs1.shape[0], size=imgs1.shape[0]) imgs2 = imgs1[rand, :, :, :] # shows images #IMG = array_to_img(imgs2[0,:,:,:]) #imgplot = plt.imshow(IMG) #plt.show() return imgs2
#transform_parameters_2 = {'theta':10, 'tx':0.1, 'ty':0.1, 'zx':1.1, 'zy':0.9} x_aug = np.zeros((3 * len(x_full), 28, 28, 1)) y_aug = np.zeros((3 * len(y_full), 10)) cur_count = 0 for i in range(3 * len(x_full)): if i % 3 == 0: x_aug[i] = x_full_reshaped[cur_count] y_aug[i] = y_full[cur_count] #cur_count += 1 elif i % 3 == 1: temp = datagen.apply_transform(x_full_reshaped[cur_count], transform_parameters) x_aug[i] = temp y_aug[i] = y_full[cur_count] transform_parameters = { 'theta': np.random.randint(-30, 30), 'tx': 0.1 * np.random.rand(), 'ty': 0.1 * np.random.rand(), 'zx': 0.9 + 0.3 * np.random.rand(), 'zy': 0.9 + 0.3 * np.random.rand() } else: temp = datagen.apply_transform(x_full_reshaped[cur_count], transform_parameters) x_aug[i] = temp y_aug[i] = y_full[cur_count]
class NiftiImageIterator(Sequence): """ Niftiのパスから各軸から1枚ずつ画像を切り出して合計3枚のデータを作成する。 roiのパスも参照して乗算をすることで任意の位置だけ活用する。 正規化して学習データとして渡す。 kerasのSequenceクラスを継承している。 generatorで学習時に__getitem__()が叩かれて バッチ分の学習データを生成して返すようになっている。 Args: x_nifti_path (np.ndarray): nifti画像のパス。 shapeは(n,) x_roi_path (np.ndarray): roi画像のパス。 shapeは(n,) y (np.ndarray): ラベル。 shapeは(n, 2) target_size (Tuple[int, int]): リサイズするときのサイズ。 (w, h) ex_size (Tuple[int, int]): 画像をはっつけるキャンバスのサイズ。 (h, w) test (bool): test用のgeneratorにするどうか。 preprocess_input (Callable): 前処理用の関数。汚いけどハードコーディングで渡してしまっている。 """ def __init__(self, x_nifti_path, x_roi_path, y, target_size=(224, 224), ex_size=(600, 600), batch_size=32, shuffle=False, test=False, preprocess_input=preprocess_input): self.x_nifti_path = x_nifti_path self.x_roi_path = x_roi_path self.y = y self.target_size = target_size self.ex_size = ex_size self.batch_size = batch_size self.shuffle = shuffle self.test = test self.sample_num = len(self.y) self.preprocess_input = preprocess_input self.__get_exploration_order() self.__create_data_gen() def __getitem__(self, idx): batch_ids = self.indexes[idx * self.batch_size:(idx + 1) * self.batch_size] x1, x2, x3, y = self.__data_generation(batch_ids) return [x1, x2, x3], y def __len__(self): return int(np.ceil(len(self.y) / self.batch_size)) def __get_exploration_order(self): self.indexes = np.arange(self.sample_num) if self.shuffle: self.indexes = np.random.shuffle(self.indexes) def __create_data_gen(self): if self.test: self.datagen = None else: self.datagen = ImageDataGenerator(rotation_range=30, horizontal_flip=True, vertical_flip=True) def __data_generation(self, batch_ids): x1, x2, x3 = self.__get_imgs(batch_ids) y = self.y[batch_ids] return x1, x2, x3, y def __get_imgs(self, batch_ids): """ 画像を読み込んで諸々の処理をしてリスト形式で返す。 """ x_nifti_path = self.x_nifti_path[batch_ids] x_roi_path = self.x_roi_path[batch_ids] img_x_list, img_y_list, img_z_list = [], [], [] for nii_path, roi_path in zip(x_nifti_path, x_roi_path): nii = nib.load(nii_path) box = nii.get_data() nir = nib.load(roi_path) roi = nir.get_data() box = self.__normalize(box) img_x, img_y, img_z = self.__get_slice(box, roi) for img, img_list in zip([img_x, img_y, img_z], [img_x_list, img_y_list, img_z_list]): img = self.__resize_array(img) img = self.preprocess_input(img) if not self.test: params = self.datagen.get_random_transform(img.shape) img = self.datagen.apply_transform(img, params) img /= 255. img_list.append(img) return np.asarray(img_x_list), np.asarray(img_y_list), np.asarray( img_z_list) def __normalize(self, arr): """ 値域をいい感じにする。 値がハードコーディングで非常にきたない。 """ arr[np.where(arr > 1024)] = 1024 arr[np.where(arr < -1024)] = -1024 arr = 255 * ((arr - arr.min()) / (arr.max() - arr.min())) return arr def __get_slice(self, arr, roi): """ 各軸で切り出した際に断面積の最も大きい画像を返す。 """ x_slice = np.zeros(self.ex_size) y_slice = np.zeros(self.ex_size) z_slice = np.zeros(self.ex_size) best_x = np.argmax(np.sum(np.sum(roi, axis=1), axis=1)) best_y = np.argmax(np.sum(np.sum(roi, axis=0), axis=1)) best_z = np.argmax(np.sum(np.sum(roi, axis=0), axis=0)) a_s = arr.shape x_slice[:a_s[1], :a_s[2]] = arr[best_x, :, :] * roi[best_x, :, :] y_slice[:a_s[0], :a_s[2]] = arr[:, best_y, :] * roi[:, best_y, :] z_slice[:a_s[0], :a_s[1]] = arr[:, :, best_z] * roi[:, :, best_z] x_slice = np.stack([x_slice for _ in range(3)], axis=2) y_slice = np.stack([y_slice for _ in range(3)], axis=2) z_slice = np.stack([z_slice for _ in range(3)], axis=2) return x_slice, y_slice, z_slice def __resize_array(self, img): pilimg = Image.fromarray(np.uint8(img)) pilimg = pilimg.resize(self.target_size) return np.asarray(pilimg).astype(np.float32)
train_y, validation_split=.2, batch_size=200, epochs=2000, callbacks=[model_checkpoint, early_stopping]) val_loss = np.min(history.history['val_loss']) if best_model["val_loss"] > val_loss: best_model = {"val_loss": val_loss, "model": model, "index": i} print(best_model) model = best_model["model"] test = 'project/test_images/' test_images = os.listdir(test) test_images = test_images[0:10] result = dict() for image in test_images: im = load_img(test + "/" + image, target_size=(224, 224)) im = np.asarray(im, dtype=np.uint8) if len(im.shape) != 3: im = np.stack((im, ) * 3, axis=-1) transformed_img = datagen.apply_transform( im, transform_parameters=dict())[np.newaxis, :] / 255.0 result[image] = int_to_class[np.where( model.predict(transformed_img))[1][0]] with open("saved_models/result.txt", "w") as result_file: print(str(result), file=result_file) print(str(result))
def mj_getNegLAEOpair(negsamples, videoname, timepos, winlen, meanSample=[0], imgsize=(64,64)): ''' Gets just one pair of negative samples :param negsamples: :param videoname: :param timepos: :param winlen: :param imgsize: (rows, cols) of output crop :return: output images are already normalized (x/255) ''' foo = 0 nvids = len(negsamples["videoname"]) vix = -1 for vix_ in range(0,nvids): if negsamples["videoname"][vix_] == videoname: vix = vix_ if vix >= 0: lTr = negsamples["tracks"][vix] if timepos >= len(lTr): return None, None ltrx = lTr[timepos] ntracks = len(ltrx) if ntracks < 2: return None, None # TODO: parametrize these values, currently, random if ntracks > 2: rnp = np.random.permutation(range(0, ntracks)) t1 = rnp[0] t2 = rnp[1] else: t1 = 0 t2 = 1 cropsvid = negsamples["crops"][vix] geomvid = negsamples["geom"][vix] pair = np.zeros((winlen, imgsize[0], 2*imgsize[1],3)) # Allocate memory for the temporal sequence if len(ltrx[t1]) < winlen or len(ltrx[t2]) < winlen: # Just in case return None, None # Define an image transformation from keras.preprocessing.image import ImageDataGenerator img_gen = ImageDataGenerator(width_shift_range=[-2, 0, 2], height_shift_range=[-2, 0, 2], brightness_range=[0.95, 1.05], channel_shift_range=0.05, zoom_range=0.015, horizontal_flip=True) transf = img_gen.get_random_transform(cropsvid[0][0].shape) transf["flip_horizontal"] = False G = 0 for tix in range(timepos,timepos+winlen): ix1 = ltrx[t1][tix-timepos] ix2 = ltrx[t2][tix-timepos] if tix >= len(geomvid): return None, None # Check which one is on the left if geomvid[tix][ix1][0] > geomvid[tix][ix2][0]: ix1, ix2 = ix2, ix1 # Swap crop1 = copy.deepcopy(cropsvid[tix][ix1]) crop1 = img_gen.apply_transform(crop1, transf) crop2 = copy.deepcopy(cropsvid[tix][ix2]) crop2 = img_gen.apply_transform(crop2, transf) geo1 = geomvid[tix][ix1] geo2 = geomvid[tix][ix2] dx = geo2[0]-geo1[0] dy = geo2[1]-geo1[1] rscale = geo1[2] / geo2[2] crop1 = crop1/255.0 crop2 = crop2/255.0 if crop1.shape[0] != imgsize[0]: crop1 = cv2.resize(crop1, imgsize) crop2 = cv2.resize(crop2, imgsize) if type(meanSample) == np.ndarray and meanSample.shape[0] == crop1.shape[0]: crop1 -= meanSample crop2 -= meanSample p = np.concatenate((crop1,crop2), axis=1) pair[tix - timepos,] = p #cv2.imshow("Pair", p/255) #cv2.waitKey(-1) G = G + np.array([dx, dy, rscale]) imgsize_ = negsamples["imsize"][vix] G = G / winlen G[0] = G[0] / imgsize_[1] G[1] = G[1] / imgsize_[0] if winlen == 1: pair = np.squeeze(pair) return pair, G else: return None, None
def transfer_output(self,rotation=[0],dataset_type = "TRAIN"): image_w = self.config["INPUT_WIDTH"] image_h = self.config["INPUT_HEIGHT"] image_c = self.config["INPUT_CHANNELS"] train_dir = self.config["SAVE_MODEL_DIR"] datagen = ImageDataGenerator() with self.sess as sess: saver=tf.train.Saver() saver.restore(sess, train_dir) #self.logits=tf.nn.bias_add(self.conv, self.biases, name=scope.name) #_, _, prediction = cal_loss(logits=self.logits,labels=self.labels_pl,number_class=self.num_classes) #prob = tf.nn.softmax(self.logits,dim = -1) if(dataset_type == "TRAIN"): test_type_path = self.config["TRAIN_FILE"] else: test_type_path = self.config["TEST_FILE"] image_filename,label_filename = get_filename_list(test_type_path, self.config) images, labels = get_all_test_data(image_filename,label_filename) print(len(images)) image_batch=np.reshape(images,[len(images),image_h,image_w,image_c]) label_batch=np.reshape(labels,[len(labels),image_h,image_w,1]) #pred_tot = [] #var_tot = [] logit_aug=[] image_aug=[] label_aug=[] for deg in rotation: logit_tot=[] image_tot=[] label_tot=[] for image_batch, label_batch in zip(images,labels): #print(image_batch.shape) #to just convert the image into the standard format #for example: 1(batch size),64,64,3 image_batch=datagen.apply_transform(image_batch,{'theta':deg}) label_batch=datagen.apply_transform(label_batch,{'theta':deg}) image_batch = np.reshape(image_batch,[1,image_h,image_w,image_c]) label_batch = np.reshape(label_batch,[1,image_h,image_w,1]) image_tot.append(image_batch) label_tot.append(label_batch) #non baysien model #fetches = [prediction] feed_dict = {self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.with_dropout_pl: True, self.batch_size_pl: 1} #pred = sess.run(fetches = fetches, feed_dict = feed_dict) logit= sess.run([self.logits],feed_dict = feed_dict) #原本是images_h* images_w, with each element the label of it #pred = np.reshape(pred,[image_h,image_w]) #pred_tot.append(pred) logit_tot.append(logit) logit_aug.append(logit_tot) image_aug.append(image_tot) label_aug.append(label_tot) return logit_aug,label_aug,image_aug#logit_tot,label_tot,image_tot
def directorySearch(directory, label, dataName, dataAugmentation=False): # print('Started directory search of {} at {}'.format(dataName, str(datetime.datetime.now()))) x, y = [], [] # landmarksOutput = [] face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') time = strftime("%Y-%m-%d--%H-%M-%S", gmtime()) if label is 0: # fileBadImages = open('{0}-BadImagesNoPain.txt'.format(time), 'w+') # fileBadFaces = open('{0}-BadFacesNoPain.txt'.format(time), 'w+') pass elif label is 1: # fileBadImages = open('{0}-BadImagesPain.txt'.format(time), 'w+') # fileBadFaces = open('{0}-BadFacesPain.txt'.format(time), 'w+') pass else: print('Error: label should be 0 or 1') return countBadImages = 0 countBadFaces = 0 # detector = dlib.get_frontal_face_detector() # predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") img_gen = ImageDataGenerator() # for file in tqdm(sklearn.utils.shuffle(os.listdir(directory))[0:100]): for file in tqdm(sklearn.utils.shuffle(os.listdir(directory))): if file.endswith('.jpg'): path = os.path.join(directory, file) img = cv2.imread(path) if img is None: # fileBadImages.write(file + '\n') countBadImages += 1 pass else: face, faceDetected = DetectFace(face_cascade, img) if faceDetected: faceResized = cv2.resize(face, (128, 128), interpolation=cv2.INTER_AREA) # print(faceResized.shape) # cv2.imwrite("Original.jpg", faceResized) x.append(faceResized) y.append(label) # landmarksOutput.append(landmarks) if dataAugmentation: # transformation types for theta_input in [-15, -10, -5, 0, 5, 10, 15]: # for flip_horizontal_input in [False, True]: for flip_horizontal_input in [False]: for flip_vertical_input in [False, True]: # for channel_shift_intencity_input in [-100,0,100]: for channel_shift_intencity_input in [0]: faceTransform = img_gen.apply_transform( faceResized, { 'theta': theta_input, 'flip_horizontal': flip_horizontal_input, 'flip_vertical': flip_vertical_input, 'channel_shift_intencity': channel_shift_intencity_input }) x.append(faceTransform) y.append(label) # print(faceRotate.shape) # cv2.imwrite("Rotate.jpg", faceRotate) # # augmented data: rotate # faceRotate = img_gen.apply_transform(faceResized, {'channel_shift_intencity':-100}) # print(faceRotate.shape) # cv2.imwrite("Rotate.jpg", faceRotate) # x.append(faceRotate) # y.append(label) # augmented data: mirror (vertical flip) # faceMirror = cv2.flip(faceResized, 1) # print(faceMirror.shape) # cv2.imwrite("Mirror.jpg", faceMirror) # x.append(faceMirror) # y.append(label) # augmented data: Gaussian Blur # faceBlur = gaussian_filter(faceResized, sigma=0.5) # print(faceBlur.shape) # cv2.imwrite("Blur.jpg", faceBlur) # x.append(faceBlur) # y.append(label) # augmented data: mirror and Gaussian Blur # faceBlurMirror = gaussian_filter(faceMirror, sigma=0.5) # x.append(faceBlurMirror) # y.append(label) # return else: # fileBadFaces.write(file + '\n') countBadFaces += 1 # if countBadImages > 0: # print('Bad images count: {}'.format(countBadImages)) # if countBadFaces > 0: # print('Bad faces count: {}'.format(countBadFaces)) # print('Ended directory search of {} at {}'.format(dataName, str(datetime.datetime.now()))) return x, y
data = np.append(data, [X_test[i]], 0) break print(data.shape, " ", X_train.shape) datagen = ImageDataGenerator() plt.imshow(data[0, :, :,0], cmap="gray") plt.xticks(np.arange(0, 28+1, 3.0)) plt.yticks(np.arange(0, 28+1, 3.0)) plt.show() datagen.fit(X_train) transform_parameters_left = { 'ty': 3} x_transform = datagen.apply_transform(data[0], transform_parameters_left) plt.imshow(x_transform[ :, :,0], cmap="gray") plt.xticks(np.arange(0, 28+1, 3.0)) plt.yticks(np.arange(0, 28+1, 3.0)) plt.show() data = np.append(data, [x_transform], 0) print(data.shape) transform_parameters_right = { 'ty': -3} x_transform = datagen.apply_transform(data[0], transform_parameters_right) plt.imshow(x_transform[ :, :,0], cmap="gray") plt.xticks(np.arange(0, 28+1, 3.0)) plt.yticks(np.arange(0, 28+1, 3.0)) plt.show()
class DataLoaderCamus: def __init__(self, dataset_path, input_name, target_name, condition_name, img_res, target_rescale, input_rescale, condition_rescale, train_ratio, valid_ratio, labels, augment): self.dataset_path = dataset_path self.img_res = tuple(img_res) self.target_rescale = target_rescale self.input_rescale = input_rescale self.condition_rescale = condition_rescale self.input_name = input_name self.target_name = target_name self.condition_name = condition_name self.augment = augment patients = sorted(glob(os.path.join(self.dataset_path, 'training', '*'))) random.Random(RANDOM_SEED).shuffle(patients) num = len(patients) num_train = int(num * train_ratio) num_valid = int(num_train * valid_ratio) self.valid_patients = patients[:num_valid] self.train_patients = patients[num_valid:num_train] self.test_patients = patients[num_train:] if train_ratio == 1.0: self.test_patients = glob(os.path.join(self.dataset_path, 'testing', '*')) print('#train:', len(self.train_patients)) print('#valid:', len(self.valid_patients)) print('#test:', len(self.test_patients)) all_labels = {0, 1, 2, 3} self.not_labels = all_labels - set(labels) data_gen_args = dict(rotation_range=augment['AUG_ROTATION_RANGE_DEGREES'], width_shift_range=augment['AUG_WIDTH_SHIFT_RANGE_RATIO'], height_shift_range=augment['AUG_HEIGHT_SHIFT_RANGE_RATIO'], shear_range=augment['AUG_SHEAR_RANGE_ANGLE'], zoom_range=augment['AUG_ZOOM_RANGE_RATIO'], fill_mode='constant', cval=0., data_format='channels_last') self.datagen = ImageDataGenerator(**data_gen_args) def read_mhd(self, img_path, is_gt): if not os.path.exists(img_path): return np.zeros(self.img_res + (1,)) img = io.imread(img_path, plugin='simpleitk').squeeze() img = np.array(Image.fromarray(img).resize(self.img_res)) img = np.expand_dims(img, axis=2) if is_gt: for not_l in self.not_labels: img[img == not_l] = 0 return img def _get_paths(self, stage): if stage == 'train': return self.train_patients elif stage == 'valid': return self.valid_patients elif stage == 'test': return self.test_patients @background(max_prefetch=NUM_PREFETCH) def get_random_batch(self, batch_size=1, stage='train'): paths = self._get_paths(stage) num = len(paths) num_batches = num // batch_size for i in range(num_batches): batch_paths = np.random.choice(paths, size=batch_size) target_imgs, condition_imgs, input_imgs, weight_imgs = self._get_batch(batch_paths, stage) target_imgs = target_imgs * self.target_rescale input_imgs = input_imgs * self.input_rescale condition_imgs = condition_imgs * self.condition_rescale yield target_imgs, condition_imgs, input_imgs, weight_imgs def get_iterative_batch(self, batch_size=1, stage='test'): paths = self._get_paths(stage) num = len(paths) num_batches = num // batch_size start_idx = 0 for i in range(num_batches): batch_paths = paths[start_idx:start_idx + batch_size] target_imgs, condition_imgs, input_imgs, weight_imgs = self._get_batch(batch_paths, stage) target_imgs = target_imgs * self.target_rescale input_imgs = input_imgs * self.input_rescale condition_imgs = condition_imgs * self.condition_rescale start_idx += batch_size yield target_imgs, condition_imgs, input_imgs, weight_imgs def _get_batch(self, paths_batch, stage): target_imgs = [] input_imgs = [] condition_imgs = [] weight_maps = [] for path in paths_batch: transform = self.datagen.get_random_transform(img_shape=self.img_res) head, patient_id = os.path.split(path) target_path = os.path.join(path, '{}_{}.mhd'.format(patient_id, self.target_name)) condition_path = os.path.join(path, '{}_{}.mhd'.format(patient_id, self.condition_name)) input_path = os.path.join(path, '{}_{}.mhd'.format(patient_id, self.input_name)) input_img = self.read_mhd(input_path, '_gt' in self.input_name) if self.augment['AUG_INPUT']: input_img = self.datagen.apply_transform(input_img, transform) input_imgs.append(input_img) target_img = self.read_mhd(target_path, '_gt' in self.target_name) condition_img = self.read_mhd(condition_path, 1) if self.augment['AUG_TARGET']: if not self.augment['AUG_SAME_FOR_BOTH']: transform = self.datagen.get_random_transform(img_shape=self.img_res) target_img = self.datagen.apply_transform(target_img, transform) condition_img = self.datagen.apply_transform(condition_img, transform) target_imgs.append(target_img) condition_imgs.append(condition_img) weight_map_condition = self.get_weight_map(condition_img) weight_maps.append(weight_map_condition) return np.array(target_imgs), np.array(condition_imgs), np.array(input_imgs), np.array(weight_maps) def get_weight_map(self, mask): # let the y axis have higher variance gauss_var = [[self.img_res[0] * 60, 0], [0, self.img_res[1] * 30]] x, y = mask[:, :, 0].nonzero() center = [x.mean(), y.mean()] from scipy.stats import multivariate_normal gauss = multivariate_normal.pdf(np.mgrid[ 0:self.img_res[1], 0:self.img_res[0]].reshape(2, -1).transpose(), mean=center, cov=gauss_var) gauss /= gauss.max() gauss = gauss.reshape((self.img_res[1], self.img_res[0], 1)) # set the gauss value of the main target part to 1 gauss[mask > 0] = 1 return gauss