-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
183 lines (136 loc) · 6.42 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os
import shutil
import sys
# import matplotlib_inline as plt_inline
import os
import sys
import subprocess
import zipfile
import requests
import glob
import shutil
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import PIL
from PIL import Image
# import matplotlib.pyplot as plt
# import matplotlib.image as mpimg
import clip
import torch
from torchvision import transforms
# import keras
from keras.preprocessing import image
from tqdm import tqdm
from alive_progress import alive_bar
def main():
# from ml4a import image
# from ml4a.models import esrgan
# instead of using ml4a, I will use the ESRGAN model directly via a downloaded copy, and CLIP to do the image comparison and selection. I will also use the PIL library to do the image manipulation. I will also use the os library to do the file manipulation.
# load the model into memory
print("Loading ESRGAN model")
# load the CLIP model into memory
# print("Loading CLIP model")
# model, preprocess = clip.load("ViT-B/32", device)
# if the model is not already downloaded, download it
# check the models folder for the model
# if it is not there, download it
# if it is there, load it
if not os.path.exists("models"):
os.mkdir("models")
elif not os.path.exists("models/ESRGAN"):
os.mkdir("models/ESRGAN")
else:
print("ESRGAN model likely already downloaded")
if not os.path.exists("models/ESRGAN/RRDB_ESRGAN_x4.pth"):
# download the ESRGAN model
print("Downloading ESRGAN model")
r = requests.get("https://data.vision.ee.ethz.ch/cvl/DIV2K/models/RRDB_ESRGAN_x4.pth", allow_redirects=True)
open("RRDB_ESRGAN_x4.pth", "wb").write(r.content)
# download the ESRGAN code
print("Downloading ESRGAN code")
r = requests.get("https://data.vision.ee.ethz.ch/cvl/DIV2K/DIV2K_train_HR.zip", allow_redirects=True)
# unzip the ESRGAN code
print("Unzipping ESRGAN code")
with zipfile.ZipFile("DIV2K_train_HR.zip", "r") as zip_ref:
zip_ref.extractall()
# move the ESRGAN code to the correct folder
print("Moving ESRGAN code")
# make a folder called 'models'
os.mkdir("models")
# move the ESRGAN model to the models folder
shutil.move("RRDB_ESRGAN_x4.pth", "models/RRDB_ESRGAN_x4.pth")
# load the model into memory (it is in esrgan/RRDB_ESRGAN_x4.pth)
print("Loading ESRGAN model")
#!model = torch.load("./models/ESRGAN/RRDB_ESRGAN_x4.pth") # I am getting a invalid load key, '<'. error here
# copilot: I think this is because the model is not in the correct format. I think it is a PyTorch model, and I am trying to load it into TensorFlow. I think I need to convert it to a TensorFlow model. I think I can do that with the following code:
# model = tf.keras.models.load_model("./models/ESRGAN/RRDB_ESRGAN_x4.pth")
try:
model = tf.keras.models.load_model("./models/ESRGAN/RRDB_ESRGAN_x4.pth", compile=False)
except Exception as e:
print(e)
# download the ESRGAN model
print("Downloading ESRGAN model")
r = requests.get("https://data.vision.ee.ethz.ch/cvl/DIV2K/models/RRDB_ESRGAN_x4.pth", allow_redirects=True)
open("./models/ESRGAN/RRDB_ESRGAN_x4.pth", "wb").write(r.content)
# try to load the model again
SAVED_MODEL_PATH = "https://tfhub.dev/captain-pool/esrgan-tf2/1"
model = hub.load(SAVED_MODEL_PATH)
model.eval() # set to evaluation mode which means that the model will not be trained and will not be updated with new data. This is important because we want to use the model as is, and not have it change as we use it.
input_images_directory = './input_images'
output_images_directory = './output_images'
# create the input and output directories if they don't exist
if not os.path.exists(input_images_directory):
os.makedirs(input_images_directory)
if not os.path.exists(output_images_directory):
os.makedirs(output_images_directory)
# take the input images and convert them to the correct format for the ESRGAN model
input_images = glob.glob(input_images_directory + '/*')
number_of_images = len(input_images)
with alive_bar(number_of_images) as bar:
for input_image in input_images:
# load the image
image = PIL.Image.open(input_image)
# resize the image to 256x256
image = image.resize((256, 256))
# save the image
image.save(input_image)
bar()
# Now that the images are in the correct format, we can run them through the ESRGAN model
input_images = glob.glob(input_images_directory + '/*')
# create a list of the output images
output_images = []
# create a list of the output image names
output_image_names = []
# begin the loop
with alive_bar(number_of_images) as bar:
# take each image and magnify it using the ESRGAN model 4x and save the output
for input_image in input_images:
# load the image
image = PIL.Image.open(input_image)
# convert the image to a numpy array
image = np.array(image)
# convert the image to a tensor
image = torch.from_numpy(image).to(device).float()
# convert the image to the correct format for the ESRGAN model
image = image.permute(2, 0, 1).unsqueeze(0)
# run the image through the ESRGAN model
with torch.no_grad():
output = model(image)
# convert the output to a numpy array
output = output.squeeze(0).permute(1, 2, 0).cpu().numpy()
# convert the output to an image
output = PIL.Image.fromarray(np.uint8(output.clip(0, 1) * 255))
# save the output image
output_image_name = os.path.basename(input_image)
output_image_name = os.path.splitext(output_image_name)[0]
output_image_name = output_image_name + '_esrgan.png'
output_image_path = os.path.join(output_images_directory, output_image_name)
output.save(output_image_path)
# add the output image to the list of output images
output_images.append(output_image_path)
# add the output image name to the list of output image names
output_image_names.append(output_image_name)
bar()
if __name__ == "__main__":
main()