Spaces:
Runtime error
Runtime error
Fixes.
Browse files- inference.py +36 -16
- inference_manga_v2.py +6 -8
inference.py
CHANGED
@@ -4,6 +4,8 @@ import cv2
|
|
4 |
import numpy as np
|
5 |
import torch
|
6 |
import architecture as arch
|
|
|
|
|
7 |
|
8 |
def is_cuda():
|
9 |
if torch.cuda.is_available():
|
@@ -37,19 +39,37 @@ for k, v in model.named_parameters():
|
|
37 |
v.requires_grad = False
|
38 |
model = model.to(device)
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import numpy as np
|
5 |
import torch
|
6 |
import architecture as arch
|
7 |
+
from split_image import split_image
|
8 |
+
from run_cmd import run_cmd
|
9 |
|
10 |
def is_cuda():
|
11 |
if torch.cuda.is_available():
|
|
|
39 |
v.requires_grad = False
|
40 |
model = model.to(device)
|
41 |
|
42 |
+
base_path = os.path.dirname(img_path)
|
43 |
+
image_name = os.path.basename(img_path)
|
44 |
+
|
45 |
+
# Split image
|
46 |
+
run_cmd(f"split-image {img_path} 7 7 --output-dir {base_path} --quiet")
|
47 |
+
|
48 |
+
for root, dirs, files in os.walk(base_path, topdown=True):
|
49 |
+
for x, name in enumerate(files):
|
50 |
+
file_path = os.path.join(root, name)
|
51 |
+
|
52 |
+
if file_path == img_path:
|
53 |
+
continue
|
54 |
+
|
55 |
+
# Read image
|
56 |
+
img = cv2.imread(file_path, cv2.IMREAD_COLOR)
|
57 |
+
img = img * 1.0 / 255
|
58 |
+
img = torch.from_numpy(np.transpose(img[:, :, [2, 1, 0]], (2, 0, 1))).float()
|
59 |
+
img_LR = img.unsqueeze(0)
|
60 |
+
img_LR = img_LR.to(device)
|
61 |
+
|
62 |
+
print(f"Start upscaling tile {x}...")
|
63 |
+
with torch.no_grad():
|
64 |
+
output = model(img_LR).data.squeeze().float().cpu().clamp_(0, 1).numpy()
|
65 |
+
output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0))
|
66 |
+
output = (output * 255.0).round()
|
67 |
+
print(f"Finished upscaling tile {x}, saving tile.")
|
68 |
+
cv2.imwrite(file_path, output)
|
69 |
+
|
70 |
+
# Join all tiles
|
71 |
+
run_cmd(f"cd {base_path} && split-image {image_name} 7 7 -r --quiet")
|
72 |
+
|
73 |
+
# Open image and save as png with the ouput name
|
74 |
+
img_out = cv2.imread(img_path);
|
75 |
+
cv2.imwrite(output_dir, img_out, [int(cv2.IMWRITE_PNG_COMPRESSION), 5])
|
inference_manga_v2.py
CHANGED
@@ -6,8 +6,6 @@ import torch
|
|
6 |
import architecture as arch
|
7 |
from split_image import split_image
|
8 |
from run_cmd import run_cmd
|
9 |
-
import re
|
10 |
-
from pathlib import Path
|
11 |
|
12 |
def is_cuda():
|
13 |
if torch.cuda.is_available():
|
@@ -35,17 +33,17 @@ for k, v in model.named_parameters():
|
|
35 |
v.requires_grad = False
|
36 |
model = model.to(device)
|
37 |
|
38 |
-
|
|
|
39 |
|
40 |
# Split image
|
41 |
-
run_cmd(f"split-image {img_path} 7 7 --output-dir {
|
42 |
|
43 |
-
for root, dirs, files in os.walk(
|
44 |
for x, name in enumerate(files):
|
45 |
file_path = os.path.join(root, name)
|
46 |
|
47 |
if file_path == img_path:
|
48 |
-
print(file_path)
|
49 |
continue
|
50 |
|
51 |
# Read image
|
@@ -64,9 +62,9 @@ for root, dirs, files in os.walk(base, topdown=True):
|
|
64 |
cv2.imwrite(file_path, output)
|
65 |
|
66 |
# Join all tiles
|
67 |
-
run_cmd(f"cd {
|
68 |
|
69 |
# Open image and save as png with the ouput name
|
70 |
img_out = cv2.imread(img_path);
|
71 |
-
cv2.imwrite(output_dir, img_out, [int(cv2.IMWRITE_PNG_COMPRESSION),
|
72 |
|
|
|
6 |
import architecture as arch
|
7 |
from split_image import split_image
|
8 |
from run_cmd import run_cmd
|
|
|
|
|
9 |
|
10 |
def is_cuda():
|
11 |
if torch.cuda.is_available():
|
|
|
33 |
v.requires_grad = False
|
34 |
model = model.to(device)
|
35 |
|
36 |
+
base_path = os.path.dirname(img_path)
|
37 |
+
image_name = os.path.basename(img_path)
|
38 |
|
39 |
# Split image
|
40 |
+
run_cmd(f"split-image {img_path} 7 7 --output-dir {base_path} --quiet")
|
41 |
|
42 |
+
for root, dirs, files in os.walk(base_path, topdown=True):
|
43 |
for x, name in enumerate(files):
|
44 |
file_path = os.path.join(root, name)
|
45 |
|
46 |
if file_path == img_path:
|
|
|
47 |
continue
|
48 |
|
49 |
# Read image
|
|
|
62 |
cv2.imwrite(file_path, output)
|
63 |
|
64 |
# Join all tiles
|
65 |
+
run_cmd(f"cd {base_path} && split-image {image_name} 7 7 -r --quiet")
|
66 |
|
67 |
# Open image and save as png with the ouput name
|
68 |
img_out = cv2.imread(img_path);
|
69 |
+
cv2.imwrite(output_dir, img_out, [int(cv2.IMWRITE_PNG_COMPRESSION), 5])
|
70 |
|