-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathImageInterleavedUpscalerV2.py
266 lines (226 loc) · 9.75 KB
/
ImageInterleavedUpscalerV2.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
import torch
import torch.nn.functional as F
from typing import Tuple
from comfy import model_management
import comfy.utils
class ImageInterleavedUpscalerV2:
"""
ComfyUI custom node for GAN-enhanced image upscaling with interlacing.
Combines interlaced processing with external upscaling models.
"""
def __init__(self):
self.device = model_management.get_torch_device()
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"upscale_model": ("UPSCALE_MODEL",),
"field_order": (["top_first", "bottom_first"], {
"default": "top_first"
}),
"blend_factor": ("FLOAT", {
"default": 0.25,
"min": 0.0,
"max": 1.0,
"step": 0.05,
"display": "slider"
}),
"field_strength": ("FLOAT", {
"default": 1.0,
"min": 0.0,
"max": 2.0,
"step": 0.1,
"display": "slider"
}),
},
"optional": {
"tile_size": ("INT", {
"default": 512,
"min": 128,
"max": 1024,
"step": 64,
}),
"tile_overlap": ("INT", {
"default": 32,
"min": 16,
"max": 256,
"step": 16,
}),
"edge_enhancement": ("FLOAT", {
"default": 0.0,
"min": 0.0,
"max": 1.0,
"step": 0.1,
}),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "upscale_interleaved"
CATEGORY = "image/upscaling"
def apply_edge_enhancement(self, frame: torch.Tensor, strength: float) -> torch.Tensor:
"""Apply edge enhancement using Sobel operator"""
if strength == 0:
return frame
sobel_x = torch.tensor([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], device=self.device).float()
sobel_y = torch.tensor([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], device=self.device).float()
frame_gray = frame.mean(dim=-1, keepdim=True)
frame_gray = frame_gray.permute(2, 0, 1).unsqueeze(0)
edges_x = F.conv2d(frame_gray, sobel_x.view(1, 1, 3, 3), padding=1)
edges_y = F.conv2d(frame_gray, sobel_y.view(1, 1, 3, 3), padding=1)
edges = torch.sqrt(edges_x.pow(2) + edges_y.pow(2))
edges = edges / edges.max() * strength
enhanced = frame + edges.squeeze(0).permute(1, 2, 0).repeat(1, 1, frame.shape[-1])
return torch.clamp(enhanced, 0, 1)
def process_image_with_model(self,
image: torch.Tensor,
upscale_model,
tile_size: int,
tile_overlap: int) -> torch.Tensor:
"""Process a single image using the upscale model with tiling"""
device = self.device
# Calculate memory requirements
memory_required = model_management.module_size(upscale_model.model)
memory_required += (tile_size * tile_size * 3) * image.element_size() * max(upscale_model.scale, 1.0) * 384.0
memory_required += image.nelement() * image.element_size()
model_management.free_memory(memory_required, device)
# Ensure image has correct shape and type
if len(image.shape) == 2: # HW
# Add channel dimension for grayscale
image = image.unsqueeze(-1).repeat(1, 1, 3)
elif len(image.shape) == 3 and image.shape[-1] == 1: # HW1
# Expand single channel to RGB
image = image.repeat(1, 1, 3)
# Add batch dimension if needed
if len(image.shape) == 3: # HWC
image = image.unsqueeze(0) # Add batch dimension
# Move channels to proper position for processing
image = image.movedim(-1, -3) # HWC to CHW
image = image.float().to(device)
# Process with tiling
try:
steps = image.shape[0] * comfy.utils.get_tiled_scale_steps(
image.shape[3], image.shape[2],
tile_x=tile_size, tile_y=tile_size,
overlap=tile_overlap
)
pbar = comfy.utils.ProgressBar(steps)
upscaled = comfy.utils.tiled_scale(
image,
lambda a: upscale_model(a),
tile_x=tile_size,
tile_y=tile_size,
overlap=tile_overlap,
upscale_amount=upscale_model.scale,
pbar=pbar
)
except model_management.OOM_EXCEPTION as e:
if tile_size < 128:
raise e
return self.process_image_with_model(
image, upscale_model,
tile_size // 2,
tile_overlap
)
# Convert back to HWC format
upscaled = upscaled.movedim(-3, -1) # CHW to HWC
if upscaled.shape[0] == 1: # Remove batch dimension if present
upscaled = upscaled.squeeze(0)
return upscaled
def create_interlaced_image(self,
image: torch.Tensor,
upscale_model,
field_order: str,
blend_factor: float,
field_strength: float,
tile_size: int,
tile_overlap: int) -> torch.Tensor:
"""Create an interlaced image using the upscale model"""
# Ensure image is on the correct device
image = image.to(self.device)
# First do the full image upscale
upscaled = self.process_image_with_model(
image, upscale_model,
tile_size, tile_overlap
).to(self.device)
# Now apply interlacing to the upscaled image
height = upscaled.shape[0]
even_mask = torch.zeros((height, 1, 1), device=self.device)
odd_mask = torch.zeros((height, 1, 1), device=self.device)
if field_order == "top_first":
even_mask[::2] = field_strength
odd_mask[1::2] = field_strength
else:
even_mask[1::2] = field_strength
odd_mask[::2] = field_strength
# Create shifted versions for field blending
shifted_up = torch.roll(upscaled, shifts=-1, dims=0)
shifted_down = torch.roll(upscaled, shifts=1, dims=0)
# Apply field masks and blend
even_field = upscaled * even_mask
odd_field = upscaled * odd_mask
# Blend with shifted fields for smoother transitions
even_field = even_field * (1 - blend_factor) + shifted_down * even_mask * blend_factor
odd_field = odd_field * (1 - blend_factor) + shifted_up * odd_mask * blend_factor
# Combine fields
interlaced = even_field + odd_field
return interlaced
def upscale_interleaved(self,
image: torch.Tensor,
upscale_model,
field_order: str,
blend_factor: float,
field_strength: float,
tile_size: int = 512,
tile_overlap: int = 32,
edge_enhancement: float = 0.0) -> Tuple[torch.Tensor]:
"""
Main processing function that combines upscaling with interlacing.
Ensures output is in BHWC format for compatibility with video pipeline.
"""
try:
# Move model to appropriate device
upscale_model.to(self.device)
# Ensure input is at least 3D (HWC)
if len(image.shape) == 2: # HW
image = image.unsqueeze(-1).repeat(1, 1, 3)
elif len(image.shape) == 3 and image.shape[-1] == 1: # HW1
image = image.repeat(1, 1, 3)
# Add batch dimension if needed
if len(image.shape) == 3: # HWC
image = image.unsqueeze(0) # BHWC
# Process image
processed = self.create_interlaced_image(
image,
upscale_model,
field_order,
blend_factor,
field_strength,
tile_size,
tile_overlap
)
# Apply edge enhancement if requested
if edge_enhancement > 0:
processed = self.apply_edge_enhancement(processed, edge_enhancement)
# Ensure output has batch dimension (BHWC)
if len(processed.shape) == 3: # HWC
processed = processed.unsqueeze(0)
# Cleanup
upscale_model.to("cpu")
torch.cuda.empty_cache()
result = torch.clamp(processed, 0, 1)
# Double-check dimensions
if len(result.shape) != 4:
raise ValueError(f"Expected 4D tensor output (BHWC), got shape {result.shape}")
return (result,)
except Exception as e:
# Ensure model is moved back to CPU on error
upscale_model.to("cpu")
torch.cuda.empty_cache()
raise e
NODE_CLASS_MAPPINGS = {
"ImageInterleavedUpscalerV2": ImageInterleavedUpscalerV2
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ImageInterleavedUpscalerV2": "Image Interleaved Upscaler V2"
}