mathsnap-deblur / deblur.py
Monishcloud8's picture
Upload 5 files
21f5137 verified
Raw History Blame Contribute Delete
3.48 kB
"""Tiled NAFNet deblurring for photos of printed pages. Images are processed in memory and never stored."""
import hashlib
import io
import os
import numpy as np
import torch
from PIL import Image
import nafnet
TILE = 384 # Equals NAFNet's TLC pooling window (train size 256 x 1.5), so global pooling per tile matches TLC.
OVERLAP = 32
BATCH = 8
MAX_EDGE = 2048
REPO, REVISION = "mlx-community/NAFNet-GoPro-width64", "09d01a2c63576d70bb4eedc38539cdc9c1469c55"
WEIGHTS_SHA256 = "53d808d8f74bac6817055b01361bf2a13f73506391a84940bac7238aa400387e"
Image.MAX_IMAGE_PIXELS = 20_000_000
def _weights():
path = os.environ.get("NAFNET_WEIGHTS")
if not path:
from huggingface_hub import hf_hub_download
path = hf_hub_download(REPO, "model.safetensors", revision=REVISION)
digest = hashlib.sha256()
with open(path, "rb") as file:
for chunk in iter(lambda: file.read(1 << 20), b""):
digest.update(chunk)
if digest.hexdigest() != WEIGHTS_SHA256:
raise RuntimeError("NAFNet weights do not match the pinned checksum")
return path
# ZeroGPU wants the model placed on cuda at import time; real GPU work happens only inside @spaces.GPU.
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
MODEL = nafnet.load(_weights()).to(DEVICE)
def _starts(size):
if size <= TILE:
return [0]
return list(range(0, size - TILE, TILE - OVERLAP)) + [size - TILE]
def _ramp():
# Feathered blend: weights rise across the overlap so tile seams fade instead of showing as lines.
edge = np.minimum(np.arange(TILE) + 1, np.arange(TILE)[::-1] + 1).astype(np.float32)
return np.clip(edge / OVERLAP, 1 / OVERLAP, 1)
def deblur_array(rgb):
"""rgb: float32 HxWx3 in [0, 1]. Returns the restored array, same shape."""
height, width, _ = rgb.shape
padded_h, padded_w = max(height, TILE), max(width, TILE)
if (padded_h, padded_w) != (height, width):
rgb = np.pad(rgb, ((0, padded_h - height), (0, padded_w - width), (0, 0)), mode="reflect")
ramp = _ramp()
weight = np.outer(ramp, ramp)[..., None]
total = np.zeros_like(rgb)
weights = np.zeros((padded_h, padded_w, 1), np.float32)
positions = [(y, x) for y in _starts(padded_h) for x in _starts(padded_w)]
with torch.inference_mode():
for i in range(0, len(positions), BATCH):
batch = positions[i:i + BATCH]
tiles = np.stack([rgb[y:y + TILE, x:x + TILE].transpose(2, 0, 1) for y, x in batch])
out = MODEL(torch.from_numpy(tiles).to(DEVICE)).clamp(0, 1).cpu().numpy().transpose(0, 2, 3, 1)
for (y, x), tile in zip(batch, out):
total[y:y + TILE, x:x + TILE] += tile * weight
weights[y:y + TILE, x:x + TILE] += weight
return (total / weights)[:height, :width]
def decode(data):
with Image.open(io.BytesIO(data)) as image:
if image.format != "JPEG":
raise ValueError("Expected a JPEG image")
image = image.convert("RGB")
if max(image.size) > MAX_EDGE:
image.thumbnail((MAX_EDGE, MAX_EDGE), Image.Resampling.LANCZOS)
if min(image.size) < 64:
raise ValueError("Image is too small")
return np.asarray(image, dtype=np.float32) / 255
def encode(rgb):
output = io.BytesIO()
Image.fromarray((rgb * 255 + 0.5).astype(np.uint8)).save(output, "JPEG", quality=92)
return output.getvalue()
def deblur_jpeg(data):
return encode(deblur_array(decode(data)))