def augment_dataset(self, params, source_samples, tagrget_dir, range = None): patch_root = params.PATCHS_ROOT_PATH[source_samples[0]] sample_filename = source_samples[1] train_list = "{}/{}".format(patch_root, sample_filename) Xtrain, Ytrain = read_csv_file(patch_root, train_list) if range is not None: Xtrain = Xtrain[range[0]:range[1]] Ytrain = Ytrain[range[0]:range[1]] target_cancer_path = "{}/{}_cancer".format(patch_root, tagrget_dir) target_normal_path = "{}/{}_noraml".format(patch_root, tagrget_dir) if (not os.path.exists(target_cancer_path)): os.makedirs(target_cancer_path) if (not os.path.exists(target_normal_path)): os.makedirs(target_normal_path) for K, (x, y) in enumerate(zip(Xtrain, Ytrain)): block = Block() block.load_img(x) img = block.get_img() aug_img = self.augment_images(img) * 255 block.set_img(aug_img) block.opcode = self.opcode if y == 0: block.save_img(target_normal_path) else: block.save_img(target_cancer_path) if (0 == K % 1000): print("{} augmenting >>> {}".format(time.asctime(time.localtime()), K))
def normalize_dataset(self, source_samples, tagrget_dir, range=None, batch_size=20): self.opcode = 19 # normal = ACDNormalization_tf("acd", dc_txt="dc.txt", w_txt="w.txt", template_path="template_normal") normal = ACDNormalization("acd", dc_txt="dc.txt", w_txt="w.txt", template_path="template_normal") patch_root = self._params.PATCHS_ROOT_PATH[source_samples[0]] sample_filename = source_samples[1] train_list = "{}/{}".format(patch_root, sample_filename) Xtrain, Ytrain = read_csv_file(patch_root, train_list) if range is not None: Xtrain = Xtrain[range[0]:range[1]] Ytrain = Ytrain[range[0]:range[1]] # prepare images = [] for patch_file in Xtrain: img = io.imread(patch_file, as_gray=False) # imgBGR = img[:, :, (2, 1, 0)] # images.append(imgBGR) images.append(img) normal.prepare(images) target_cancer_path = "{}/{}_cancer".format(patch_root, tagrget_dir) target_normal_path = "{}/{}_normal".format(patch_root, tagrget_dir) if (not os.path.exists(target_cancer_path)): os.makedirs(target_cancer_path) if (not os.path.exists(target_normal_path)): os.makedirs(target_normal_path) n = 0 batch_images = [] batch_y = [] batch_blocks = [] for K, (x, y) in enumerate(zip(Xtrain, Ytrain)): new_block = Block() new_block.load_img(x) img = np.array(new_block.get_img()) # imgBGR = img[:, :, (2, 1, 0)] # batch_images.append(imgBGR) batch_images.append(img) batch_y.append(y) batch_blocks.append(new_block) n = n + 1 if n >= batch_size: norm_images = normal.normalize_on_batch(batch_images) for block, norm_img, y in zip(batch_blocks, norm_images, batch_y): # block.set_img(255 * norm_img[:, :, (2, 1, 0)]) block.set_img(255 * norm_img) block.opcode = self.opcode if y == 0: block.save_img(target_normal_path) else: block.save_img(target_cancer_path) batch_images = [] batch_y = [] batch_blocks = [] n = 0 if (0 == K % 1000): print("{} normalizing >>> {}".format( time.asctime(time.localtime()), K)) if n > 0: norm_images = normal.normalize_on_batch(batch_images) for block, norm_img, y in zip(batch_blocks, norm_images, batch_y): # block.set_img(255 * norm_img[:, :, (2, 1, 0)]) block.set_img(255 * norm_img) block.opcode = self.opcode if y == 0: block.save_img(target_normal_path) else: block.save_img(target_cancer_path) return