File size: 4,649 Bytes
f376a33
 
3262d11
f376a33
3262d11
f376a33
3262d11
f376a33
 
3262d11
f376a33
 
3262d11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
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)