Repository navigation
Expand file tree
/
Copy pathnoise_da_processing.py
More file actions
308 lines (241 loc) · 11.6 KB
/
Copy pathnoise_da_processing.py
File metadata and controls
308 lines (241 loc) · 11.6 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
"""
Noise-DA Model Integration for ComfyUI
Based on the official implementation from:
"Denoising as Adaptation: Noise-Space Domain Adaptation for Image Restoration"
Paper: https://arxiv.org/abs/2406.18516
Code: https://github.com/KangLiao929/Noise-DA
Original Authors:
- Kang Liao (kang.liao@ntu.edu.sg)
- Zongsheng Yue
- Zhouxia Wang
- Chen Change Loy
S-Lab, Nanyang Technological University
Citation:
@article{liao2024denoising,
title={Denoising as Adaptation: Noise-Space Domain Adaptation for Image Restoration},
author={Liao, Kang and Yue, Zongsheng and Wang, Zhouxia and Loy, Chen Change},
journal={arXiv preprint arXiv:2406.18516},
year={2024}
}
This ComfyUI integration uses the RestorationNet architecture from the pretrained models.
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from pathlib import Path
from typing import Tuple, Dict, Optional
def conv3x3(in_chn, out_chn, bias=True):
"""3x3 convolution with padding"""
layer = nn.Conv2d(in_chn, out_chn, kernel_size=3, stride=1, padding=1, bias=bias)
return layer
class ConvBlock(nn.Module):
"""Basic convolution block with LeakyReLU"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, slope=0.2):
super().__init__()
self.block = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, bias=True),
nn.LeakyReLU(slope, inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size, stride, padding, bias=True),
nn.LeakyReLU(slope, inplace=True)
)
def forward(self, x):
return self.block(x)
class UpConvBlock(nn.Module):
"""Upsampling block with convolution"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, slope=0.2):
super().__init__()
self.conv_up = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, bias=True)
self.conv_block = ConvBlock(in_channels, out_channels, kernel_size, stride, padding, slope)
def forward(self, x, skip=None):
# Upsample
x_up = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
x_up = self.conv_up(x_up)
# Concatenate with skip connection if provided
if skip is not None:
# Crop skip connection to match upsampled size
if skip.shape[2:] != x_up.shape[2:]:
skip = self._center_crop(skip, x_up.shape[2:])
x_up = torch.cat([x_up, skip], dim=1)
# Apply convolution block
return self.conv_block(x_up)
def _center_crop(self, tensor, target_size):
"""Center crop tensor to target size"""
_, _, h, w = tensor.size()
target_h, target_w = target_size
start_h = (h - target_h) // 2
start_w = (w - target_w) // 2
return tensor[:, :, start_h:start_h + target_h, start_w:start_w + target_w]
class RestorationNet(nn.Module):
"""
RestorationNet that matches the exact structure from Noise-DA models
"""
def __init__(self, in_channels=3, out_channels=3, depth=4, wf=64, slope=0.2):
super().__init__()
self.depth = depth
# Downsampling path
self.down_path = nn.ModuleList()
prev_channels = in_channels
for i in range(depth):
self.down_path.append(ConvBlock(prev_channels, (2**i) * wf, slope=slope))
prev_channels = (2**i) * wf
# Upsampling path
self.up_path = nn.ModuleList()
for i in reversed(range(depth - 1)):
self.up_path.append(UpConvBlock(prev_channels, (2**i) * wf, slope=slope))
prev_channels = (2**i) * wf
# Final output layer
self.last = nn.Conv2d(prev_channels, out_channels, 3, padding=1, bias=True)
def forward(self, x):
# Store skip connections
blocks = []
# Downsampling
for i, down in enumerate(self.down_path):
x = down(x)
if i != len(self.down_path) - 1:
blocks.append(x)
x = F.avg_pool2d(x, 2)
# Upsampling
for i, up in enumerate(self.up_path):
x = up(x, blocks[-i-1])
# Final output
return self.last(x)
class NoiseDAProcessor:
"""Processor for Noise-DA models with proper RestorationNet loading"""
def __init__(self, model_type: str = 'denoise', device: str = 'cuda'):
self.model_type = model_type
self.device = device if torch.cuda.is_available() else 'cpu'
self.model = None
self._load_model()
def _load_model(self):
"""Load the appropriate pretrained model"""
model_path = Path(__file__).parent.parent / 'models' / f'noise_da_{self.model_type}.pth'
if not model_path.exists():
raise FileNotFoundError(f"Model file not found: {model_path}")
# Initialize model with correct architecture
self.model = RestorationNet(in_channels=3, out_channels=3, depth=4, wf=64)
# Load pretrained weights
checkpoint = torch.load(model_path, map_location=self.device)
# Extract RestorationNet weights
restoration_weights = {}
for key, value in checkpoint.items():
if key.startswith('RestorationNet.'):
# Remove the 'RestorationNet.' prefix
clean_key = key[14:]
# Remove leading dot if present
if clean_key.startswith('.'):
clean_key = clean_key[1:]
restoration_weights[clean_key] = value
# Load the weights
missing_keys, unexpected_keys = self.model.load_state_dict(restoration_weights, strict=False)
if len(missing_keys) == 0 and len(unexpected_keys) == 0:
print(f"✅ Successfully loaded all RestorationNet weights for {self.model_type}")
else:
print(f"⚠️ Partially loaded RestorationNet weights for {self.model_type}")
if missing_keys:
print(f" Missing keys: {missing_keys[:5]}...") # Show first 5
if unexpected_keys:
print(f" Unexpected keys: {unexpected_keys[:5]}...") # Show first 5
self.model.to(self.device)
self.model.eval()
def process_image(self, image: np.ndarray, residual_mode: bool = True, residual_scale_factor: float = 0.5) -> np.ndarray:
"""
Process image with Noise-DA model
Args:
image: Input image as numpy array (H, W, C) in [0, 255] range
residual_mode: Whether to treat model output as residual (True) or direct output (False)
residual_scale_factor: Multiplier for residual scaling
"""
if self.model is None:
raise ValueError("Model not loaded")
# Store original image
original_image = image.copy()
# Convert to tensor with proper size handling
input_tensor, original_size = self._preprocess(image)
# Process with model
with torch.no_grad():
model_output = self.model(input_tensor)
# Convert model output back to numpy (keep in normalized space)
# The RestorationNet outputs RESIDUALS, not clean images
model_residuals = self._postprocess_normalized(model_output, original_size)
# Normalize original image to [0, 1] space
if original_image.dtype == np.uint8:
original_norm = original_image.astype(np.float32) / 255.0
else:
original_norm = original_image.astype(np.float32)
if residual_mode:
# Apply residuals with scaling factor
# Residuals are typically small corrections that should be added to the input
result_norm = original_norm + model_residuals * residual_scale_factor
result_norm = np.clip(result_norm, 0, 1)
else:
# Direct output mode - still apply residuals but without scaling
result_norm = original_norm + model_residuals
result_norm = np.clip(result_norm, 0, 1)
# Convert back to [0, 255] range
result = (result_norm * 255).astype(np.uint8)
return result
def _preprocess(self, image: np.ndarray) -> Tuple[torch.Tensor, Tuple[int, int]]:
"""Preprocess image for RestorationNet input"""
# Store original dimensions
original_height, original_width = image.shape[:2]
# Convert to float and normalize to [0, 1]
if image.dtype == np.uint8:
image = image.astype(np.float32) / 255.0
elif image.dtype == np.uint16:
image = image.astype(np.float32) / 65535.0
# RestorationNet uses depth=4, so we need padding for 2^4 = 16
pad_factor = 16
# Calculate padding needed
pad_h = (pad_factor - (original_height % pad_factor)) % pad_factor
pad_w = (pad_factor - (original_width % pad_factor)) % pad_factor
# Pad the image if needed
if pad_h > 0 or pad_w > 0:
# Use reflection padding to avoid edge artifacts
image = np.pad(image, ((0, pad_h), (0, pad_w), (0, 0)), mode='reflect')
# Convert to tensor format (C, H, W)
image = torch.from_numpy(image.transpose(2, 0, 1)).float()
# Add batch dimension
image = image.unsqueeze(0)
return image.to(self.device), (original_height, original_width)
def _postprocess_normalized(self, tensor: torch.Tensor, original_size: Tuple[int, int]) -> np.ndarray:
"""Convert model output back to normalized image [0, 1] space"""
# Remove batch dimension and convert to numpy
image = tensor.squeeze(0).cpu().detach().numpy()
# Convert from (C, H, W) to (H, W, C)
image = image.transpose(1, 2, 0)
# Crop back to original size
original_height, original_width = original_size
image = image[:original_height, :original_width, :]
# Keep in [0, 1] space, clamp to valid range
image = np.clip(image, 0, 1)
return image.astype(np.float32)
def _postprocess(self, tensor: torch.Tensor, original_size: Tuple[int, int]) -> np.ndarray:
"""Convert model output back to image"""
# Remove batch dimension and convert to numpy
image = tensor.squeeze(0).cpu().detach().numpy()
# Convert from (C, H, W) to (H, W, C)
image = image.transpose(1, 2, 0)
# Crop back to original size
original_height, original_width = original_size
image = image[:original_height, :original_width, :]
# Convert to [0, 255] and uint8
image = np.clip(image * 255, 0, 255).astype(np.uint8)
return image
# Test function for standalone usage
def test_noise_da_processor():
"""Test the NoiseDAProcessor independently"""
import cv2
# Create a simple test image
test_image = np.random.rand(256, 256, 3) * 255
test_image = test_image.astype(np.uint8)
# Test all models
for model_type in ['denoise', 'deblur', 'derain']:
try:
processor = NoiseDAProcessor(model_type)
result = processor.process_image(test_image)
print(f"{model_type} model: SUCCESS - Output shape: {result.shape}")
except Exception as e:
print(f"{model_type} model: FAILED - {e}")
if __name__ == "__main__":
test_noise_da_processor()