File size: 6,849 Bytes
9b0b32e ff09d94 af59832 ff09d94 9b0b32e ff09d94 0b16ac5 ff09d94 9b0b32e ff09d94 af59832 ff09d94 af59832 ff09d94 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
from PIL import Image
from base64 import b64encode
import os
import torch
from torch import autocast
from torch.nn import functional as F
from diffusers import StableDiffusionPipeline, AutoencoderKL
from diffusers import UNet2DConditionModel, PNDMScheduler, LMSDiscreteScheduler
from diffusers.schedulers.scheduling_ddim import DDIMScheduler
#from transformers import CLIPTextModel, CLIPTokenizer
from tqdm.auto import tqdm
from huggingface_hub import notebook_login
import torch.nn as nn
device = 'cpu'
from Multilingual_CLIP.multilingual_clip import Config_MCLIP
import transformers
import torch
class MultilingualCLIP(transformers.PreTrainedModel):
config_class = Config_MCLIP.MCLIPConfig
def __init__(self, config, *args, **kwargs):
super().__init__(config, *args, **kwargs)
self.transformer = transformers.AutoModel.from_pretrained(config.modelBase)
self.LinearTransformation = torch.nn.Linear(in_features=config.transformerDimensions,
out_features=config.numDims)
def forward(self, txt, tokenizer, device):
txt_tok = tokenizer(txt, padding='max_length', max_length=77, truncation=True, return_tensors='pt').to(device)
embs = self.transformer(**txt_tok)
embs = embs[0]
att = txt_tok['attention_mask']
embs = (embs * att.unsqueeze(2)) / att.sum(dim=1)[:, None].unsqueeze(2)
return self.LinearTransformation(embs)
@classmethod
def _load_state_dict_into_model(cls, model, state_dict, pretrained_model_name_or_path, _fast_init=True):
model.load_state_dict(state_dict)
return model, [], [], []
# Define the adaptation layer, 'checkpoint_9.pth'
class AdaptationLayer(nn.Module):
def __init__(self, input_dim, output_dim):
super(AdaptationLayer, self).__init__()
self.fc1 = nn.Linear(input_dim, output_dim*2)
torch.nn.init.kaiming_uniform_(self.fc1.weight, nonlinearity='relu')
self.bn1 = nn.BatchNorm1d(77)
self.fc2 = nn.Linear(input_dim*2, output_dim*2)
torch.nn.init.kaiming_uniform_(self.fc2.weight, nonlinearity='relu')
self.bn2 = nn.BatchNorm1d(77)
self.fc3 = nn.Linear(input_dim*2, output_dim)
torch.nn.init.kaiming_uniform_(self.fc3.weight, nonlinearity='relu')
self.bn3 = nn.BatchNorm1d(77)
self.fc4 = nn.Linear(input_dim, output_dim)
torch.nn.init.kaiming_uniform_(self.fc4.weight, nonlinearity='relu')
self.bn4 = nn.BatchNorm1d(77)
self.fc5 = nn.Linear(input_dim, output_dim)
def forward(self, x):
x = nn.functional.normalize(x, p=2.0, dim=1, eps=1e-12, out=None)
x = torch.relu(self.bn1(self.fc1(x)))
x = torch.relu(self.bn2(self.fc2(x)))
x = torch.relu(self.bn3(self.fc3(x)))
x = torch.relu(self.bn4(self.fc4(x)))
return self.fc5(x)
adapt_model = AdaptationLayer(768,768)
adapt_model.to(device)
state_dict = torch.load('weights/checkpoint_9.pth', map_location=torch.device('cpu'))
adapt_model.load_state_dict(state_dict)
from Multilingual_CLIP.multilingual_clip import pt_multilingual_clip
texts = [
'قطة تقرأ كتابا'
]
model_name = 'M-CLIP/LABSE-Vit-L-14'
# Load Model & Tokenizer
text_model = pt_multilingual_clip.MultilingualCLIP.from_pretrained(model_name)
text_model = text_model.to(device)
text_tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
embeddings= text_model.forward(texts, text_tokenizer, device )
# 1. Load the autoencoder model which will be used to decode the latents into image space.
vae = AutoencoderKL.from_pretrained(
'CompVis/stable-diffusion-v1-4', subfolder='vae', use_auth_token=True, token=os.environ['HF_API_TOKEN'])
vae = vae.to(device)
# 2. Load the tokenizer and text encoder to tokenize and encode the text.
tokenizer = text_tokenizer
text_encoder = text_model
# 3. The UNet model for generating the latents.
unet = UNet2DConditionModel.from_pretrained(
'CompVis/stable-diffusion-v1-4', subfolder='unet', use_auth_token=True, token=os.environ['HF_API_TOKEN'])
unet = unet.to(device)
# 4. Create a scheduler for inference
scheduler = LMSDiscreteScheduler(
beta_start=0.00085, beta_end=0.012,
beta_schedule='scaled_linear', num_train_timesteps=1000)
def get_text_embeds(prompt):
with torch.no_grad():
text_embeddings = text_model(prompt, text_tokenizer, device)
text_embeddings = adapt_model(text_embeddings)
# Do the same for unconditional embeddings
with torch.no_grad():
uncond_embeddings = text_model([''] * len(prompt), text_tokenizer, device)
uncond_embeddings = adapt_model(uncond_embeddings)
# Cat for final embeddings
text_embeddings = torch.cat([uncond_embeddings, text_embeddings])
return text_embeddings
def produce_latents(text_embeddings, height=512, width=512,
num_inference_steps=50, guidance_scale=7.5, latents=None):
if latents is None:
latents = torch.randn((text_embeddings.shape[0] // 2, unet.in_channels, \
height // 8, width // 8))
latents = latents.to(device)
scheduler.set_timesteps(num_inference_steps)
latents = latents * scheduler.sigmas[0]
with autocast('cpu'):
for i, t in tqdm(enumerate(scheduler.timesteps)):
# expand the latents if we are doing classifier-free guidance to avoid doing two forward passes.
latent_model_input = torch.cat([latents] * 2)
sigma = scheduler.sigmas[i]
latent_model_input = latent_model_input / ((sigma**2 + 1) ** 0.5)
# predict the noise residual
with torch.no_grad():
noise_pred = unet(latent_model_input, t, encoder_hidden_states=text_embeddings.to(device))['sample']
# perform guidance
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
# compute the previous noisy sample x_t -> x_t-1
latents = scheduler.step(noise_pred, i, latents)['prev_sample']
return latents
def decode_img_latents(latents):
latents = 1 / 0.18215 * latents
with torch.no_grad():
imgs = vae.decode(latents)
imgs = (imgs / 2 + 0.5).clamp(0, 1)
imgs = imgs.detach().cpu().permute(0, 2, 3, 1).numpy()
imgs = (imgs * 255).round().astype('uint8')
pil_images = [Image.fromarray(image) for image in imgs]
return pil_images
def prompt_to_img(prompts, height=512, width=512, num_inference_steps=50,
guidance_scale=7.5, latents=None):
if isinstance(prompts, str):
prompts = [prompts]
# Prompts -> text embeds
text_embeds = get_text_embeds(prompts)
# Text embeds -> img latents
latents = produce_latents(
text_embeds, height=height, width=width, latents=latents,
num_inference_steps=num_inference_steps, guidance_scale=guidance_scale)
# Img latents -> imgs
imgs = decode_img_latents(latents)
return imgs
|