End-to-end repository for training, optimizing, and deploying neural style transfer models for real-time AR experiences on iOS and visionOS devices.
This repository provides a complete pipeline for:
- Dataset Preparation: Download and preprocess training datasets (COCO, WikiArt, custom)
- Model Training: PyTorch and TensorFlow implementations of fast neural style transfer
- Core ML Conversion: Export models to Apple's Core ML format with optimization
- Validation & Benchmarking: Test performance, accuracy, and visual quality
- Deployment Manifest: Generate metadata for seamless app integration
Key Features:
- β Dual framework support (PyTorch & TensorFlow)
- β Optimized for Apple Neural Engine
- β Configurable quantization and compute units
- β Real-time inference (<50ms on modern devices)
- β StyleDescriptor manifest for easy app integration
- β Comprehensive validation and testing tools
style-transfer-models/
βββ datasets/ # Dataset management
β βββ scripts/
β β βββ download_prepare.py
β βββ sample/ # Sample images for testing
β βββ README.md
βββ training/ # Training pipelines
β βββ pytorch/
β β βββ train.py
β β βββ model.py
β β βββ utils.py
β βββ tensorflow/
β β βββ train.py
β β βββ model.py
β β βββ utils.py
β βββ README.md
βββ conversion/ # Core ML conversion
β βββ convert_coreml.py
β βββ validate_coreml.ipynb
β βββ README.md
βββ models/ # Trained models
β βββ exported/ # .mlmodel files
β βββ checkpoints/ # Training checkpoints
βββ manifest/ # StyleDescriptor manifest
β βββ styles.json
β βββ styles_schema.json
β βββ generate_manifest.py
βββ tests/ # Unit tests
βββ requirements.txt # Python dependencies
βββ LICENSE # MIT License
βββ README.md # This file
- Python 3.9 or higher
- macOS (recommended for Core ML conversion)
- 8GB+ RAM
- GPU (optional but recommended for training)
- Clone the repository:
git clone https://github.com/yourusername/style-transfer-models.git
cd style-transfer-models- Create a virtual environment:
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txtFor Apple Silicon Macs, TensorFlow Metal acceleration will be automatically installed.
Train a style transfer model with sample data:
# Prepare sample dataset
cd datasets/scripts
python download_prepare.py \
--dataset custom \
--source-dir ../sample/content \
--output-dir ../processed/sample
# Train PyTorch model (2 epochs, fast)
cd ../../training/pytorch
python train.py \
--content-dir ../../datasets/processed/sample/train \
--style-image ../../datasets/sample/styles/sci-fi.jpg \
--style-name sci-fi \
--epochs 2 \
--batch-size 4
# Convert to Core ML
cd ../../conversion
python convert_coreml.py \
--framework pytorch \
--model-path ../models/checkpoints/sci-fi_best.pth \
--output-path ../models/exported/sci-fi.mlmodel \
--style-name sci-fi \
--quantizecd datasets/scripts
python download_prepare.py --dataset coco --data-dir ../This downloads the COCO 2017 training set (~18GB).
python download_prepare.py \
--dataset custom \
--source-dir ../coco/train2017 \
--output-dir ../processed/coco_256 \
--target-size 256 256 \
--split-ratios 0.8 0.1 0.1Organize your style images by category:
datasets/wikiart_styles/
βββ sci-fi/
β βββ style_001.jpg
β βββ style_002.jpg
βββ fantasy/
β βββ style_001.jpg
βββ modern/
βββ style_001.jpg
Process them:
python download_prepare.py \
--dataset custom \
--source-dir ../wikiart_styles \
--output-dir ../processed/styles \
--prepare-stylesSee datasets/README.md for more details.
cd training/pytorch
python train.py \
--content-dir ../../datasets/processed/coco_256/train \
--val-dir ../../datasets/processed/coco_256/val \
--style-image ../../datasets/processed/styles/sci-fi/sci-fi_001.jpg \
--style-name sci-fi \
--epochs 10 \
--batch-size 8 \
--image-size 256 \
--lr 1e-3 \
--content-weight 1.0 \
--style-weight 1e5 \
--tv-weight 1e-6 \
--save-interval 2Key Arguments:
--content-weight: Controls content preservation (default: 1.0)--style-weight: Controls style strength (default: 1e5)--tv-weight: Total variation loss for smoothness (default: 1e-6)--lr: Learning rate (default: 1e-3)
cd training/tensorflow
python train.py \
--content-dir ../../datasets/processed/coco_256/train \
--val-dir ../../datasets/processed/coco_256/val \
--style-image ../../datasets/processed/styles/fantasy/fantasy_001.jpg \
--style-name fantasy \
--epochs 10 \
--batch-size 8 \
--image-size 256Performance Tips:
- Use
--num-workers 4for faster data loading - Enable
--lr-schedulerfor better convergence - Use
--early-stoppingto prevent overfitting - Reduce
--batch-sizeif running out of memory
See training/README.md for advanced options.
cd conversion
python convert_coreml.py \
--framework pytorch \
--model-path ../models/checkpoints/sci-fi_best.pth \
--output-path ../models/exported/sci-fi.mlmodel \
--style-name sci-fi \
--input-size 256 256 \
--compute-units ALL \
--quantizepython convert_coreml.py \
--framework tensorflow \
--model-path ../models/checkpoints/fantasy_best.h5 \
--output-path ../models/exported/fantasy.mlmodel \
--style-name fantasy \
--input-size 256 256 \
--compute-units CPU_AND_NE \
--quantizeCompute Units Options:
ALL: CPU, GPU, and Neural Engine (recommended)CPU_AND_GPU: CPU and GPU onlyCPU_AND_NE: CPU and Neural Engine (best for modern devices)CPU_ONLY: CPU only (slowest)
Quantization:
--quantize: Apply float16 quantization (~50% size reduction, minimal quality loss)
Open the Jupyter notebook for interactive validation:
cd conversion
jupyter notebook validate_coreml.ipynbThe notebook provides:
- Model metadata inspection
- Inference testing with sample images
- Performance benchmarking (FPS, latency)
- Visual quality assessment
- Automated validation reports
See conversion/README.md for details.
Create the styles.json manifest for app integration:
cd manifest
# Generate from exported models
python generate_manifest.py generate \
--models-dir ../models/exported \
--output styles.json \
--version 1.0.0
# Validate manifest
python generate_manifest.py validate \
--manifest styles.json \
--schema styles_schema.jsonThe manifest maps each model to a StyleDescriptor with:
- Model path and metadata
- Input/output specifications
- Performance metrics
- Recommended compute units
Typical performance on Apple devices (256x256 input):
| Device | Neural Engine | Avg Latency | FPS | Notes |
|---|---|---|---|---|
| iPhone 15 Pro | Yes | ~35ms | 28 | Quantized, ALL compute units |
| iPhone 14 | Yes | ~45ms | 22 | Quantized, ALL compute units |
| iPad Pro M2 | Yes | ~30ms | 33 | Quantized, CPU_AND_NE |
| Vision Pro | Yes | ~40ms | 25 | Real-time AR capable |
| Mac M1/M2 | Yes | ~25ms | 40 | Desktop testing |
Benchmarks with float16 quantization and optimized compute units.
The generated manifest/styles.json provides a standardized interface for your iOS/visionOS app:
{
"id": "sci-fi",
"displayName": "Sci-Fi",
"description": "Futuristic aesthetic...",
"category": "sci-fi",
"modelPath": "../models/exported/sci-fi.mlmodel",
"inputSize": {"width": 256, "height": 256},
"computeUnits": "ALL",
"performance": {
"avgInferenceMs": 45.0,
"fps": 22.0
},
"metadata": {
"framework": "pytorch",
"quantized": true
}
}App Integration Example (Swift):
struct StyleDescriptor: Codable {
let id: String
let displayName: String
let modelPath: String
let inputSize: ImageSize
let computeUnits: String
// ... other fields
}
// Load manifest
let manifestURL = Bundle.main.url(forResource: "styles", withExtension: "json")
let manifest = try JSONDecoder().decode(StyleManifest.self, from: Data(contentsOf: manifestURL))
// Use styles in your app
for style in manifest.styles {
let model = try MLModel(contentsOf: URL(fileURLWithPath: style.modelPath))
// Apply style transfer...
}Run unit tests:
pytest tests/ -v --cov=.Test coverage includes:
- Dataset loading and preprocessing
- Model architecture validation
- Conversion pipeline integrity
- Manifest generation and validation
- Input Size: 256x256 is optimal for real-time performance
- Quantization: Always use float16 quantization
- Compute Units: Use
ALLorCPU_AND_NE - Model Size: Target <10MB for quick loading
- Input Size: 512x512 or higher
- Quantization: Optional (sacrifices size for quality)
- Training Epochs: 10-20 epochs
- Style Weight: Experiment with 1e4 to 1e6
- Batch Size: Maximize based on GPU memory
- Learning Rate: Start with 1e-3, use scheduler
- Validation: Use smaller validation set
- Checkpointing: Save every 2-5 epochs
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-style) - Commit your changes (
git commit -m 'Add amazing style') - Push to the branch (
git push origin feature/amazing-style) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- COCO Dataset: CC BY 4.0
- VGG Weights: CC BY
- Style Transfer Architecture: Based on Johnson et al. (2016), MIT License
See LICENSE for complete attribution details.
- Perceptual Losses for Real-Time Style Transfer (Johnson et al., 2016)
- A Neural Algorithm of Artistic Style (Gatys et al., 2015)
Out of Memory During Training:
# Reduce batch size
python train.py --batch-size 2 ...
# Reduce image size
python train.py --image-size 128 ...Slow Training:
# Use more workers
python train.py --num-workers 8 ...
# Enable GPU
python train.py --device cuda ...Core ML Conversion Errors:
# Make sure coremltools is up to date
pip install --upgrade coremltools
# Try different compute units
python convert_coreml.py --compute-units CPU_ONLY ...- Open an issue
- Check existing discussions
- Review FAQ
For questions, feature requests, or collaboration:
- GitHub Issues: Create an issue
- Email: your.email@example.com
Made with β€οΈ for the AR/ML community
β Star this repo if you find it useful!