A small, fast artistic style transfer model built with PyTorch as a learning project.
Applies 4 artistic styles to any photo in under 1 second on CPU.
1import torch
2from torchvision import transforms
3from PIL import Image
4from model import StyleNet
5
6# 1. Load model
7model = StyleNet()
8model.load_state_dict(torch.load("starry_night.pth", map_location="cpu"))
9model.eval()
10
11# 2. Prepare your image
12img = Image.open("my_photo.jpg").convert("RGB")
13to_tensor = transforms.Compose([
14 transforms.ToTensor(),
15 transforms.Normalize(mean=[0.485, 0.456, 0.406],
16 std=[0.229, 0.224, 0.225]),
17])
18tensor = to_tensor(img).unsqueeze(0)
19
20# 3. Run inference
21with torch.no_grad():
22 output = model(tensor).squeeze(0).clamp(0, 1)
23
24# 4. Save result
25result = transforms.ToPILImage()(output)
26result.save("styled_output.jpg")
27print("Done! Open styled_output.jpg")
mini-style-transfer/
├── model.py ← StyleNet architecture
├── train.py ← Training script
├── run.py ← Inference script
├── starry_night.pth ← Trained weights (starry night style)
├── mosaic.pth ← Trained weights (mosaic style)
├── candy.pth ← Trained weights (candy style)
├── sketch.pth ← Trained weights (sketch style)
└── README.md ← This file