File size: 16,499 Bytes
dd06d6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# inpaint the ball on an image
# this one is design for general image that does not require special location to place 


import torch
import argparse
import numpy as np
import torch.distributed as dist
import os
from PIL import Image
from tqdm.auto import tqdm
import json


from relighting.inpainter import BallInpainter

from relighting.mask_utils import MaskGenerator
from relighting.ball_processor import (
    get_ideal_normal_ball,
    crop_ball
)
from relighting.dataset import GeneralLoader
from relighting.utils import name2hash
import relighting.dist_utils as dist_util
import time


# cross import from inpaint_multi-illum.py
from relighting.argument import (
    SD_MODELS, 
    CONTROLNET_MODELS,
    VAE_MODELS
)

def create_argparser():    
    parser = argparse.ArgumentParser()
    parser.add_argument("--dataset", type=str, required=True ,help='directory that contain the image') #dataset name or directory 
    parser.add_argument("--ball_size", type=int, default=256, help="size of the ball in pixel")
    parser.add_argument("--ball_dilate", type=int, default=20, help="How much pixel to dilate the ball to make a sharper edge")
    parser.add_argument("--prompt", type=str, default="a perfect mirrored reflective chrome ball sphere") 
    parser.add_argument("--prompt_dark", type=str, default="a perfect black dark mirrored reflective chrome ball sphere") 
    parser.add_argument("--negative_prompt", type=str, default="matte, diffuse, flat, dull") 
    parser.add_argument("--model_option", default="sdxl", help='selecting fancy model option (sd15_old, sd15_new, sd21, sdxl, sdxl_turbo)') # [sd15_old, sd15_new, or sd21]
    parser.add_argument("--output_dir", required=True, type=str, help="output directory")
    parser.add_argument("--img_height", type=int, default=1024, help="Dataset Image Height")
    parser.add_argument("--img_width", type=int, default=1024, help="Dataset Image Width")
    # some good seed 0, 37, 71, 125, 140, 196, 307, 434, 485, 575 | 9021, 9166, 9560, 9814, but default auto is for fairness
    parser.add_argument("--seed", default="auto", type=str, help="Seed: right now we use single seed instead to reduce the time, (Auto will use hash file name to generate seed)")
    parser.add_argument("--denoising_step", default=30, type=int, help="number of denoising step of diffusion model")
    parser.add_argument("--control_scale", default=0.5, type=float, help="controlnet conditioning scale")
    parser.add_argument("--guidance_scale", default=5.0, type=float, help="guidance scale (also known as CFG)")
    
    parser.add_argument('--no_controlnet', dest='use_controlnet', action='store_false', help='by default we using controlnet, we have option to disable to see the different')
    parser.set_defaults(use_controlnet=True)
    
    parser.add_argument('--no_force_square', dest='force_square', action='store_false', help='SDXL is trained for square image, we prefered the square input. but you use this option to disable reshape')
    parser.set_defaults(force_square=True)
    
    parser.add_argument('--no_random_loader', dest='random_loader', action='store_false', help="by default, we random how dataset load. This make us able to peak into the trend of result without waiting entire dataset. but can disable if prefereed")
    parser.set_defaults(random_loader=True)

    parser.add_argument('--cpu', dest='is_cpu', action='store_true', help="using CPU inference instead of GPU inference")
    parser.set_defaults(is_cpu=False)

    parser.add_argument('--offload', dest='offload', action='store_false', help="to enable diffusers cpu offload")
    parser.set_defaults(offload=False)
    
    parser.add_argument("--limit_input", default=0, type=int, help="limit number of image to process to n image (0 = no limit), useful for run smallset")


    # LoRA stuff
    parser.add_argument('--no_lora', dest='use_lora', action='store_false', help='by default we using lora, we have option to disable to see the different')
    parser.set_defaults(use_lora=True)

    parser.add_argument("--lora_path", default="models/ThisIsTheFinal-lora-hdr-continuous-largeT@900/0_-5/checkpoint-2500", type=str, help="LoRA Checkpoint path")
    parser.add_argument("--lora_scale", default=0.75, type=float, help="LoRA scale factor")

    # speed optimization stuff
    parser.add_argument('--no_torch_compile', dest='use_torch_compile', action='store_false', help='by default we using torch compile for faster processing speed. disable it if your environemnt is lower than pytorch2.0')
    parser.set_defaults(use_torch_compile=True)
    
    # algorithm + iterative stuff
    parser.add_argument("--algorithm", type=str, default="iterative", choices=["iterative", "normal"], help="Selecting between iterative or normal (single pass inpaint) algorithm")

    parser.add_argument("--agg_mode", default="median", type=str)
    parser.add_argument("--strength", default=0.8, type=float)
    parser.add_argument("--num_iteration", default=2, type=int)
    parser.add_argument("--ball_per_iteration", default=30, type=int)
    parser.add_argument('--no_save_intermediate', dest='save_intermediate', action='store_false')
    parser.set_defaults(save_intermediate=True)
    parser.add_argument("--cache_dir", default="./temp_inpaint_iterative", type=str, help="cache directory for iterative inpaint")
    
    # pararelle processing
    parser.add_argument("--idx", default=0, type=int, help="index of the current process, useful for running on multiple node")
    parser.add_argument("--total", default=1, type=int, help="total number of process")

    # for HDR stuff
    parser.add_argument("--max_negative_ev", default=-5, type=int, help="maximum negative EV for lora")
    parser.add_argument("--ev", default="0,-2.5,-5", type=str, help="EV: list of EV to generate")

    return parser

def get_ball_location(image_data, args):
    if 'boundary' in image_data:
        # support predefined boundary if need
        x = image_data["boundary"]["x"]
        y = image_data["boundary"]["y"]
        r = image_data["boundary"]["size"]
        
        # support ball dilation
        half_dilate = args.ball_dilate // 2

        # check if not left out-of-bound
        if x - half_dilate < 0: x += half_dilate
        if y - half_dilate < 0: y += half_dilate

        # check if not right out-of-bound
        if x + r + half_dilate > args.img_width: x -= half_dilate
        if y + r + half_dilate > args.img_height: y -= half_dilate   
            
    else:
        # we use top-left corner notation
        x, y, r = ((args.img_width // 2) - (args.ball_size // 2), (args.img_height // 2) - (args.ball_size // 2), args.ball_size)
    return x, y, r

def interpolate_embedding(pipe, args):
    print("interpolate embedding...")

    # get list of all EVs
    ev_list = [float(x) for x in args.ev.split(",")]
    interpolants = [ev / args.max_negative_ev for ev in ev_list]

    print("EV : ", ev_list)
    print("EV : ", interpolants)

    # calculate prompt embeddings
    prompt_normal = args.prompt
    prompt_dark = args.prompt_dark
    prompt_embeds_normal, _, pooled_prompt_embeds_normal, _ = pipe.pipeline.encode_prompt(prompt_normal)
    prompt_embeds_dark, _, pooled_prompt_embeds_dark, _ = pipe.pipeline.encode_prompt(prompt_dark)

    # interpolate embeddings
    interpolate_embeds = []
    for t in interpolants:
        int_prompt_embeds = prompt_embeds_normal + t * (prompt_embeds_dark - prompt_embeds_normal)
        int_pooled_prompt_embeds = pooled_prompt_embeds_normal + t * (pooled_prompt_embeds_dark - pooled_prompt_embeds_normal)

        interpolate_embeds.append((int_prompt_embeds, int_pooled_prompt_embeds))

    return dict(zip(ev_list, interpolate_embeds))

def main():
    # load arguments
    args = create_argparser().parse_args()
        
    # get local rank
    if args.is_cpu:
        device = torch.device("cpu")
        torch_dtype = torch.float32
    else:
        device = dist_util.dev()
        torch_dtype = torch.float16
    
    # so, we need ball_dilate >= 16 (2*vae_scale_factor) to make our mask shape = (272, 272)
    assert args.ball_dilate % 2 == 0 # ball dilation should be symmetric
    
    # create controlnet pipeline 
    if args.model_option in ["sdxl", "sdxl_fast", "sdxl_turbo"] and args.use_controlnet:
        model, controlnet = SD_MODELS[args.model_option], CONTROLNET_MODELS[args.model_option]
        pipe = BallInpainter.from_sdxl(
            model=model, 
            controlnet=controlnet, 
            device=device,
            torch_dtype = torch_dtype,
            offload = args.offload
        )
    elif args.model_option in ["sdxl", "sdxl_fast", "sdxl_turbo"] and not args.use_controlnet:
        model = SD_MODELS[args.model_option]
        pipe = BallInpainter.from_sdxl(
            model=model,
            controlnet=None,
            device=device,
            torch_dtype = torch_dtype,
            offload = args.offload
        )
    elif args.use_controlnet:
        model, controlnet = SD_MODELS[args.model_option], CONTROLNET_MODELS[args.model_option]
        pipe = BallInpainter.from_sd(
            model=model,
            controlnet=controlnet,
            device=device,
            torch_dtype = torch_dtype,
            offload = args.offload
        )
    else:
        model = SD_MODELS[args.model_option]
        pipe = BallInpainter.from_sd(
            model=model,
            controlnet=None,
            device=device,
            torch_dtype = torch_dtype,
            offload = args.offload
        )

    if args.model_option in ["sdxl_turbo"]:
        # Guidance scale is not supported in sdxl_turbo
        args.guidance_scale = 0.0
    
    if args.lora_scale > 0 and args.lora_path is None:
        raise ValueError("lora scale is not 0 but lora path is not set")
    
    if (args.lora_path is not None) and (args.use_lora):
        print(f"using lora path {args.lora_path}")
        print(f"using lora scale {args.lora_scale}")
        pipe.pipeline.load_lora_weights(args.lora_path)
        pipe.pipeline.fuse_lora(lora_scale=args.lora_scale) # fuse lora weight w' = w + \alpha \Delta w
        enabled_lora = True
    else:
        enabled_lora = False

    if args.use_torch_compile:
        try:
            print("compiling unet model")
            start_time = time.time()
            pipe.pipeline.unet = torch.compile(pipe.pipeline.unet, mode="reduce-overhead", fullgraph=True)
            print("Model compilation time: ", time.time() - start_time)
        except:
            pass
                
    # default height for sdxl is 1024, if not set, we set default height.
    if args.model_option == "sdxl" and args.img_height == 0 and args.img_width == 0:
        args.img_height = 1024
        args.img_width = 1024
          
    # load dataset
    dataset = GeneralLoader(
        root=args.dataset,
        resolution=(args.img_width, args.img_height),
        force_square=args.force_square,
        return_dict=True,
        random_shuffle=args.random_loader,
        process_id=args.idx,
        process_total=args.total,
        limit_input=args.limit_input,
    )

    # interpolate embedding
    embedding_dict = interpolate_embedding(pipe, args)
    
    # prepare mask and normal ball
    mask_generator = MaskGenerator()
    normal_ball, mask_ball = get_ideal_normal_ball(size=args.ball_size+args.ball_dilate)
    _, mask_ball_for_crop = get_ideal_normal_ball(size=args.ball_size)
    
    
    # make output directory if not exist
    raw_output_dir = os.path.join(args.output_dir, "raw")
    control_output_dir = os.path.join(args.output_dir, "control")
    square_output_dir = os.path.join(args.output_dir, "square")
    os.makedirs(args.output_dir, exist_ok=True)    
    os.makedirs(raw_output_dir, exist_ok=True)
    os.makedirs(control_output_dir, exist_ok=True)
    os.makedirs(square_output_dir, exist_ok=True)
    
    # create split seed
    # please DO NOT manual replace this line, use --seed option instead
    seeds = args.seed.split(",")
    
    for image_data in tqdm(dataset):
        input_image = image_data["image"] 
        image_path = image_data["path"]
        
        for ev, (prompt_embeds, pooled_prompt_embeds) in embedding_dict.items():
            # create output file name (we always use png to prevent quality loss)
            ev_str = str(ev).replace(".", "") if ev != 0 else "-00"
            outname = os.path.basename(image_path).split(".")[0] + f"_ev{ev_str}"

            # we use top-left corner notation (which is different from aj.aek's center point notation)
            x, y, r = get_ball_location(image_data, args)
            
            # create inpaint mask
            mask = mask_generator.generate_single(
                input_image, mask_ball, 
                x - (args.ball_dilate // 2),
                y - (args.ball_dilate // 2),
                r + args.ball_dilate
            )
                
            seeds = tqdm(seeds, desc="seeds") if len(seeds) > 10 else seeds   
                
            #replacely create image with differnt seed
            for seed in seeds:
                start_time = time.time()
                # set seed, if seed auto we use file name as seed
                if seed == "auto":
                    filename = os.path.basename(image_path).split(".")[0]
                    seed = name2hash(filename) 
                    outpng = f"{outname}.png"
                    cache_name = f"{outname}"
                else:
                    seed = int(seed)
                    outpng = f"{outname}_seed{seed}.png"
                    cache_name = f"{outname}_seed{seed}"
                # skip if file exist, useful for resuming
                if os.path.exists(os.path.join(square_output_dir, outpng)):
                    continue
                generator = torch.Generator().manual_seed(seed)
                kwargs = {
                    "prompt_embeds": prompt_embeds,
                    "pooled_prompt_embeds": pooled_prompt_embeds,
                    'negative_prompt': args.negative_prompt,
                    'num_inference_steps': args.denoising_step,
                    'generator': generator,
                    'image': input_image,
                    'mask_image': mask,
                    'strength': 1.0,
                    'current_seed': seed, # we still need seed in the pipeline!
                    'controlnet_conditioning_scale': args.control_scale,
                    'height': args.img_height,
                    'width': args.img_width,
                    'normal_ball': normal_ball,
                    'mask_ball': mask_ball,
                    'x': x,
                    'y': y,
                    'r': r,
                    'guidance_scale': args.guidance_scale,
                }
                
                if enabled_lora:
                    kwargs["cross_attention_kwargs"] = {"scale": args.lora_scale}
                
                if args.algorithm == "normal":
                    output_image = pipe.inpaint(**kwargs).images[0]
                elif args.algorithm == "iterative":
                    # This is still buggy
                    print("using inpainting iterative, this is going to take a while...")
                    kwargs.update({
                        "strength": args.strength,
                        "num_iteration": args.num_iteration,
                        "ball_per_iteration": args.ball_per_iteration,
                        "agg_mode": args.agg_mode,
                        "save_intermediate": args.save_intermediate,
                        "cache_dir": os.path.join(args.cache_dir, cache_name),
                    })
                    output_image = pipe.inpaint_iterative(**kwargs)
                else:
                    raise NotImplementedError(f"Unknown algorithm {args.algorithm}")
                
                
                square_image = output_image.crop((x, y, x+r, y+r))

                # return the most recent control_image for sanity check
                control_image = pipe.get_cache_control_image()
                if control_image is not None:
                    control_image.save(os.path.join(control_output_dir, outpng))
                
                # save image 
                output_image.save(os.path.join(raw_output_dir, outpng))
                square_image.save(os.path.join(square_output_dir, outpng))

                          
if __name__ == "__main__":
    main()