Upload layer_diff_dataset/test_inp.py with huggingface_hub
Browse files
layer_diff_dataset/test_inp.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import PIL.Image
|
| 8 |
+
import json
|
| 9 |
+
|
| 10 |
+
pipe = AutoPipelineForInpainting.from_pretrained("/mnt/workspace/workgroup/sihui.jsh/alpha_work/diffusers/stable-diffusion-xl-1.0-inpainting_", torch_dtype=torch.float16, variant="fp16").to("cuda:1")
|
| 11 |
+
generator = torch.Generator(device="cuda:1").manual_seed(0)
|
| 12 |
+
# img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
|
| 13 |
+
# mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
|
| 14 |
+
|
| 15 |
+
# image = load_image(img_url).resize((1024, 1024))
|
| 16 |
+
# mask_image = load_image(mask_url).resize((1024, 1024))
|
| 17 |
+
|
| 18 |
+
folder_path_0 = '/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/try/im'
|
| 19 |
+
folder_path = '/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/try/gt_resized'
|
| 20 |
+
folder_path_ = '/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/try/gt_inp_1'
|
| 21 |
+
os.makedirs(folder_path_,exist_ok=True)
|
| 22 |
+
file_list = os.listdir(folder_path)
|
| 23 |
+
file_list = [i for i in file_list if i.endswith('.png')]
|
| 24 |
+
file_list.sort()
|
| 25 |
+
|
| 26 |
+
pbar = tqdm(enumerate(file_list),total=len(file_list))
|
| 27 |
+
with open('/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/try/im_rgba_base.json', 'r') as file:
|
| 28 |
+
data = json.load(file)
|
| 29 |
+
for i, image_name in pbar:
|
| 30 |
+
# if i==1:
|
| 31 |
+
# break
|
| 32 |
+
# gt_name = data[i]["images"].split('/')[-1]
|
| 33 |
+
# print(image_name,gt_name)
|
| 34 |
+
# prompt = data[i]["prompt_fg"]
|
| 35 |
+
prompt = 'hazy background with nothing on'
|
| 36 |
+
# print(prompt)
|
| 37 |
+
mask_image = load_image(os.path.join(folder_path,image_name)).resize((512, 512))
|
| 38 |
+
image = load_image(os.path.join(folder_path_0,image_name.split('.png')[0]+'.jpg')).resize((512, 512))
|
| 39 |
+
image = pipe(
|
| 40 |
+
prompt=prompt,
|
| 41 |
+
image=image,
|
| 42 |
+
mask_image=mask_image,
|
| 43 |
+
guidance_scale=8.0,
|
| 44 |
+
num_inference_steps=20, # steps between 15 and 30 work well for us
|
| 45 |
+
strength=1.0, # make sure to use `strength` below 1.0
|
| 46 |
+
generator=generator,
|
| 47 |
+
).images[0]
|
| 48 |
+
image.save(os.path.join(folder_path_,image_name))
|
| 49 |
+
# cv2.imwrite(os.path.join(folder_path_,image_name),image)
|
| 50 |
+
|
| 51 |
+
# mask_image = cv2.imread('/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/try/gt_reverse/1#Accessories#1#Bag#2339506821_83cf9f1d22_o.png')
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|