File size: 7,151 Bytes
0f079b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94cabf8
0f079b2
 
 
c459ff0
0f079b2
 
 
 
 
 
 
94cabf8
 
0f079b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94cabf8
0f079b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94cabf8
0f079b2
d3516a7
c459ff0
 
0f079b2
 
 
 
 
 
c459ff0
d3516a7
 
 
c459ff0
0f079b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import gradio as gr
import numpy as np
import torch
import PIL
from PIL import Image
import os
import sys
import rembg
import time
import json
import cv2
from datetime import datetime
from einops import repeat, rearrange
from omegaconf import OmegaConf
from typing import Dict, Optional, Tuple, List
from dataclasses import dataclass
from .utils import *
from huggingface_hub import hf_hub_download
import spaces

parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


class GenMVImage(object):
    def __init__(self, device):
        self.seed = 1024
        self.guidance_scale = 7.5
        self.step = 50
        self.pipelines = {}
        self.device = device
    
    @spaces.GPU
    def gen_image_from_crm(self, image):
        from .third_party.CRM.pipelines import TwoStagePipeline
        stage1_config = OmegaConf.load(f"{parent_dir}/apps/third_party/CRM/configs/nf7_v3_SNR_rd_size_stroke.yaml").config
        stage1_sampler_config = stage1_config.sampler
        stage1_model_config = stage1_config.models
        stage1_model_config.resume = hf_hub_download(repo_id="Zhengyi/CRM", filename="pixel-diffusion.pth", repo_type="model")
        stage1_model_config.config = f"{parent_dir}/apps/third_party/CRM/" + stage1_model_config.config
        if "crm" in self.pipelines.keys():
            pipeline = self.pipelines['crm']
        else:
            self.pipelines['crm'] = TwoStagePipeline(
                                        stage1_model_config,
                                        stage1_sampler_config,
                                        device=self.device,
                                        dtype=torch.float16
                                    )
            pipeline = self.pipelines['crm']
        pipeline.set_seed(self.seed)
        rt_dict = pipeline(image, scale=self.guidance_scale, step=self.step)
        mv_imgs = rt_dict["stage1_images"]
        return mv_imgs[5], mv_imgs[3], mv_imgs[2], mv_imgs[0]
    
    @spaces.GPU
    def gen_image_from_mvdream(self, image, text):
        from .third_party.mvdream_diffusers.pipeline_mvdream import MVDreamPipeline
        if image is None:
            if "mvdream" in self.pipelines.keys():
                pipe_MVDream = self.pipelines['mvdream']
            else:
                self.pipelines['mvdream'] = MVDreamPipeline.from_pretrained(
                    "ashawkey/mvdream-sd2.1-diffusers", # remote weights
                    torch_dtype=torch.float16,
                    trust_remote_code=True,
                )
                self.pipelines['mvdream'] = self.pipelines['mvdream'].to(self.device)
                pipe_MVDream = self.pipelines['mvdream']
            mv_imgs = pipe_MVDream(
                    text,
                    negative_prompt="ugly, deformed, disfigured, poor details, bad anatomy",
                    num_inference_steps=self.step,
                    guidance_scale=self.guidance_scale,
                    generator = torch.Generator(self.device).manual_seed(self.seed)
                )
        else:
            image = np.array(image)
            image = image.astype(np.float32) / 255.0
            image = image[..., :3] * image[..., 3:4] + (1 - image[..., 3:4])
            if "imagedream" in self.pipelines.keys():
                pipe_imagedream = self.pipelines['imagedream']
            else:
                self.pipelines['imagedream'] = MVDreamPipeline.from_pretrained(
                        "ashawkey/imagedream-ipmv-diffusers", # remote weights
                        torch_dtype=torch.float16,
                        trust_remote_code=True,
                    )
                self.pipelines['imagedream'] = self.pipelines['imagedream'].to(self.device)
                pipe_imagedream = self.pipelines['imagedream']
            mv_imgs = pipe_imagedream(
                        text,
                        image,
                        negative_prompt="ugly, deformed, disfigured, poor details, bad anatomy",
                        num_inference_steps=self.step,
                        guidance_scale=self.guidance_scale,
                        generator = torch.Generator(self.device).manual_seed(self.seed)
                    )
        return mv_imgs[1], mv_imgs[2], mv_imgs[3], mv_imgs[0]
    
    @spaces.GPU
    def gen_image_from_wonder3d(self, image, crop_size):
        sys.path.append(f"{parent_dir}/apps/third_party/Wonder3D")
        from diffusers import DiffusionPipeline  # only tested on diffusers[torch]==0.19.3, may have conflicts with newer versions of diffusers

        weight_dtype = torch.float16
        batch = prepare_data(image, crop_size)

        if "wonder3d" in self.pipelines.keys():
            pipeline = self.pipelines['wonder3d']
        else:
            self.pipelines['wonder3d'] =  DiffusionPipeline.from_pretrained(
                'flamehaze1115/wonder3d-v1.0', # or use local checkpoint './ckpts'
                custom_pipeline='flamehaze1115/wonder3d-pipeline',
                torch_dtype=torch.float16
            )
            self.pipelines['wonder3d'].unet.enable_xformers_memory_efficient_attention()
            self.pipelines['wonder3d'].to(self.device)
            self.pipelines['wonder3d'].set_progress_bar_config(disable=True)
            pipeline = self.pipelines['wonder3d']
            
        generator = torch.Generator(device=pipeline.unet.device).manual_seed(self.seed)
        # repeat  (2B, Nv, 3, H, W)
        imgs_in = torch.cat([batch['imgs_in']] * 2, dim=0).to(weight_dtype)

        # (2B, Nv, Nce)
        camera_embeddings = torch.cat([batch['camera_embeddings']] * 2, dim=0).to(weight_dtype)

        task_embeddings = torch.cat([batch['normal_task_embeddings'], batch['color_task_embeddings']], dim=0).to(weight_dtype)

        camera_embeddings = torch.cat([camera_embeddings, task_embeddings], dim=-1).to(weight_dtype)

        # (B*Nv, 3, H, W)
        imgs_in = rearrange(imgs_in, "Nv C H W -> (Nv) C H W")
        # (B*Nv, Nce)

        out = pipeline(
            imgs_in,
            # camera_embeddings,
            generator=generator,
            guidance_scale=self.guidance_scale,
            num_inference_steps=self.step,
            output_type='pt',
            num_images_per_prompt=1,
            **{'eta': 1.0},
        ).images

        bsz = out.shape[0] // 2
        normals_pred = out[:bsz]
        images_pred = out[bsz:]

        normals_pred = [save_image(normals_pred[i]) for i in range(bsz)]
        images_pred = [save_image(images_pred[i]) for i in range(bsz)]

        mv_imgs = images_pred
        return mv_imgs[0], mv_imgs[2], mv_imgs[4], mv_imgs[5]
        
    def run(self, mvimg_model, text, image, crop_size, seed, guidance_scale, step):
        self.seed = seed
        self.guidance_scale = guidance_scale
        self.step = step
        if mvimg_model.upper() == "CRM":
            return self.gen_image_from_crm(image)
        elif mvimg_model.upper() == "IMAGEDREAM":
            return self.gen_image_from_mvdream(image, text)
        elif mvimg_model.upper() == "WONDER3D":
            return self.gen_image_from_wonder3d(image, crop_size)