From e1392e90dfe36105dda1141209ae9932bf837381 Mon Sep 17 00:00:00 2001 From: Chandradithya Date: Thu, 6 Mar 2025 23:18:25 +0530 Subject: [PATCH 1/2] Fixed dataset combination issue in CycleGAN --- datasets/combine_A_and_B.py | 124 ++++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 54 deletions(-) diff --git a/datasets/combine_A_and_B.py b/datasets/combine_A_and_B.py index d3d88aa318e..b1b305494af 100644 --- a/datasets/combine_A_and_B.py +++ b/datasets/combine_A_and_B.py @@ -4,64 +4,80 @@ import argparse from multiprocessing import Pool - def image_write(path_A, path_B, path_AB): - im_A = cv2.imread(path_A, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR - im_B = cv2.imread(path_B, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR + im_A = cv2.imread(path_A, 1) + im_B = cv2.imread(path_B, 1) im_AB = np.concatenate([im_A, im_B], 1) cv2.imwrite(path_AB, im_AB) - -parser = argparse.ArgumentParser('create image pairs') -parser.add_argument('--fold_A', dest='fold_A', help='input directory for image A', type=str, default='../dataset/50kshoes_edges') -parser.add_argument('--fold_B', dest='fold_B', help='input directory for image B', type=str, default='../dataset/50kshoes_jpg') -parser.add_argument('--fold_AB', dest='fold_AB', help='output directory', type=str, default='../dataset/test_AB') -parser.add_argument('--num_imgs', dest='num_imgs', help='number of images', type=int, default=1000000) -parser.add_argument('--use_AB', dest='use_AB', help='if true: (0001_A, 0001_B) to (0001_AB)', action='store_true') -parser.add_argument('--no_multiprocessing', dest='no_multiprocessing', help='If used, chooses single CPU execution instead of parallel execution', action='store_true',default=False) -args = parser.parse_args() - -for arg in vars(args): - print('[%s] = ' % arg, getattr(args, arg)) - -splits = os.listdir(args.fold_A) - -if not args.no_multiprocessing: - pool=Pool() - -for sp in splits: - img_fold_A = os.path.join(args.fold_A, sp) - img_fold_B = os.path.join(args.fold_B, sp) - img_list = os.listdir(img_fold_A) - if args.use_AB: - img_list = [img_path for img_path in img_list if '_A.' in img_path] - - num_imgs = min(args.num_imgs, len(img_list)) - print('split = %s, use %d/%d images' % (sp, num_imgs, len(img_list))) - img_fold_AB = os.path.join(args.fold_AB, sp) - if not os.path.isdir(img_fold_AB): - os.makedirs(img_fold_AB) - print('split = %s, number of images = %d' % (sp, num_imgs)) - for n in range(num_imgs): - name_A = img_list[n] - path_A = os.path.join(img_fold_A, name_A) - if args.use_AB: - name_B = name_A.replace('_A.', '_B.') - else: - name_B = name_A - path_B = os.path.join(img_fold_B, name_B) - if os.path.isfile(path_A) and os.path.isfile(path_B): - name_AB = name_A - if args.use_AB: - name_AB = name_AB.replace('_A.', '.') # remove _A - path_AB = os.path.join(img_fold_AB, name_AB) - if not args.no_multiprocessing: +def process_images_multiprocessing(fold_A, fold_B, fold_AB, num_imgs, use_AB): + splits = os.listdir(fold_A) + pool = Pool() + + for sp in splits: + img_fold_A = os.path.join(fold_A, sp) + img_fold_B = os.path.join(fold_B, sp) + img_list = os.listdir(img_fold_A) + if use_AB: + img_list = [img_path for img_path in img_list if '_A.' in img_path] + + num_imgs = min(num_imgs, len(img_list)) + img_fold_AB = os.path.join(fold_AB, sp) + if not os.path.isdir(img_fold_AB): + os.makedirs(img_fold_AB) + + for n in range(num_imgs): + name_A = img_list[n] + path_A = os.path.join(img_fold_A, name_A) + name_B = name_A.replace('_A.', '_B.') if use_AB else name_A + path_B = os.path.join(img_fold_B, name_B) + if os.path.isfile(path_A) and os.path.isfile(path_B): + name_AB = name_A.replace('_A.', '.') if use_AB else name_A + path_AB = os.path.join(img_fold_AB, name_AB) pool.apply_async(image_write, args=(path_A, path_B, path_AB)) - else: - im_A = cv2.imread(path_A, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR - im_B = cv2.imread(path_B, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR - im_AB = np.concatenate([im_A, im_B], 1) - cv2.imwrite(path_AB, im_AB) -if not args.no_multiprocessing: + pool.close() pool.join() + +def process_images_single(fold_A, fold_B, fold_AB, num_imgs, use_AB): + splits = os.listdir(fold_A) + + for sp in splits: + img_fold_A = os.path.join(fold_A, sp) + img_fold_B = os.path.join(fold_B, sp) + img_list = os.listdir(img_fold_A) + if use_AB: + img_list = [img_path for img_path in img_list if '_A.' in img_path] + + num_imgs = min(num_imgs, len(img_list)) + img_fold_AB = os.path.join(fold_AB, sp) + if not os.path.isdir(img_fold_AB): + os.makedirs(img_fold_AB) + + for n in range(num_imgs): + name_A = img_list[n] + path_A = os.path.join(img_fold_A, name_A) + name_B = name_A.replace('_A.', '_B.') if use_AB else name_A + path_B = os.path.join(img_fold_B, name_B) + if os.path.isfile(path_A) and os.path.isfile(path_B): + name_AB = name_A.replace('_A.', '.') if use_AB else name_A + path_AB = os.path.join(img_fold_AB, name_AB) + im_A = cv2.imread(path_A, 1) + im_B = cv2.imread(path_B, 1) + im_AB = np.concatenate([im_A, im_B], 1) + cv2.imwrite(path_AB, im_AB) + +if __name__ == "__main__": + parser = argparse.ArgumentParser('create image pairs') + parser.add_argument('--fold_A', type=str, required=True, help='Input directory for image A') + parser.add_argument('--fold_B', type=str, required=True, help='Input directory for image B') + parser.add_argument('--fold_AB', type=str, required=True, help='Output directory') + parser.add_argument('--num_imgs', type=int, default=1000000, help='Number of images') + parser.add_argument('--use_AB', action='store_true', help='(0001_A, 0001_B) to (0001_AB)') + parser.add_argument('--no_multiprocessing', action='store_true', help='Use single CPU execution') + args = parser.parse_args() + + if args.no_multiprocessing: + process_images_single(args.fold_A, args.fold_B, args.fold_AB, args.num_imgs, args.use_AB) + else: + process_images_multiprocessing(args.fold_A, args.fold_B, args.fold_AB, args.num_imgs, args.use_AB) From d6f2562384edbc028e12b464e879eb72fef4610f Mon Sep 17 00:00:00 2001 From: Chandradithya Date: Thu, 6 Mar 2025 23:32:59 +0530 Subject: [PATCH 2/2] Delete combine_A_and_B.py --- datasets/combine_A_and_B.py | 83 ------------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 datasets/combine_A_and_B.py diff --git a/datasets/combine_A_and_B.py b/datasets/combine_A_and_B.py deleted file mode 100644 index b1b305494af..00000000000 --- a/datasets/combine_A_and_B.py +++ /dev/null @@ -1,83 +0,0 @@ -import os -import numpy as np -import cv2 -import argparse -from multiprocessing import Pool - -def image_write(path_A, path_B, path_AB): - im_A = cv2.imread(path_A, 1) - im_B = cv2.imread(path_B, 1) - im_AB = np.concatenate([im_A, im_B], 1) - cv2.imwrite(path_AB, im_AB) - -def process_images_multiprocessing(fold_A, fold_B, fold_AB, num_imgs, use_AB): - splits = os.listdir(fold_A) - pool = Pool() - - for sp in splits: - img_fold_A = os.path.join(fold_A, sp) - img_fold_B = os.path.join(fold_B, sp) - img_list = os.listdir(img_fold_A) - if use_AB: - img_list = [img_path for img_path in img_list if '_A.' in img_path] - - num_imgs = min(num_imgs, len(img_list)) - img_fold_AB = os.path.join(fold_AB, sp) - if not os.path.isdir(img_fold_AB): - os.makedirs(img_fold_AB) - - for n in range(num_imgs): - name_A = img_list[n] - path_A = os.path.join(img_fold_A, name_A) - name_B = name_A.replace('_A.', '_B.') if use_AB else name_A - path_B = os.path.join(img_fold_B, name_B) - if os.path.isfile(path_A) and os.path.isfile(path_B): - name_AB = name_A.replace('_A.', '.') if use_AB else name_A - path_AB = os.path.join(img_fold_AB, name_AB) - pool.apply_async(image_write, args=(path_A, path_B, path_AB)) - - pool.close() - pool.join() - -def process_images_single(fold_A, fold_B, fold_AB, num_imgs, use_AB): - splits = os.listdir(fold_A) - - for sp in splits: - img_fold_A = os.path.join(fold_A, sp) - img_fold_B = os.path.join(fold_B, sp) - img_list = os.listdir(img_fold_A) - if use_AB: - img_list = [img_path for img_path in img_list if '_A.' in img_path] - - num_imgs = min(num_imgs, len(img_list)) - img_fold_AB = os.path.join(fold_AB, sp) - if not os.path.isdir(img_fold_AB): - os.makedirs(img_fold_AB) - - for n in range(num_imgs): - name_A = img_list[n] - path_A = os.path.join(img_fold_A, name_A) - name_B = name_A.replace('_A.', '_B.') if use_AB else name_A - path_B = os.path.join(img_fold_B, name_B) - if os.path.isfile(path_A) and os.path.isfile(path_B): - name_AB = name_A.replace('_A.', '.') if use_AB else name_A - path_AB = os.path.join(img_fold_AB, name_AB) - im_A = cv2.imread(path_A, 1) - im_B = cv2.imread(path_B, 1) - im_AB = np.concatenate([im_A, im_B], 1) - cv2.imwrite(path_AB, im_AB) - -if __name__ == "__main__": - parser = argparse.ArgumentParser('create image pairs') - parser.add_argument('--fold_A', type=str, required=True, help='Input directory for image A') - parser.add_argument('--fold_B', type=str, required=True, help='Input directory for image B') - parser.add_argument('--fold_AB', type=str, required=True, help='Output directory') - parser.add_argument('--num_imgs', type=int, default=1000000, help='Number of images') - parser.add_argument('--use_AB', action='store_true', help='(0001_A, 0001_B) to (0001_AB)') - parser.add_argument('--no_multiprocessing', action='store_true', help='Use single CPU execution') - args = parser.parse_args() - - if args.no_multiprocessing: - process_images_single(args.fold_A, args.fold_B, args.fold_AB, args.num_imgs, args.use_AB) - else: - process_images_multiprocessing(args.fold_A, args.fold_B, args.fold_AB, args.num_imgs, args.use_AB)