File size: 10,477 Bytes
2d1e0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
180819f
2d1e0bb
 
180819f
 
2d1e0bb
 
 
 
 
 
 
 
 
108d3f4
 
 
180819f
2d1e0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4366823
108d3f4
2d1e0bb
 
180819f
2d1e0bb
 
 
 
 
180819f
2d1e0bb
108d3f4
 
 
 
2d1e0bb
 
108d3f4
48c1bdf
 
108d3f4
 
 
 
48c1bdf
 
108d3f4
 
2d1e0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a3ce5e
2d1e0bb
 
 
 
 
 
 
 
 
 
180819f
 
2d1e0bb
8b9ace1
180819f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212c199
8b9ace1
 
180819f
 
 
8b9ace1
 
 
 
212c199
2d1e0bb
180819f
212c199
2d1e0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c686e22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d1e0bb
c686e22
 
 
 
 
 
 
2d1e0bb
 
b56f62c
2d1e0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40c9ea6
 
2d1e0bb
8b9ace1
40c9ea6
 
 
 
 
 
 
 
 
 
 
 
 
212c199
8b9ace1
 
40c9ea6
 
2d1e0bb
8b9ace1
 
 
 
212c199
40c9ea6
 
212c199
2d1e0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c686e22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340cc7b
c686e22
 
 
 
 
 
 
2d1e0bb
c686e22
2d1e0bb
 
b56f62c
2d1e0bb
 
 
 
 
 
 
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import gc
import spaces
from safetensors.torch import load_file
from autoregressive.models.gpt_t2i import GPT_models
from tokenizer.tokenizer_image.vq_model import VQ_models
from language.t5 import T5Embedder
import torch
import numpy as np
import PIL
from PIL import Image
from condition.canny import CannyDetector
import time
from autoregressive.models.generate import generate
from condition.midas.depth import MidasDetector
from preprocessor import Preprocessor

models = {
    "edge": "checkpoints/edge_base.safetensors",
    "depth": "checkpoints/depth_base.safetensors",
}
class Model:
    def __init__(self):
        self.device = torch.device(
            "cuda")
        self.base_model_id = ""
        self.task_name = ""
        self.vq_model = self.load_vq()
        self.t5_model = self.load_t5()
        # self.gpt_model_edge = self.load_gpt(condition_type='edge')
        # self.gpt_model_depth = self.load_gpt(condition_type='depth')
        self.gpt_model = self.load_gpt()
        self.preprocessor = Preprocessor()

    def to(self, device):
        self.gpt_model_canny.to('cuda')

    def load_vq(self):
        vq_model = VQ_models["VQ-16"](codebook_size=16384,
                                      codebook_embed_dim=8)
        vq_model.eval()
        checkpoint = torch.load(f"checkpoints/vq_ds16_t2i.pt",
                                map_location="cpu")
        vq_model.load_state_dict(checkpoint["model"])
        del checkpoint
        print("image tokenizer is loaded")
        return vq_model

    def load_gpt(self, condition_type='edge'):
        # gpt_ckpt = models[condition_type]
        # precision = torch.bfloat16
        precision = torch.float32
        latent_size = 512 // 16
        gpt_model = GPT_models["GPT-XL"](
            block_size=latent_size**2,
            cls_token_num=120,
            model_type='t2i',
            condition_type=condition_type,
            adapter_size='base',
        ).to(device='cpu', dtype=precision)
        # model_weight = load_file(gpt_ckpt)
        # gpt_model.load_state_dict(model_weight, strict=False)
        # gpt_model.eval()
        # print("gpt model is loaded")
        return gpt_model

    def load_gpt_weight(self, condition_type='edge'):
        torch.cuda.empty_cache()
        gc.collect()
        gpt_ckpt = models[condition_type]
        model_weight = load_file(gpt_ckpt)
        self.gpt_model.load_state_dict(model_weight, strict=False)
        self.gpt_model.eval()
        torch.cuda.empty_cache()
        gc.collect()
        # print("gpt model is loaded")
        
    def load_t5(self):
        # precision = torch.bfloat16
        precision = torch.float32
        t5_model = T5Embedder(
            device=self.device,
            local_cache=True,
            cache_dir='checkpoints/flan-t5-xl',
            dir_or_name='flan-t5-xl',
            torch_dtype=precision,
            model_max_length=120,
        )
        return t5_model

    @torch.no_grad()
    @spaces.GPU(enable_queue=True)
    def process_edge(
        self,
        image: np.ndarray,
        prompt: str,
        cfg_scale: float,
        temperature: float,
        top_k: int,
        top_p: int,
        seed: int,
        low_threshold: int,
        high_threshold: int,
        control_strength: float,
        preprocessor_name: str,
    ) -> list[PIL.Image.Image]:
        
        if isinstance(image, np.ndarray):
            image = Image.fromarray(image)
        origin_W, origin_H = image.size
        if preprocessor_name == 'Canny':
            self.preprocessor.load("Canny")
            condition_img = self.preprocessor(
                image=image, low_threshold=low_threshold, high_threshold=high_threshold, detect_resolution=512)
        elif preprocessor_name == 'Hed':
            self.preprocessor.load("HED")
            condition_img = self.preprocessor(
                image=image,image_resolution=512, detect_resolution=512)
        elif preprocessor_name == 'Lineart':
            self.preprocessor.load("Lineart")
            condition_img = self.preprocessor(
                image=image,image_resolution=512, detect_resolution=512)
        elif preprocessor_name == 'No preprocess':
            condition_img = image
        print('get edge')
        del self.preprocessor.model
        torch.cuda.empty_cache()
        condition_img = condition_img.resize((512,512))
        W, H = condition_img.size

        self.t5_model.model.to('cuda').to(torch.bfloat16)
        self.load_gpt_weight('edge')
        self.gpt_model.to('cuda').to(torch.bfloat16)
        self.vq_model.to('cuda')
        condition_img = torch.from_numpy(np.array(condition_img)).unsqueeze(0).permute(0,3,1,2).repeat(1,1,1,1)
        condition_img = condition_img.to(self.device)
        condition_img = 2*(condition_img/255 - 0.5)
        prompts = [prompt] * 1
        caption_embs, emb_masks = self.t5_model.get_text_embeddings(prompts)

        print(f"processing left-padding...")
        new_emb_masks = torch.flip(emb_masks, dims=[-1])
        new_caption_embs = []
        for idx, (caption_emb,
                  emb_mask) in enumerate(zip(caption_embs, emb_masks)):
            valid_num = int(emb_mask.sum().item())
            print(f'  prompt {idx} token len: {valid_num}')
            new_caption_emb = torch.cat(
                [caption_emb[valid_num:], caption_emb[:valid_num]])
            new_caption_embs.append(new_caption_emb)
        new_caption_embs = torch.stack(new_caption_embs)
        c_indices = new_caption_embs * new_emb_masks[:, :, None]
        c_emb_masks = new_emb_masks
        qzshape = [len(c_indices), 8, H // 16, W // 16]
        t1 = time.time()
        print(caption_embs.device)
        index_sample = generate(
            self.gpt_model,
            c_indices,
            (H // 16) * (W // 16),
            c_emb_masks,
            condition=condition_img,
            cfg_scale=cfg_scale,
            temperature=temperature,
            top_k=top_k,
            top_p=top_p,
            sample_logits=True,
            control_strength=control_strength,
        )
        sampling_time = time.time() - t1
        print(f"Full sampling takes about {sampling_time:.2f} seconds.")

        t2 = time.time()
        print(index_sample.shape)
        samples = self.vq_model.decode_code(
            index_sample, qzshape)  # output value is between [-1, 1]
        decoder_time = time.time() - t2
        print(f"decoder takes about {decoder_time:.2f} seconds.")
        # samples = condition_img[0:1]
        samples = torch.cat((condition_img[0:1], samples), dim=0)
        samples = 255 * (samples * 0.5 + 0.5)
        samples = [
            Image.fromarray(
                sample.permute(1, 2, 0).cpu().detach().numpy().clip(
                    0, 255).astype(np.uint8)) for sample in samples
        ]
        del condition_img
        torch.cuda.empty_cache()
        return samples

    @torch.no_grad()
    @spaces.GPU(enable_queue=True)
    def process_depth(
        self,
        image: np.ndarray,
        prompt: str,
        cfg_scale: float,
        temperature: float,
        top_k: int,
        top_p: int,
        seed: int,
        control_strength: float,
        preprocessor_name: str
    ) -> list[PIL.Image.Image]:
        
        if isinstance(image, np.ndarray):
            image = Image.fromarray(image)
        origin_W, origin_H = image.size
        # print(image)
        if preprocessor_name == 'depth':
            self.preprocessor.load("Depth")
            condition_img = self.preprocessor(
                    image=image,
                    image_resolution=512,
                    detect_resolution=512,
                )
        elif preprocessor_name == 'No preprocess':
            condition_img = image
        print('get depth')
        del self.preprocessor.model
        torch.cuda.empty_cache()
        condition_img = condition_img.resize((512,512))
        W, H = condition_img.size

        self.t5_model.model.to(self.device).to(torch.bfloat16)
        self.load_gpt_weight('depth')
        self.gpt_model.to('cuda').to(torch.bfloat16)
        self.vq_model.to(self.device)
        condition_img = torch.from_numpy(np.array(condition_img)).unsqueeze(0).permute(0,3,1,2).repeat(1,1,1,1)
        condition_img = condition_img.to(self.device)
        condition_img = 2*(condition_img/255 - 0.5)
        prompts = [prompt] * 1
        caption_embs, emb_masks = self.t5_model.get_text_embeddings(prompts)

        print(f"processing left-padding...")
        new_emb_masks = torch.flip(emb_masks, dims=[-1])
        new_caption_embs = []
        for idx, (caption_emb,
                  emb_mask) in enumerate(zip(caption_embs, emb_masks)):
            valid_num = int(emb_mask.sum().item())
            print(f'  prompt {idx} token len: {valid_num}')
            new_caption_emb = torch.cat(
                [caption_emb[valid_num:], caption_emb[:valid_num]])
            new_caption_embs.append(new_caption_emb)
        new_caption_embs = torch.stack(new_caption_embs)

        c_indices = new_caption_embs * new_emb_masks[:, :, None]
        c_emb_masks = new_emb_masks
        qzshape = [len(c_indices), 8, H // 16, W // 16]
        t1 = time.time()
        index_sample = generate(
            self.gpt_model,
            c_indices,
            (H // 16) * (W // 16),
            c_emb_masks,
            condition=condition_img,
            cfg_scale=cfg_scale,
            temperature=temperature,
            top_k=top_k,
            top_p=top_p,
            sample_logits=True,
            control_strength=control_strength,
        )
        sampling_time = time.time() - t1
        print(f"Full sampling takes about {sampling_time:.2f} seconds.")

        t2 = time.time()
        print(index_sample.shape)
        samples = self.vq_model.decode_code(index_sample, qzshape)
        decoder_time = time.time() - t2
        print(f"decoder takes about {decoder_time:.2f} seconds.")
        condition_img = condition_img.cpu()
        samples = samples.cpu()

        # samples = condition_img[0:1]
        samples = torch.cat((condition_img[0:1], samples), dim=0)
        samples = 255 * (samples * 0.5 + 0.5)
        samples = [
            Image.fromarray(
                sample.permute(1, 2, 0).cpu().detach().numpy().clip(0, 255).astype(np.uint8))
            for sample in samples
        ]
        del condition_img
        torch.cuda.empty_cache()
        return samples