Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Adaptive Instance Normalization (AdaIN)

A concise, faithful PyTorch reproduction of Arbitrary Style Transfer in Real-time with Adaptive Instance Normalization by Huang and Belongie (ICCV 2017).

The method achieves real-time arbitrary style transfer by aligning the channel-wise mean and variance of content feature activations with those of an arbitrary style image at a deep VGG layer (relu4_1). A lightweight feed-forward decoder then inverts the normalized features back into pixel space. Content-style trade-offs are controlled continuously at test time via feature interpolation without retraining. The implementation is fully feed-forward, deterministic during inference, and runs efficiently on a GPU.

Examples

Arbitrary style transfer results evaluated on the canonical benchmark pairs from the original paper (Huang & Belongie, ICCV 2017), generated by our trained decoder network (decoder_160000.pth, $\alpha = 1.0$):

Style Reference
Content Image
Stylized
Impronte d'artista
Impronte d'artista
Avril
Avril
Stylized Avril
Avril × Impronte d'artista
Woman with a Hat
Woman with a Hat (Matisse)
Cornell
Cornell
Stylized Cornell
Cornell × Matisse
Asheville
Asheville
Chicago
Chicago
Stylized Chicago
Chicago × Asheville
Sketch
Sketch
Sailboat
Sailboat
Stylized Sailboat
Sailboat × Sketch
Goeritz
Goeritz
Modern Architecture
Modern Architecture
Stylized Modern
Modern × Goeritz
En Campo Gris
En Campo Gris
Lenna
Lenna
Stylized Lenna
Lenna × En Campo Gris

Installation

  1. Create and activate a Conda environment:
conda create -n adain python=3.12 -y
conda activate adain
  1. Install PyTorch with CUDA support matching your system (see pytorch.org):

  2. Install the remaining dependencies:

pip install -r requirements.txt

Usage

Run the command-line entry point with content and style images:

python stylize.py --content assets/content.jpg --style assets/style.jpg

Content-style trade-offs can be controlled continuously at test time using --alpha (default: 1.0):

Style Reference
Content Image
α = 0.3
α = 0.6
α = 1.0
Style reference Content input Stylized alpha 0.3 Stylized alpha 0.6 Stylized alpha 1.0

In general, use --alpha 0.5 to 0.7 for natural, recognizable results that preserve content structure, and increase --alpha up to 1.0 for heavy artistic abstraction.

Use --content-size 0 to preserve the original input resolution during inference.

Options

Argument Type Default Description
--content path required Source content image or directory of images.
--style path required Style reference image or directory of images.
--decoder path models/decoder.pth Path to trained decoder .pth or training checkpoint latest.pt.
--vgg path models/vgg_normalised.pth Path to normalized VGG-19 encoder weights.
--alpha float 1.0 Content-style interpolation factor in [0, 1]. 0.6 is recommended for balanced painterly results.
--content-size integer 512 Target short-side resolution for content images; 0 preserves original size.
--style-size integer 512 Target short-side resolution for style images; 0 preserves original size.
--output-dir path outputs/stylized Output directory where stylized images will be written.
--device string auto Execution device. Supports auto, cuda, cpu, and mps.
-h, --help flag - Show command-line help and exit.

The defaults reproduce the paper's primary inference configuration with relu4_1 feature transfer.

Training

We use Unsplash Lite for content images and ArtBench-10 for style images.

To train the decoder from scratch on unpaired content and style datasets:

# Standard training for 160,000 steps
python train.py --content-dir /path/to/content --style-dir /path/to/style

# Resume interrupted training from checkpoint
python train.py --resume outputs/runs/adain/latest.pt

Key training parameters configured in configs/train.json or overridden via CLI:

Parameter Type Default Description
--batch-size integer 16 Training batch size of unpaired content-style pairs.
--max-steps integer 160000 Total training iterations (matches the paper).
--learning-rate float 1e-4 Initial Adam learning rate with inverse time decay (5e-5).
--style-weight float 10.0 Relative weight for style loss $\mathcal{L}_s$ over content loss $\mathcal{L}_c$.
--resize-size integer 512 Short-side resize dimension before random cropping.
--crop-size integer 256 Square crop size for training patches.
--resume path None Path to latest.pt checkpoint to resume training state seamlessly.

Project Structure

.
|-- assets/
|   |-- examples/               # Benchmark style transfer examples from ICCV 2017
|   |-- readme/                 # Result images used by this README
|   |-- content.jpg             # Example input content image
|   `-- style.jpg               # Example input style image
|-- configs/
|   `-- train.json              # Training configuration and hyperparameters
|-- models/
|   |-- adain.py                # Adaptive Instance Normalization layer
|   |-- decoder.py              # Mirrored VGG reconstruction network
|   |-- decoder.pth             # Pretrained feed-forward decoder weights
|   |-- encoder.py              # Normalized VGG-19 feature extractor
|   `-- network.py              # Combined AdaIN style transfer network
|-- utils/
|   |-- checkpoint.py           # Atomic checkpoint saving and loading
|   |-- config.py               # Path resolution and config management
|   |-- dataset.py              # Unpaired dataset loaders and samplers
|   |-- image.py                # Image I/O and format conversion
|   |-- reproducibility.py      # Deterministic seed and state utilities
|   `-- transforms.py           # Aspect-preserving resize and crop transforms
|-- stylize.py                  # Public API and command-line entry point
|-- train.py                    # Complete training loop and evaluation
|-- requirements.txt
|-- LICENSE
`-- README.md

Reference

Xun Huang and Serge Belongie. Arbitrary Style Transfer in Real-time with Adaptive Instance Normalization. In IEEE International Conference on Computer Vision (ICCV), 2017. Paper

License

Released under the MIT License.