einsafutdinov commited on
Commit
0f8b4e3
1 Parent(s): 201e424

v0.01 demo

Browse files
app.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ sys.path.append("flash3d")
3
+
4
+ from omegaconf import OmegaConf
5
+ import gradio as gr
6
+ import spaces
7
+ import torch
8
+ import torchvision.transforms as TT
9
+ import torchvision.transforms.functional as TTF
10
+ from huggingface_hub import hf_hub_download
11
+
12
+ from networks.gaussian_predictor import GaussianPredictor
13
+ from util.vis3d import save_ply
14
+
15
+
16
+ def main():
17
+ if torch.cuda.is_available():
18
+ device = "cuda:0"
19
+ else:
20
+ device = "cpu"
21
+
22
+ model_cfg_path = hf_hub_download(repo_id="einsafutdinov/flash3d",
23
+ filename="config_re10k_v1.yaml")
24
+ model_path = hf_hub_download(repo_id="einsafutdinov/flash3d",
25
+ filename="model_re10k_v1.pth")
26
+
27
+ cfg = OmegaConf.load(model_cfg_path)
28
+ model = GaussianPredictor(cfg)
29
+ device = torch.device("cuda:0")
30
+ model.to(device)
31
+ model.load_model(model_path)
32
+
33
+ pad_border_fn = TT.Pad((cfg.dataset.pad_border_aug, cfg.dataset.pad_border_aug))
34
+ to_tensor = TT.ToTensor()
35
+
36
+ def check_input_image(input_image):
37
+ if input_image is None:
38
+ raise gr.Error("No image uploaded!")
39
+
40
+ def preprocess(image):
41
+ image = TTF.resize(
42
+ image, (cfg.dataset.height, cfg.dataset.width),
43
+ interpolation=TT.InterpolationMode.BICUBIC
44
+ )
45
+ image = pad_border_fn(image)
46
+ return image
47
+
48
+ @spaces.GPU()
49
+ def reconstruct_and_export(image):
50
+ """
51
+ Passes image through model, outputs reconstruction in form of a dict of tensors.
52
+ """
53
+ image = to_tensor(image).to(device).unsqueeze(0)
54
+ inputs = {
55
+ ("color_aug", 0, 0): image,
56
+ }
57
+
58
+ outputs = model(inputs)
59
+
60
+ # export reconstruction to ply
61
+ save_ply(outputs, ply_out_path, num_gauss=2)
62
+
63
+ return ply_out_path
64
+
65
+ ply_out_path = f'./mesh.ply'
66
+
67
+ css = """
68
+ h1 {
69
+ text-align: center;
70
+ display:block;
71
+ }
72
+ """
73
+
74
+ with gr.Blocks(css=css) as demo:
75
+ gr.Markdown(
76
+ """
77
+ # Flash3D
78
+ """
79
+ )
80
+ with gr.Row(variant="panel"):
81
+ with gr.Column(scale=1):
82
+ with gr.Row():
83
+ input_image = gr.Image(
84
+ label="Input Image",
85
+ image_mode="RGBA",
86
+ sources="upload",
87
+ type="pil",
88
+ elem_id="content_image",
89
+ )
90
+ with gr.Row():
91
+ submit = gr.Button("Generate", elem_id="generate", variant="primary")
92
+
93
+ with gr.Row(variant="panel"):
94
+ gr.Examples(
95
+ examples=[
96
+ './demo_examples/bedroom_01.png',
97
+ './demo_examples/kitti_02.png',
98
+ './demo_examples/kitti_03.png',
99
+ './demo_examples/re10k_04.jpg',
100
+ './demo_examples/re10k_05.jpg',
101
+ './demo_examples/re10k_06.jpg',
102
+ ],
103
+ inputs=[input_image],
104
+ cache_examples=False,
105
+ label="Examples",
106
+ examples_per_page=20,
107
+ )
108
+
109
+ with gr.Row():
110
+ processed_image = gr.Image(label="Processed Image", interactive=False)
111
+
112
+ with gr.Column(scale=2):
113
+ with gr.Row():
114
+ with gr.Tab("Reconstruction"):
115
+ output_model = gr.Model3D(
116
+ height=512,
117
+ label="Output Model",
118
+ interactive=False
119
+ )
120
+
121
+ # gr.Markdown(
122
+ # """
123
+ # ## Comments:
124
+ # 1. If you run the demo online, the first example you upload should take about 4.5 seconds (with preprocessing, saving and overhead), the following take about 1.5s.
125
+ # 2. The 3D viewer shows a .ply mesh extracted from a mix of 3D Gaussians. This is only an approximations and artefacts might show.
126
+ # 3. Known limitations include:
127
+ # - a black dot appearing on the model from some viewpoints
128
+ # - see-through parts of objects, especially on the back: this is due to the model performing less well on more complicated shapes
129
+ # - back of objects are blurry: this is a model limiation due to it being deterministic
130
+ # 4. Our model is of comparable quality to state-of-the-art methods, and is **much** cheaper to train and run.
131
+ # ## How does it work?
132
+ # Splatter Image formulates 3D reconstruction as an image-to-image translation task. It maps the input image to another image,
133
+ # in which every pixel represents one 3D Gaussian and the channels of the output represent parameters of these Gaussians, including their shapes, colours and locations.
134
+ # The resulting image thus represents a set of Gaussians (almost like a point cloud) which reconstruct the shape and colour of the object.
135
+ # The method is very cheap: the reconstruction amounts to a single forward pass of a neural network with only 2D operators (2D convolutions and attention).
136
+ # The rendering is also very fast, due to using Gaussian Splatting.
137
+ # Combined, this results in very cheap training and high-quality results.
138
+ # For more results see the [project page](https://szymanowiczs.github.io/splatter-image) and the [CVPR article](https://arxiv.org/abs/2312.13150).
139
+ # """
140
+ # )
141
+
142
+ submit.click(fn=check_input_image, inputs=[input_image]).success(
143
+ fn=preprocess,
144
+ inputs=[input_image],
145
+ outputs=[processed_image],
146
+ ).success(
147
+ fn=reconstruct_and_export,
148
+ inputs=[processed_image],
149
+ outputs=[output_model],
150
+ )
151
+
152
+ demo.queue(max_size=1)
153
+ demo.launch(share=True)
154
+
155
+
156
+ if __name__ == "__main__":
157
+ main()
demo_examples/bedroom_01.png ADDED
demo_examples/kitti_02.png ADDED
demo_examples/kitti_03.png ADDED
demo_examples/re10k_04.jpg ADDED
demo_examples/re10k_05.jpg ADDED
demo_examples/re10k_06.jpg ADDED
pre-requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
- --extra-index-url https://download.pytorch.org/whl/cu113
2
- torch
3
- torchvision
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu118
2
+ torch==2.2.2
3
+ torchvision
4
+ torchaudio
5
+ xformers==0.0.25.post1
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ einops
2
+ huggingface-hub>=0.22.0
3
+ imageio
4
+ matplotlib
5
+ safetensors
6
+ scipy
7
+ timm
8
+ tqdm
9
+ wandb
10
+ neptune
11
+ scikit-image
12
+ plyfile
13
+ omegaconf
14
+ jaxtyping
15
+ gradio
16
+ spaces