remove stray nerve_arch.py
Browse files- nerve_arch.py +0 -97
nerve_arch.py
DELETED
|
@@ -1,97 +0,0 @@
|
|
| 1 |
-
# NERVE — Norm-free Efficient Restoration for Various Edge devices.
|
| 2 |
-
#
|
| 3 |
-
# A lightweight super-resolution network designed for ease of use rather than
|
| 4 |
-
# benchmark chasing: pure convolution (Conv-ReLU-Conv residual blocks, no
|
| 5 |
-
# normalization, no attention, no gating), a single readable file, and standard
|
| 6 |
-
# ops only (Conv / Add / ReLU / PixelShuffle / bicubic Resize) so the checkpoint
|
| 7 |
-
# exports to dynamic-shape ONNX and converts to NCNN/CoreML without custom ops
|
| 8 |
-
# or fused/unfused checkpoint pairs.
|
| 9 |
-
#
|
| 10 |
-
# A 5x5 stem opens the receptive field for real-world degradations; a global
|
| 11 |
-
# residual and a bicubic input residual let the net learn only what bicubic
|
| 12 |
-
# upscaling misses. Default dim=64 / n_blocks=24 (~1.8M params at 4x).
|
| 13 |
-
|
| 14 |
-
from __future__ import annotations
|
| 15 |
-
|
| 16 |
-
import math
|
| 17 |
-
|
| 18 |
-
import torch
|
| 19 |
-
from spandrel.util import store_hyperparameters
|
| 20 |
-
from torch import Tensor, nn
|
| 21 |
-
from torch.nn import functional as F # noqa: N812
|
| 22 |
-
|
| 23 |
-
def _icnr_init(conv: nn.Conv2d, upscale: int) -> None:
|
| 24 |
-
"""ICNR initialization for a Conv2d that feeds a PixelShuffle(upscale).
|
| 25 |
-
|
| 26 |
-
Every sub-pixel phase starts from the same base kernel, so the upsampling
|
| 27 |
-
head cannot begin with a checkerboard bias. Init-only: no graph or runtime
|
| 28 |
-
cost, and no effect once a checkpoint is loaded.
|
| 29 |
-
"""
|
| 30 |
-
out_c = conv.out_channels
|
| 31 |
-
sub = upscale * upscale
|
| 32 |
-
assert out_c % sub == 0
|
| 33 |
-
base_c = out_c // sub
|
| 34 |
-
base = nn.init.kaiming_uniform_(conv.weight[:base_c].clone(), a=math.sqrt(5))
|
| 35 |
-
with torch.no_grad():
|
| 36 |
-
conv.weight.data.zero_()
|
| 37 |
-
for i in range(sub):
|
| 38 |
-
conv.weight.data[i::sub] = base
|
| 39 |
-
if conv.bias is not None:
|
| 40 |
-
nn.init.zeros_(conv.bias)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
class NerveBlock(nn.Module):
|
| 44 |
-
"""Conv-ReLU-Conv residual block. No normalization, no shortcuts."""
|
| 45 |
-
|
| 46 |
-
def __init__(self, dim: int) -> None:
|
| 47 |
-
super().__init__()
|
| 48 |
-
self.conv1 = nn.Conv2d(dim, dim, 3, padding=1, bias=False)
|
| 49 |
-
self.conv2 = nn.Conv2d(dim, dim, 3, padding=1, bias=False)
|
| 50 |
-
|
| 51 |
-
def forward(self, x: Tensor) -> Tensor:
|
| 52 |
-
return x + self.conv2(F.relu(self.conv1(x)))
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
@store_hyperparameters()
|
| 56 |
-
class NERVE(nn.Module):
|
| 57 |
-
"""5x5 stem -> n_blocks residual blocks -> PixelShuffle head -> + bicubic(x)."""
|
| 58 |
-
|
| 59 |
-
hyperparameters = {} # noqa: RUF012
|
| 60 |
-
|
| 61 |
-
def __init__(
|
| 62 |
-
self,
|
| 63 |
-
*,
|
| 64 |
-
in_ch: int = 3,
|
| 65 |
-
out_ch: int = 3,
|
| 66 |
-
dim: int = 64,
|
| 67 |
-
n_blocks: int = 24,
|
| 68 |
-
upscale: int = 4,
|
| 69 |
-
) -> None:
|
| 70 |
-
super().__init__()
|
| 71 |
-
self.upscale = upscale
|
| 72 |
-
|
| 73 |
-
self.stem = nn.Conv2d(in_ch, dim, 5, padding=2, bias=False)
|
| 74 |
-
self.blocks = nn.Sequential(*[NerveBlock(dim) for _ in range(n_blocks)])
|
| 75 |
-
head = nn.Conv2d(dim, out_ch * upscale**2, 3, padding=1, bias=False)
|
| 76 |
-
self.upsampler = nn.Sequential(head, nn.PixelShuffle(upscale))
|
| 77 |
-
_icnr_init(head, upscale)
|
| 78 |
-
|
| 79 |
-
def forward(self, x: Tensor) -> Tensor:
|
| 80 |
-
feat = self.stem(x)
|
| 81 |
-
out = feat + self.blocks(feat)
|
| 82 |
-
out = self.upsampler(out)
|
| 83 |
-
base = F.interpolate(
|
| 84 |
-
x, scale_factor=self.upscale, mode="bicubic", align_corners=False
|
| 85 |
-
)
|
| 86 |
-
return out + base
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
def nerve(
|
| 90 |
-
scale: int = 4,
|
| 91 |
-
*,
|
| 92 |
-
in_ch: int = 3,
|
| 93 |
-
out_ch: int = 3,
|
| 94 |
-
dim: int = 64,
|
| 95 |
-
n_blocks: int = 24,
|
| 96 |
-
) -> NERVE:
|
| 97 |
-
return NERVE(in_ch=in_ch, out_ch=out_ch, dim=dim, n_blocks=n_blocks, upscale=scale)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|