zjuJish commited on
Commit
7ffb693
·
verified ·
1 Parent(s): 5bae118

Upload layer_diff_dataset/test_inp_sd_1.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. layer_diff_dataset/test_inp_sd_1.py +72 -0
layer_diff_dataset/test_inp_sd_1.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import AutoPipelineForInpainting
2
+ from diffusers.utils import load_image
3
+ import torch
4
+ import os
5
+ from tqdm import tqdm
6
+ import cv2
7
+ from PIL import Image
8
+
9
+ pipe = AutoPipelineForInpainting.from_pretrained("../alpha_work/diffusers/stable-diffusion-xl-1.0-inpainting_", torch_dtype=torch.float16, variant="fp16").to("cuda")
10
+ # print('pipe',pipe)
11
+ # StableDiffusionXLInpaintPipeline
12
+
13
+ preprocessed_root_path = '../data/video_dataset/YoutubeVOS/train/ra_preprocessed'
14
+ # folder_path_0 = '../codes/Inpaint-Anything/results/0b6f9105fc'
15
+ mask_root_path = '../data/video_dataset/YoutubeVOS/train/mask'
16
+ output_root_path = '../data/video_dataset/YoutubeVOS/train/inp_preprocess_sd_0.9_base'
17
+ os.makedirs(output_root_path,exist_ok=True)
18
+ # img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
19
+ # mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
20
+
21
+
22
+ prompt = "hazy background with nothing on"
23
+ generator = torch.Generator(device="cuda").manual_seed(22)
24
+ # base_image = Image.open(base_image_path).resize((1024, 1024))
25
+
26
+ vid_list = os.listdir(preprocessed_root_path)
27
+ pbar = tqdm(enumerate(vid_list),total=len(vid_list))
28
+ for i, vid_name in pbar:
29
+ # if idx < 800:
30
+ # continue
31
+ if i>=800:
32
+ break
33
+ if i%100==0:
34
+ print('i: ',i)
35
+ img_folder = os.path.join(preprocessed_root_path, vid_name)
36
+ img_list = os.listdir(img_folder)
37
+ img_list = [i for i in img_list if i.endswith('.png')]
38
+ img_list.sort()
39
+ # print(len(img_list))
40
+ mask_folder = os.path.join(mask_root_path, vid_name)
41
+ output_folder = os.path.join(output_root_path, vid_name)
42
+ os.makedirs(output_folder,exist_ok=True)
43
+
44
+ # if os.path.exists(os.path.join(folder_path_,image_name)):
45
+ # continue
46
+ for i,image_name in enumerate(img_list):
47
+ image_path = os.path.join(img_folder,image_name)
48
+ mask_path = os.path.join(mask_folder,image_name)
49
+ image = Image.open(image_path).resize((1024, 1024))
50
+ mask_image = Image.open(mask_path).resize((1024, 1024))
51
+ # image = cv2.resize(cv2.imread(image_path),(1024,1024))
52
+ # mask_image = cv2.resize(cv2.imread(mask_path,cv2.IMREAD_GRAYSCALE),(1024,1024))
53
+ # image = load_image(img_url).resize((1024, 1024))
54
+ # mask_image = load_image(mask_url).resize((1024, 1024))
55
+ if i==0:
56
+ base_image = image
57
+ # strength = 0.99
58
+ image_out = pipe(
59
+ prompt=prompt,
60
+ image=image,
61
+ base_image=base_image,
62
+ mask_image=mask_image,
63
+ guidance_scale=8.0,
64
+ num_inference_steps=20, # steps between 15 and 30 work well for us
65
+ strength=0.9, # make sure to use `strength` below 1.0
66
+ generator=generator,
67
+ ).images[0]
68
+ image_out.save(os.path.join(output_folder,image_name))
69
+ if i==0:
70
+ base_image = image_out
71
+ # strength = 0.8
72
+ # exit(0)