File size: 8,274 Bytes
ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e 17ea36c b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e 17ea36c b789e6e 17ea36c ffbcf9e 17ea36c ffbcf9e b789e6e 17ea36c ffbcf9e b789e6e ffbcf9e b789e6e 5d25b5d 17ea36c b789e6e 17ea36c b789e6e 17ea36c ffbcf9e b789e6e ffbcf9e 17ea36c b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e 17ea36c ffbcf9e 17ea36c ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e b789e6e ffbcf9e |
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 |
import sys
import spaces
sys.path.append("flash3d") # Add the flash3d directory to the system path for importing local modules
from omegaconf import OmegaConf
import gradio as gr
import torch
import torchvision.transforms as TT
import torchvision.transforms.functional as TTF
from huggingface_hub import hf_hub_download
import numpy as np
from networks.gaussian_predictor import GaussianPredictor
from util.vis3d import save_ply
def main():
print("[INFO] Starting main function...")
# Determine if CUDA (GPU) is available and set the device accordingly
if torch.cuda.is_available():
device = "cuda:0"
print("[INFO] CUDA is available. Using GPU device.")
else:
device = "cpu"
print("[INFO] CUDA is not available. Using CPU device.")
# Download model configuration and weights from Hugging Face Hub
print("[INFO] Downloading model configuration...")
model_cfg_path = hf_hub_download(repo_id="einsafutdinov/flash3d",
filename="config_re10k_v1.yaml")
print("[INFO] Downloading model weights...")
model_path = hf_hub_download(repo_id="einsafutdinov/flash3d",
filename="model_re10k_v1.pth")
# Load model configuration using OmegaConf
print("[INFO] Loading model configuration...")
cfg = OmegaConf.load(model_cfg_path)
# Initialize the GaussianPredictor model with the loaded configuration
print("[INFO] Initializing GaussianPredictor model...")
model = GaussianPredictor(cfg)
try:
device = torch.device(device)
model.to(device) # Move the model to the specified device (CPU or GPU)
except Exception as e:
print(f"[ERROR] Failed to set device: {e}")
raise
# Load the pre-trained model weights
print("[INFO] Loading model weights...")
model.load_model(model_path)
# Define transformation functions for image preprocessing
pad_border_fn = TT.Pad((cfg.dataset.pad_border_aug, cfg.dataset.pad_border_aug)) # Padding to augment the image borders
to_tensor = TT.ToTensor() # Convert image to tensor
# Function to check if an image is uploaded by the user
def check_input_image(input_image):
print("[DEBUG] Checking input image...")
if input_image is None:
print("[ERROR] No image uploaded!")
raise gr.Error("No image uploaded!")
print("[INFO] Input image is valid.")
# Function to preprocess the input image before passing it to the model
def preprocess(image, padding_value, resize_height, resize_width):
print("[DEBUG] Preprocessing image...")
# Resize the image to the desired height and width specified in the user input
image = TTF.resize(
image, (resize_height, resize_width),
interpolation=TT.InterpolationMode.BICUBIC
)
# Apply padding to the image
pad_border_fn = TT.Pad((padding_value, padding_value))
image = pad_border_fn(image)
print("[INFO] Image preprocessing complete.")
return image
# Function to reconstruct the 3D model from the input image and export it as a PLY file
@spaces.GPU(duration=120) # Decorator to allocate a GPU for this function during execution
def reconstruct_and_export(image, num_gauss, scale_factor):
"""
Passes image through model, outputs reconstruction in form of a dict of tensors.
"""
print("[DEBUG] Starting reconstruction and export...")
# Convert the preprocessed image to a tensor and move it to the specified device
image = to_tensor(image).to(device).unsqueeze(0)
inputs = {
("color_aug", 0, 0): image,
}
# Pass the image through the model to get the output
print("[INFO] Passing image through the model...")
outputs = model(inputs)
# Export the reconstruction to a PLY file
print(f"[INFO] Saving output to {ply_out_path} with scale factor {scale_factor}...")
save_ply(outputs, ply_out_path, num_gauss=num_gauss, scale_factor=scale_factor)
print("[INFO] Reconstruction and export complete.")
return ply_out_path
# Path to save the output PLY file
ply_out_path = f'./mesh.ply'
# CSS styling for the Gradio interface
css = """
h1 {
text-align: center;
display:block;
}
"""
# Create the Gradio user interface
with gr.Blocks(css=css) as demo:
gr.Markdown(
"""
# Flash3D
"""
)
with gr.Row(variant="panel"):
with gr.Column(scale=1):
with gr.Row():
# Input image component for the user to upload an image
input_image = gr.Image(
label="Input Image",
image_mode="RGBA",
sources="upload",
type="pil",
elem_id="content_image",
)
with gr.Row():
# Sliders for configurable parameters
num_gauss = gr.Slider(minimum=1, maximum=20, step=1, label="Number of Gaussians per Pixel", value=10)
scale_factor = gr.Slider(minimum=0.5, maximum=5.0, step=0.1, label="Scale Factor for Model Size", value=1.5, info="Test this range for stability, as extreme values may cause visual distortions or unexpected outputs.")
padding_value = gr.Slider(minimum=0, maximum=128, step=8, label="Padding Amount for Output Processing", value=32)
resize_height = gr.Slider(minimum=256, maximum=1024, step=64, label="Resize Height for Image", value=cfg.dataset.height)
resize_width = gr.Slider(minimum=256, maximum=1024, step=64, label="Resize Width for Image", value=cfg.dataset.width)
with gr.Row():
# Button to trigger the generation process
submit = gr.Button("Generate", elem_id="generate", variant="primary")
with gr.Row(variant="panel"):
# Examples panel to provide sample images for users
gr.Examples(
examples=[
'./demo_examples/bedroom_01.png',
'./demo_examples/kitti_02.png',
'./demo_examples/kitti_03.png',
'./demo_examples/re10k_04.jpg',
'./demo_examples/re10k_05.jpg',
'./demo_examples/re10k_06.jpg',
],
inputs=[input_image],
cache_examples=False,
label="Examples",
examples_per_page=20,
)
with gr.Row():
# Display the preprocessed image (after resizing and padding)
processed_image = gr.Image(label="Processed Image", interactive=False)
with gr.Column(scale=2):
with gr.Row():
with gr.Tab("Reconstruction"):
# 3D model viewer to display the reconstructed model
output_model = gr.Model3D(
height=512,
label="Output Model",
interactive=False
)
# Define the workflow for the Generate button
submit.click(fn=check_input_image, inputs=[input_image]).success(
fn=preprocess,
inputs=[input_image, padding_value, resize_height, resize_width],
outputs=[processed_image],
).success(
fn=reconstruct_and_export,
inputs=[processed_image, num_gauss, scale_factor],
outputs=[output_model],
)
# Queue the requests to handle them sequentially (to avoid GPU resource conflicts)
demo.queue(max_size=1)
print("[INFO] Launching Gradio demo...")
demo.launch(share=True) # Launch the Gradio interface and allow public sharing
if __name__ == "__main__":
print("[INFO] Running application...")
main() |