Spaces:
Running
Running
Commit Β·
80867d6
1
Parent(s): e75f322
fix(enhance): load FFA-Net and Zero-DCE++ weights correctly
Browse filesFFA-Net: the .pk file is a torch.save training checkpoint, not a pickled
module. pickle.load returned the legacy magic number (an int), so startup
failed with "'int' object has no attribute 'eval'". Build the official
FFA-Net (gps=3, blocks=19, two post convs) and load ckpt['model'].
Zero-DCE++: the app defined the original Zero-DCE network, whose layer
names do not match the Zero-DCE++ checkpoint. strict=False loaded no
weights, so low-light enhancement ran with random weights. Use the
official depthwise-separable architecture and curve formula.
Both loaders now use strict=True and fall back to CLAHE on mismatch.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -180,24 +180,55 @@ def _pull_from_hub():
|
|
| 180 |
|
| 181 |
# ββ enhancement loaders βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
def _load_zero_dce(weights_path: str):
|
| 184 |
-
"""Load Zero-DCE++ for low-light
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
try:
|
| 186 |
import torch
|
| 187 |
import torch.nn as nn
|
| 188 |
|
| 189 |
-
class
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
def __init__(self):
|
| 191 |
super().__init__()
|
| 192 |
-
self.relu = nn.ReLU(inplace=True)
|
| 193 |
n = 32
|
| 194 |
-
self.
|
| 195 |
-
self.
|
| 196 |
-
self.
|
| 197 |
-
self.
|
| 198 |
-
self.
|
| 199 |
-
self.
|
| 200 |
-
self.
|
|
|
|
| 201 |
|
| 202 |
def forward(self, x):
|
| 203 |
x1 = self.relu(self.e_conv1(x))
|
|
@@ -207,16 +238,14 @@ def _load_zero_dce(weights_path: str):
|
|
| 207 |
x5 = self.relu(self.e_conv5(torch.cat([x3, x4], 1)))
|
| 208 |
x6 = self.relu(self.e_conv6(torch.cat([x2, x5], 1)))
|
| 209 |
x_r = torch.tanh(self.e_conv7(torch.cat([x1, x6], 1)))
|
| 210 |
-
|
| 211 |
out = x
|
| 212 |
-
for
|
| 213 |
-
out = out +
|
| 214 |
-
return out
|
| 215 |
-
|
| 216 |
-
net =
|
| 217 |
-
|
| 218 |
-
state = ckpt.get("state_dict", ckpt)
|
| 219 |
-
net.load_state_dict(state, strict=False)
|
| 220 |
net.eval()
|
| 221 |
print(f"[startup] Zero-DCE++ loaded: {weights_path}")
|
| 222 |
return net
|
|
@@ -226,12 +255,94 @@ def _load_zero_dce(weights_path: str):
|
|
| 226 |
|
| 227 |
|
| 228 |
def _load_ffa(weights_path: str):
|
| 229 |
-
"""Load FFA-Net for dehazing.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
try:
|
| 231 |
import torch
|
| 232 |
-
import
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
net.eval()
|
| 236 |
print(f"[startup] FFA-Net loaded: {weights_path}")
|
| 237 |
return net
|
|
|
|
| 180 |
|
| 181 |
# ββ enhancement loaders βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 182 |
|
| 183 |
+
def _load_state_dict(weights_path: str) -> dict:
|
| 184 |
+
"""Read a checkpoint and return a clean state dict.
|
| 185 |
+
|
| 186 |
+
Handles raw state dicts and training checkpoints ({'model': ...}), and
|
| 187 |
+
strips the 'module.' prefix that nn.DataParallel adds.
|
| 188 |
+
"""
|
| 189 |
+
import torch
|
| 190 |
+
ckpt = torch.load(weights_path, map_location="cpu", weights_only=False)
|
| 191 |
+
if isinstance(ckpt, dict):
|
| 192 |
+
for key in ("model", "state_dict"):
|
| 193 |
+
if key in ckpt and isinstance(ckpt[key], dict):
|
| 194 |
+
ckpt = ckpt[key]
|
| 195 |
+
break
|
| 196 |
+
return {k[len("module."):] if k.startswith("module.") else k: v
|
| 197 |
+
for k, v in ckpt.items()}
|
| 198 |
+
|
| 199 |
+
|
| 200 |
def _load_zero_dce(weights_path: str):
|
| 201 |
+
"""Load Zero-DCE++ (official Zero-DCE_extension architecture) for low-light.
|
| 202 |
+
|
| 203 |
+
Returns a module that maps an RGB tensor in [0,1] to an enhanced tensor,
|
| 204 |
+
or None (CLAHE fallback) if the weights do not match the architecture.
|
| 205 |
+
"""
|
| 206 |
try:
|
| 207 |
import torch
|
| 208 |
import torch.nn as nn
|
| 209 |
|
| 210 |
+
class _CSDN(nn.Module):
|
| 211 |
+
"""Depthwise-separable conv (CSDN_Tem in the official repo)."""
|
| 212 |
+
def __init__(self, in_ch, out_ch):
|
| 213 |
+
super().__init__()
|
| 214 |
+
self.depth_conv = nn.Conv2d(in_ch, in_ch, 3, 1, 1, groups=in_ch)
|
| 215 |
+
self.point_conv = nn.Conv2d(in_ch, out_ch, 1, 1, 0)
|
| 216 |
+
|
| 217 |
+
def forward(self, x):
|
| 218 |
+
return self.point_conv(self.depth_conv(x))
|
| 219 |
+
|
| 220 |
+
class _ZeroDCEpp(nn.Module):
|
| 221 |
def __init__(self):
|
| 222 |
super().__init__()
|
|
|
|
| 223 |
n = 32
|
| 224 |
+
self.relu = nn.ReLU(inplace=True)
|
| 225 |
+
self.e_conv1 = _CSDN(3, n)
|
| 226 |
+
self.e_conv2 = _CSDN(n, n)
|
| 227 |
+
self.e_conv3 = _CSDN(n, n)
|
| 228 |
+
self.e_conv4 = _CSDN(n, n)
|
| 229 |
+
self.e_conv5 = _CSDN(n * 2, n)
|
| 230 |
+
self.e_conv6 = _CSDN(n * 2, n)
|
| 231 |
+
self.e_conv7 = _CSDN(n * 2, 3)
|
| 232 |
|
| 233 |
def forward(self, x):
|
| 234 |
x1 = self.relu(self.e_conv1(x))
|
|
|
|
| 238 |
x5 = self.relu(self.e_conv5(torch.cat([x3, x4], 1)))
|
| 239 |
x6 = self.relu(self.e_conv6(torch.cat([x2, x5], 1)))
|
| 240 |
x_r = torch.tanh(self.e_conv7(torch.cat([x1, x6], 1)))
|
| 241 |
+
# Zero-DCE++ applies one shared curve map 8 times: LE(x) = x + a(x^2 - x)
|
| 242 |
out = x
|
| 243 |
+
for _ in range(8):
|
| 244 |
+
out = out + x_r * (torch.pow(out, 2) - out)
|
| 245 |
+
return out.clamp(0, 1)
|
| 246 |
+
|
| 247 |
+
net = _ZeroDCEpp()
|
| 248 |
+
net.load_state_dict(_load_state_dict(weights_path), strict=True)
|
|
|
|
|
|
|
| 249 |
net.eval()
|
| 250 |
print(f"[startup] Zero-DCE++ loaded: {weights_path}")
|
| 251 |
return net
|
|
|
|
| 255 |
|
| 256 |
|
| 257 |
def _load_ffa(weights_path: str):
|
| 258 |
+
"""Load FFA-Net (gps=3, blocks=19, official architecture) for dehazing.
|
| 259 |
+
|
| 260 |
+
The .pk file is a torch.save() training checkpoint, not a pickled module,
|
| 261 |
+
so it must be read with torch.load and applied to a freshly built network.
|
| 262 |
+
"""
|
| 263 |
try:
|
| 264 |
import torch
|
| 265 |
+
import torch.nn as nn
|
| 266 |
+
|
| 267 |
+
def conv(in_c, out_c, k, bias=True):
|
| 268 |
+
return nn.Conv2d(in_c, out_c, k, padding=k // 2, bias=bias)
|
| 269 |
+
|
| 270 |
+
class _PALayer(nn.Module):
|
| 271 |
+
def __init__(self, c):
|
| 272 |
+
super().__init__()
|
| 273 |
+
self.pa = nn.Sequential(
|
| 274 |
+
nn.Conv2d(c, c // 8, 1, bias=True), nn.ReLU(inplace=True),
|
| 275 |
+
nn.Conv2d(c // 8, 1, 1, bias=True), nn.Sigmoid())
|
| 276 |
+
|
| 277 |
+
def forward(self, x):
|
| 278 |
+
return x * self.pa(x)
|
| 279 |
+
|
| 280 |
+
class _CALayer(nn.Module):
|
| 281 |
+
def __init__(self, c):
|
| 282 |
+
super().__init__()
|
| 283 |
+
self.avg_pool = nn.AdaptiveAvgPool2d(1)
|
| 284 |
+
self.ca = nn.Sequential(
|
| 285 |
+
nn.Conv2d(c, c // 8, 1, bias=True), nn.ReLU(inplace=True),
|
| 286 |
+
nn.Conv2d(c // 8, c, 1, bias=True), nn.Sigmoid())
|
| 287 |
+
|
| 288 |
+
def forward(self, x):
|
| 289 |
+
return x * self.ca(self.avg_pool(x))
|
| 290 |
+
|
| 291 |
+
class _Block(nn.Module):
|
| 292 |
+
def __init__(self, dim, k):
|
| 293 |
+
super().__init__()
|
| 294 |
+
self.conv1 = conv(dim, dim, k)
|
| 295 |
+
self.act1 = nn.ReLU(inplace=True)
|
| 296 |
+
self.conv2 = conv(dim, dim, k)
|
| 297 |
+
self.calayer = _CALayer(dim)
|
| 298 |
+
self.palayer = _PALayer(dim)
|
| 299 |
+
|
| 300 |
+
def forward(self, x):
|
| 301 |
+
res = self.act1(self.conv1(x)) + x
|
| 302 |
+
res = self.palayer(self.calayer(self.conv2(res)))
|
| 303 |
+
return res + x
|
| 304 |
+
|
| 305 |
+
class _Group(nn.Module):
|
| 306 |
+
def __init__(self, dim, k, blocks):
|
| 307 |
+
super().__init__()
|
| 308 |
+
mods = [_Block(dim, k) for _ in range(blocks)]
|
| 309 |
+
mods.append(conv(dim, dim, k))
|
| 310 |
+
self.gp = nn.Sequential(*mods)
|
| 311 |
+
|
| 312 |
+
def forward(self, x):
|
| 313 |
+
return self.gp(x) + x
|
| 314 |
+
|
| 315 |
+
class _FFANet(nn.Module):
|
| 316 |
+
def __init__(self, gps=3, blocks=19):
|
| 317 |
+
super().__init__()
|
| 318 |
+
self.gps, self.dim, k = gps, 64, 3
|
| 319 |
+
self.g1 = _Group(self.dim, k, blocks)
|
| 320 |
+
self.g2 = _Group(self.dim, k, blocks)
|
| 321 |
+
self.g3 = _Group(self.dim, k, blocks)
|
| 322 |
+
self.ca = nn.Sequential(
|
| 323 |
+
nn.AdaptiveAvgPool2d(1),
|
| 324 |
+
nn.Conv2d(self.dim * gps, self.dim // 16, 1, bias=True),
|
| 325 |
+
nn.ReLU(inplace=True),
|
| 326 |
+
nn.Conv2d(self.dim // 16, self.dim * gps, 1, bias=True),
|
| 327 |
+
nn.Sigmoid())
|
| 328 |
+
self.palayer = _PALayer(self.dim)
|
| 329 |
+
self.pre = nn.Sequential(conv(3, self.dim, k))
|
| 330 |
+
self.post = nn.Sequential(conv(self.dim, self.dim, k),
|
| 331 |
+
conv(self.dim, 3, k))
|
| 332 |
+
|
| 333 |
+
def forward(self, x1):
|
| 334 |
+
x = self.pre(x1)
|
| 335 |
+
res1 = self.g1(x)
|
| 336 |
+
res2 = self.g2(res1)
|
| 337 |
+
res3 = self.g3(res2)
|
| 338 |
+
w = self.ca(torch.cat([res1, res2, res3], dim=1))
|
| 339 |
+
w = w.view(-1, self.gps, self.dim)[:, :, :, None, None]
|
| 340 |
+
out = w[:, 0] * res1 + w[:, 1] * res2 + w[:, 2] * res3
|
| 341 |
+
out = self.palayer(out)
|
| 342 |
+
return (self.post(out) + x1).clamp(0, 1)
|
| 343 |
+
|
| 344 |
+
net = _FFANet()
|
| 345 |
+
net.load_state_dict(_load_state_dict(weights_path), strict=True)
|
| 346 |
net.eval()
|
| 347 |
print(f"[startup] FFA-Net loaded: {weights_path}")
|
| 348 |
return net
|