Fabrice-TIERCELIN commited on
Commit
6ed0aea
1 Parent(s): aaf459b

Delete test.py

Browse files
Files changed (1) hide show
  1. test.py +0 -106
test.py DELETED
@@ -1,106 +0,0 @@
1
- import torch.cuda
2
- import argparse
3
- from SUPIR.util import create_SUPIR_model, PIL2Tensor, Tensor2PIL, convert_dtype
4
- from PIL import Image
5
- from llava.llava_agent import LLavaAgent
6
- from CKPT_PTH import LLAVA_MODEL_PATH
7
- import os
8
- from torch.nn.functional import interpolate
9
-
10
- if torch.cuda.device_count() >= 2:
11
- SUPIR_device = 'cuda:0'
12
- LLaVA_device = 'cuda:1'
13
- elif torch.cuda.device_count() == 1:
14
- SUPIR_device = 'cuda:0'
15
- LLaVA_device = 'cuda:0'
16
- else:
17
- raise ValueError('Currently support CUDA only.')
18
-
19
- # hyparams here
20
- parser = argparse.ArgumentParser()
21
- parser.add_argument("--img_dir", type=str)
22
- parser.add_argument("--save_dir", type=str)
23
- parser.add_argument("--upscale", type=int, default=1)
24
- parser.add_argument("--SUPIR_sign", type=str, default='Q', choices=['F', 'Q'])
25
- parser.add_argument("--seed", type=int, default=1234)
26
- parser.add_argument("--min_size", type=int, default=1024)
27
- parser.add_argument("--edm_steps", type=int, default=50)
28
- parser.add_argument("--s_stage1", type=int, default=-1)
29
- parser.add_argument("--s_churn", type=int, default=5)
30
- parser.add_argument("--s_noise", type=float, default=1.003)
31
- parser.add_argument("--s_cfg", type=float, default=7.5)
32
- parser.add_argument("--s_stage2", type=float, default=1.)
33
- parser.add_argument("--num_samples", type=int, default=1)
34
- parser.add_argument("--a_prompt", type=str,
35
- default='Cinematic, High Contrast, highly detailed, taken using a Canon EOS R '
36
- 'camera, hyper detailed photo - realistic maximum detail, 32k, Color '
37
- 'Grading, ultra HD, extreme meticulous detailing, skin pore detailing, '
38
- 'hyper sharpness, perfect without deformations.')
39
- parser.add_argument("--n_prompt", type=str,
40
- default='painting, oil painting, illustration, drawing, art, sketch, oil painting, '
41
- 'cartoon, CG Style, 3D render, unreal engine, blurring, dirty, messy, '
42
- 'worst quality, low quality, frames, watermark, signature, jpeg artifacts, '
43
- 'deformed, lowres, over-smooth')
44
- parser.add_argument("--color_fix_type", type=str, default='Wavelet', choices=["None", "AdaIn", "Wavelet"])
45
- parser.add_argument("--linear_CFG", action='store_true', default=True)
46
- parser.add_argument("--linear_s_stage2", action='store_true', default=False)
47
- parser.add_argument("--spt_linear_CFG", type=float, default=4.0)
48
- parser.add_argument("--spt_linear_s_stage2", type=float, default=0.)
49
- parser.add_argument("--ae_dtype", type=str, default="bf16", choices=['fp32', 'bf16'])
50
- parser.add_argument("--diff_dtype", type=str, default="fp16", choices=['fp32', 'fp16', 'bf16'])
51
- parser.add_argument("--no_llava", action='store_true', default=False)
52
- parser.add_argument("--loading_half_params", action='store_true', default=False)
53
- parser.add_argument("--use_tile_vae", action='store_true', default=False)
54
- parser.add_argument("--encoder_tile_size", type=int, default=512)
55
- parser.add_argument("--decoder_tile_size", type=int, default=64)
56
- parser.add_argument("--load_8bit_llava", action='store_true', default=False)
57
- args = parser.parse_args()
58
- print(args)
59
- use_llava = not args.no_llava
60
-
61
- # load SUPIR
62
- model = create_SUPIR_model('options/SUPIR_v0.yaml', SUPIR_sign=args.SUPIR_sign)
63
- if args.loading_half_params:
64
- model = model.half()
65
- if args.use_tile_vae:
66
- model.init_tile_vae(encoder_tile_size=args.encoder_tile_size, decoder_tile_size=args.decoder_tile_size)
67
- model.ae_dtype = convert_dtype(args.ae_dtype)
68
- model.model.dtype = convert_dtype(args.diff_dtype)
69
- model = model.to(SUPIR_device)
70
- # load LLaVA
71
- if use_llava:
72
- llava_agent = LLavaAgent(LLAVA_MODEL_PATH, device=LLaVA_device, load_8bit=args.load_8bit_llava, load_4bit=False)
73
- else:
74
- llava_agent = None
75
-
76
- os.makedirs(args.save_dir, exist_ok=True)
77
- for img_pth in os.listdir(args.img_dir):
78
- img_name = os.path.splitext(img_pth)[0]
79
-
80
- LQ_ips = Image.open(os.path.join(args.img_dir, img_pth))
81
- LQ_img, h0, w0 = PIL2Tensor(LQ_ips, upsacle=args.upscale, min_size=args.min_size)
82
- LQ_img = LQ_img.unsqueeze(0).to(SUPIR_device)[:, :3, :, :]
83
-
84
- # step 1: Pre-denoise for LLaVA, resize to 512
85
- LQ_img_512, h1, w1 = PIL2Tensor(LQ_ips, upsacle=args.upscale, min_size=args.min_size, fix_resize=512)
86
- LQ_img_512 = LQ_img_512.unsqueeze(0).to(SUPIR_device)[:, :3, :, :]
87
- clean_imgs = model.batchify_denoise(LQ_img_512)
88
- clean_PIL_img = Tensor2PIL(clean_imgs[0], h1, w1)
89
-
90
- # step 2: LLaVA
91
- if use_llava:
92
- captions = llava_agent.gen_image_caption([clean_PIL_img])
93
- else:
94
- captions = ['']
95
- print(captions)
96
-
97
- # # step 3: Diffusion Process
98
- samples = model.batchify_sample(LQ_img, captions, num_steps=args.edm_steps, restoration_scale=args.s_stage1, s_churn=args.s_churn,
99
- s_noise=args.s_noise, cfg_scale=args.s_cfg, control_scale=args.s_stage2, seed=args.seed,
100
- num_samples=args.num_samples, p_p=args.a_prompt, n_p=args.n_prompt, color_fix_type=args.color_fix_type,
101
- use_linear_CFG=args.linear_CFG, use_linear_control_scale=args.linear_s_stage2,
102
- cfg_scale_start=args.spt_linear_CFG, control_scale_start=args.spt_linear_s_stage2)
103
- # save
104
- for _i, sample in enumerate(samples):
105
- Tensor2PIL(sample, h0, w0).save(f'{args.save_dir}/{img_name}_{_i}.png')
106
-