Packaging of Open-source replication of Google's MAGVIT-v2 tokenizer
Project description
OPEN-MAGVIT2 package
This is a fork of OPEN-MAGVIT2: An Open-source Project Toward Democratizing Auto-Regressive Visual Generation in order to make it a package for easy usage.
Install
pip install open-magvit2
Example of usage
1. Download the checkpoint from huggingface
wget https://huggingface.co/TencentARC/Open-MAGVIT2/resolve/main/imagenet_256_L.ckpt
2. Load the model
import pkg_resources
import torch
from omegaconf import OmegaConf
from open_magvit2.reconstruct import load_vqgan_new
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
config_path = pkg_resources.resource_filename('open_magvit2', 'configs/gpu/imagenet_lfqgan_256_L.yaml')
config = OmegaConf.load(config_path)
model = load_vqgan_new(config, "imagenet_256_L.ckpt").to(DEVICE)
3. Encode an image
from PIL import Image
import torchvision.transforms as transforms
image = Image.open('1165.jpg')
image_tensor = transforms.ToTensor()(image)
batch = image_tensor.unsqueeze(0)
with torch.no_grad():
quant, emb_loss, tokens, loss_breakdown = model.encode(image_tensor)
4. Decode
- decode from embeddings
from open_magvit2.reconstruct import custom_to_pil
with torch.no_grad():
tensor = model.decode(quant)
reconstructed_image = custom_to_pil(tensor[0])
- decode from tokens (i.e. ids)
from einops import rearrange
from open_magvit2.reconstruct import custom_to_pil
x = rearrange(tokens, "(b s) -> b s", b=1)
q = model.quantize.get_codebook_entry(x, (1, 16, 16, 18), order='')
with torch.no_grad():
tensor2 = model.decode(q)
reconstructed_image2 = custom_to_pil(tensor2[0])
Example colab
Check this notebook open-MAGVIT2-package-inference-example.ipynb
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
open_magvit2-1.1.tar.gz
(59.8 kB
view details)
Built Distribution
File details
Details for the file open_magvit2-1.1.tar.gz
.
File metadata
- Download URL: open_magvit2-1.1.tar.gz
- Upload date:
- Size: 59.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e51a2a1ecf10ea8b3de668a0d95630783e821bd8c9db83dc41f5eafe39b846e3 |
|
MD5 | 8b4d0eb12563d506aaa134bceee66de9 |
|
BLAKE2b-256 | 183b74a06efbd48e1ecdd0c7fb4b5a97c566621e9e2e432deb0a977a197ba732 |
File details
Details for the file open_magvit2-1.1-py3-none-any.whl
.
File metadata
- Download URL: open_magvit2-1.1-py3-none-any.whl
- Upload date:
- Size: 83.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af0918af2691247665bbafc9908ced29dd2928cf449d7bb223c24d437256e6dc |
|
MD5 | 996e74fe03772574a56234970adfe5c5 |
|
BLAKE2b-256 | 846faebec821a1462f50289a7f6ba53b79dfa52b40f8fcaeb71b748c3657b93c |