Delete apps/mv_models.py
Browse files- apps/mv_models.py +0 -168
apps/mv_models.py
DELETED
|
@@ -1,168 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
-
import torch
|
| 4 |
-
import PIL
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import os
|
| 7 |
-
import sys
|
| 8 |
-
import rembg
|
| 9 |
-
import time
|
| 10 |
-
import json
|
| 11 |
-
import cv2
|
| 12 |
-
from datetime import datetime
|
| 13 |
-
from einops import repeat, rearrange
|
| 14 |
-
from omegaconf import OmegaConf
|
| 15 |
-
from typing import Dict, Optional, Tuple, List
|
| 16 |
-
from dataclasses import dataclass
|
| 17 |
-
from .utils import *
|
| 18 |
-
from huggingface_hub import hf_hub_download
|
| 19 |
-
import spaces
|
| 20 |
-
|
| 21 |
-
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
class GenMVImage(object):
|
| 25 |
-
def __init__(self, device):
|
| 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 |
-
if "crm" in self.pipelines.keys():
|
| 41 |
-
pipeline = self.pipelines['crm']
|
| 42 |
-
else:
|
| 43 |
-
self.pipelines['crm'] = TwoStagePipeline(
|
| 44 |
-
stage1_model_config,
|
| 45 |
-
stage1_sampler_config,
|
| 46 |
-
device=self.device,
|
| 47 |
-
dtype=torch.float16
|
| 48 |
-
)
|
| 49 |
-
pipeline = self.pipelines['crm']
|
| 50 |
-
pipeline.set_seed(self.seed)
|
| 51 |
-
rt_dict = pipeline(image, scale=self.guidance_scale, step=self.step)
|
| 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 |
-
sys.path.append(f"{parent_dir}/apps/third_party/mvdream_diffusers")
|
| 58 |
-
|
| 59 |
-
from .third_party.mvdream_diffusers.pipeline_mvdream import MVDreamPipeline
|
| 60 |
-
if image is None:
|
| 61 |
-
if "mvdream" in self.pipelines.keys():
|
| 62 |
-
pipe_MVDream = self.pipelines['mvdream']
|
| 63 |
-
else:
|
| 64 |
-
self.pipelines['mvdream'] = MVDreamPipeline.from_pretrained(
|
| 65 |
-
"ashawkey/mvdream-sd2.1-diffusers", # remote weights
|
| 66 |
-
torch_dtype=torch.float16,
|
| 67 |
-
trust_remote_code=True,
|
| 68 |
-
)
|
| 69 |
-
self.pipelines['mvdream'] = self.pipelines['mvdream'].to(self.device)
|
| 70 |
-
pipe_MVDream = self.pipelines['mvdream']
|
| 71 |
-
mv_imgs = pipe_MVDream(
|
| 72 |
-
text,
|
| 73 |
-
negative_prompt="ugly, deformed, disfigured, poor details, bad anatomy",
|
| 74 |
-
num_inference_steps=self.step,
|
| 75 |
-
guidance_scale=self.guidance_scale,
|
| 76 |
-
generator = torch.Generator(self.device).manual_seed(self.seed)
|
| 77 |
-
)
|
| 78 |
-
else:
|
| 79 |
-
image = np.array(image)
|
| 80 |
-
image = image.astype(np.float32) / 255.0
|
| 81 |
-
image = image[..., :3] * image[..., 3:4] + (1 - image[..., 3:4])
|
| 82 |
-
if "imagedream" in self.pipelines.keys():
|
| 83 |
-
pipe_imagedream = self.pipelines['imagedream']
|
| 84 |
-
else:
|
| 85 |
-
self.pipelines['imagedream'] = MVDreamPipeline.from_pretrained(
|
| 86 |
-
"ashawkey/imagedream-ipmv-diffusers", # remote weights
|
| 87 |
-
torch_dtype=torch.float16,
|
| 88 |
-
trust_remote_code=True,
|
| 89 |
-
)
|
| 90 |
-
self.pipelines['imagedream'] = self.pipelines['imagedream'].to(self.device)
|
| 91 |
-
pipe_imagedream = self.pipelines['imagedream']
|
| 92 |
-
mv_imgs = pipe_imagedream(
|
| 93 |
-
text,
|
| 94 |
-
image,
|
| 95 |
-
negative_prompt="ugly, deformed, disfigured, poor details, bad anatomy",
|
| 96 |
-
num_inference_steps=self.step,
|
| 97 |
-
guidance_scale=self.guidance_scale,
|
| 98 |
-
generator = torch.Generator(self.device).manual_seed(self.seed)
|
| 99 |
-
)
|
| 100 |
-
return mv_imgs[1], mv_imgs[2], mv_imgs[3], mv_imgs[0]
|
| 101 |
-
|
| 102 |
-
@spaces.GPU
|
| 103 |
-
def gen_image_from_wonder3d(self, image, crop_size):
|
| 104 |
-
sys.path.append(f"{parent_dir}/apps/third_party/Wonder3D")
|
| 105 |
-
from diffusers import DiffusionPipeline # only tested on diffusers[torch]==0.19.3, may have conflicts with newer versions of diffusers
|
| 106 |
-
|
| 107 |
-
weight_dtype = torch.float16
|
| 108 |
-
batch = prepare_data(image, crop_size)
|
| 109 |
-
|
| 110 |
-
if "wonder3d" in self.pipelines.keys():
|
| 111 |
-
pipeline = self.pipelines['wonder3d']
|
| 112 |
-
else:
|
| 113 |
-
self.pipelines['wonder3d'] = DiffusionPipeline.from_pretrained(
|
| 114 |
-
'flamehaze1115/wonder3d-v1.0', # or use local checkpoint './ckpts'
|
| 115 |
-
custom_pipeline='flamehaze1115/wonder3d-pipeline',
|
| 116 |
-
torch_dtype=torch.float16
|
| 117 |
-
)
|
| 118 |
-
self.pipelines['wonder3d'].unet.enable_xformers_memory_efficient_attention()
|
| 119 |
-
self.pipelines['wonder3d'].to(self.device)
|
| 120 |
-
self.pipelines['wonder3d'].set_progress_bar_config(disable=True)
|
| 121 |
-
pipeline = self.pipelines['wonder3d']
|
| 122 |
-
|
| 123 |
-
generator = torch.Generator(device=pipeline.unet.device).manual_seed(self.seed)
|
| 124 |
-
# repeat (2B, Nv, 3, H, W)
|
| 125 |
-
imgs_in = torch.cat([batch['imgs_in']] * 2, dim=0).to(weight_dtype)
|
| 126 |
-
|
| 127 |
-
# (2B, Nv, Nce)
|
| 128 |
-
camera_embeddings = torch.cat([batch['camera_embeddings']] * 2, dim=0).to(weight_dtype)
|
| 129 |
-
|
| 130 |
-
task_embeddings = torch.cat([batch['normal_task_embeddings'], batch['color_task_embeddings']], dim=0).to(weight_dtype)
|
| 131 |
-
|
| 132 |
-
camera_embeddings = torch.cat([camera_embeddings, task_embeddings], dim=-1).to(weight_dtype)
|
| 133 |
-
|
| 134 |
-
# (B*Nv, 3, H, W)
|
| 135 |
-
imgs_in = rearrange(imgs_in, "Nv C H W -> (Nv) C H W")
|
| 136 |
-
# (B*Nv, Nce)
|
| 137 |
-
|
| 138 |
-
out = pipeline(
|
| 139 |
-
imgs_in,
|
| 140 |
-
generator=generator,
|
| 141 |
-
guidance_scale=self.guidance_scale,
|
| 142 |
-
num_inference_steps=self.step,
|
| 143 |
-
output_type='pt',
|
| 144 |
-
num_images_per_prompt=1,
|
| 145 |
-
**{'eta': 1.0},
|
| 146 |
-
).images
|
| 147 |
-
|
| 148 |
-
bsz = out.shape[0] // 2
|
| 149 |
-
normals_pred = out[:bsz]
|
| 150 |
-
images_pred = out[bsz:]
|
| 151 |
-
|
| 152 |
-
normals_pred = [save_image(normals_pred[i]) for i in range(bsz)]
|
| 153 |
-
images_pred = [save_image(images_pred[i]) for i in range(bsz)]
|
| 154 |
-
|
| 155 |
-
mv_imgs = images_pred
|
| 156 |
-
return mv_imgs[0], mv_imgs[2], mv_imgs[4], mv_imgs[5]
|
| 157 |
-
|
| 158 |
-
@spaces.GPU
|
| 159 |
-
def run(self, mvimg_model, text, image, crop_size, seed, guidance_scale, step):
|
| 160 |
-
self.seed = seed
|
| 161 |
-
self.guidance_scale = guidance_scale
|
| 162 |
-
self.step = step
|
| 163 |
-
if mvimg_model.upper() == "CRM":
|
| 164 |
-
return self.gen_image_from_crm(image)
|
| 165 |
-
elif mvimg_model.upper() == "IMAGEDREAM":
|
| 166 |
-
return self.gen_image_from_mvdream(image, text)
|
| 167 |
-
elif mvimg_model.upper() == "WONDER3D":
|
| 168 |
-
return self.gen_image_from_wonder3d(image, crop_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|