Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,80 @@
|
|
| 1 |
---
|
| 2 |
license: mit
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
+
pipeline_tag: image-to-image
|
| 4 |
+
tags:
|
| 5 |
+
- style-transfer
|
| 6 |
+
- pytorch
|
| 7 |
---
|
| 8 |
+
|
| 9 |
+
# Fast Neural Style Transfer β Starry Night
|
| 10 |
+
|
| 11 |
+
This repository contains weights for a Fast Neural Style Transfer network based on Johnson et al. It is trained on the COCO val2017 dataset to instantly apply Vincent van Gogh's *The Starry Night* style to any input image.
|
| 12 |
+
|
| 13 |
+
## Style Transfer Preview
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
| Content Image | Stylized Output |
|
| 17 |
+
| :---: | :---: |
|
| 18 |
+
| <img src="before.jpg" width="400"> | <img src="after.jpg" width="400"> |
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## How to Use Programmatically
|
| 23 |
+
|
| 24 |
+
You can run inference using the Hugging Face `transformers` library. The script automatically downloads your weights and applies the necessary ImageNet normalization matching the training routine.
|
| 25 |
+
|
| 26 |
+
### Dependencies
|
| 27 |
+
Ensure you have the required packages installed:
|
| 28 |
+
```bash
|
| 29 |
+
pip install torch torchvision transformers pillow huggingface_hub
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
### Inference Script (`inference.py`)
|
| 33 |
+
Save the following code as `inference.py`. You can run it via terminal with `python inference.py your_image.jpg`.
|
| 34 |
+
|
| 35 |
+
```python
|
| 36 |
+
import sys
|
| 37 |
+
import torch
|
| 38 |
+
from PIL import Image
|
| 39 |
+
from torchvision import transforms
|
| 40 |
+
from torchvision.utils import save_image
|
| 41 |
+
from transformers import AutoModel
|
| 42 |
+
|
| 43 |
+
# ββ CONFIG βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 44 |
+
REPO_ID = "Rohanify/Brawnz-StyleTransfer" # Your repository ID
|
| 45 |
+
IMG_SIZE = 512
|
| 46 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 47 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 48 |
+
|
| 49 |
+
# ββ LOAD IMAGE βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
+
if len(sys.argv) < 2:
|
| 51 |
+
print("Usage: python inference.py path_to_input_image.jpg")
|
| 52 |
+
sys.exit(1)
|
| 53 |
+
|
| 54 |
+
input_path = sys.argv[1]
|
| 55 |
+
output_path = "output_styled.jpg"
|
| 56 |
+
|
| 57 |
+
transform = transforms.Compose([
|
| 58 |
+
transforms.Resize((IMG_SIZE, IMG_SIZE)),
|
| 59 |
+
transforms.ToTensor(),
|
| 60 |
+
transforms.Normalize([0.485, 0.456, 0.406],
|
| 61 |
+
[0.229, 0.224, 0.225]),
|
| 62 |
+
])
|
| 63 |
+
|
| 64 |
+
img = Image.open(input_path).convert("RGB")
|
| 65 |
+
x = transform(img).unsqueeze(0).to(DEVICE)
|
| 66 |
+
|
| 67 |
+
# ββ LOAD MODEL FROM HUGGING FACE βββββββββββββββββββββββββββββ
|
| 68 |
+
print("Loading model weights from Hugging Face Hub...")
|
| 69 |
+
model = AutoModel.from_pretrained(REPO_ID, trust_remote_code=True).to(DEVICE)
|
| 70 |
+
model.eval()
|
| 71 |
+
|
| 72 |
+
# ββ RUN INFERENCE ββββββββββββββββββββββββββββββββββββββββββββ
|
| 73 |
+
print(f"Running inference on device: {DEVICE}...")
|
| 74 |
+
with torch.no_grad():
|
| 75 |
+
out = model(x)
|
| 76 |
+
|
| 77 |
+
# De-normalize and save output image
|
| 78 |
+
save_image(out[0] * 0.5 + 0.5, output_path)
|
| 79 |
+
print(f"Success! Styled image saved to: {output_path}")
|
| 80 |
+
```
|