Spaces:
Running
on
T4
Running
on
T4
Commit
·
d26bbd5
1
Parent(s):
8f3d49d
feat: auto downsample if it is oversize
Browse files- .gitignore +2 -0
- __assets__/lr_inputs/naruto.jpg +0 -0
- app.py +5 -1
- test_code/inference.py +12 -9
.gitignore
CHANGED
|
@@ -23,6 +23,8 @@ tmp/*
|
|
| 23 |
*.zip
|
| 24 |
*.mp4
|
| 25 |
*.csv
|
|
|
|
|
|
|
| 26 |
|
| 27 |
!__assets__/lr_inputs/*
|
| 28 |
!__assets__/*
|
|
|
|
| 23 |
*.zip
|
| 24 |
*.mp4
|
| 25 |
*.csv
|
| 26 |
+
*.jpeg
|
| 27 |
+
*.webp
|
| 28 |
|
| 29 |
!__assets__/lr_inputs/*
|
| 30 |
!__assets__/*
|
__assets__/lr_inputs/naruto.jpg
ADDED
|
app.py
CHANGED
|
@@ -54,11 +54,12 @@ def inference(img_path, model_name):
|
|
| 54 |
|
| 55 |
|
| 56 |
# In default, we will automatically use crop to match 4x size
|
| 57 |
-
super_resolved_img = super_resolve_img(generator, img_path, output_path=None, weight_dtype=weight_dtype, crop_for_4x=True)
|
| 58 |
store_name = str(time.time()) + ".png"
|
| 59 |
save_image(super_resolved_img, store_name)
|
| 60 |
outputs = cv2.imread(store_name)
|
| 61 |
outputs = cv2.cvtColor(outputs, cv2.COLOR_RGB2BGR)
|
|
|
|
| 62 |
|
| 63 |
return outputs
|
| 64 |
|
|
@@ -78,6 +79,8 @@ if __name__ == '__main__':
|
|
| 78 |
|
| 79 |
APISR aims at restoring and enhancing low-quality low-resolution anime images and video sources with various degradations from real-world scenarios.
|
| 80 |
|
|
|
|
|
|
|
| 81 |
If APISR is helpful, please help star the GitHub Repo. Thanks!
|
| 82 |
"""
|
| 83 |
|
|
@@ -112,6 +115,7 @@ if __name__ == '__main__':
|
|
| 112 |
["__assets__/lr_inputs/image-00440.png"],
|
| 113 |
["__assets__/lr_inputs/image-00164.jpg"],
|
| 114 |
["__assets__/lr_inputs/img_eva.jpeg"],
|
|
|
|
| 115 |
],
|
| 116 |
[input_image],
|
| 117 |
)
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
# In default, we will automatically use crop to match 4x size
|
| 57 |
+
super_resolved_img = super_resolve_img(generator, img_path, output_path=None, weight_dtype=weight_dtype, downsample_threshold=720, crop_for_4x=True)
|
| 58 |
store_name = str(time.time()) + ".png"
|
| 59 |
save_image(super_resolved_img, store_name)
|
| 60 |
outputs = cv2.imread(store_name)
|
| 61 |
outputs = cv2.cvtColor(outputs, cv2.COLOR_RGB2BGR)
|
| 62 |
+
os.remove(store_name)
|
| 63 |
|
| 64 |
return outputs
|
| 65 |
|
|
|
|
| 79 |
|
| 80 |
APISR aims at restoring and enhancing low-quality low-resolution anime images and video sources with various degradations from real-world scenarios.
|
| 81 |
|
| 82 |
+
### Note: Due to memory restriction, all images whose short side is over 720 pixel will be downsampled to 720 pixel with the same aspect ratio. E.g., 1920x1080 -> 1280x720
|
| 83 |
+
|
| 84 |
If APISR is helpful, please help star the GitHub Repo. Thanks!
|
| 85 |
"""
|
| 86 |
|
|
|
|
| 115 |
["__assets__/lr_inputs/image-00440.png"],
|
| 116 |
["__assets__/lr_inputs/image-00164.jpg"],
|
| 117 |
["__assets__/lr_inputs/img_eva.jpeg"],
|
| 118 |
+
["__assets__/lr_inputs/naruto.jpg"],
|
| 119 |
],
|
| 120 |
[input_image],
|
| 121 |
)
|
test_code/inference.py
CHANGED
|
@@ -19,19 +19,30 @@ from test_code.test_utils import load_grl, load_rrdb, load_cunet
|
|
| 19 |
|
| 20 |
|
| 21 |
@torch.no_grad # You must add these time, else it will have Out of Memory
|
| 22 |
-
def super_resolve_img(generator, input_path, output_path=None, weight_dtype=torch.float32, crop_for_4x=True):
|
| 23 |
''' Super Resolve a low resolution image
|
| 24 |
Args:
|
| 25 |
generator (torch): the generator class that is already loaded
|
| 26 |
input_path (str): the path to the input lr images
|
| 27 |
output_path (str): the directory to store the generated images
|
| 28 |
weight_dtype (bool): the weight type (float32/float16)
|
|
|
|
| 29 |
crop_for_4x (bool): whether we crop the lr images to match 4x scale (needed for some situation)
|
| 30 |
'''
|
| 31 |
print("Processing image {}".format(input_path))
|
| 32 |
|
| 33 |
# Read the image and do preprocess
|
| 34 |
img_lr = cv2.imread(input_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Crop if needed
|
| 36 |
if crop_for_4x:
|
| 37 |
h, w, _ = img_lr.shape
|
|
@@ -139,12 +150,4 @@ if __name__ == "__main__":
|
|
| 139 |
# In default, we will automatically use crop to match 4x size
|
| 140 |
super_resolve_img(generator, input_dir, output_path, weight_dtype, crop_for_4x=True)
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
@torch.no_grad # You must add these time, else it will have Out of Memory
|
| 22 |
+
def super_resolve_img(generator, input_path, output_path=None, weight_dtype=torch.float32, downsample_threshold=720, crop_for_4x=True):
|
| 23 |
''' Super Resolve a low resolution image
|
| 24 |
Args:
|
| 25 |
generator (torch): the generator class that is already loaded
|
| 26 |
input_path (str): the path to the input lr images
|
| 27 |
output_path (str): the directory to store the generated images
|
| 28 |
weight_dtype (bool): the weight type (float32/float16)
|
| 29 |
+
downsample_threshold (int): the threshold of height/width (short side) to downsample the input
|
| 30 |
crop_for_4x (bool): whether we crop the lr images to match 4x scale (needed for some situation)
|
| 31 |
'''
|
| 32 |
print("Processing image {}".format(input_path))
|
| 33 |
|
| 34 |
# Read the image and do preprocess
|
| 35 |
img_lr = cv2.imread(input_path)
|
| 36 |
+
h, w, c = img_lr.shape
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Downsample if needed
|
| 40 |
+
short_side = min(h, w)
|
| 41 |
+
if downsample_threshold != -1 and short_side > downsample_threshold:
|
| 42 |
+
resize_ratio = short_side / downsample_threshold
|
| 43 |
+
img_lr = cv2.resize(img_lr, (int(w/resize_ratio), int(h/resize_ratio)), interpolation = cv2.INTER_LINEAR)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
# Crop if needed
|
| 47 |
if crop_for_4x:
|
| 48 |
h, w, _ = img_lr.shape
|
|
|
|
| 150 |
# In default, we will automatically use crop to match 4x size
|
| 151 |
super_resolve_img(generator, input_dir, output_path, weight_dtype, crop_for_4x=True)
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|