--- title: SpectraGAN emoji: 🖼️ colorFrom: blue colorTo: green sdk: gradio sdk_version: 5.31.0 app_file: app.py pinned: false license: apache-2.0 --- # 🖼️ SpectraGAN — Multi-Model Upscaler Comparison A Gradio web app that lets you upscale an image with **multiple SR models simultaneously** and compare results side by side. Supported models: | Model | Architecture | Scale | |-------|-------------|-------| | Real-ESRGAN ×2 | GAN (residual-in-residual dense block) | ×2 | | Real-ESRGAN ×4 | GAN (residual-in-residual dense block) | ×4 | | SRCNN ×4 | Shallow 3-layer CNN | ×4 | | HResNet ×4 | Deep residual network (EDSR-style) | ×4 | | SR3 *(stub)* | Diffusion model | ×4 — see note below | --- ## 📑 Table of Contents 1. [Features](#features) 2. [Project Structure](#project-structure) 3. [Prerequisites](#prerequisites) 4. [Installation](#installation) 5. [Adding Your ONNX Models](#adding-your-onnx-models) 6. [Running Locally](#running-locally) 7. [SR3 Integration Guide](#sr3-integration-guide) 8. [Contributing](#contributing) 9. [License](#license) --- ## ✨ Features - **Side-by-side comparison** — run up to 4 models at once, results displayed in a 4-panel grid. - **Selective execution** — toggle any model on/off before running; unchecked models are skipped. - **×8 post-resize** — optionally apply a bicubic ×2 pass on top of any ×4 result. - **Tile-based inference** — large images are split into tiles matching each model's fixed input size, then stitched back together seamlessly. - **Per-result download** — each panel has its own PNG download button. - **Graceful degradation** — if a model file is missing (e.g. Drive ID not yet set), that panel is skipped without crashing the others. --- ## 📁 Project Structure ``` spectragan/ ├── model/ │ ├── Real-ESRGAN_x2plus.onnx # auto-downloaded │ ├── Real-ESRGAN-x4plus.onnx # auto-downloaded │ ├── SRCNN_x4.onnx # you provide — see below │ └── HResNet_x4.onnx # you provide — see below ├── app.py ├── requirements.txt └── README.md ``` --- ## ⚙️ Prerequisites - Python 3.10+ - `git` - A terminal / command prompt --- ## 🔧 Installation ```bash git clone https://github.com/ParamAhuja/SpectraGAN.git cd SpectraGAN python -m venv .venv source .venv/bin/activate # Linux/macOS # .venv\Scripts\activate # Windows pip install -r requirements.txt ``` --- ## 🗂️ Adding Your ONNX Models The Real-ESRGAN weights are downloaded automatically from Google Drive on first run. For **SRCNN** and **HResNet** you need to: 1. Export your trained PyTorch model to ONNX: ```python import torch # SRCNN example from srcnn import SRCNN model = SRCNN() model.load_state_dict(torch.load("srcnn.pth")) model.eval() dummy = torch.randn(1, 3, 128, 128) torch.onnx.export( model, dummy, "SRCNN_x4.onnx", input_names=["input"], output_names=["output"], dynamic_axes={"input": {2: "H", 3: "W"}, "output": {2: "H", 3: "W"}} ) ``` 2. Upload the `.onnx` file to Google Drive and set **"Anyone with the link can view"**. 3. Copy the file ID from the share URL and update `DRIVE_IDS` in `app.py`: ```python DRIVE_IDS = { ... "srcnn_x4": "YOUR_SRCNN_DRIVE_FILE_ID_HERE", "hresnet_x4": "YOUR_HRESNET_DRIVE_FILE_ID_HERE", } ``` --- ## 🚀 Running Locally ```bash python app.py ``` Open `http://127.0.0.1:7860` in your browser. --- ## 🌀 SR3 Integration Guide SR3 (Super-Resolution via Repeated Refinement) is a **diffusion model** — it cannot be exported to a static ONNX graph because its inference involves a variable-length denoising loop. To add SR3: 1. Clone the reference implementation: ```bash git clone https://github.com/Janspiry/Image-Super-Resolution-via-Iterative-Refinement ``` 2. Place your trained checkpoint at `model/sr3_x4.pth`. 3. Add `torch` and `torchvision` to `requirements.txt`. 4. Write a wrapper in `app.py`: ```python def run_sr3(input_img: Image.Image) -> Image.Image: # load config + model, run the denoising loop, return result ... ``` 5. Add `"sr3_x4"` to the `PANEL_KEYS` list and wire `run_sr3` into `compare_models`. --- ## 🤝 Contributing Pull requests welcome. Please open an issue first to discuss significant changes. --- ## 📄 License Apache 2.0 — see `LICENSE`. --- ## 👤 Author & Credits - Real-ESRGAN by [xinntao](https://github.com/xinntao/Real-ESRGAN) - SRCNN by Dong et al. (2014) - HResNet / EDSR by Lim et al. (2017) - SR3 by Ho et al. (2022) — [paper](https://arxiv.org/abs/2104.07636)