From 9dc3c8f0ff915b7135d593651595f5fb3daf9bcc Mon Sep 17 00:00:00 2001 From: James Betker Date: Tue, 29 Dec 2020 20:24:41 -0700 Subject: [PATCH] Script updates --- codes/scripts/extract_square_images.py | 18 +++++++---------- codes/scripts/extract_subimages_with_ref.py | 20 ++++++++++--------- .../scripts/srflow_latent_space_playground.py | 7 ++++--- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/codes/scripts/extract_square_images.py b/codes/scripts/extract_square_images.py index 09be5bb9..848da7aa 100644 --- a/codes/scripts/extract_square_images.py +++ b/codes/scripts/extract_square_images.py @@ -19,9 +19,9 @@ def main(): # compression time. If read raw images during training, use 0 for faster IO speed. opt['dest'] = 'file' - opt['input_folder'] = ['F:\\4k6k\\datasets\\images\\youtube\\images'] - opt['save_folder'] = 'F:\\4k6k\\datasets\\images\\ge_full_1024' - opt['imgsize'] = 1024 + opt['input_folder'] = ['F:\\4k6k\\datasets\\ns_images\\imagesets\\imgset4'] + opt['save_folder'] = 'F:\\4k6k\\datasets\\ns_images\\256_unsupervised_new' + opt['imgsize'] = 256 #opt['bottom_crop'] = 120 save_folder = opt['save_folder'] @@ -52,6 +52,7 @@ class TiledDataset(data.Dataset): print("Error with ", path) return None if len(img.shape) == 2: + print("Skipping due to greyscale") return None # Perform explicit crops first. These are generally used to get rid of watermarks so we dont even want to @@ -61,7 +62,8 @@ class TiledDataset(data.Dataset): h, w, c = img.shape # Uncomment to filter any image that doesnt meet a threshold size. - if min(h,w) < 1024: + if min(h,w) < 256: + print("Skipping due to threshold") return None # We must convert the image into a square. @@ -70,13 +72,7 @@ class TiledDataset(data.Dataset): img = img[(h - dim) // 2:dim + (h - dim) // 2, (w - dim) // 2:dim + (w - dim) // 2, :] img = cv2.resize(img, (self.opt['imgsize'], self.opt['imgsize']), interpolation=cv2.INTER_AREA) - # I was having some issues with unicode filenames with cv2. Hence using PIL. - # cv2.imwrite(osp.join(self.opt['save_folder'], basename + ".jpg"), img, [cv2.IMWRITE_JPEG_QUALITY, self.opt['compression_level']]) - - img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) - img = (img * 255).astype(np.uint8) - img = Image.fromarray(img) - img.save(osp.join(self.opt['save_folder'], basename + ".jpg"), "JPEG", quality=self.opt['compression_level'], optimize=True) + cv2.imwrite(osp.join(self.opt['save_folder'], basename + ".jpg"), img, [cv2.IMWRITE_JPEG_QUALITY, self.opt['compression_level']]) return None def __len__(self): diff --git a/codes/scripts/extract_subimages_with_ref.py b/codes/scripts/extract_subimages_with_ref.py index 1ca5abd1..c6dd1182 100644 --- a/codes/scripts/extract_subimages_with_ref.py +++ b/codes/scripts/extract_subimages_with_ref.py @@ -13,19 +13,19 @@ import torch def main(): split_img = False opt = {} - opt['n_thread'] = 7 + opt['n_thread'] = 4 opt['compression_level'] = 90 # JPEG compression quality rating. # CV_IMWRITE_PNG_COMPRESSION from 0 to 9. A higher value means a smaller size and longer # compression time. If read raw images during training, use 0 for faster IO speed. opt['dest'] = 'file' - opt['input_folder'] = 'F:\\4k6k\\datasets\\images\youtube\\images_cook' - opt['save_folder'] = 'F:\\4k6k\\datasets\\images\\youtube_massive_cook' - opt['crop_sz'] = [512, 1024, 2048] # the size of each sub-image - opt['step'] = [256, 512, 1024] # step of the sliding crop window + opt['input_folder'] = 'F:\\4k6k\\datasets\\ns_images\\imagesets\\images' + opt['save_folder'] = 'F:\\4k6k\\datasets\\ns_images\\imagesets\\512_with_ref_new' + opt['crop_sz'] = [1024, 2048] # the size of each sub-image + opt['step'] = [700, 1200] # step of the sliding crop window opt['exclusions'] = [[],[],[]] # image names matching these terms wont be included in the processing. - opt['thres_sz'] = 128 # size threshold - opt['resize_final_img'] = [.5, .25, .125] + opt['thres_sz'] = 256 # size threshold + opt['resize_final_img'] = [.5, .25] opt['only_resize'] = False opt['vertical_split'] = False opt['input_image_max_size_before_being_halved'] = 5500 # As described, images larger than this dimensional size will be halved before anything else is done. @@ -245,7 +245,7 @@ class TiledDataset(data.Dataset): for exc in exclusions: if exc in path: excluded = True - break; + break if excluded: continue results.extend(self.get_for_scale(img, crop_sz, step, resize_factor, ref_resize_factor)) @@ -262,6 +262,7 @@ def extract_single(opt, writer): dataset = TiledDataset(opt) dataloader = data.DataLoader(dataset, num_workers=opt['n_thread'], collate_fn=identity) tq = tqdm(dataloader) + i = 0 for spl_imgs in tq: if spl_imgs is None: continue @@ -272,7 +273,8 @@ def extract_single(opt, writer): imgs, path = imgs if imgs is None or len(imgs) <= 1: continue - path = path + "_" + lbl + path = f'{path}_{lbl}_{i}' + i += 1 ref_id = writer.write_reference_image(imgs[0], path) for tile in imgs[1:]: writer.write_tile_image(ref_id, tile) diff --git a/codes/scripts/srflow_latent_space_playground.py b/codes/scripts/srflow_latent_space_playground.py index 621f671b..8cbce442 100644 --- a/codes/scripts/srflow_latent_space_playground.py +++ b/codes/scripts/srflow_latent_space_playground.py @@ -162,7 +162,8 @@ if __name__ == "__main__": torch.backends.cudnn.benchmark = True srg_analyze = False parser = argparse.ArgumentParser() - parser.add_argument('-opt', type=str, help='Path to options YAML file.', default='../../options/train_exd_imgsetext_srflow_bigboi_frompsnr.yml') + parser.add_argument('-opt', type=str, help='Path to options YAML file.', default='../options/train_exd_imgsetext_srflow_bigboi_ganbase.yml') + #parser.add_argument('-opt', type=str, help='Path to options YAML file.', default='../../options/train_exd_imgsetext_srflow_bigboi_frompsnr.yml') opt = option.parse(parser.parse_args().opt, is_train=False) opt = option.dict_to_nonedict(opt) utils.util.loaded_options = opt @@ -180,8 +181,8 @@ if __name__ == "__main__": gen.eval() mode = "feed_through" # temperature | restore | latent_transfer | feed_through - imgs_to_resample_pattern = "F:\\4k6k\\datasets\\ns_images\\adrianna\\val2\\lr\\*" - #imgs_to_resample_pattern = "F:\\4k6k\\datasets\\ns_images\\adrianna\\analyze\\analyze_xx\\*" + #imgs_to_resample_pattern = "F:\\4k6k\\datasets\\ns_images\\adrianna\\val2\\lr\\*" + imgs_to_resample_pattern = "F:\\4k6k\\datasets\\ns_images\\adrianna\\analyze\\analyze_xx\\*" #imgs_to_resample_pattern = "F:\\4k6k\\datasets\\ns_images\\imagesets\\images-half\\*lanette*" scale = 2 resample_factor = 2 # When != 1, the HR image is upsampled by this factor using a bicubic to get the local latents. E.g. set this to '2' to get 2x upsampling.