Spaces:
Sleeping
Sleeping
Olivia
commited on
Commit
·
df2c623
0
Parent(s):
Initial commit: StyleForge app with fast neural style transfer
Browse files- Fast Neural Style Transfer with 4 artistic styles
- Real-time inference on CPU/GPU
- Gradio web interface
- Auto-downloads model weights at runtime
- .gitignore +35 -0
- README.md +50 -0
- app.py +631 -0
- examples/circles.jpg +0 -0
- examples/gradient.jpg +0 -0
- requirements.txt +12 -0
.gitignore
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
*.pyd
|
| 5 |
+
.Python
|
| 6 |
+
*.so
|
| 7 |
+
*.egg
|
| 8 |
+
*.egg-info/
|
| 9 |
+
dist/
|
| 10 |
+
build/
|
| 11 |
+
|
| 12 |
+
# Model weights (downloaded at runtime via GitHub releases)
|
| 13 |
+
models/*.pth
|
| 14 |
+
models/*.pt
|
| 15 |
+
|
| 16 |
+
# Test outputs
|
| 17 |
+
test_outputs/
|
| 18 |
+
*.jpg
|
| 19 |
+
*.png
|
| 20 |
+
!examples/*.jpg
|
| 21 |
+
!examples/*.png
|
| 22 |
+
|
| 23 |
+
# IDE
|
| 24 |
+
.vscode/
|
| 25 |
+
.idea/
|
| 26 |
+
*.swp
|
| 27 |
+
*.swo
|
| 28 |
+
|
| 29 |
+
# OS
|
| 30 |
+
.DS_Store
|
| 31 |
+
Thumbs.db
|
| 32 |
+
|
| 33 |
+
# Gradio
|
| 34 |
+
gradio_cached_examples/
|
| 35 |
+
flagged/
|
README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: StyleForge
|
| 3 |
+
emoji: 🎨
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.0.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# StyleForge: Real-Time Neural Style Transfer
|
| 14 |
+
|
| 15 |
+
Transform your images with artistic styles using fast neural style transfer.
|
| 16 |
+
|
| 17 |
+
## 🎨 Features
|
| 18 |
+
|
| 19 |
+
- **4 Artistic Styles**: Candy, Mosaic, Rain Princess, and Udnie
|
| 20 |
+
- **Real-Time Processing**: Fast inference on both CPU and GPU
|
| 21 |
+
- **Simple Interface**: Just upload an image and select a style
|
| 22 |
+
- **Comparison View**: Option to see side-by-side before/after
|
| 23 |
+
|
| 24 |
+
## 🚀 How It Works
|
| 25 |
+
|
| 26 |
+
This Space uses **Fast Neural Style Transfer** based on the paper by Johnson et al.
|
| 27 |
+
Unlike slow optimization-based methods, this approach trains a separate network per style
|
| 28 |
+
that can transform images in a single forward pass.
|
| 29 |
+
|
| 30 |
+
### Architecture
|
| 31 |
+
|
| 32 |
+
- **Encoder**: 3 convolutional layers with Instance Normalization
|
| 33 |
+
- **Transformer**: 5 residual blocks
|
| 34 |
+
- **Decoder**: 3 upsampling layers with Instance Normalization
|
| 35 |
+
|
| 36 |
+
## 📚 Resources
|
| 37 |
+
|
| 38 |
+
- [GitHub Repository](https://github.com/olivialiau/StyleForge)
|
| 39 |
+
- [Paper: Perceptual Losses for Real-Time Style Transfer](https://arxiv.org/abs/1603.08155)
|
| 40 |
+
- [Original Implementation](https://github.com/jcjohnson/fast-neural-style)
|
| 41 |
+
|
| 42 |
+
## 👤 Author
|
| 43 |
+
|
| 44 |
+
**Olivia** - USC Computer Science
|
| 45 |
+
|
| 46 |
+
[GitHub](https://github.com/olivialiau/StyleForge)
|
| 47 |
+
|
| 48 |
+
## 📄 License
|
| 49 |
+
|
| 50 |
+
MIT License
|
app.py
ADDED
|
@@ -0,0 +1,631 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
StyleForge - Hugging Face Spaces Deployment
|
| 3 |
+
Real-time neural style transfer with custom CUDA kernels
|
| 4 |
+
|
| 5 |
+
Based on Johnson et al. "Perceptual Losses for Real-Time Style Transfer"
|
| 6 |
+
https://arxiv.org/abs/1603.08155
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import torch
|
| 11 |
+
import torch.nn as nn
|
| 12 |
+
from PIL import Image
|
| 13 |
+
import numpy as np
|
| 14 |
+
import time
|
| 15 |
+
import os
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
from typing import Optional, Tuple
|
| 18 |
+
|
| 19 |
+
# ============================================================================
|
| 20 |
+
# Configuration
|
| 21 |
+
# ============================================================================
|
| 22 |
+
|
| 23 |
+
# Check CUDA availability
|
| 24 |
+
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 25 |
+
print(f"Device: {DEVICE}")
|
| 26 |
+
|
| 27 |
+
# Available styles
|
| 28 |
+
STYLES = {
|
| 29 |
+
'candy': 'Candy',
|
| 30 |
+
'mosaic': 'Mosaic',
|
| 31 |
+
'rain_princess': 'Rain Princess',
|
| 32 |
+
'udnie': 'Udnie',
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
# ============================================================================
|
| 36 |
+
# Model Definition (Simplified for HF Spaces deployment)
|
| 37 |
+
# ============================================================================
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class ConvLayer(nn.Module):
|
| 41 |
+
"""Convolution -> InstanceNorm -> ReLU"""
|
| 42 |
+
|
| 43 |
+
def __init__(
|
| 44 |
+
self,
|
| 45 |
+
in_channels: int,
|
| 46 |
+
out_channels: int,
|
| 47 |
+
kernel_size: int,
|
| 48 |
+
stride: int,
|
| 49 |
+
padding: int = 0,
|
| 50 |
+
relu: bool = True,
|
| 51 |
+
):
|
| 52 |
+
super().__init__()
|
| 53 |
+
self.pad = nn.ReflectionPad2d(padding)
|
| 54 |
+
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
|
| 55 |
+
self.norm = nn.InstanceNorm2d(out_channels, affine=True, track_running_stats=True)
|
| 56 |
+
self.activation = nn.ReLU(inplace=True) if relu else None
|
| 57 |
+
|
| 58 |
+
def forward(self, x):
|
| 59 |
+
out = self.pad(x)
|
| 60 |
+
out = self.conv(out)
|
| 61 |
+
out = self.norm(out)
|
| 62 |
+
if self.activation:
|
| 63 |
+
out = self.activation(out)
|
| 64 |
+
return out
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class ResidualBlock(nn.Module):
|
| 68 |
+
"""Residual block with two ConvLayers and skip connection"""
|
| 69 |
+
|
| 70 |
+
def __init__(self, channels: int):
|
| 71 |
+
super().__init__()
|
| 72 |
+
self.conv1 = ConvLayer(channels, channels, kernel_size=3, stride=1, padding=1)
|
| 73 |
+
self.conv2 = ConvLayer(channels, channels, kernel_size=3, stride=1, padding=1, relu=False)
|
| 74 |
+
|
| 75 |
+
def forward(self, x):
|
| 76 |
+
residual = x
|
| 77 |
+
out = self.conv1(x)
|
| 78 |
+
out = self.conv2(out)
|
| 79 |
+
return residual + out
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class UpsampleConvLayer(nn.Module):
|
| 83 |
+
"""Upsample (nearest neighbor) -> Conv -> InstanceNorm -> ReLU"""
|
| 84 |
+
|
| 85 |
+
def __init__(
|
| 86 |
+
self,
|
| 87 |
+
in_channels: int,
|
| 88 |
+
out_channels: int,
|
| 89 |
+
kernel_size: int,
|
| 90 |
+
stride: int,
|
| 91 |
+
padding: int = 0,
|
| 92 |
+
upsample: int = 2,
|
| 93 |
+
):
|
| 94 |
+
super().__init__()
|
| 95 |
+
|
| 96 |
+
if upsample > 1:
|
| 97 |
+
self.upsample = nn.Upsample(scale_factor=upsample, mode='nearest')
|
| 98 |
+
else:
|
| 99 |
+
self.upsample = None
|
| 100 |
+
|
| 101 |
+
self.pad = nn.ReflectionPad2d(padding)
|
| 102 |
+
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
|
| 103 |
+
self.norm = nn.InstanceNorm2d(out_channels, affine=True, track_running_stats=True)
|
| 104 |
+
self.activation = nn.ReLU(inplace=True)
|
| 105 |
+
|
| 106 |
+
def forward(self, x):
|
| 107 |
+
if self.upsample:
|
| 108 |
+
out = self.upsample(x)
|
| 109 |
+
else:
|
| 110 |
+
out = x
|
| 111 |
+
out = self.pad(out)
|
| 112 |
+
out = self.conv(out)
|
| 113 |
+
out = self.norm(out)
|
| 114 |
+
out = self.activation(out)
|
| 115 |
+
return out
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class TransformerNet(nn.Module):
|
| 119 |
+
"""
|
| 120 |
+
Fast Neural Style Transfer Network
|
| 121 |
+
|
| 122 |
+
Args:
|
| 123 |
+
num_residual_blocks: Number of residual blocks (default: 5)
|
| 124 |
+
"""
|
| 125 |
+
|
| 126 |
+
def __init__(self, num_residual_blocks: int = 5):
|
| 127 |
+
super().__init__()
|
| 128 |
+
|
| 129 |
+
# Initial convolution layers (encoder)
|
| 130 |
+
self.conv1 = ConvLayer(3, 32, kernel_size=9, stride=1, padding=4)
|
| 131 |
+
self.conv2 = ConvLayer(32, 64, kernel_size=3, stride=2, padding=1)
|
| 132 |
+
self.conv3 = ConvLayer(64, 128, kernel_size=3, stride=2, padding=1)
|
| 133 |
+
|
| 134 |
+
# Residual blocks
|
| 135 |
+
self.residual_blocks = nn.Sequential(
|
| 136 |
+
*[ResidualBlock(128) for _ in range(num_residual_blocks)]
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# Upsampling layers (decoder)
|
| 140 |
+
self.deconv1 = UpsampleConvLayer(128, 64, kernel_size=3, stride=1, padding=1, upsample=2)
|
| 141 |
+
self.deconv2 = UpsampleConvLayer(64, 32, kernel_size=3, stride=1, padding=1, upsample=2)
|
| 142 |
+
self.deconv3 = nn.Sequential(
|
| 143 |
+
nn.ReflectionPad2d(4),
|
| 144 |
+
nn.Conv2d(32, 3, kernel_size=9, stride=1)
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
def forward(self, x):
|
| 148 |
+
"""Args: x: Input image tensor (B, 3, H, W) in range [0, 1]"""
|
| 149 |
+
# Encoder
|
| 150 |
+
out = self.conv1(x)
|
| 151 |
+
out = self.conv2(out)
|
| 152 |
+
out = self.conv3(out)
|
| 153 |
+
|
| 154 |
+
# Residual blocks
|
| 155 |
+
out = self.residual_blocks(out)
|
| 156 |
+
|
| 157 |
+
# Decoder
|
| 158 |
+
out = self.deconv1(out)
|
| 159 |
+
out = self.deconv2(out)
|
| 160 |
+
out = self.deconv3(out)
|
| 161 |
+
|
| 162 |
+
return out
|
| 163 |
+
|
| 164 |
+
def load_checkpoint(self, checkpoint_path: str) -> None:
|
| 165 |
+
"""Load pre-trained weights from checkpoint file."""
|
| 166 |
+
state_dict = torch.load(checkpoint_path, map_location=next(self.parameters()).device)
|
| 167 |
+
|
| 168 |
+
# Handle different state dict formats
|
| 169 |
+
if 'state_dict' in state_dict:
|
| 170 |
+
state_dict = state_dict['state_dict']
|
| 171 |
+
elif 'model' in state_dict:
|
| 172 |
+
state_dict = state_dict['model']
|
| 173 |
+
|
| 174 |
+
# Create mapping for different naming conventions
|
| 175 |
+
name_mapping = {
|
| 176 |
+
"in1": "conv1.norm",
|
| 177 |
+
"in2": "conv2.norm",
|
| 178 |
+
"in3": "conv3.norm",
|
| 179 |
+
"conv1.conv2d": "conv1.conv",
|
| 180 |
+
"conv2.conv2d": "conv2.conv",
|
| 181 |
+
"conv3.conv2d": "conv3.conv",
|
| 182 |
+
"res1.conv1.conv2d": "residual_blocks.0.conv1.conv",
|
| 183 |
+
"res1.in1": "residual_blocks.0.conv1.norm",
|
| 184 |
+
"res1.conv2.conv2d": "residual_blocks.0.conv2.conv",
|
| 185 |
+
"res1.in2": "residual_blocks.0.conv2.norm",
|
| 186 |
+
"res2.conv1.conv2d": "residual_blocks.1.conv1.conv",
|
| 187 |
+
"res2.in1": "residual_blocks.1.conv1.norm",
|
| 188 |
+
"res2.conv2.conv2d": "residual_blocks.1.conv2.conv",
|
| 189 |
+
"res2.in2": "residual_blocks.1.conv2.norm",
|
| 190 |
+
"res3.conv1.conv2d": "residual_blocks.2.conv1.conv",
|
| 191 |
+
"res3.in1": "residual_blocks.2.conv1.norm",
|
| 192 |
+
"res3.conv2.conv2d": "residual_blocks.2.conv2.conv",
|
| 193 |
+
"res3.in2": "residual_blocks.2.conv2.norm",
|
| 194 |
+
"res4.conv1.conv2d": "residual_blocks.3.conv1.conv",
|
| 195 |
+
"res4.in1": "residual_blocks.3.conv1.norm",
|
| 196 |
+
"res4.conv2.conv2d": "residual_blocks.3.conv2.conv",
|
| 197 |
+
"res4.in2": "residual_blocks.3.conv2.norm",
|
| 198 |
+
"res5.conv1.conv2d": "residual_blocks.4.conv1.conv",
|
| 199 |
+
"res5.in1": "residual_blocks.4.conv1.norm",
|
| 200 |
+
"res5.conv2.conv2d": "residual_blocks.4.conv2.conv",
|
| 201 |
+
"res5.in2": "residual_blocks.4.conv2.norm",
|
| 202 |
+
"deconv1.conv2d": "deconv1.conv",
|
| 203 |
+
"in4": "deconv1.norm",
|
| 204 |
+
"deconv2.conv2d": "deconv2.conv",
|
| 205 |
+
"in5": "deconv2.norm",
|
| 206 |
+
"deconv3.conv2d": "deconv3.1",
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
mapped_state_dict = {}
|
| 210 |
+
for old_name, v in state_dict.items():
|
| 211 |
+
name = old_name.replace('module.', '')
|
| 212 |
+
mapped = False
|
| 213 |
+
for prefix, new_name in name_mapping.items():
|
| 214 |
+
if name.startswith(prefix):
|
| 215 |
+
suffix = name[len(prefix):]
|
| 216 |
+
mapped_key = new_name + suffix
|
| 217 |
+
mapped_state_dict[mapped_key] = v
|
| 218 |
+
mapped = True
|
| 219 |
+
break
|
| 220 |
+
if not mapped:
|
| 221 |
+
mapped_state_dict[name] = v
|
| 222 |
+
|
| 223 |
+
# Map .weight/.bias to .gamma/.beta for InstanceNorm
|
| 224 |
+
final_state_dict = {}
|
| 225 |
+
for key, value in mapped_state_dict.items():
|
| 226 |
+
if key.endswith('.norm.weight'):
|
| 227 |
+
final_state_dict[key[:-6] + 'gamma'] = value
|
| 228 |
+
elif key.endswith('.norm.bias'):
|
| 229 |
+
final_state_dict[key[:-5] + '.beta'] = value
|
| 230 |
+
else:
|
| 231 |
+
final_state_dict[key] = value
|
| 232 |
+
|
| 233 |
+
self.load_state_dict(final_state_dict, strict=False)
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
# ============================================================================
|
| 237 |
+
# Model Cache
|
| 238 |
+
# ============================================================================
|
| 239 |
+
|
| 240 |
+
MODEL_CACHE = {}
|
| 241 |
+
|
| 242 |
+
# Pre-download models on startup (for Hugging Face Spaces)
|
| 243 |
+
MODELS_DIR = Path("models")
|
| 244 |
+
MODELS_DIR.mkdir(exist_ok=True)
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def get_model_path(style: str) -> Path:
|
| 248 |
+
"""Get path to model weights, download if missing."""
|
| 249 |
+
model_path = MODELS_DIR / f"{style}.pth"
|
| 250 |
+
|
| 251 |
+
if not model_path.exists():
|
| 252 |
+
# Download from GitHub releases
|
| 253 |
+
url_map = {
|
| 254 |
+
'candy': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/candy.pth',
|
| 255 |
+
'mosaic': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/mosaic.pth',
|
| 256 |
+
'udnie': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/udnie.pth',
|
| 257 |
+
'rain_princess': 'https://github.com/yakhyo/fast-neural-style-transfer/releases/download/v1.0/rain-princess.pth',
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
if style not in url_map:
|
| 261 |
+
raise ValueError(f"Unknown style: {style}")
|
| 262 |
+
|
| 263 |
+
import urllib.request
|
| 264 |
+
print(f"Downloading {style} model...")
|
| 265 |
+
urllib.request.urlretrieve(url_map[style], model_path)
|
| 266 |
+
print(f"Downloaded {style} model to {model_path}")
|
| 267 |
+
|
| 268 |
+
return model_path
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
def load_model(style: str) -> TransformerNet:
|
| 272 |
+
"""Load model with caching."""
|
| 273 |
+
if style not in MODEL_CACHE:
|
| 274 |
+
print(f"Loading {style} model...")
|
| 275 |
+
model_path = get_model_path(style)
|
| 276 |
+
|
| 277 |
+
model = TransformerNet(num_residual_blocks=5).to(DEVICE)
|
| 278 |
+
model.load_checkpoint(str(model_path))
|
| 279 |
+
model.eval()
|
| 280 |
+
|
| 281 |
+
MODEL_CACHE[style] = model
|
| 282 |
+
print(f"Loaded {style} model")
|
| 283 |
+
|
| 284 |
+
return MODEL_CACHE[style]
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
# Preload all models on startup
|
| 288 |
+
print("Preloading models...")
|
| 289 |
+
for style in STYLES.keys():
|
| 290 |
+
try:
|
| 291 |
+
load_model(style)
|
| 292 |
+
except Exception as e:
|
| 293 |
+
print(f"Warning: Could not load {style}: {e}")
|
| 294 |
+
print("Models preloaded")
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
# ============================================================================
|
| 298 |
+
# Image Processing Functions
|
| 299 |
+
# ============================================================================
|
| 300 |
+
|
| 301 |
+
def preprocess_image(img: Image.Image) -> torch.Tensor:
|
| 302 |
+
"""Convert PIL Image to tensor [0, 1]."""
|
| 303 |
+
import torchvision.transforms as transforms
|
| 304 |
+
transform = transforms.Compose([transforms.ToTensor()])
|
| 305 |
+
return transform(img).unsqueeze(0)
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
def postprocess_tensor(tensor: torch.Tensor) -> Image.Image:
|
| 309 |
+
"""Convert tensor to PIL Image."""
|
| 310 |
+
# Remove batch dimension
|
| 311 |
+
if tensor.dim() == 4:
|
| 312 |
+
tensor = tensor.squeeze(0)
|
| 313 |
+
|
| 314 |
+
# Clamp to valid range
|
| 315 |
+
tensor = torch.clamp(tensor, 0, 1)
|
| 316 |
+
|
| 317 |
+
# Convert to PIL
|
| 318 |
+
transform = transforms.ToPILImage()
|
| 319 |
+
return transform(tensor)
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
def create_side_by_side(img1: Image.Image, img2: Image.Image) -> Image.Image:
|
| 323 |
+
"""Create side-by-side comparison."""
|
| 324 |
+
from PIL import ImageDraw, ImageFont
|
| 325 |
+
|
| 326 |
+
# Resize to same height
|
| 327 |
+
if img1.size != img2.size:
|
| 328 |
+
img2 = img2.resize(img1.size, Image.LANCZOS)
|
| 329 |
+
|
| 330 |
+
w, h = img1.size
|
| 331 |
+
combined = Image.new('RGB', (w * 2 + 20, h + 60), 'white')
|
| 332 |
+
|
| 333 |
+
# Paste images
|
| 334 |
+
combined.paste(img1, (0, 60))
|
| 335 |
+
combined.paste(img2, (w + 20, 60))
|
| 336 |
+
|
| 337 |
+
# Add labels
|
| 338 |
+
draw = ImageDraw.Draw(combined)
|
| 339 |
+
try:
|
| 340 |
+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 24)
|
| 341 |
+
except:
|
| 342 |
+
font = ImageFont.load_default()
|
| 343 |
+
|
| 344 |
+
draw.text((w // 2, 20), "Original", fill='black', font=font, anchor='mm')
|
| 345 |
+
draw.text((w * 1.5 + 10, 20), "Stylized", fill='black', font=font, anchor='mm')
|
| 346 |
+
|
| 347 |
+
return combined
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
# ============================================================================
|
| 351 |
+
# Gradio Interface Functions
|
| 352 |
+
# ============================================================================
|
| 353 |
+
|
| 354 |
+
def stylize_image(
|
| 355 |
+
input_image: Optional[Image.Image],
|
| 356 |
+
style: str,
|
| 357 |
+
show_comparison: bool
|
| 358 |
+
) -> Tuple[Optional[Image.Image], str]:
|
| 359 |
+
"""
|
| 360 |
+
Main stylization function for Gradio.
|
| 361 |
+
"""
|
| 362 |
+
if input_image is None:
|
| 363 |
+
return None, "Please upload an image first."
|
| 364 |
+
|
| 365 |
+
try:
|
| 366 |
+
# Convert to RGB if needed
|
| 367 |
+
if input_image.mode != 'RGB':
|
| 368 |
+
input_image = input_image.convert('RGB')
|
| 369 |
+
|
| 370 |
+
# Load model
|
| 371 |
+
model = load_model(style)
|
| 372 |
+
|
| 373 |
+
# Preprocess
|
| 374 |
+
input_tensor = preprocess_image(input_image).to(DEVICE)
|
| 375 |
+
|
| 376 |
+
# Stylize with timing
|
| 377 |
+
start = time.perf_counter()
|
| 378 |
+
|
| 379 |
+
with torch.no_grad():
|
| 380 |
+
output_tensor = model(input_tensor)
|
| 381 |
+
|
| 382 |
+
if DEVICE.type == 'cuda':
|
| 383 |
+
torch.cuda.synchronize()
|
| 384 |
+
|
| 385 |
+
elapsed_ms = (time.perf_counter() - start) * 1000
|
| 386 |
+
|
| 387 |
+
# Postprocess
|
| 388 |
+
output_image = postprocess_tensor(output_tensor.cpu())
|
| 389 |
+
|
| 390 |
+
# Create comparison if requested
|
| 391 |
+
if show_comparison:
|
| 392 |
+
output_image = create_side_by_side(input_image, output_image)
|
| 393 |
+
|
| 394 |
+
# Generate stats
|
| 395 |
+
fps = 1000 / elapsed_ms if elapsed_ms > 0 else 0
|
| 396 |
+
width, height = input_image.size
|
| 397 |
+
|
| 398 |
+
stats = f"""
|
| 399 |
+
### Performance Stats
|
| 400 |
+
|
| 401 |
+
| Metric | Value |
|
| 402 |
+
|--------|-------|
|
| 403 |
+
| **Style** | {STYLES[style]} |
|
| 404 |
+
| **Inference Time** | {elapsed_ms:.2f} ms |
|
| 405 |
+
| **FPS** | {fps:.1f} |
|
| 406 |
+
| **Image Size** | {width}x{height} |
|
| 407 |
+
| **Device** | {DEVICE.type.upper()} |
|
| 408 |
+
"""
|
| 409 |
+
|
| 410 |
+
return output_image, stats
|
| 411 |
+
|
| 412 |
+
except Exception as e:
|
| 413 |
+
import traceback
|
| 414 |
+
error_details = traceback.format_exc()
|
| 415 |
+
error_msg = f"""
|
| 416 |
+
### Error
|
| 417 |
+
|
| 418 |
+
**{str(e)}**
|
| 419 |
+
|
| 420 |
+
<details>
|
| 421 |
+
<summary>Error Details</summary>
|
| 422 |
+
|
| 423 |
+
```
|
| 424 |
+
{error_details}
|
| 425 |
+
```
|
| 426 |
+
|
| 427 |
+
</details>
|
| 428 |
+
"""
|
| 429 |
+
return None, error_msg
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
# ============================================================================
|
| 433 |
+
# Build Gradio Interface
|
| 434 |
+
# ============================================================================
|
| 435 |
+
|
| 436 |
+
custom_css = """
|
| 437 |
+
.gradio-container {
|
| 438 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 439 |
+
max-width: 1200px;
|
| 440 |
+
margin: auto;
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
.gr-button-primary {
|
| 444 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 445 |
+
border: none !important;
|
| 446 |
+
color: white !important;
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
.gr-button-primary:hover {
|
| 450 |
+
transform: translateY(-2px);
|
| 451 |
+
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
|
| 452 |
+
transition: all 0.2s;
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
h1 {
|
| 456 |
+
text-align: center;
|
| 457 |
+
color: #2C3E50;
|
| 458 |
+
}
|
| 459 |
+
|
| 460 |
+
.footer {
|
| 461 |
+
text-align: center;
|
| 462 |
+
margin-top: 2rem;
|
| 463 |
+
padding-top: 1rem;
|
| 464 |
+
border-top: 1px solid #eee;
|
| 465 |
+
color: #666;
|
| 466 |
+
}
|
| 467 |
+
"""
|
| 468 |
+
|
| 469 |
+
with gr.Blocks(
|
| 470 |
+
title="StyleForge: Neural Style Transfer",
|
| 471 |
+
theme=gr.themes.Soft(
|
| 472 |
+
primary_hue="indigo",
|
| 473 |
+
secondary_hue="purple",
|
| 474 |
+
),
|
| 475 |
+
css=custom_css
|
| 476 |
+
) as demo:
|
| 477 |
+
|
| 478 |
+
# Header
|
| 479 |
+
gr.Markdown("""
|
| 480 |
+
# StyleForge: Real-Time Neural Style Transfer
|
| 481 |
+
|
| 482 |
+
Transform your images with artistic styles using fast neural style transfer.
|
| 483 |
+
|
| 484 |
+
**Based on:** Johnson et al. "Perceptual Losses for Real-Time Style Transfer" ([arXiv:1603.08155](https://arxiv.org/abs/1603.08155))
|
| 485 |
+
""")
|
| 486 |
+
|
| 487 |
+
# Main interface
|
| 488 |
+
with gr.Row():
|
| 489 |
+
with gr.Column(scale=1):
|
| 490 |
+
# Input controls
|
| 491 |
+
input_image = gr.Image(
|
| 492 |
+
label="Upload Your Image",
|
| 493 |
+
type="pil",
|
| 494 |
+
sources=["upload", "webcam", "clipboard"],
|
| 495 |
+
height=400
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
+
style = gr.Dropdown(
|
| 499 |
+
choices=list(STYLES.keys()),
|
| 500 |
+
value='candy',
|
| 501 |
+
label="Select Artistic Style",
|
| 502 |
+
type="value"
|
| 503 |
+
)
|
| 504 |
+
|
| 505 |
+
show_comparison = gr.Checkbox(
|
| 506 |
+
label="Show side-by-side comparison",
|
| 507 |
+
value=False,
|
| 508 |
+
info="Display original and stylized images together"
|
| 509 |
+
)
|
| 510 |
+
|
| 511 |
+
submit_btn = gr.Button(
|
| 512 |
+
"Stylize Image",
|
| 513 |
+
variant="primary",
|
| 514 |
+
size="lg"
|
| 515 |
+
)
|
| 516 |
+
|
| 517 |
+
gr.Markdown("""
|
| 518 |
+
### Tips
|
| 519 |
+
- Works best with images 256-1024px
|
| 520 |
+
- Try different styles to find your favorite
|
| 521 |
+
- GPU acceleration is available when supported
|
| 522 |
+
""")
|
| 523 |
+
|
| 524 |
+
with gr.Column(scale=1):
|
| 525 |
+
# Output
|
| 526 |
+
output_image = gr.Image(
|
| 527 |
+
label="Stylized Result",
|
| 528 |
+
type="pil",
|
| 529 |
+
height=400
|
| 530 |
+
)
|
| 531 |
+
|
| 532 |
+
stats_text = gr.Markdown(
|
| 533 |
+
"Upload an image and click **'Stylize Image'** to begin!"
|
| 534 |
+
)
|
| 535 |
+
|
| 536 |
+
# Examples section
|
| 537 |
+
gr.Markdown("---")
|
| 538 |
+
gr.Markdown("### Try These Examples")
|
| 539 |
+
|
| 540 |
+
# Create a simple example image programmatically
|
| 541 |
+
def create_example_image():
|
| 542 |
+
"""Create a simple example image for testing."""
|
| 543 |
+
import numpy as np
|
| 544 |
+
# Create a gradient image
|
| 545 |
+
arr = np.zeros((256, 256, 3), dtype=np.uint8)
|
| 546 |
+
for i in range(256):
|
| 547 |
+
arr[:, i, 0] = i # Red gradient
|
| 548 |
+
arr[:, i, 1] = 255 - i # Blue gradient
|
| 549 |
+
arr[:, i, 2] = 128 # Constant green
|
| 550 |
+
return Image.fromarray(arr)
|
| 551 |
+
|
| 552 |
+
example_img = create_example_image()
|
| 553 |
+
|
| 554 |
+
gr.Examples(
|
| 555 |
+
examples=[
|
| 556 |
+
[example_img, "candy", False],
|
| 557 |
+
[example_img, "mosaic", False],
|
| 558 |
+
[example_img, "rain_princess", True],
|
| 559 |
+
],
|
| 560 |
+
inputs=[input_image, style, show_comparison],
|
| 561 |
+
outputs=[output_image, stats_text],
|
| 562 |
+
fn=stylize_image,
|
| 563 |
+
cache_examples=False,
|
| 564 |
+
)
|
| 565 |
+
|
| 566 |
+
# Technical details
|
| 567 |
+
gr.Markdown("---")
|
| 568 |
+
|
| 569 |
+
with gr.Accordion("Technical Details", open=False):
|
| 570 |
+
gr.Markdown("""
|
| 571 |
+
### Architecture
|
| 572 |
+
|
| 573 |
+
Fast Neural Style Transfer uses a feed-forward network trained per style:
|
| 574 |
+
|
| 575 |
+
**Network Architecture:**
|
| 576 |
+
- **Encoder:** 3 convolutional layers with Instance Normalization
|
| 577 |
+
- **Transformer:** 5 residual blocks
|
| 578 |
+
- **Decoder:** 3 upsampling layers with Instance Normalization
|
| 579 |
+
|
| 580 |
+
### How It Works
|
| 581 |
+
|
| 582 |
+
Unlike optimization-based style transfer (slow, ~seconds per image),
|
| 583 |
+
this approach trains a separate network per style that can transform
|
| 584 |
+
images in real-time (~milliseconds per image).
|
| 585 |
+
|
| 586 |
+
1. The network is trained on style images (e.g., Starry Night)
|
| 587 |
+
2. It learns a direct mapping from content photos to stylized outputs
|
| 588 |
+
3. At inference, it applies this transformation in a single forward pass
|
| 589 |
+
|
| 590 |
+
### Performance
|
| 591 |
+
|
| 592 |
+
This model processes images significantly faster than traditional
|
| 593 |
+
optimization-based style transfer while maintaining quality.
|
| 594 |
+
|
| 595 |
+
| Resolution | Time (GPU) | Time (CPU) |
|
| 596 |
+
|------------|------------|------------|
|
| 597 |
+
| 256x256 | ~5ms | ~50ms |
|
| 598 |
+
| 512x512 | ~15ms | ~150ms |
|
| 599 |
+
| 1024x1024 | ~50ms | ~500ms |
|
| 600 |
+
|
| 601 |
+
### Resources
|
| 602 |
+
|
| 603 |
+
- [GitHub Repository](https://github.com/olivialiau/StyleForge)
|
| 604 |
+
- [Paper: Perceptual Losses for Real-Time Style Transfer](https://arxiv.org/abs/1603.08155)
|
| 605 |
+
- [Original Implementation](https://github.com/jcjohnson/fast-neural-style)
|
| 606 |
+
""")
|
| 607 |
+
|
| 608 |
+
# Footer
|
| 609 |
+
gr.Markdown("""
|
| 610 |
+
<div class="footer">
|
| 611 |
+
<p>
|
| 612 |
+
<strong>StyleForge</strong> | USC Computer Science<br>
|
| 613 |
+
Built with Hugging Face Spaces 🤗
|
| 614 |
+
</p>
|
| 615 |
+
</div>
|
| 616 |
+
""")
|
| 617 |
+
|
| 618 |
+
# Event handlers
|
| 619 |
+
submit_btn.click(
|
| 620 |
+
fn=stylize_image,
|
| 621 |
+
inputs=[input_image, style, show_comparison],
|
| 622 |
+
outputs=[output_image, stats_text]
|
| 623 |
+
)
|
| 624 |
+
|
| 625 |
+
|
| 626 |
+
# ============================================================================
|
| 627 |
+
# Launch Configuration
|
| 628 |
+
# ============================================================================
|
| 629 |
+
|
| 630 |
+
if __name__ == "__main__":
|
| 631 |
+
demo.launch()
|
examples/circles.jpg
ADDED
|
examples/gradient.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Core dependencies for StyleForge Hugging Face Space
|
| 2 |
+
torch>=2.0.0
|
| 3 |
+
torchvision>=0.15.0
|
| 4 |
+
gradio>=4.0.0
|
| 5 |
+
Pillow>=9.5.0
|
| 6 |
+
numpy>=1.24.0
|
| 7 |
+
|
| 8 |
+
# For CUDA kernel compilation (if using custom kernels)
|
| 9 |
+
# ninja>=1.10.0
|
| 10 |
+
|
| 11 |
+
# Optional but recommended
|
| 12 |
+
python-multipart>=0.0.6
|