-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathApisr.py
155 lines (115 loc) · 4.58 KB
/
Apisr.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
import torch
import os
import folder_paths
import numpy as np
from PIL import Image
from .test_code.test_utils import load_grl, load_rrdb, load_cunet
device = "cuda" if torch.cuda.is_available() else "cpu"
folder_paths.folder_names_and_paths["apisr"] = ([os.path.join(folder_paths.models_dir, "apisr")], folder_paths.supported_pt_extensions)
def tensor2pil(image):
return Image.fromarray(np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))
def pil2tensor(image):
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
class APISR_ModelLoader_Zho:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"apisr_model": (folder_paths.get_filename_list("apisr"), ),
}
}
RETURN_TYPES = ("APISRMODEL",)
RETURN_NAMES = ("pipe",)
FUNCTION = "load_model"
CATEGORY = "🔎APISR"
def load_model(self, apisr_model):
if not apisr_model:
raise ValueError("Please provide the apisr_model parameter with the name of the model file.")
apisr_path = folder_paths.get_full_path("apisr", apisr_model)
if apisr_model == "4x_APISR_GRL_GAN_generator.pth":
generator = load_grl(apisr_path, scale=4)
elif apisr_model == "2x_APISR_RRDB_GAN_generator.pth":
generator = load_rrdb(apisr_path, scale=2)
else:
raise gr.Error(error)
return [generator]
class APISR_Zho:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"pipe": ("APISRMODEL",),
"image": ("IMAGE",),
"crop_for_4x": ("BOOLEAN", {"default": True}),
"dtype": (["float32", "float16"], ),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "sr_image"
CATEGORY = "🔎APISR"
def sr_image(self, pipe, image, crop_for_4x, dtype):
if dtype == "float32":
weight_dtype = torch.float32
elif dtype == "float16":
weight_dtype = torch.float16
pipe = pipe.to(device=device, dtype=weight_dtype)
img_tensor = image.permute(0, 3, 1, 2).to(device=device, dtype=weight_dtype)
if crop_for_4x:
_, _, h, w = img_tensor.shape
if h % 4 != 0:
img_tensor = img_tensor[:, :, :4 * (h // 4), :]
if w % 4 != 0:
img_tensor = img_tensor[:, :, :, :4 * (w // 4)]
super_resolved_img = pipe(img_tensor)
super_resolved_img_nhwc = super_resolved_img.permute(0, 2, 3, 1).cpu()
return (super_resolved_img_nhwc,)
class APISR_Lterative_Zho:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"pipe": ("APISRMODEL",),
"image": ("IMAGE",),
"crop_for_4x": ("BOOLEAN", {"default": True}),
"dtype": (["float32", "float16"], ),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "sr_image"
CATEGORY = "🔎APISR"
def sr_image(self, pipe, image, crop_for_4x, dtype):
if dtype == "float32":
weight_dtype = torch.float32
elif dtype == "float16":
weight_dtype = torch.float16
pipe = pipe.to(device=device, dtype=weight_dtype)
processed_images = []
for img_tensor in image:
img_tensor = img_tensor.to(device=device, dtype=weight_dtype).unsqueeze(0).permute(0, 3, 1, 2)
if crop_for_4x:
_, _, h, w = img_tensor.shape
if h % 4 != 0:
img_tensor = img_tensor[:, :, :4 * (h // 4), :]
if w % 4 != 0:
img_tensor = img_tensor[:, :, :, :4 * (w // 4)]
with torch.no_grad(): # 确保在推理时不计算梯度,节省内存
super_resolved_img = pipe(img_tensor)
super_resolved_img_nhwc = super_resolved_img.permute(0, 2, 3, 1).squeeze(0).cpu()
processed_images.append(super_resolved_img_nhwc)
return (processed_images,)
NODE_CLASS_MAPPINGS = {
"APISR_ModelLoader_Zho": APISR_ModelLoader_Zho,
"APISR_Zho": APISR_Zho,
"APISR_Lterative_Zho": APISR_Lterative_Zho
}
NODE_DISPLAY_NAME_MAPPINGS = {
"APISR_ModelLoader_Zho": "🔎APISR ModelLoader",
"APISR_Zho": "🔎APISR",
"APISR_Lterative_Zho": "🔎APISR Lterative",
}