File size: 7,932 Bytes
3ab16a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import torch
from torch import nn
from einops import rearrange
import numpy as np
from typing import List
from models.id_embedding.helpers import get_rep_pos, shift_tensor_dim0
from models.id_embedding.meta_net import StyleVectorizer
from models.celeb_embeddings import _get_celeb_embeddings_basis

from functools import partial
import torch.nn.functional as F
import torch.nn as nn
import torch.nn.init as init


DEFAULT_PLACEHOLDER_TOKEN = ["*"]

PROGRESSIVE_SCALE = 2000

def get_clip_token_for_string(tokenizer, string):
    batch_encoding = tokenizer(string, return_length=True, padding=True, truncation=True, return_overflowing_tokens=False, return_tensors="pt")
    tokens = batch_encoding["input_ids"]
    
    return tokens 


def get_embedding_for_clip_token(embedder, token):
    return embedder(token.unsqueeze(0))


class EmbeddingManagerId_adain(nn.Module):
    def __init__(
            self,
            tokenizer,
            text_encoder,
            device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu"),  
            experiment_name = "normal_GAN",                      
            num_embeds_per_token: int = 2,  
            loss_type: str = None,
            mlp_depth: int = 2,    
            token_dim: int = 1024,   
            input_dim: int = 1024, 
            **kwargs
    ):
        super().__init__()
        self.device = device
        self.num_es = num_embeds_per_token

        self.get_token_for_string = partial(get_clip_token_for_string, tokenizer)        
        self.get_embedding_for_tkn = partial(get_embedding_for_clip_token, text_encoder.text_model.embeddings)  
        

        self.token_dim = token_dim

        ''' 1. Placeholder mapping dicts '''
        self.placeholder_token = self.get_token_for_string("*")[0][1]    
        
        if experiment_name == "normal_GAN":
            self.celeb_embeddings_mean, self.celeb_embeddings_std = _get_celeb_embeddings_basis(tokenizer, text_encoder, "datasets_face/good_names.txt")
        elif experiment_name == "man_GAN":
            self.celeb_embeddings_mean, self.celeb_embeddings_std = _get_celeb_embeddings_basis(tokenizer, text_encoder, "datasets_face/good_names_man.txt")
        elif experiment_name == "woman_GAN":            
            self.celeb_embeddings_mean, self.celeb_embeddings_std = _get_celeb_embeddings_basis(tokenizer, text_encoder, "datasets_face/good_names_woman.txt")
        else:
            print("Hello, please notice this ^_^")
            assert 0
        print("now experiment_name:", experiment_name)
        
        self.celeb_embeddings_mean = self.celeb_embeddings_mean.to(device)   
        self.celeb_embeddings_std = self.celeb_embeddings_std.to(device)  

        self.name_projection_layer = StyleVectorizer(input_dim, self.token_dim * self.num_es, depth=mlp_depth, lr_mul=0.1) 
        self.embedding_discriminator = Embedding_discriminator(self.token_dim * self.num_es, dropout_rate = 0.2)

        self.adain_mode = 0
        
    def forward(
            self,
            tokenized_text, 
            embedded_text, 
            name_batch,
            random_embeddings = None,
            timesteps = None,
    ):
        
        if tokenized_text is not None:
            batch_size, n, device = *tokenized_text.shape, tokenized_text.device
        other_return_dict = {}
        
        if random_embeddings is not None:
            mlp_output_embedding = self.name_projection_layer(random_embeddings)   
            total_embedding = mlp_output_embedding.view(mlp_output_embedding.shape[0], 2, 1024)   

            if self.adain_mode == 0:          
                adained_total_embedding = total_embedding * self.celeb_embeddings_std + self.celeb_embeddings_mean
            else:
                adained_total_embedding = total_embedding
                
            other_return_dict["total_embedding"] = total_embedding
            other_return_dict["adained_total_embedding"] = adained_total_embedding

        if name_batch is not None:
            if isinstance(name_batch, list): 
                name_tokens = self.get_token_for_string(name_batch)[:, 1:3]
                name_embeddings = self.get_embedding_for_tkn(name_tokens.to(random_embeddings.device))[0] 
                
                other_return_dict["name_embeddings"] = name_embeddings
            else:
                assert 0

        if tokenized_text is not None:
            placeholder_pos = get_rep_pos(tokenized_text,
                                        [self.placeholder_token])
            placeholder_pos = np.array(placeholder_pos)
            if len(placeholder_pos) != 0:
                batch_size = adained_total_embedding.shape[0]  
                end_index = min(batch_size, placeholder_pos.shape[0]) 
                embedded_text[placeholder_pos[:, 0], placeholder_pos[:, 1]] = adained_total_embedding[:end_index,0,:]
                embedded_text[placeholder_pos[:, 0], placeholder_pos[:, 1] + 1] = adained_total_embedding[:end_index,1,:]

        return embedded_text, other_return_dict



    def load(self, ckpt_path):
        ckpt = torch.load(ckpt_path, map_location='cuda')
        if ckpt.get("name_projection_layer") is not None:
            self.name_projection_layer = ckpt.get("name_projection_layer").float()

        print('[Embedding Manager] weights loaded.')



    def save(self, ckpt_path):
        save_dict = {}
        save_dict["name_projection_layer"] = self.name_projection_layer
        
        torch.save(save_dict, ckpt_path)


    def trainable_projection_parameters(self):  
        trainable_list = []
        trainable_list.extend(list(self.name_projection_layer.parameters())) 

        return trainable_list
   


class Embedding_discriminator(nn.Module):
    def __init__(self, input_size, dropout_rate):
        super(Embedding_discriminator, self).__init__()
        self.input_size = input_size

        self.fc1 = nn.Linear(input_size, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc3 = nn.Linear(256, 1)

        self.LayerNorm1 = nn.LayerNorm(512)
        self.LayerNorm2 = nn.LayerNorm(256)

        self.leaky_relu = nn.LeakyReLU(0.2)

        self.dropout_rate = dropout_rate
        if self.dropout_rate > 0:
            self.dropout1 = nn.Dropout(dropout_rate)
            self.dropout2 = nn.Dropout(dropout_rate)

    def forward(self, input):
        x = input.view(-1, self.input_size)

        if self.dropout_rate > 0:
            x = self.leaky_relu(self.dropout1(self.fc1(x)))
        else:
            x = self.leaky_relu(self.fc1(x))
        
        if self.dropout_rate > 0:
            x = self.leaky_relu(self.dropout2(self.fc2(x)))
        else:
            x = self.leaky_relu(self.fc2(x))

        x = self.fc3(x)

        return x
    
    
    def save(self, ckpt_path):
        save_dict = {}
 
        save_dict["fc1"] = self.fc1
        save_dict["fc2"] = self.fc2
        save_dict["fc3"] = self.fc3
        save_dict["LayerNorm1"] = self.LayerNorm1
        save_dict["LayerNorm2"] = self.LayerNorm2
        save_dict["leaky_relu"] = self.leaky_relu
        save_dict["dropout1"] = self.dropout1
        save_dict["dropout2"] = self.dropout2
        
        torch.save(save_dict, ckpt_path)
    
    def load(self, ckpt_path):
        ckpt = torch.load(ckpt_path, map_location='cuda')
        
        if ckpt.get("first_name_proj_layer") is not None:
            self.fc1 = ckpt.get("fc1").float()
            self.fc2 = ckpt.get("fc2").float()
            self.fc3 = ckpt.get("fc3").float()
            self.LayerNorm1 = ckpt.get("LayerNorm1").float()
            self.LayerNorm2 = ckpt.get("LayerNorm2").float()
            self.leaky_relu = ckpt.get("leaky_relu").float()
            self.dropout1 = ckpt.get("dropout1").float()
            self.dropout2 = ckpt.get("dropout2").float()
            
        print('[Embedding D] weights loaded.')