forked from YichengDuan/axground3D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffusion_process.py
More file actions
51 lines (46 loc) · 1.72 KB
/
Copy pathdiffusion_process.py
File metadata and controls
51 lines (46 loc) · 1.72 KB
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
import os
from diffusers import StableDiffusionXLInpaintPipeline, DPMSolverMultistepScheduler
import torch
from PIL import Image
import gc
class ViewDiffuser:
def __init__(self,):
self.DIFFUSER_MODEL = "./model/RealVisXL_V4.0_inpainting"
self.DEVICE = (
torch.device("mps")
if torch.backends.mps.is_available()
else (
torch.device("cuda")
if torch.cuda.is_available()
else torch.device("cpu")
)
)
self.PIPE = StableDiffusionXLInpaintPipeline.from_pretrained(
self.DIFFUSER_MODEL, revision="fp16", variant="fp16",
)
# 2) swap in DPM++ 2M Karras scheduler
self.PIPE.scheduler = DPMSolverMultistepScheduler.from_config(
self.PIPE.scheduler.config, use_karras_sigmas=True
)
self.PIPE = self.PIPE.to(self.DEVICE)
self.PIPE.enable_attention_slicing() # reduce VRAM usage
def fill_in(self, init_image: Image.Image, mask_image: Image.Image) -> Image.Image:
# Inpaint
blurred_mask = self.PIPE.mask_processor.blur(mask_image, blur_factor=5)
result = self.PIPE(
prompt="a indoor room space, with furnitures ", # empty prompt to rely solely on surrounding context
image=init_image,
mask_image=blurred_mask,
mask_blur=0,
height=512,
width=512,
guidance_scale=7.5, # adjust as needed
num_inference_steps=25,
).images[0]
# gc
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
elif torch.backends.mps.is_available():
torch.mps.empty_cache()
return result