Spaces:
Running
on
Zero
Running
on
Zero
- apps/__pycache__/mv_models.cpython-38.pyc +0 -0
- apps/mv_models.py +65 -71
apps/__pycache__/mv_models.cpython-38.pyc
CHANGED
Binary files a/apps/__pycache__/mv_models.cpython-38.pyc and b/apps/__pycache__/mv_models.cpython-38.pyc differ
|
|
apps/mv_models.py
CHANGED
@@ -26,99 +26,92 @@ class GenMVImage(object):
|
|
26 |
self.seed = 1024
|
27 |
self.guidance_scale = 7.5
|
28 |
self.step = 50
|
29 |
-
self.pipelines = {}
|
30 |
self.device = device
|
31 |
-
|
32 |
-
@spaces.GPU
|
33 |
-
def gen_image_from_crm(self, image):
|
34 |
from .third_party.CRM.pipelines import TwoStagePipeline
|
35 |
stage1_config = OmegaConf.load(f"{parent_dir}/apps/third_party/CRM/configs/nf7_v3_SNR_rd_size_stroke.yaml").config
|
36 |
stage1_sampler_config = stage1_config.sampler
|
37 |
stage1_model_config = stage1_config.models
|
38 |
stage1_model_config.resume = hf_hub_download(repo_id="Zhengyi/CRM", filename="pixel-diffusion.pth", repo_type="model")
|
39 |
stage1_model_config.config = f"{parent_dir}/apps/third_party/CRM/" + stage1_model_config.config
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
mv_imgs = rt_dict["stage1_images"]
|
53 |
return mv_imgs[5], mv_imgs[3], mv_imgs[2], mv_imgs[0]
|
54 |
|
55 |
@spaces.GPU
|
56 |
def gen_image_from_mvdream(self, image, text):
|
57 |
-
from .third_party.mvdream_diffusers.pipeline_mvdream import MVDreamPipeline
|
58 |
if image is None:
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
self.
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
self.pipelines['mvdream'] = self.pipelines['mvdream'].to(self.device)
|
68 |
-
pipe_MVDream = self.pipelines['mvdream']
|
69 |
-
mv_imgs = pipe_MVDream(
|
70 |
-
text,
|
71 |
-
negative_prompt="ugly, deformed, disfigured, poor details, bad anatomy",
|
72 |
-
num_inference_steps=self.step,
|
73 |
-
guidance_scale=self.guidance_scale,
|
74 |
-
generator = torch.Generator(self.device).manual_seed(self.seed)
|
75 |
-
)
|
76 |
-
else:
|
77 |
image = np.array(image)
|
78 |
image = image.astype(np.float32) / 255.0
|
79 |
image = image[..., :3] * image[..., 3:4] + (1 - image[..., 3:4])
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
pipe_imagedream = self.pipelines['imagedream']
|
90 |
-
mv_imgs = pipe_imagedream(
|
91 |
-
text,
|
92 |
-
image,
|
93 |
-
negative_prompt="ugly, deformed, disfigured, poor details, bad anatomy",
|
94 |
-
num_inference_steps=self.step,
|
95 |
-
guidance_scale=self.guidance_scale,
|
96 |
-
generator = torch.Generator(self.device).manual_seed(self.seed)
|
97 |
-
)
|
98 |
return mv_imgs[1], mv_imgs[2], mv_imgs[3], mv_imgs[0]
|
99 |
|
100 |
@spaces.GPU
|
101 |
def gen_image_from_wonder3d(self, image, crop_size):
|
102 |
-
sys.path.append(f"{parent_dir}/apps/third_party/Wonder3D")
|
103 |
-
from diffusers import DiffusionPipeline # only tested on diffusers[torch]==0.19.3, may have conflicts with newer versions of diffusers
|
104 |
|
105 |
weight_dtype = torch.float16
|
106 |
batch = prepare_data(image, crop_size)
|
107 |
|
108 |
-
|
109 |
-
pipeline = self.pipelines['wonder3d']
|
110 |
-
else:
|
111 |
-
self.pipelines['wonder3d'] = DiffusionPipeline.from_pretrained(
|
112 |
-
'flamehaze1115/wonder3d-v1.0', # or use local checkpoint './ckpts'
|
113 |
-
custom_pipeline='flamehaze1115/wonder3d-pipeline',
|
114 |
-
torch_dtype=torch.float16
|
115 |
-
)
|
116 |
-
self.pipelines['wonder3d'].unet.enable_xformers_memory_efficient_attention()
|
117 |
-
self.pipelines['wonder3d'].to(self.device)
|
118 |
-
self.pipelines['wonder3d'].set_progress_bar_config(disable=True)
|
119 |
-
pipeline = self.pipelines['wonder3d']
|
120 |
-
|
121 |
-
generator = torch.Generator(device=pipeline.unet.device).manual_seed(self.seed)
|
122 |
# repeat (2B, Nv, 3, H, W)
|
123 |
imgs_in = torch.cat([batch['imgs_in']] * 2, dim=0).to(weight_dtype)
|
124 |
|
@@ -133,7 +126,7 @@ class GenMVImage(object):
|
|
133 |
imgs_in = rearrange(imgs_in, "Nv C H W -> (Nv) C H W")
|
134 |
# (B*Nv, Nce)
|
135 |
|
136 |
-
out =
|
137 |
imgs_in,
|
138 |
# camera_embeddings,
|
139 |
generator=generator,
|
@@ -154,6 +147,7 @@ class GenMVImage(object):
|
|
154 |
mv_imgs = images_pred
|
155 |
return mv_imgs[0], mv_imgs[2], mv_imgs[4], mv_imgs[5]
|
156 |
|
|
|
157 |
def run(self, mvimg_model, text, image, crop_size, seed, guidance_scale, step):
|
158 |
self.seed = seed
|
159 |
self.guidance_scale = guidance_scale
|
@@ -161,6 +155,6 @@ class GenMVImage(object):
|
|
161 |
if mvimg_model.upper() == "CRM":
|
162 |
return self.gen_image_from_crm(image)
|
163 |
elif mvimg_model.upper() == "IMAGEDREAM":
|
164 |
-
return self.gen_image_from_mvdream(image,
|
165 |
elif mvimg_model.upper() == "WONDER3D":
|
166 |
return self.gen_image_from_wonder3d(image, crop_size)
|
|
|
26 |
self.seed = 1024
|
27 |
self.guidance_scale = 7.5
|
28 |
self.step = 50
|
|
|
29 |
self.device = device
|
30 |
+
|
|
|
|
|
31 |
from .third_party.CRM.pipelines import TwoStagePipeline
|
32 |
stage1_config = OmegaConf.load(f"{parent_dir}/apps/third_party/CRM/configs/nf7_v3_SNR_rd_size_stroke.yaml").config
|
33 |
stage1_sampler_config = stage1_config.sampler
|
34 |
stage1_model_config = stage1_config.models
|
35 |
stage1_model_config.resume = hf_hub_download(repo_id="Zhengyi/CRM", filename="pixel-diffusion.pth", repo_type="model")
|
36 |
stage1_model_config.config = f"{parent_dir}/apps/third_party/CRM/" + stage1_model_config.config
|
37 |
+
self.crm_pipeline = TwoStagePipeline(
|
38 |
+
stage1_model_config,
|
39 |
+
stage1_sampler_config,
|
40 |
+
device=self.device,
|
41 |
+
dtype=torch.float16
|
42 |
+
)
|
43 |
+
self.crm_pipeline.set_seed(self.seed)
|
44 |
+
|
45 |
+
sys.path.append(f"{parent_dir}/apps/third_party/Wonder3D")
|
46 |
+
from diffusers import DiffusionPipeline # only tested on diffusers[torch]==0.19.3, may have conflicts with newer versions of diffusers
|
47 |
+
self.wonder3d_pipeline = DiffusionPipeline.from_pretrained(
|
48 |
+
'flamehaze1115/wonder3d-v1.0', # or use local checkpoint './ckpts'
|
49 |
+
custom_pipeline='flamehaze1115/wonder3d-pipeline',
|
50 |
+
torch_dtype=torch.float16
|
51 |
+
)
|
52 |
+
self.wonder3d_pipeline.unet.enable_xformers_memory_efficient_attention()
|
53 |
+
self.wonder3d_pipeline.to(self.device)
|
54 |
+
self.wonder3d_pipeline.set_progress_bar_config(disable=True)
|
55 |
+
|
56 |
+
|
57 |
+
sys.path.append(f"{parent_dir}/apps/third_party/mvdream_diffusers")
|
58 |
+
from .third_party.mvdream_diffusers.pipeline_mvdream import MVDreamPipeline
|
59 |
+
self.mvdream_pipeline = MVDreamPipeline.from_pretrained(
|
60 |
+
"ashawkey/mvdream-sd2.1-diffusers", # remote weights
|
61 |
+
torch_dtype=torch.float16,
|
62 |
+
trust_remote_code=True,
|
63 |
+
)
|
64 |
+
self.mvdream_pipeline = self.mvdream_pipeline.to(self.device)
|
65 |
+
# self.imagedream_pipeline = MVDreamPipeline.from_pretrained(
|
66 |
+
# "ashawkey/imagedream-ipmv-diffusers", # remote weights
|
67 |
+
# torch_dtype=torch.float16,
|
68 |
+
# trust_remote_code=True,
|
69 |
+
# )
|
70 |
+
# self.imagedream_pipeline = self.imagedream_pipeline.to(self.device)
|
71 |
+
|
72 |
+
|
73 |
+
@spaces.GPU
|
74 |
+
def gen_image_from_crm(self, image):
|
75 |
+
rt_dict = self.crm_pipeline(
|
76 |
+
image,
|
77 |
+
scale=self.guidance_scale,
|
78 |
+
step=self.step
|
79 |
+
)
|
80 |
mv_imgs = rt_dict["stage1_images"]
|
81 |
return mv_imgs[5], mv_imgs[3], mv_imgs[2], mv_imgs[0]
|
82 |
|
83 |
@spaces.GPU
|
84 |
def gen_image_from_mvdream(self, image, text):
|
|
|
85 |
if image is None:
|
86 |
+
mv_imgs = self.mvdream_pipeline(
|
87 |
+
text,
|
88 |
+
negative_prompt="ugly, deformed, disfigured, poor details, bad anatomy",
|
89 |
+
num_inference_steps=self.step,
|
90 |
+
guidance_scale=self.guidance_scale,
|
91 |
+
generator = torch.Generator(self.device).manual_seed(self.seed)
|
92 |
+
)
|
93 |
+
elif text is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
image = np.array(image)
|
95 |
image = image.astype(np.float32) / 255.0
|
96 |
image = image[..., :3] * image[..., 3:4] + (1 - image[..., 3:4])
|
97 |
+
|
98 |
+
mv_imgs = self.imagedream_pipeline(
|
99 |
+
text,
|
100 |
+
image,
|
101 |
+
negative_prompt="ugly, deformed, disfigured, poor details, bad anatomy",
|
102 |
+
num_inference_steps=self.step,
|
103 |
+
guidance_scale=self.guidance_scale,
|
104 |
+
generator = torch.Generator(self.device).manual_seed(self.seed)
|
105 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
return mv_imgs[1], mv_imgs[2], mv_imgs[3], mv_imgs[0]
|
107 |
|
108 |
@spaces.GPU
|
109 |
def gen_image_from_wonder3d(self, image, crop_size):
|
|
|
|
|
110 |
|
111 |
weight_dtype = torch.float16
|
112 |
batch = prepare_data(image, crop_size)
|
113 |
|
114 |
+
generator = torch.Generator(device=self.wonder3d_pipeline.unet.device).manual_seed(self.seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
# repeat (2B, Nv, 3, H, W)
|
116 |
imgs_in = torch.cat([batch['imgs_in']] * 2, dim=0).to(weight_dtype)
|
117 |
|
|
|
126 |
imgs_in = rearrange(imgs_in, "Nv C H W -> (Nv) C H W")
|
127 |
# (B*Nv, Nce)
|
128 |
|
129 |
+
out = self.wonder3d_pipeline(
|
130 |
imgs_in,
|
131 |
# camera_embeddings,
|
132 |
generator=generator,
|
|
|
147 |
mv_imgs = images_pred
|
148 |
return mv_imgs[0], mv_imgs[2], mv_imgs[4], mv_imgs[5]
|
149 |
|
150 |
+
@spaces.GPU
|
151 |
def run(self, mvimg_model, text, image, crop_size, seed, guidance_scale, step):
|
152 |
self.seed = seed
|
153 |
self.guidance_scale = guidance_scale
|
|
|
155 |
if mvimg_model.upper() == "CRM":
|
156 |
return self.gen_image_from_crm(image)
|
157 |
elif mvimg_model.upper() == "IMAGEDREAM":
|
158 |
+
return self.gen_image_from_mvdream(image, None)
|
159 |
elif mvimg_model.upper() == "WONDER3D":
|
160 |
return self.gen_image_from_wonder3d(image, crop_size)
|