File size: 6,223 Bytes
222734d
 
 
 
 
 
053509f
222734d
 
f2dd2b8
222734d
f2dd2b8
222734d
 
790bf34
222734d
 
 
 
 
 
 
 
 
1d88534
 
 
 
 
 
 
 
222734d
 
 
 
 
 
 
790bf34
 
 
 
 
 
 
 
 
 
 
f2dd2b8
790bf34
 
 
 
 
 
 
 
 
 
222734d
 
 
 
 
 
 
 
 
 
 
 
 
790bf34
 
 
 
 
 
 
 
 
 
 
222734d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2dd2b8
222734d
 
 
 
 
 
 
 
 
 
 
1d88534
222734d
 
 
 
 
 
1d88534
 
 
 
222734d
 
 
1d88534
222734d
 
 
 
 
 
 
f2dd2b8
222734d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e75ca4
222734d
 
 
 
 
5e75ca4
222734d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
053509f
 
222734d
 
 
06b3db9
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import os
import sys
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = "1"
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))


import gradio as gr
import numpy as np
import torch
from olbedo import OlbedoIIDOutput, OlbedoIIDPipeline
from src.util.image_util import read_img_from_file, img_hwc2chw, img_linear2srgb, is_hdr
from olbedo.util.image_util import float2int
from src.util.seeding import seed_all
import logging
from huggingface_hub import snapshot_download

seed = 1234
seed_all(seed)
if torch.cuda.is_available():
    device = torch.device("cuda")
else:
    device = torch.device("cpu")
    logging.warning("CUDA is not available. Running on CPU will be slow.")

available_models = [
    "marigold_appearance/finetuned",
    "marigold_appearance/pretrained",
    "marigold_lighting/finetuned",
    "marigold_lighting/pretrained",
    "rgbx/finetuned",
    "rgbx/pretrained"
]

loaded_models = {}

prompts = ["Albedo (diffuse basecolor)", "Camera-space Normal","Roughness", "Metallicness","Irradiance (diffuse lighting)"]

def get_demo():

    def load_model(selected_model):
        if selected_model in loaded_models:
            return loaded_models[selected_model]

        local_dir = snapshot_download(
            repo_id="GDAOSU/olbedo",
            allow_patterns=f"{selected_model}/*",
        )

        model_path = os.path.join(local_dir, selected_model)

        pipe = OlbedoIIDPipeline.from_pretrained(
            model_path,
            torch_dtype=torch.float32,
        ).to(device)

        if "rgbx" in selected_model:
            pipe.mode = "rgbx"

        loaded_models[selected_model] = pipe
        return pipe

    def callback(
        photo,
        inference_step,
        selected_model,
        selected_prompt,
        processing_res
    ):
        if "rgbx" in selected_model:
            mode = "rgbx"
            prompt = selected_prompt
        else:
            mode = "other"
            prompt = None
        # if selected_model not in loaded_models:
        #     pipe = MarigoldIIDPipeline.from_pretrained(
        #         f"GDAOSU/olbedo/{selected_model}",
        #         torch_dtype=torch.float32
        #     ).to(device)
        #     pipe.mode = mode
        #     loaded_models[selected_model] = pipe
        # else:
        #     pipe = loaded_models[selected_model]

        pipe = load_model(selected_model)

        generator = torch.Generator(device=device)
        generator.manual_seed(seed)
        img = read_img_from_file(photo)
        if len(img.shape) == 3: 
            img = img_hwc2chw(img)
        if is_hdr(photo):
            img = img_linear2srgb(img)
        if img.shape[0] == 4:
            img = img[:3, :, :]
        rgb_float = torch.from_numpy(img).float()
        input_image = float2int(rgb_float).unsqueeze(0)

        if "rgbx" in selected_model:
            pipe.prompt = prompt

        pipe_out: OlbedoIIDOutput = pipe(
            input_image,
            denoising_steps=inference_step,
            ensemble_size=1,
            processing_res=processing_res,
            match_input_res=1,
            batch_size=0,
            show_progress_bar=False,
            resample_method="bilinear",
            generator=generator,
        )
        target_pred = pipe_out["albedo"].array
        if prompt is not None and ("Metallicness" in prompt or "Roughness" in prompt):
            target_pred = np.repeat(target_pred[0:1,:], 3, axis=0)
        generated_image = target_pred.transpose(1, 2, 0)
        if generated_image.dtype != np.uint8:
            generated_image = np.clip(generated_image, 0, 1)
            generated_image = (generated_image * 255).astype(np.uint8)

        TMP_DIR = "/tmp"
        os.makedirs(TMP_DIR, exist_ok=True)

        npy_path = os.path.join(TMP_DIR, "target_pred.npy")
        np.save(npy_path, target_pred)

        from PIL import Image
        png_path = os.path.join(TMP_DIR, "target_pred.png")
        Image.fromarray(generated_image).save(png_path)

        return png_path, npy_path, generated_image

    block = gr.Blocks()
    with block:
        with gr.Row():
            gr.Markdown("## Olbedo: An Albedo and Shading Aerial Dataset for Large-Scale Outdoor Environments")
        with gr.Row():
            # Input side
            with gr.Column():
                gr.Markdown("### Given Image")
                photo = gr.Image(label="Photo",type="filepath")

                gr.Markdown("### Parameters")
                run_button = gr.Button(value="Run")
                with gr.Accordion("Advanced options", open=False):
                    inference_step = gr.Slider(
                        label="Inference Step",
                        minimum=1,
                        maximum=100,
                        step=1,
                        value=4,
                    )
                    processing_res = gr.Number(value=1000, label="Processing Resolution (processing_res)", precision=0)

                gr.Markdown("### Select Model")
                model_selector = gr.Dropdown(
                    label="Checkpoint",
                    choices=available_models,
                    value="rgbx/finetuned"
                )

                gr.Markdown("### Select Prompt (only for rgbx models)")
                prompt_selector = gr.Dropdown(
                    label="Prompts",
                    choices=prompts,
                    value=prompts[0]
                )

            # Output side
            with gr.Column():
                gr.Markdown("### Output Gallery")
                result_image = gr.Image(label="Output Image", interactive=False)
                result_png = gr.File(label="Download Generated Image (.png)")
                result_npy = gr.File(label="Download Target Albedo (.npy)")

        inputs = [
            photo,
            inference_step,
            model_selector,
            prompt_selector,
            processing_res
        ]
        outputs = [result_png, result_npy, result_image]
        run_button.click(fn=callback, inputs=inputs, outputs=outputs, queue=True)

    return block


if __name__ == "__main__":
    demo = get_demo()
    demo.queue(max_size=1)
    demo.launch()