Skip to main content

A Pytorch port of the code from "Jet: A Modern Transformer-Based Normalizing Flow"

Project description

Jet PyTorch

This repo contains a PyTorch port of the code from Jet: A Modern Transformer-Based Normalizing Flow which was originally written in Jax and included as part of big_vision.

A checkpoint pretrained on downsampled imagenet 64x64 is provided via the hugging face hub as well as barebones single-GPU training and inference scripts.

Installation

The code in this repo requires a CUDA-capable GPU which can perform operations at bfloat16 precision.

pip install jet-pytorch

The models from the original paper/this reproduction are trained using downsampled imagenet which is availabe from a few different sources, including Kaggle. The ImageDataset provided in this repository does not load/support labels and does not require any specific directory structure.

Usage

from jet_pytorch import Jet

jet_config = dict(
    patch_size=4,
    patch_dim=48,
    n_patches=256,
    coupling_layers=32,
    block_depth=2,
    block_width=512,
    num_heads=8,
    scale_factor=2.0,
    coupling_types=(
        "channels", "channels",
        "channels", "channels",
        "spatial",
    ),
    spatial_coupling_projs=(
        "checkerboard", "checkerboard-inv",
        "vstripes", "vstripes-inv",
        "hstripes", "hstripes-inv",
    )
)
model = Jet(**jet_config)

This is the default Jet configuration and that which matches the pretrained weights available on the huggingface hub

Download and/or load pretrained imagenet 64x64 weights

from jet_pytorch.util import get_pretrained

weights = get_pretrained()
model.load_state_dict(weights)

Sample from a Jet

from torch.distributions import Normal

batch_size = 16
n_patches = 256
patch_dim = 48
pdf = Normal(0, 1)
z = pdf.sample((batch_size, n_patches, patch_dim))
img, logdet = model.inverse(z)

Training a Jet

from jet_pytorch.train import train

jet_config = dict(...)
train(
    jet_config=jet_config,
    batch_size=64,
    accumulate_steps=16,
    device="cuda:0",
    epochs=50,
    warmup_percentage=0.1,
    max_grad_norm=1.0,
    learning_rate=3e-4,
    weight_decay=1e-5,
    adam_betas=(0.9, 0.95),
    images_path_train="/path/to/train/images",
    images_path_valid="/path/to/validation/images",
    num_workers=8,
    checkpoint_path="jet.pt",
)

The training code favors gradient accumulation over alternatives like gradient checkpointing, allowing this script to run on GPUs with less than 8GB of VRAM. The true batch size is thus equal to batch_size * accumulate_steps. Note that the default configuration assumes at least 24GB of VRAM.

Create visualizations

from jet_pytorch.sample import sample

# Creates visualization using the default Jet config/pretrained weights
sample("path/to/your/images")

# Creates visualization using default Jet config/a local checkpoint
sample(
    "path/to/your/images",
    checkpoint_path="path/to/your/checkpoint.pt",
)

# Creates visualization using custom Jet config/a local checkpoint
jet_config = dict(...)
sample(
    "path/to/your/images",
    jet_config=jet_config,
    checkpoint_path="path/to/your/checkpoint.pt",
)

Visualization results are stored at output/jet.png

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jet_pytorch-0.0.1.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jet_pytorch-0.0.1-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file jet_pytorch-0.0.1.tar.gz.

File metadata

  • Download URL: jet_pytorch-0.0.1.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for jet_pytorch-0.0.1.tar.gz
Algorithm Hash digest
SHA256 782027a1f86b323c8688d78383aad7fb81a41be9990387aed8eb31d5224317ee
MD5 825e2ab1bd284169da008b1223f59c4c
BLAKE2b-256 e563c93a9a26b27f18e1015e4e5223a79c4de90b791d98cf995a1b45a3ef7465

See more details on using hashes here.

File details

Details for the file jet_pytorch-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: jet_pytorch-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for jet_pytorch-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9ed3a42a96a1b7cc4f71b07cfafbed997d9c1a924665b418226805e884cc33b7
MD5 7bae58846de06ed9727147288b1ee499
BLAKE2b-256 92a956c3ff67fd15e83510e6621a5aad00023b45255f91ec195321d1335b018b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page