-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper_resolve.py
83 lines (71 loc) · 2.96 KB
/
super_resolve.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
# Each of the LR faces is upscaled to 224x224 using of the SR techniques.
# (a) bicubic interpolation, (b) SCN, (c) sparse representation super-resolution (ScSR), (d) LapSRN, (e) SRGAN
from srgan import SRGAN # put srgan.py in the folder
import numpy as np
import imageio
import scipy
import os
from glob import glob
import random
def upscale(gan,root_path, img_LR_path, img_HR_path):
lr = 16
hr = 64
img_lr = scipy.misc.imread(img_LR_path, mode='RGB').astype(np.float)
img_hr = scipy.misc.imread(img_HR_path, mode='RGB').astype(np.float)
imgs_hr = []
imgs_lr = []
imgs_hr.append(scipy.misc.imresize(img_hr, (hr, hr)))
imgs_lr.append(scipy.misc.imresize(img_lr, (lr, lr)))
imgs_hr = np.array(imgs_hr) / 127.5 - 1.
imgs_lr = np.array(imgs_lr) / 127.5 - 1.
fake_hr = gan.generator.predict(imgs_lr)
fake_hr = 0.5 * fake_hr + 0.5
imgs_hr = 0.5 * imgs_hr + 0.5
parts = img_LR_path.split("/")
fake_path = root_path + "fake_HR_{}/{}".format(str(hr), parts[len(parts)-1])
imageio.imwrite(fake_path, np.squeeze(fake_hr))
parts = img_HR_path.split("/")
hr_path = root_path + "HR_{}/{}".format(str(hr), parts[len(parts)-1])
imageio.imwrite(hr_path, imgs_hr[0])
return np.squeeze(fake_hr), imgs_hr
def main():
gan = SRGAN()
# gan.generator.load_weights('srgan_28-28-to-224-224.h5') # trained on vgg train
gan.generator.load_weights('/home/nbayat5/Desktop/srgan/saved_model/VGG_saved_model/VGG16to64.h5')
# root_path = "/imaging/nbayat/AR/LRFR_Pairs"
# root_path = "/home/nbayat5/Desktop/LFW/LR_HR_pairs/"
root_path = "/home/nbayat5/Desktop/celebA/LR_HR_pairs/"
gallery_paths = glob(root_path + "/*1.jpg")
# selected_gallery_paths = random.sample(gallery_paths, 100)
for img_LR_path in gallery_paths:
print("LR Image Path: ", img_LR_path)
parts = img_LR_path.split("-")
parts[len(parts) - 1] = "14.jpg"
img_HR_path = '-'.join(parts)
if os.path.exists(img_HR_path):
upscale(gan,root_path, img_LR_path, img_HR_path)
if __name__ == "__main__":
main()
# select_fifty()
def select_fifty():
root_path = "/imaging/nbayat/AR/LRFR_Pairs/fake_HR"
male_count = 0
female_count = 0
for filename in os.listdir(root_path):
img_LR_path = os.path.join(root_path, filename)
parts = filename.split('-')
parts[len(parts) - 1] = "14.jpg"
HR_filename = '-'.join(parts)
img_HR_path = os.path.join("/imaging/nbayat/AR/LRFR_Pairs/HR", HR_filename)
if parts[0] == "m":
if male_count >= 50:
print(filename+" is removed.")
os.system("rm {}".format(img_LR_path))
os.system("rm {}".format(img_HR_path))
male_count += 1
else:
if female_count >= 50:
print(filename + " is removed.")
os.system("rm {}".format(img_LR_path))
os.system("rm {}".format(img_HR_path))
female_count += 1