2022-06-29 02:22:54 +00:00
|
|
|
import os
|
2022-06-29 13:42:12 +00:00
|
|
|
from PIL import Image
|
2022-06-28 16:16:44 +00:00
|
|
|
from typing import Dict
|
2022-07-01 18:06:50 +00:00
|
|
|
import numpy
|
2022-06-29 13:42:12 +00:00
|
|
|
from torch import LongTensor
|
2022-06-27 15:57:56 +00:00
|
|
|
import torch
|
2022-07-01 18:06:50 +00:00
|
|
|
import json
|
2022-07-01 22:16:55 +00:00
|
|
|
import requests
|
2022-06-29 01:28:36 +00:00
|
|
|
torch.set_grad_enabled(False)
|
2022-06-29 02:22:54 +00:00
|
|
|
torch.set_num_threads(os.cpu_count())
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-07-01 22:16:55 +00:00
|
|
|
MIN_DALLE_REPO = 'https://huggingface.co/kuprel/min-dalle/resolve/main/'
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-07-01 22:16:55 +00:00
|
|
|
from .text_tokenizer import TextTokenizer
|
|
|
|
from .models import (
|
|
|
|
DalleBartEncoderTorch,
|
|
|
|
DalleBartDecoderTorch,
|
|
|
|
VQGanDetokenizer
|
|
|
|
)
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-07-01 18:06:50 +00:00
|
|
|
class MinDalleTorch:
|
2022-06-30 10:43:10 +00:00
|
|
|
def __init__(
|
2022-07-01 22:16:55 +00:00
|
|
|
self,
|
2022-06-30 10:43:10 +00:00
|
|
|
is_mega: bool,
|
2022-06-30 15:25:24 +00:00
|
|
|
is_reusable: bool = True,
|
2022-07-01 22:16:55 +00:00
|
|
|
models_root: str = 'pretrained',
|
2022-07-01 19:53:39 +00:00
|
|
|
sample_token_count: int = 256
|
2022-06-30 10:43:10 +00:00
|
|
|
):
|
2022-06-30 18:54:08 +00:00
|
|
|
print("initializing MinDalleTorch")
|
2022-07-01 22:16:55 +00:00
|
|
|
self.is_mega = is_mega
|
2022-07-01 19:53:39 +00:00
|
|
|
self.is_reusable = is_reusable
|
|
|
|
self.sample_token_count = sample_token_count
|
|
|
|
self.batch_count = 2
|
|
|
|
self.text_token_count = 64
|
|
|
|
self.image_token_count = 256
|
|
|
|
self.layer_count = 24 if is_mega else 12
|
|
|
|
self.attention_head_count = 32 if is_mega else 16
|
|
|
|
self.embed_count = 2048 if is_mega else 1024
|
|
|
|
self.glu_embed_count = 4096 if is_mega else 2730
|
|
|
|
self.text_vocab_count = 50272 if is_mega else 50264
|
|
|
|
self.image_vocab_count = 16415 if is_mega else 16384
|
|
|
|
|
2022-07-01 18:06:50 +00:00
|
|
|
model_name = 'dalle_bart_{}'.format('mega' if is_mega else 'mini')
|
2022-07-01 22:16:55 +00:00
|
|
|
dalle_path = os.path.join(models_root, model_name)
|
|
|
|
vqgan_path = os.path.join(models_root, 'vqgan')
|
|
|
|
if not os.path.exists(dalle_path): os.makedirs(dalle_path)
|
|
|
|
if not os.path.exists(vqgan_path): os.makedirs(vqgan_path)
|
|
|
|
self.vocab_path = os.path.join(dalle_path, 'vocab.json')
|
|
|
|
self.merges_path = os.path.join(dalle_path, 'merges.txt')
|
|
|
|
self.encoder_params_path = os.path.join(dalle_path, 'encoder.pt')
|
|
|
|
self.decoder_params_path = os.path.join(dalle_path, 'decoder.pt')
|
|
|
|
self.detoker_params_path = os.path.join(vqgan_path, 'detoker.pt')
|
2022-07-01 19:53:39 +00:00
|
|
|
|
|
|
|
self.init_tokenizer()
|
|
|
|
if is_reusable:
|
|
|
|
self.init_encoder()
|
|
|
|
self.init_decoder()
|
|
|
|
self.init_detokenizer()
|
2022-07-01 18:06:50 +00:00
|
|
|
|
2022-07-01 22:16:55 +00:00
|
|
|
|
|
|
|
def download_tokenizer(self):
|
|
|
|
print("downloading tokenizer params")
|
|
|
|
suffix = '' if self.is_mega else '_mini'
|
|
|
|
vocab = requests.get(MIN_DALLE_REPO + 'vocab{}.json'.format(suffix))
|
|
|
|
merges = requests.get(MIN_DALLE_REPO + 'merges{}.txt'.format(suffix))
|
|
|
|
with open(self.vocab_path, 'wb') as f: f.write(vocab.content)
|
|
|
|
with open(self.merges_path, 'wb') as f: f.write(merges.content)
|
|
|
|
|
|
|
|
|
|
|
|
def download_encoder(self):
|
|
|
|
print("downloading encoder params")
|
|
|
|
suffix = '' if self.is_mega else '_mini'
|
|
|
|
params = requests.get(MIN_DALLE_REPO + 'encoder{}.pt'.format(suffix))
|
|
|
|
with open(self.encoder_params_path, 'wb') as f: f.write(params.content)
|
|
|
|
|
|
|
|
|
|
|
|
def download_decoder(self):
|
|
|
|
print("downloading decoder params")
|
|
|
|
suffix = '' if self.is_mega else '_mini'
|
|
|
|
params = requests.get(MIN_DALLE_REPO + 'decoder{}.pt'.format(suffix))
|
|
|
|
with open(self.decoder_params_path, 'wb') as f: f.write(params.content)
|
|
|
|
|
|
|
|
|
|
|
|
def download_detokenizer(self):
|
|
|
|
print("downloading detokenizer params")
|
|
|
|
params = requests.get(MIN_DALLE_REPO + 'detoker.pt')
|
|
|
|
with open(self.detoker_params_path, 'wb') as f: f.write(params.content)
|
|
|
|
|
|
|
|
|
2022-07-01 19:53:39 +00:00
|
|
|
def init_tokenizer(self):
|
2022-07-01 22:16:55 +00:00
|
|
|
is_downloaded = os.path.exists(self.vocab_path)
|
|
|
|
is_downloaded &= os.path.exists(self.merges_path)
|
|
|
|
if not is_downloaded: self.download_tokenizer()
|
|
|
|
print("intializing TextTokenizer")
|
|
|
|
with open(self.vocab_path, 'r', encoding='utf8') as f:
|
2022-07-01 18:06:50 +00:00
|
|
|
vocab = json.load(f)
|
2022-07-01 22:16:55 +00:00
|
|
|
with open(self.merges_path, 'r', encoding='utf8') as f:
|
2022-07-01 18:06:50 +00:00
|
|
|
merges = f.read().split("\n")[1:-1]
|
|
|
|
self.tokenizer = TextTokenizer(vocab, merges)
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-06-30 10:43:10 +00:00
|
|
|
|
|
|
|
def init_encoder(self):
|
2022-07-01 22:16:55 +00:00
|
|
|
is_downloaded = os.path.exists(self.encoder_params_path)
|
|
|
|
if not is_downloaded: self.download_encoder()
|
2022-06-30 10:43:10 +00:00
|
|
|
print("initializing DalleBartEncoderTorch")
|
2022-06-29 13:42:12 +00:00
|
|
|
self.encoder = DalleBartEncoderTorch(
|
2022-07-01 19:53:39 +00:00
|
|
|
attention_head_count = self.attention_head_count,
|
|
|
|
embed_count = self.embed_count,
|
|
|
|
glu_embed_count = self.glu_embed_count,
|
|
|
|
text_token_count = self.text_token_count,
|
|
|
|
text_vocab_count = self.text_vocab_count,
|
|
|
|
layer_count = self.layer_count
|
2022-06-29 13:42:12 +00:00
|
|
|
)
|
2022-06-30 18:54:08 +00:00
|
|
|
params = torch.load(self.encoder_params_path)
|
2022-06-30 10:43:10 +00:00
|
|
|
self.encoder.load_state_dict(params, strict=False)
|
2022-06-30 15:09:09 +00:00
|
|
|
del params
|
2022-06-30 15:44:36 +00:00
|
|
|
if torch.cuda.is_available(): self.encoder = self.encoder.cuda()
|
2022-06-30 10:43:10 +00:00
|
|
|
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-06-30 10:43:10 +00:00
|
|
|
def init_decoder(self):
|
2022-07-01 22:16:55 +00:00
|
|
|
is_downloaded = os.path.exists(self.decoder_params_path)
|
|
|
|
if not is_downloaded: self.download_decoder()
|
2022-06-30 10:43:10 +00:00
|
|
|
print("initializing DalleBartDecoderTorch")
|
2022-06-29 13:42:12 +00:00
|
|
|
self.decoder = DalleBartDecoderTorch(
|
2022-07-01 19:53:39 +00:00
|
|
|
sample_token_count = self.sample_token_count,
|
|
|
|
image_token_count = self.image_token_count,
|
|
|
|
image_vocab_count = self.image_vocab_count,
|
|
|
|
attention_head_count = self.attention_head_count,
|
|
|
|
embed_count = self.embed_count,
|
|
|
|
glu_embed_count = self.glu_embed_count,
|
|
|
|
layer_count = self.layer_count,
|
|
|
|
start_token = self.image_vocab_count,
|
|
|
|
batch_count = self.batch_count
|
2022-06-29 13:42:12 +00:00
|
|
|
)
|
2022-06-30 18:54:08 +00:00
|
|
|
params = torch.load(self.decoder_params_path)
|
2022-06-30 10:43:10 +00:00
|
|
|
self.decoder.load_state_dict(params, strict=False)
|
2022-06-30 15:09:09 +00:00
|
|
|
del params
|
2022-06-30 15:44:36 +00:00
|
|
|
if torch.cuda.is_available(): self.decoder = self.decoder.cuda()
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-07-01 14:17:29 +00:00
|
|
|
|
2022-06-30 10:43:10 +00:00
|
|
|
def init_detokenizer(self):
|
2022-07-01 22:16:55 +00:00
|
|
|
is_downloaded = os.path.exists(self.detoker_params_path)
|
|
|
|
if not is_downloaded: self.download_detokenizer()
|
2022-07-01 14:17:29 +00:00
|
|
|
print("initializing VQGanDetokenizer")
|
|
|
|
self.detokenizer = VQGanDetokenizer()
|
|
|
|
params = torch.load(self.detoker_params_path)
|
|
|
|
self.detokenizer.load_state_dict(params)
|
|
|
|
del params
|
|
|
|
if torch.cuda.is_available(): self.detokenizer = self.detokenizer.cuda()
|
2022-07-01 18:06:50 +00:00
|
|
|
|
|
|
|
|
2022-07-01 19:53:39 +00:00
|
|
|
def generate_image_tokens(self, text: str, seed: int) -> LongTensor:
|
2022-07-01 18:06:50 +00:00
|
|
|
print("tokenizing text")
|
|
|
|
tokens = self.tokenizer.tokenize(text)
|
|
|
|
print("text tokens", tokens)
|
|
|
|
text_tokens = numpy.ones((2, 64), dtype=numpy.int32)
|
|
|
|
text_tokens[0, :2] = [tokens[0], tokens[-1]]
|
|
|
|
text_tokens[1, :len(tokens)] = tokens
|
|
|
|
|
2022-06-29 13:42:12 +00:00
|
|
|
text_tokens = torch.tensor(text_tokens).to(torch.long)
|
|
|
|
if torch.cuda.is_available(): text_tokens = text_tokens.cuda()
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-06-30 15:25:24 +00:00
|
|
|
if not self.is_reusable: self.init_encoder()
|
2022-06-29 13:42:12 +00:00
|
|
|
print("encoding text tokens")
|
|
|
|
encoder_state = self.encoder.forward(text_tokens)
|
2022-06-30 15:25:24 +00:00
|
|
|
if not self.is_reusable: del self.encoder
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-06-30 15:25:24 +00:00
|
|
|
if not self.is_reusable: self.init_decoder()
|
2022-06-29 13:42:12 +00:00
|
|
|
print("sampling image tokens")
|
|
|
|
torch.manual_seed(seed)
|
|
|
|
image_tokens = self.decoder.forward(text_tokens, encoder_state)
|
2022-06-30 15:25:24 +00:00
|
|
|
if not self.is_reusable: del self.decoder
|
2022-06-29 13:42:12 +00:00
|
|
|
return image_tokens
|
|
|
|
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-06-29 13:42:12 +00:00
|
|
|
def generate_image(self, text: str, seed: int) -> Image.Image:
|
|
|
|
image_tokens = self.generate_image_tokens(text, seed)
|
2022-06-30 15:25:24 +00:00
|
|
|
if not self.is_reusable: self.init_detokenizer()
|
2022-06-29 13:42:12 +00:00
|
|
|
print("detokenizing image")
|
|
|
|
image = self.detokenizer.forward(image_tokens).to(torch.uint8)
|
2022-06-30 15:25:24 +00:00
|
|
|
if not self.is_reusable: del self.detokenizer
|
2022-06-29 13:42:12 +00:00
|
|
|
image = Image.fromarray(image.to('cpu').detach().numpy())
|
|
|
|
return image
|