-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgp_gan_one_to_one.py
105 lines (73 loc) · 2.92 KB
/
gp_gan_one_to_one.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import glob
import os
from shutil import copyfile
import random
import itertools
import json
def createPath(curr_subdir):
"""[summary]
Args:
curr_subdir ([type]): [description]
"""
if not os.path.exists(curr_subdir):
os.makedirs(curr_subdir)
print(curr_subdir + " directory was made")
def run_one_to_one(gen_aug_dir, gen_dst_dir, results_dir, domains, n):
"""[summary]
Args:
src_dir ([type]): [description]
dst_dir ([type]): [description]
store_dir ([type]): [description]
results_dir ([type]): [description]
domains ([type]): [description]
n ([type]): [description]
"""
random.seed(42)
domain_combos = list(itertools.product(domains, repeat=2))
my_dict = {}
for (src_domain, target_domain) in domain_combos:
src_dir = f"{gen_aug_dir}{src_domain}/canvases/"
mask_dir = f"{gen_aug_dir}{src_domain}/masks/"
lbl_dir = f"{gen_aug_dir}{src_domain}/labels/"
all_srcs = glob.glob(src_dir + "*.jpg")
all_masks = glob.glob(mask_dir + "*.png")
all_txts = glob.glob(lbl_dir + "*.txt")
all_srcs.sort()
all_masks.sort()
all_txts.sort()
#dst_dir = "/scratch/public/jitter/wt/images/"
#REPLACE/Background/
#dest_dir = f"{gen_dst_dir}{target_domain}/"
dest_dir = f"{gen_dst_dir}{target_domain}/Background/"
#Have background destination directories
all_dsts = glob.glob(dest_dir + "*.jpg")
dst_imgs = random.sample(all_dsts, n)
#Could shuffle if desired
#store_fname = ""
#with open(store_dir + store_fname, "w") as f:
# for file in MW_dsts:
# f.write(file + "\n")
dict_name = f"s_{src_domain}_t_{target_domain}"
current_subdir = f"{results_dir}{dict_name}/"
createPath(current_subdir)
#Augmented: {s_src_t_target: [(augment, background)]
# }
my_dict[dict_name] = []
for i in range(n):
my_src = all_srcs[i]
my_mask = all_masks[i]
my_txt = all_txts[i]
src_address = my_src[my_src.rfind("/")+1:my_src.find(".jpg")]
my_dst = dst_imgs[i]
dst_address = my_dst[my_dst.rfind("/")+1:my_dst.find(".jpg")]
my_dict[dict_name].append((src_address, dst_address))
blended_out = f"{current_subdir}{dst_address}.jpg"
#Removes space for GP GAN
blended_out = blended_out.replace(" ", "")
#Copies txt file of mask to synthetic output
copyfile(my_txt, blended_out.replace(".jpg",".txt"))
cmd = f"python run_gp_gan.py --src_image {my_src} --dst_image \"{my_dst}\" --mask_image {my_mask} --blended_image {my_mask} --gpu 1"
print("Running command:")
print(cmd)
#os.system(cmd)
return my_dict