2022-06-27 16:43:47 +00:00
|
|
|
import jax
|
|
|
|
import numpy
|
2022-06-29 13:42:12 +00:00
|
|
|
from PIL import Image
|
|
|
|
import torch
|
2022-06-27 16:43:47 +00:00
|
|
|
|
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_flax import DalleBartEncoderFlax
|
|
|
|
from .models.dalle_bart_decoder_flax import DalleBartDecoderFlax
|
2022-06-27 16:43:47 +00:00
|
|
|
|
2022-06-30 18:54:08 +00:00
|
|
|
from .load_params import load_dalle_bart_flax_params
|
|
|
|
|
2022-06-27 16:43:47 +00:00
|
|
|
|
2022-06-30 10:43:10 +00:00
|
|
|
class MinDalleFlax(MinDalleBase):
|
2022-06-30 15:25:24 +00:00
|
|
|
def __init__(self, is_mega: bool, is_reusable: bool = True):
|
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-29 13:42:12 +00:00
|
|
|
print("initializing MinDalleFlax")
|
2022-06-30 18:54:08 +00:00
|
|
|
self.model_params = load_dalle_bart_flax_params(self.model_path)
|
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-29 13:42:12 +00:00
|
|
|
|
2022-06-30 10:43:10 +00:00
|
|
|
|
|
|
|
def init_encoder(self):
|
|
|
|
print("initializing DalleBartEncoderFlax")
|
|
|
|
self.encoder: DalleBartEncoderFlax = DalleBartEncoderFlax(
|
2022-06-29 13:42:12 +00:00
|
|
|
attention_head_count = self.config['encoder_attention_heads'],
|
|
|
|
embed_count = self.config['d_model'],
|
|
|
|
glu_embed_count = self.config['encoder_ffn_dim'],
|
|
|
|
text_token_count = self.config['max_text_length'],
|
|
|
|
text_vocab_count = self.config['encoder_vocab_size'],
|
|
|
|
layer_count = self.config['encoder_layers']
|
2022-06-30 15:09:09 +00:00
|
|
|
).bind({'params': self.model_params.pop('encoder')})
|
2022-06-29 13:42:12 +00:00
|
|
|
|
2022-06-30 10:43:10 +00:00
|
|
|
|
|
|
|
def init_decoder(self):
|
|
|
|
print("initializing DalleBartDecoderFlax")
|
2022-06-29 13:42:12 +00:00
|
|
|
self.decoder = DalleBartDecoderFlax(
|
|
|
|
image_token_count = self.config['image_length'],
|
|
|
|
text_token_count = self.config['max_text_length'],
|
|
|
|
image_vocab_count = self.config['image_vocab_size'],
|
|
|
|
attention_head_count = self.config['decoder_attention_heads'],
|
|
|
|
embed_count = self.config['d_model'],
|
|
|
|
glu_embed_count = self.config['decoder_ffn_dim'],
|
|
|
|
layer_count = self.config['decoder_layers'],
|
|
|
|
start_token = self.config['decoder_start_token_id']
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_image(self, text: str, seed: int) -> Image.Image:
|
|
|
|
text_tokens = self.tokenize_text(text)
|
|
|
|
|
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(text_tokens)
|
2022-06-30 15:25:24 +00:00
|
|
|
if not self.is_reusable: del self.encoder
|
2022-06-29 13:42:12 +00:00
|
|
|
|
2022-06-30 15:25:24 +00:00
|
|
|
if not self.is_reusable:
|
2022-06-30 15:09:09 +00:00
|
|
|
self.init_decoder()
|
|
|
|
params = self.model_params.pop('decoder')
|
|
|
|
else:
|
|
|
|
params = self.model_params['decoder']
|
2022-06-29 13:42:12 +00:00
|
|
|
print("sampling image tokens")
|
|
|
|
image_tokens = self.decoder.sample_image_tokens(
|
|
|
|
text_tokens,
|
|
|
|
encoder_state,
|
|
|
|
jax.random.PRNGKey(seed),
|
2022-06-30 15:09:09 +00:00
|
|
|
params
|
2022-06-29 13:42:12 +00:00
|
|
|
)
|
2022-06-30 15:25:24 +00:00
|
|
|
if not self.is_reusable: del self.decoder
|
2022-06-29 13:42:12 +00:00
|
|
|
|
|
|
|
image_tokens = torch.tensor(numpy.array(image_tokens))
|
|
|
|
|
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
|