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-06-29 13:42:12 +00:00
|
|
|
from torch import LongTensor
|
2022-06-27 15:57:56 +00:00
|
|
|
import torch
|
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 14:17:29 +00:00
|
|
|
from .load_params import convert_and_save_torch_params
|
2022-06-30 10:43:10 +00:00
|
|
|
from .min_dalle_base import MinDalleBase
|
2022-06-27 19:46:04 +00:00
|
|
|
from .models.dalle_bart_encoder_torch import DalleBartEncoderTorch
|
|
|
|
from .models.dalle_bart_decoder_torch import DalleBartDecoderTorch
|
2022-07-01 14:17:29 +00:00
|
|
|
from .models.vqgan_detokenizer import VQGanDetokenizer
|
2022-06-27 15:57:56 +00:00
|
|
|
|
|
|
|
|
2022-06-30 10:43:10 +00:00
|
|
|
class MinDalleTorch(MinDalleBase):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
is_mega: bool,
|
2022-06-30 15:25:24 +00:00
|
|
|
is_reusable: bool = True,
|
2022-06-30 10:43:10 +00:00
|
|
|
token_count: int = 256
|
|
|
|
):
|
2022-06-30 18:54:08 +00:00
|
|
|
print("initializing MinDalleTorch")
|
2022-06-29 13:42:12 +00:00
|
|
|
super().__init__(is_mega)
|
2022-06-30 15:25:24 +00:00
|
|
|
self.is_reusable = is_reusable
|
2022-06-30 10:43:10 +00:00
|
|
|
self.token_count = token_count
|
2022-06-30 18:54:08 +00:00
|
|
|
|
|
|
|
self.encoder_params_path = os.path.join(self.model_path, 'encoder.pt')
|
|
|
|
self.decoder_params_path = os.path.join(self.model_path, 'decoder.pt')
|
2022-07-01 14:17:29 +00:00
|
|
|
self.detoker_params_path = os.path.join('pretrained', 'vqgan', 'detokenizer.pt')
|
2022-06-30 18:54:08 +00:00
|
|
|
|
|
|
|
is_converted = os.path.exists(self.encoder_params_path)
|
|
|
|
is_converted &= os.path.exists(self.decoder_params_path)
|
2022-07-01 14:17:29 +00:00
|
|
|
is_converted &= os.path.exists(self.detoker_params_path)
|
2022-06-30 18:54:08 +00:00
|
|
|
if not is_converted:
|
2022-06-30 20:50:04 +00:00
|
|
|
convert_and_save_torch_params(is_mega, self.model_path)
|
2022-06-30 18:54:08 +00:00
|
|
|
|
2022-06-30 15:25:24 +00:00
|
|
|
if is_reusable:
|
2022-06-30 10:43:10 +00:00
|
|
|
self.init_encoder()
|
|
|
|
self.init_decoder()
|
|
|
|
self.init_detokenizer()
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-06-30 10:43:10 +00:00
|
|
|
|
|
|
|
def init_encoder(self):
|
|
|
|
print("initializing DalleBartEncoderTorch")
|
2022-06-29 13:42:12 +00:00
|
|
|
self.encoder = DalleBartEncoderTorch(
|
|
|
|
layer_count = self.config['encoder_layers'],
|
|
|
|
embed_count = self.config['d_model'],
|
|
|
|
attention_head_count = self.config['encoder_attention_heads'],
|
|
|
|
text_vocab_count = self.config['encoder_vocab_size'],
|
|
|
|
text_token_count = self.config['max_text_length'],
|
|
|
|
glu_embed_count = self.config['encoder_ffn_dim']
|
|
|
|
)
|
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):
|
|
|
|
print("initializing DalleBartDecoderTorch")
|
2022-06-29 13:42:12 +00:00
|
|
|
self.decoder = DalleBartDecoderTorch(
|
|
|
|
image_vocab_size = self.config['image_vocab_size'],
|
|
|
|
image_token_count = self.config['image_length'],
|
2022-06-30 10:43:10 +00:00
|
|
|
sample_token_count = self.token_count,
|
2022-06-29 13:42:12 +00:00
|
|
|
embed_count = self.config['d_model'],
|
|
|
|
attention_head_count = self.config['decoder_attention_heads'],
|
|
|
|
glu_embed_count = self.config['decoder_ffn_dim'],
|
|
|
|
layer_count = self.config['decoder_layers'],
|
|
|
|
batch_count = 2,
|
|
|
|
start_token = self.config['decoder_start_token_id'],
|
|
|
|
is_verbose = True
|
|
|
|
)
|
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 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-06-30 10:43:10 +00:00
|
|
|
|
2022-06-27 15:57:56 +00:00
|
|
|
|
2022-06-29 13:42:12 +00:00
|
|
|
def generate_image_tokens(self, text: str, seed: int) -> LongTensor:
|
|
|
|
text_tokens = self.tokenize_text(text)
|
|
|
|
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
|