forked from somnathjena2011/ISRO
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline.py
295 lines (267 loc) · 10 KB
/
pipeline.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import sys
import os
import copy
import torch
argList = sys.argv[1:]
print(argList)
i = 0
commands = []
scales = []
types = []
models = []
input = "inputs/input.png"
output = None
compressOutput = True
inputFolder = None
outputFolder = None
#paths of the trained models for varius super resolution DL networks
model_dict = {
"realesrgan": {
"sr_path": "ESRGAN",
"model_path": "experiments/pretrained_models/net_g_15000_realesrgan.pth"
},
"msrresnet": {
"sr_path": "MSR_SWINIR",
"model_path": "pretrained_weights/msrresnet_x4_psnr.pth"
},
"swinir": {
"sr_path": "MSR_SWINIR",
"model_path": "pretrained_weights/20000_G_swinIr.pth"
},
"HAT": {
"sr_path": "HAT/hat",
"model_path": "../experiments/pretrained_models/net_g_5000_hat.pth"
}
}
#parsing the command line arguments
while i < len(argList):
if argList[i] in {"-i","--input"}:
input = argList[i+1]
i+=2
elif argList[i] in {"-o","--output"}:
output = argList[i+1]
i+=2
elif argList[i] in {"-co","--compress_output"}:
co = argList[i+1]
if co=="False":
compressOutput = False
i+=2
elif argList[i] in {"-if","--input_folder"}:
inputFolder = argList[i+1]
i+=2
elif argList[i] in {"-of","--output_folder"}:
outputFolder = argList[i+1]
i+=2
elif argList[i] in {"sr"}:
srPath = "ESRGAN"
modelCode = "main_test_{}.py"
model = "realesrgan"
tile = None
model_path = None
folder_lq = ""
file_name = ""
output_path = ""
scale = 4
i+=1
while i<len(argList) and argList[i].startswith("-"):
if argList[i] == "--sr_path" or argList[i] == "-sp":
srPath = argList[i+1]
elif argList[i] == "--sr_model" or argList[i] == "-sm":
model = argList[i+1]
elif argList[i] == "--tile" or argList[i] == "-t":
tile = int(argList[i+1])
elif argList[i] == "--model_path" or argList[i] == "-mp":
model_path = argList[i+1]
elif argList[i] == "--scale" or argList[i] == "-s":
scale = int(argList[i+1])
else:
i-=1
i+=2
if srPath is None:
srPath = model_dict[model]["sr_path"]
if model_path is None:
model_path = model_dict[model]["model_path"]
modelCode = modelCode.format(model)
command = f"cd {srPath} && python {modelCode}"
if tile is not None:
command += f" --tile {tile}"
if model_path is not None:
command += f" --model_path {model_path}"
command += f" --scale {scale}"
command += f" --model_name {model}"
types.append("sr")
models.append(model)
scales.append(scale)
commands.append(command)
elif argList[i] in {"int"}:
inPath = "Interpolate/interpolate.py"
scale = 4
model = "bicubic"
i+=1
while i<len(argList) and argList[i].startswith("-"):
if argList[i] == "--scale" or argList[i] == "-s":
scale = int(argList[i+1])
elif argList[i] == "--int_path" or argList[i] == "-ip":
inPath = argList[i+1]
elif argList[i] == "--int_model" or argList[i] == "-im":
model = argList[i+1]
else:
i-=1
i+=2
command = f"python {inPath} --sf {scale}"
types.append("int")
models.append(model)
scales.append(scale)
commands.append(command)
elif i<len(argList) and argList[i] in {"enh"}:
enPath = "LightEnhancement"
scale = 1
model = "URetinex"
i+=1
while i<len(argList) and argList[i].startswith("-"):
if argList[i] == "--scale" or argList[i] == "-s":
_ = int(argList[i+1])
elif argList[i] == "--enh_path" or argList[i] == "-ep":
enPath = argList[i+1]
elif argList[i] == "--enh_model" or argList[i] == "-em":
model = argList[i+1]
else:
i-=1
i+=2
command = f"cd {enPath} && python test.py"
types.append("enh")
models.append(model)
scales.append(scale)
commands.append(command)
elif i<len(argList) and argList[i] in {"shp"}:
shpPath = "Sharpen_Denoise"
scale = 1
model = "blur"
i+=1
while i<len(argList) and argList[i].startswith("-"):
if argList[i] == "--scale" or argList[i] == "-s":
_ = int(argList[i+1])
else:
i-=1
i+=2
command = f"cd {shpPath} && python sharpen.py"
types.append("shp")
models.append(model)
scales.append(scale)
commands.append(command)
elif i<len(argList) and argList[i] in ("den"):
denPath = "Sharpen_Denoise/NAFNet"
scale = 1
model = "NAFNet"
tile = None
i+=1
while i<len(argList) and argList[i].startswith("-"):
if argList[i] == "--scale" or argList[i] == "-s":
_ = int(argList[i+1])
elif argList[i] == "--tile" or argList[i] == "-t":
tile = int(argList[i+1])
else:
i-=1
i+=2
command = f"cd {denPath} && python denoise.py"
if tile is not None:
command += f" --tile {tile}"
types.append("den")
models.append(model)
scales.append(scale)
commands.append(command)
#processing a single file
if inputFolder is None:
ext = input.split(".")[1]
if outputFolder is None:
outputFolder = "/content/drive/MyDrive/outputs/"
if not outputFolder.endswith("/"):
outputFolder+="/"
outputFormat = outputFolder + "{}_{}_{}." + ext
outputPaths = [outputFormat for _ in models]
inputPaths = [outputFormat for _ in models]
inputPaths[0] = input
img_name, ext = os.path.splitext(os.path.basename(input))
outputPaths[0] = outputFormat.format(img_name, models[0], scales[0])
for i in range(1, len(outputPaths)):
inputPaths[i] = outputPaths[i-1]
img_name, ext = os.path.splitext(os.path.basename(inputPaths[i]))
outputPaths[i] = outputFormat.format(img_name, models[i], scales[i])
if output is not None:
outputPaths[-1] = output
rm_command = "rm -rf {}"
for i in range(len(commands)):
if types[i]=="enh":
commands[i] += f" --img_path {inputPaths[i]}"
commands[i] += f" --output {os.path.dirname(outputPaths[i])}"
commands[i] += f" --output_path {outputPaths[i]}"
elif types[i]=="sr":
commands[i] += f" --folder_lq {os.path.dirname(inputPaths[i])}"
commands[i] += f" --file_name {os.path.basename(inputPaths[i])}"
commands[i] += f" --output_path {outputPaths[i]}"
elif types[i]=="int":
commands[i] += f" --in_path {inputPaths[i]}"
commands[i] += f" --out_path {outputPaths[i]}"
elif types[i]=="shp":
commands[i] += f" --in_path {inputPaths[i]}"
commands[i] += f" --out_path {outputPaths[i]}"
elif types[i]=="den":
commands[i] += f" --in_path {inputPaths[i]}"
commands[i] += f" --out_path {outputPaths[i]}"
if compressOutput and i>0:
commands[i] += " && " + rm_command.format(inputPaths[i])
for command in commands:
print("Executing command")
print(command)
os.system(command)
torch.cuda.empty_cache()
#processing all files in a folder
#PLEASE MAKE SURE ALL ARE IMAGES ONLY IF FOLDER IS PASSED
else:
copy_commands = copy.deepcopy(commands)
for file in os.listdir(inputFolder):
commands = copy.deepcopy(copy_commands)
input = os.path.join(inputFolder, file)
ext = input.split(".")[1]
if outputFolder is None:
outputFolder = "/content/drive/MyDrive/outputs/"
if not outputFolder.endswith("/"):
outputFolder+="/"
outputFormat = outputFolder + "{}_{}_{}." + ext
outputPaths = [outputFormat for _ in models]
inputPaths = [outputFormat for _ in models]
inputPaths[0] = input
img_name, ext = os.path.splitext(os.path.basename(input))
outputPaths[0] = outputFormat.format(img_name, models[0], scales[0])
for i in range(1, len(outputPaths)):
inputPaths[i] = outputPaths[i-1]
img_name, ext = os.path.splitext(os.path.basename(inputPaths[i]))
outputPaths[i] = outputFormat.format(img_name, models[i], scales[i])
if output is not None:
outputPaths[-1] = output
rm_command = "rm -rf {}"
for i in range(len(commands)):
if types[i]=="enh":
commands[i] += f" --img_path {inputPaths[i]}"
commands[i] += f" --output {os.path.dirname(outputPaths[i])}"
commands[i] += f" --output_path {outputPaths[i]}"
elif types[i]=="sr":
commands[i] += f" --folder_lq {os.path.dirname(inputPaths[i])}"
commands[i] += f" --file_name {os.path.basename(inputPaths[i])}"
commands[i] += f" --output_path {outputPaths[i]}"
elif types[i]=="int":
commands[i] += f" --in_path {inputPaths[i]}"
commands[i] += f" --out_path {outputPaths[i]}"
elif types[i]=="shp":
commands[i] += f" --in_path {inputPaths[i]}"
commands[i] += f" --out_path {outputPaths[i]}"
elif types[i]=="den":
commands[i] += f" --in_path {inputPaths[i]}"
commands[i] += f" --out_path {outputPaths[i]}"
if compressOutput and i>0:
commands[i] += " && " + rm_command.format(inputPaths[i])
for command in commands:
print("Executing command")
print(command)
os.system(command)
torch.cuda.empty_cache()