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.2.tar.gz (13.7 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.2-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jet_pytorch-0.0.2.tar.gz
  • Upload date:
  • Size: 13.7 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.2.tar.gz
Algorithm Hash digest
SHA256 7491252d95d713410e4ab34c58e9c1e3e84f252199e4c6ad0d16d4b0cf0f52c1
MD5 4e4066b92c37292121e9156708008f28
BLAKE2b-256 f99db88610821b6289fa1f4ae62b56f684fcff7724365275d5fb75b8c8058eb9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jet_pytorch-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 14.1 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c1dbacd02b2a22ae132a91c1952176d9a9d11a4f55fd3f2c90879ab82cac0013
MD5 290462c1756c19d61ddc0ef9e0639a30
BLAKE2b-256 51d0adb4e3b3a98be189a278907ddeb9a377e03c22509872c87652fc907523a5

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