Spaces:
Runtime error
Runtime error
Commit
·
4d85df4
0
Parent(s):
up
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +28 -0
- .gitignore +4 -0
- README.md +13 -0
- __pycache__/fusion.cpython-37.pyc +0 -0
- app.py +115 -0
- calib.txt +5 -0
- fusion.py +507 -0
- helpers.py +336 -0
- images/08/000010.jpg +0 -0
- images/08/000085.jpg +0 -0
- images/08/000103.jpg +0 -0
- images/08/000187.jpg +0 -0
- images/08/000234.jpg +0 -0
- images/08/000290.jpg +0 -0
- images/08/000465.jpg +0 -0
- images/08/000511.jpg +0 -0
- images/08/000618.jpg +0 -0
- images/08/000790.jpg +0 -0
- images/08/000822.jpg +0 -0
- images/08/001005.jpg +0 -0
- images/08/001122.jpg +0 -0
- images/08/001380.jpg +0 -0
- images/08/001446.jpg +0 -0
- images/08/001530.jpg +0 -0
- images/08/001687.jpg +0 -0
- images/08/001842.jpg +0 -0
- images/08/002010.jpg +0 -0
- images/08/002128.jpg +0 -0
- images/08/002272.jpg +0 -0
- images/08/002360.jpg +0 -0
- images/08/002505.jpg +0 -0
- images/08/002716.jpg +0 -0
- images/08/002944.jpg +0 -0
- images/08/003149.jpg +0 -0
- images/08/003365.jpg +0 -0
- images/08/003533.jpg +0 -0
- images/08/003790.jpg +0 -0
- images/08/003929.jpg +0 -0
- images/08/004059.jpg +0 -0
- monoscene/.ipynb_checkpoints/CRP3D-checkpoint.py +97 -0
- monoscene/.ipynb_checkpoints/config-checkpoint.py +34 -0
- monoscene/.ipynb_checkpoints/modules-checkpoint.py +194 -0
- monoscene/.ipynb_checkpoints/monoscene-checkpoint.py +123 -0
- monoscene/.ipynb_checkpoints/monoscene_model-checkpoint.py +22 -0
- monoscene/.ipynb_checkpoints/unet3d_kitti-checkpoint.py +88 -0
- monoscene/.ipynb_checkpoints/unet3d_nyu-checkpoint.py +90 -0
- monoscene/CRP3D.py +97 -0
- monoscene/DDR.py +139 -0
- monoscene/__init__.py +0 -0
- monoscene/__pycache__/CRP3D.cpython-37.pyc +0 -0
.gitattributes
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
monoscene_kitti.ckpt filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
.ipynb_checkpoints
|
| 3 |
+
*.ckpt
|
| 4 |
+
gradio*
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: MonoScene
|
| 3 |
+
emoji: 🦀
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.0.20
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
__pycache__/fusion.cpython-37.pyc
ADDED
|
Binary file (14.9 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
import torch
|
| 5 |
+
from helpers import *
|
| 6 |
+
import sys
|
| 7 |
+
import csv
|
| 8 |
+
from monoscene.monoscene import MonoScene
|
| 9 |
+
|
| 10 |
+
csv.field_size_limit(sys.maxsize)
|
| 11 |
+
torch.set_grad_enabled(False)
|
| 12 |
+
|
| 13 |
+
# pipeline = pipeline(model="anhquancao/monoscene_kitti")
|
| 14 |
+
# model = AutoModel.from_pretrained(
|
| 15 |
+
# "anhquancao/monoscene_kitti", trust_remote_code=True, revision='bf033f87c2a86b60903ab811b790a1532c1ae313'
|
| 16 |
+
# )#.cuda()
|
| 17 |
+
model = MonoScene.load_from_checkpoint(
|
| 18 |
+
"monoscene_kitti.ckpt",
|
| 19 |
+
dataset="kitti",
|
| 20 |
+
n_classes=20,
|
| 21 |
+
feature = 64,
|
| 22 |
+
project_scale = 2,
|
| 23 |
+
full_scene_size = (256, 256, 32),
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
img_W, img_H = 1220, 370
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def predict(img):
|
| 30 |
+
img = np.array(img, dtype=np.float32, copy=False) / 255.0
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
normalize_rgb = transforms.Compose(
|
| 34 |
+
[
|
| 35 |
+
transforms.ToTensor(),
|
| 36 |
+
transforms.Normalize(
|
| 37 |
+
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
|
| 38 |
+
),
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
img = normalize_rgb(img)
|
| 42 |
+
|
| 43 |
+
batch = get_projections(img_W, img_H)
|
| 44 |
+
batch["img"] = img
|
| 45 |
+
for k in batch:
|
| 46 |
+
batch[k] = batch[k].unsqueeze(0)#.cuda()
|
| 47 |
+
|
| 48 |
+
pred = model(batch).squeeze()
|
| 49 |
+
# print(pred.shape)
|
| 50 |
+
pred = majority_pooling(pred, k_size=2)
|
| 51 |
+
fig = draw(pred, batch['fov_mask_2'])
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
return fig
|
| 55 |
+
|
| 56 |
+
description = """
|
| 57 |
+
MonoScene Demo on SemanticKITTI Validation Set (Sequence 08), which uses the <b>camera parameters of Sequence 08</b>. \n
|
| 58 |
+
Due to the <b>CPU-only</b> inference, it might take up to 20s to predict a scene. \n
|
| 59 |
+
The output is <b>downsampled by 2</b> to be able to be rendered in browsers. <b>Darker</b> colors represent the <b>scenery outside the Field of View</b>, i.e. not visible on the image. \n
|
| 60 |
+
Project page: https://cv-rits.github.io/MonoScene/
|
| 61 |
+
"""
|
| 62 |
+
title = "MonoScene: Monocular 3D Semantic Scene Completion"
|
| 63 |
+
article="""
|
| 64 |
+
<center>
|
| 65 |
+
<img src='https://visitor-badge.glitch.me/badge?page_id=anhquancao.MonoScene&left_color=darkmagenta&right_color=purple' alt='visitor badge'>
|
| 66 |
+
</center>
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
examples = [
|
| 70 |
+
'images/08/000010.jpg',
|
| 71 |
+
'images/08/000085.jpg',
|
| 72 |
+
'images/08/000290.jpg',
|
| 73 |
+
'images/08/000465.jpg',
|
| 74 |
+
'images/08/000790.jpg',
|
| 75 |
+
'images/08/001005.jpg',
|
| 76 |
+
'images/08/001380.jpg',
|
| 77 |
+
'images/08/001530.jpg',
|
| 78 |
+
'images/08/002360.jpg',
|
| 79 |
+
'images/08/002505.jpg',
|
| 80 |
+
'images/08/004059.jpg',
|
| 81 |
+
'images/08/003149.jpg',
|
| 82 |
+
'images/08/001446.jpg',
|
| 83 |
+
'images/08/001122.jpg',
|
| 84 |
+
'images/08/003533.jpg',
|
| 85 |
+
'images/08/003365.jpg',
|
| 86 |
+
'images/08/002944.jpg',
|
| 87 |
+
'images/08/000822.jpg',
|
| 88 |
+
'images/08/000103.jpg',
|
| 89 |
+
'images/08/002716.jpg',
|
| 90 |
+
'images/08/000187.jpg',
|
| 91 |
+
'images/08/002128.jpg',
|
| 92 |
+
'images/08/000511.jpg',
|
| 93 |
+
'images/08/000618.jpg',
|
| 94 |
+
'images/08/002010.jpg',
|
| 95 |
+
'images/08/000234.jpg',
|
| 96 |
+
'images/08/001842.jpg',
|
| 97 |
+
'images/08/001687.jpg',
|
| 98 |
+
'images/08/003929.jpg',
|
| 99 |
+
'images/08/002272.jpg',
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
demo = gr.Interface(
|
| 105 |
+
predict,
|
| 106 |
+
gr.Image(shape=(1220, 370)),
|
| 107 |
+
gr.Plot(),
|
| 108 |
+
article=article,
|
| 109 |
+
title=title,
|
| 110 |
+
enable_queue=True,
|
| 111 |
+
examples=examples,
|
| 112 |
+
description=description)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
demo.launch(enable_queue=True, debug=True)
|
calib.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
P0: 7.188560000000e+02 0.000000000000e+00 6.071928000000e+02 0.000000000000e+00 0.000000000000e+00 7.188560000000e+02 1.852157000000e+02 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 1.000000000000e+00 0.000000000000e+00
|
| 2 |
+
P1: 7.188560000000e+02 0.000000000000e+00 6.071928000000e+02 -3.861448000000e+02 0.000000000000e+00 7.188560000000e+02 1.852157000000e+02 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 1.000000000000e+00 0.000000000000e+00
|
| 3 |
+
P2: 7.188560000000e+02 0.000000000000e+00 6.071928000000e+02 4.538225000000e+01 0.000000000000e+00 7.188560000000e+02 1.852157000000e+02 -1.130887000000e-01 0.000000000000e+00 0.000000000000e+00 1.000000000000e+00 3.779761000000e-03
|
| 4 |
+
P3: 7.188560000000e+02 0.000000000000e+00 6.071928000000e+02 -3.372877000000e+02 0.000000000000e+00 7.188560000000e+02 1.852157000000e+02 2.369057000000e+00 0.000000000000e+00 0.000000000000e+00 1.000000000000e+00 4.915215000000e-03
|
| 5 |
+
Tr: 4.276802385584e-04 -9.999672484946e-01 -8.084491683471e-03 -1.198459927713e-02 -7.210626507497e-03 8.081198471645e-03 -9.999413164504e-01 -5.403984729748e-02 9.999738645903e-01 4.859485810390e-04 -7.206933692422e-03 -2.921968648686e-01
|
fusion.py
ADDED
|
@@ -0,0 +1,507 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Most of the code is taken from https://github.com/andyzeng/tsdf-fusion-python/blob/master/fusion.py
|
| 3 |
+
|
| 4 |
+
@inproceedings{zeng20163dmatch,
|
| 5 |
+
title={3DMatch: Learning Local Geometric Descriptors from RGB-D Reconstructions},
|
| 6 |
+
author={Zeng, Andy and Song, Shuran and Nie{\ss}ner, Matthias and Fisher, Matthew and Xiao, Jianxiong and Funkhouser, Thomas},
|
| 7 |
+
booktitle={CVPR},
|
| 8 |
+
year={2017}
|
| 9 |
+
}
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
from numba import njit, prange
|
| 15 |
+
from skimage import measure
|
| 16 |
+
|
| 17 |
+
FUSION_GPU_MODE = 0
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class TSDFVolume:
|
| 21 |
+
"""Volumetric TSDF Fusion of RGB-D Images."""
|
| 22 |
+
|
| 23 |
+
def __init__(self, vol_bnds, voxel_size, use_gpu=True):
|
| 24 |
+
"""Constructor.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
vol_bnds (ndarray): An ndarray of shape (3, 2). Specifies the
|
| 28 |
+
xyz bounds (min/max) in meters.
|
| 29 |
+
voxel_size (float): The volume discretization in meters.
|
| 30 |
+
"""
|
| 31 |
+
vol_bnds = np.asarray(vol_bnds)
|
| 32 |
+
assert vol_bnds.shape == (3, 2), "[!] `vol_bnds` should be of shape (3, 2)."
|
| 33 |
+
|
| 34 |
+
# Define voxel volume parameters
|
| 35 |
+
self._vol_bnds = vol_bnds
|
| 36 |
+
self._voxel_size = float(voxel_size)
|
| 37 |
+
self._trunc_margin = 5 * self._voxel_size # truncation on SDF
|
| 38 |
+
# self._trunc_margin = 10 # truncation on SDF
|
| 39 |
+
self._color_const = 256 * 256
|
| 40 |
+
|
| 41 |
+
# Adjust volume bounds and ensure C-order contiguous
|
| 42 |
+
self._vol_dim = (
|
| 43 |
+
np.ceil((self._vol_bnds[:, 1] - self._vol_bnds[:, 0]) / self._voxel_size)
|
| 44 |
+
.copy(order="C")
|
| 45 |
+
.astype(int)
|
| 46 |
+
)
|
| 47 |
+
self._vol_bnds[:, 1] = self._vol_bnds[:, 0] + self._vol_dim * self._voxel_size
|
| 48 |
+
self._vol_origin = self._vol_bnds[:, 0].copy(order="C").astype(np.float32)
|
| 49 |
+
|
| 50 |
+
print(
|
| 51 |
+
"Voxel volume size: {} x {} x {} - # points: {:,}".format(
|
| 52 |
+
self._vol_dim[0],
|
| 53 |
+
self._vol_dim[1],
|
| 54 |
+
self._vol_dim[2],
|
| 55 |
+
self._vol_dim[0] * self._vol_dim[1] * self._vol_dim[2],
|
| 56 |
+
)
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Initialize pointers to voxel volume in CPU memory
|
| 60 |
+
self._tsdf_vol_cpu = np.zeros(self._vol_dim).astype(np.float32)
|
| 61 |
+
# for computing the cumulative moving average of observations per voxel
|
| 62 |
+
self._weight_vol_cpu = np.zeros(self._vol_dim).astype(np.float32)
|
| 63 |
+
self._color_vol_cpu = np.zeros(self._vol_dim).astype(np.float32)
|
| 64 |
+
|
| 65 |
+
self.gpu_mode = use_gpu and FUSION_GPU_MODE
|
| 66 |
+
|
| 67 |
+
# Copy voxel volumes to GPU
|
| 68 |
+
if self.gpu_mode:
|
| 69 |
+
self._tsdf_vol_gpu = cuda.mem_alloc(self._tsdf_vol_cpu.nbytes)
|
| 70 |
+
cuda.memcpy_htod(self._tsdf_vol_gpu, self._tsdf_vol_cpu)
|
| 71 |
+
self._weight_vol_gpu = cuda.mem_alloc(self._weight_vol_cpu.nbytes)
|
| 72 |
+
cuda.memcpy_htod(self._weight_vol_gpu, self._weight_vol_cpu)
|
| 73 |
+
self._color_vol_gpu = cuda.mem_alloc(self._color_vol_cpu.nbytes)
|
| 74 |
+
cuda.memcpy_htod(self._color_vol_gpu, self._color_vol_cpu)
|
| 75 |
+
|
| 76 |
+
# Cuda kernel function (C++)
|
| 77 |
+
self._cuda_src_mod = SourceModule(
|
| 78 |
+
"""
|
| 79 |
+
__global__ void integrate(float * tsdf_vol,
|
| 80 |
+
float * weight_vol,
|
| 81 |
+
float * color_vol,
|
| 82 |
+
float * vol_dim,
|
| 83 |
+
float * vol_origin,
|
| 84 |
+
float * cam_intr,
|
| 85 |
+
float * cam_pose,
|
| 86 |
+
float * other_params,
|
| 87 |
+
float * color_im,
|
| 88 |
+
float * depth_im) {
|
| 89 |
+
// Get voxel index
|
| 90 |
+
int gpu_loop_idx = (int) other_params[0];
|
| 91 |
+
int max_threads_per_block = blockDim.x;
|
| 92 |
+
int block_idx = blockIdx.z*gridDim.y*gridDim.x+blockIdx.y*gridDim.x+blockIdx.x;
|
| 93 |
+
int voxel_idx = gpu_loop_idx*gridDim.x*gridDim.y*gridDim.z*max_threads_per_block+block_idx*max_threads_per_block+threadIdx.x;
|
| 94 |
+
int vol_dim_x = (int) vol_dim[0];
|
| 95 |
+
int vol_dim_y = (int) vol_dim[1];
|
| 96 |
+
int vol_dim_z = (int) vol_dim[2];
|
| 97 |
+
if (voxel_idx > vol_dim_x*vol_dim_y*vol_dim_z)
|
| 98 |
+
return;
|
| 99 |
+
// Get voxel grid coordinates (note: be careful when casting)
|
| 100 |
+
float voxel_x = floorf(((float)voxel_idx)/((float)(vol_dim_y*vol_dim_z)));
|
| 101 |
+
float voxel_y = floorf(((float)(voxel_idx-((int)voxel_x)*vol_dim_y*vol_dim_z))/((float)vol_dim_z));
|
| 102 |
+
float voxel_z = (float)(voxel_idx-((int)voxel_x)*vol_dim_y*vol_dim_z-((int)voxel_y)*vol_dim_z);
|
| 103 |
+
// Voxel grid coordinates to world coordinates
|
| 104 |
+
float voxel_size = other_params[1];
|
| 105 |
+
float pt_x = vol_origin[0]+voxel_x*voxel_size;
|
| 106 |
+
float pt_y = vol_origin[1]+voxel_y*voxel_size;
|
| 107 |
+
float pt_z = vol_origin[2]+voxel_z*voxel_size;
|
| 108 |
+
// World coordinates to camera coordinates
|
| 109 |
+
float tmp_pt_x = pt_x-cam_pose[0*4+3];
|
| 110 |
+
float tmp_pt_y = pt_y-cam_pose[1*4+3];
|
| 111 |
+
float tmp_pt_z = pt_z-cam_pose[2*4+3];
|
| 112 |
+
float cam_pt_x = cam_pose[0*4+0]*tmp_pt_x+cam_pose[1*4+0]*tmp_pt_y+cam_pose[2*4+0]*tmp_pt_z;
|
| 113 |
+
float cam_pt_y = cam_pose[0*4+1]*tmp_pt_x+cam_pose[1*4+1]*tmp_pt_y+cam_pose[2*4+1]*tmp_pt_z;
|
| 114 |
+
float cam_pt_z = cam_pose[0*4+2]*tmp_pt_x+cam_pose[1*4+2]*tmp_pt_y+cam_pose[2*4+2]*tmp_pt_z;
|
| 115 |
+
// Camera coordinates to image pixels
|
| 116 |
+
int pixel_x = (int) roundf(cam_intr[0*3+0]*(cam_pt_x/cam_pt_z)+cam_intr[0*3+2]);
|
| 117 |
+
int pixel_y = (int) roundf(cam_intr[1*3+1]*(cam_pt_y/cam_pt_z)+cam_intr[1*3+2]);
|
| 118 |
+
// Skip if outside view frustum
|
| 119 |
+
int im_h = (int) other_params[2];
|
| 120 |
+
int im_w = (int) other_params[3];
|
| 121 |
+
if (pixel_x < 0 || pixel_x >= im_w || pixel_y < 0 || pixel_y >= im_h || cam_pt_z<0)
|
| 122 |
+
return;
|
| 123 |
+
// Skip invalid depth
|
| 124 |
+
float depth_value = depth_im[pixel_y*im_w+pixel_x];
|
| 125 |
+
if (depth_value == 0)
|
| 126 |
+
return;
|
| 127 |
+
// Integrate TSDF
|
| 128 |
+
float trunc_margin = other_params[4];
|
| 129 |
+
float depth_diff = depth_value-cam_pt_z;
|
| 130 |
+
if (depth_diff < -trunc_margin)
|
| 131 |
+
return;
|
| 132 |
+
float dist = fmin(1.0f,depth_diff/trunc_margin);
|
| 133 |
+
float w_old = weight_vol[voxel_idx];
|
| 134 |
+
float obs_weight = other_params[5];
|
| 135 |
+
float w_new = w_old + obs_weight;
|
| 136 |
+
weight_vol[voxel_idx] = w_new;
|
| 137 |
+
tsdf_vol[voxel_idx] = (tsdf_vol[voxel_idx]*w_old+obs_weight*dist)/w_new;
|
| 138 |
+
// Integrate color
|
| 139 |
+
float old_color = color_vol[voxel_idx];
|
| 140 |
+
float old_b = floorf(old_color/(256*256));
|
| 141 |
+
float old_g = floorf((old_color-old_b*256*256)/256);
|
| 142 |
+
float old_r = old_color-old_b*256*256-old_g*256;
|
| 143 |
+
float new_color = color_im[pixel_y*im_w+pixel_x];
|
| 144 |
+
float new_b = floorf(new_color/(256*256));
|
| 145 |
+
float new_g = floorf((new_color-new_b*256*256)/256);
|
| 146 |
+
float new_r = new_color-new_b*256*256-new_g*256;
|
| 147 |
+
new_b = fmin(roundf((old_b*w_old+obs_weight*new_b)/w_new),255.0f);
|
| 148 |
+
new_g = fmin(roundf((old_g*w_old+obs_weight*new_g)/w_new),255.0f);
|
| 149 |
+
new_r = fmin(roundf((old_r*w_old+obs_weight*new_r)/w_new),255.0f);
|
| 150 |
+
color_vol[voxel_idx] = new_b*256*256+new_g*256+new_r;
|
| 151 |
+
}"""
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
self._cuda_integrate = self._cuda_src_mod.get_function("integrate")
|
| 155 |
+
|
| 156 |
+
# Determine block/grid size on GPU
|
| 157 |
+
gpu_dev = cuda.Device(0)
|
| 158 |
+
self._max_gpu_threads_per_block = gpu_dev.MAX_THREADS_PER_BLOCK
|
| 159 |
+
n_blocks = int(
|
| 160 |
+
np.ceil(
|
| 161 |
+
float(np.prod(self._vol_dim))
|
| 162 |
+
/ float(self._max_gpu_threads_per_block)
|
| 163 |
+
)
|
| 164 |
+
)
|
| 165 |
+
grid_dim_x = min(gpu_dev.MAX_GRID_DIM_X, int(np.floor(np.cbrt(n_blocks))))
|
| 166 |
+
grid_dim_y = min(
|
| 167 |
+
gpu_dev.MAX_GRID_DIM_Y, int(np.floor(np.sqrt(n_blocks / grid_dim_x)))
|
| 168 |
+
)
|
| 169 |
+
grid_dim_z = min(
|
| 170 |
+
gpu_dev.MAX_GRID_DIM_Z,
|
| 171 |
+
int(np.ceil(float(n_blocks) / float(grid_dim_x * grid_dim_y))),
|
| 172 |
+
)
|
| 173 |
+
self._max_gpu_grid_dim = np.array(
|
| 174 |
+
[grid_dim_x, grid_dim_y, grid_dim_z]
|
| 175 |
+
).astype(int)
|
| 176 |
+
self._n_gpu_loops = int(
|
| 177 |
+
np.ceil(
|
| 178 |
+
float(np.prod(self._vol_dim))
|
| 179 |
+
/ float(
|
| 180 |
+
np.prod(self._max_gpu_grid_dim)
|
| 181 |
+
* self._max_gpu_threads_per_block
|
| 182 |
+
)
|
| 183 |
+
)
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
else:
|
| 187 |
+
# Get voxel grid coordinates
|
| 188 |
+
xv, yv, zv = np.meshgrid(
|
| 189 |
+
range(self._vol_dim[0]),
|
| 190 |
+
range(self._vol_dim[1]),
|
| 191 |
+
range(self._vol_dim[2]),
|
| 192 |
+
indexing="ij",
|
| 193 |
+
)
|
| 194 |
+
self.vox_coords = (
|
| 195 |
+
np.concatenate(
|
| 196 |
+
[xv.reshape(1, -1), yv.reshape(1, -1), zv.reshape(1, -1)], axis=0
|
| 197 |
+
)
|
| 198 |
+
.astype(int)
|
| 199 |
+
.T
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
@staticmethod
|
| 203 |
+
@njit(parallel=True)
|
| 204 |
+
def vox2world(vol_origin, vox_coords, vox_size, offsets=(0.5, 0.5, 0.5)):
|
| 205 |
+
"""Convert voxel grid coordinates to world coordinates."""
|
| 206 |
+
vol_origin = vol_origin.astype(np.float32)
|
| 207 |
+
vox_coords = vox_coords.astype(np.float32)
|
| 208 |
+
# print(np.min(vox_coords))
|
| 209 |
+
cam_pts = np.empty_like(vox_coords, dtype=np.float32)
|
| 210 |
+
|
| 211 |
+
for i in prange(vox_coords.shape[0]):
|
| 212 |
+
for j in range(3):
|
| 213 |
+
cam_pts[i, j] = (
|
| 214 |
+
vol_origin[j]
|
| 215 |
+
+ (vox_size * vox_coords[i, j])
|
| 216 |
+
+ vox_size * offsets[j]
|
| 217 |
+
)
|
| 218 |
+
return cam_pts
|
| 219 |
+
|
| 220 |
+
@staticmethod
|
| 221 |
+
@njit(parallel=True)
|
| 222 |
+
def cam2pix(cam_pts, intr):
|
| 223 |
+
"""Convert camera coordinates to pixel coordinates."""
|
| 224 |
+
intr = intr.astype(np.float32)
|
| 225 |
+
fx, fy = intr[0, 0], intr[1, 1]
|
| 226 |
+
cx, cy = intr[0, 2], intr[1, 2]
|
| 227 |
+
pix = np.empty((cam_pts.shape[0], 2), dtype=np.int64)
|
| 228 |
+
for i in prange(cam_pts.shape[0]):
|
| 229 |
+
pix[i, 0] = int(np.round((cam_pts[i, 0] * fx / cam_pts[i, 2]) + cx))
|
| 230 |
+
pix[i, 1] = int(np.round((cam_pts[i, 1] * fy / cam_pts[i, 2]) + cy))
|
| 231 |
+
return pix
|
| 232 |
+
|
| 233 |
+
@staticmethod
|
| 234 |
+
@njit(parallel=True)
|
| 235 |
+
def integrate_tsdf(tsdf_vol, dist, w_old, obs_weight):
|
| 236 |
+
"""Integrate the TSDF volume."""
|
| 237 |
+
tsdf_vol_int = np.empty_like(tsdf_vol, dtype=np.float32)
|
| 238 |
+
# print(tsdf_vol.shape)
|
| 239 |
+
w_new = np.empty_like(w_old, dtype=np.float32)
|
| 240 |
+
for i in prange(len(tsdf_vol)):
|
| 241 |
+
w_new[i] = w_old[i] + obs_weight
|
| 242 |
+
tsdf_vol_int[i] = (w_old[i] * tsdf_vol[i] + obs_weight * dist[i]) / w_new[i]
|
| 243 |
+
return tsdf_vol_int, w_new
|
| 244 |
+
|
| 245 |
+
def integrate(self, color_im, depth_im, cam_intr, cam_pose, obs_weight=1.0):
|
| 246 |
+
"""Integrate an RGB-D frame into the TSDF volume.
|
| 247 |
+
|
| 248 |
+
Args:
|
| 249 |
+
color_im (ndarray): An RGB image of shape (H, W, 3).
|
| 250 |
+
depth_im (ndarray): A depth image of shape (H, W).
|
| 251 |
+
cam_intr (ndarray): The camera intrinsics matrix of shape (3, 3).
|
| 252 |
+
cam_pose (ndarray): The camera pose (i.e. extrinsics) of shape (4, 4).
|
| 253 |
+
obs_weight (float): The weight to assign for the current observation. A higher
|
| 254 |
+
value
|
| 255 |
+
"""
|
| 256 |
+
im_h, im_w = depth_im.shape
|
| 257 |
+
|
| 258 |
+
# Fold RGB color image into a single channel image
|
| 259 |
+
color_im = color_im.astype(np.float32)
|
| 260 |
+
color_im = np.floor(
|
| 261 |
+
color_im[..., 2] * self._color_const
|
| 262 |
+
+ color_im[..., 1] * 256
|
| 263 |
+
+ color_im[..., 0]
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
+
if self.gpu_mode: # GPU mode: integrate voxel volume (calls CUDA kernel)
|
| 267 |
+
for gpu_loop_idx in range(self._n_gpu_loops):
|
| 268 |
+
self._cuda_integrate(
|
| 269 |
+
self._tsdf_vol_gpu,
|
| 270 |
+
self._weight_vol_gpu,
|
| 271 |
+
self._color_vol_gpu,
|
| 272 |
+
cuda.InOut(self._vol_dim.astype(np.float32)),
|
| 273 |
+
cuda.InOut(self._vol_origin.astype(np.float32)),
|
| 274 |
+
cuda.InOut(cam_intr.reshape(-1).astype(np.float32)),
|
| 275 |
+
cuda.InOut(cam_pose.reshape(-1).astype(np.float32)),
|
| 276 |
+
cuda.InOut(
|
| 277 |
+
np.asarray(
|
| 278 |
+
[
|
| 279 |
+
gpu_loop_idx,
|
| 280 |
+
self._voxel_size,
|
| 281 |
+
im_h,
|
| 282 |
+
im_w,
|
| 283 |
+
self._trunc_margin,
|
| 284 |
+
obs_weight,
|
| 285 |
+
],
|
| 286 |
+
np.float32,
|
| 287 |
+
)
|
| 288 |
+
),
|
| 289 |
+
cuda.InOut(color_im.reshape(-1).astype(np.float32)),
|
| 290 |
+
cuda.InOut(depth_im.reshape(-1).astype(np.float32)),
|
| 291 |
+
block=(self._max_gpu_threads_per_block, 1, 1),
|
| 292 |
+
grid=(
|
| 293 |
+
int(self._max_gpu_grid_dim[0]),
|
| 294 |
+
int(self._max_gpu_grid_dim[1]),
|
| 295 |
+
int(self._max_gpu_grid_dim[2]),
|
| 296 |
+
),
|
| 297 |
+
)
|
| 298 |
+
else: # CPU mode: integrate voxel volume (vectorized implementation)
|
| 299 |
+
# Convert voxel grid coordinates to pixel coordinates
|
| 300 |
+
cam_pts = self.vox2world(
|
| 301 |
+
self._vol_origin, self.vox_coords, self._voxel_size
|
| 302 |
+
)
|
| 303 |
+
cam_pts = rigid_transform(cam_pts, np.linalg.inv(cam_pose))
|
| 304 |
+
pix_z = cam_pts[:, 2]
|
| 305 |
+
pix = self.cam2pix(cam_pts, cam_intr)
|
| 306 |
+
pix_x, pix_y = pix[:, 0], pix[:, 1]
|
| 307 |
+
|
| 308 |
+
# Eliminate pixels outside view frustum
|
| 309 |
+
valid_pix = np.logical_and(
|
| 310 |
+
pix_x >= 0,
|
| 311 |
+
np.logical_and(
|
| 312 |
+
pix_x < im_w,
|
| 313 |
+
np.logical_and(pix_y >= 0, np.logical_and(pix_y < im_h, pix_z > 0)),
|
| 314 |
+
),
|
| 315 |
+
)
|
| 316 |
+
depth_val = np.zeros(pix_x.shape)
|
| 317 |
+
depth_val[valid_pix] = depth_im[pix_y[valid_pix], pix_x[valid_pix]]
|
| 318 |
+
|
| 319 |
+
# Integrate TSDF
|
| 320 |
+
depth_diff = depth_val - pix_z
|
| 321 |
+
|
| 322 |
+
valid_pts = np.logical_and(depth_val > 0, depth_diff >= -10)
|
| 323 |
+
dist = depth_diff
|
| 324 |
+
|
| 325 |
+
valid_vox_x = self.vox_coords[valid_pts, 0]
|
| 326 |
+
valid_vox_y = self.vox_coords[valid_pts, 1]
|
| 327 |
+
valid_vox_z = self.vox_coords[valid_pts, 2]
|
| 328 |
+
w_old = self._weight_vol_cpu[valid_vox_x, valid_vox_y, valid_vox_z]
|
| 329 |
+
tsdf_vals = self._tsdf_vol_cpu[valid_vox_x, valid_vox_y, valid_vox_z]
|
| 330 |
+
valid_dist = dist[valid_pts]
|
| 331 |
+
tsdf_vol_new, w_new = self.integrate_tsdf(
|
| 332 |
+
tsdf_vals, valid_dist, w_old, obs_weight
|
| 333 |
+
)
|
| 334 |
+
self._weight_vol_cpu[valid_vox_x, valid_vox_y, valid_vox_z] = w_new
|
| 335 |
+
self._tsdf_vol_cpu[valid_vox_x, valid_vox_y, valid_vox_z] = tsdf_vol_new
|
| 336 |
+
|
| 337 |
+
# Integrate color
|
| 338 |
+
old_color = self._color_vol_cpu[valid_vox_x, valid_vox_y, valid_vox_z]
|
| 339 |
+
old_b = np.floor(old_color / self._color_const)
|
| 340 |
+
old_g = np.floor((old_color - old_b * self._color_const) / 256)
|
| 341 |
+
old_r = old_color - old_b * self._color_const - old_g * 256
|
| 342 |
+
new_color = color_im[pix_y[valid_pts], pix_x[valid_pts]]
|
| 343 |
+
new_b = np.floor(new_color / self._color_const)
|
| 344 |
+
new_g = np.floor((new_color - new_b * self._color_const) / 256)
|
| 345 |
+
new_r = new_color - new_b * self._color_const - new_g * 256
|
| 346 |
+
new_b = np.minimum(
|
| 347 |
+
255.0, np.round((w_old * old_b + obs_weight * new_b) / w_new)
|
| 348 |
+
)
|
| 349 |
+
new_g = np.minimum(
|
| 350 |
+
255.0, np.round((w_old * old_g + obs_weight * new_g) / w_new)
|
| 351 |
+
)
|
| 352 |
+
new_r = np.minimum(
|
| 353 |
+
255.0, np.round((w_old * old_r + obs_weight * new_r) / w_new)
|
| 354 |
+
)
|
| 355 |
+
self._color_vol_cpu[valid_vox_x, valid_vox_y, valid_vox_z] = (
|
| 356 |
+
new_b * self._color_const + new_g * 256 + new_r
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
def get_volume(self):
|
| 360 |
+
if self.gpu_mode:
|
| 361 |
+
cuda.memcpy_dtoh(self._tsdf_vol_cpu, self._tsdf_vol_gpu)
|
| 362 |
+
cuda.memcpy_dtoh(self._color_vol_cpu, self._color_vol_gpu)
|
| 363 |
+
return self._tsdf_vol_cpu, self._color_vol_cpu
|
| 364 |
+
|
| 365 |
+
def get_point_cloud(self):
|
| 366 |
+
"""Extract a point cloud from the voxel volume."""
|
| 367 |
+
tsdf_vol, color_vol = self.get_volume()
|
| 368 |
+
|
| 369 |
+
# Marching cubes
|
| 370 |
+
verts = measure.marching_cubes_lewiner(tsdf_vol, level=0)[0]
|
| 371 |
+
verts_ind = np.round(verts).astype(int)
|
| 372 |
+
verts = verts * self._voxel_size + self._vol_origin
|
| 373 |
+
|
| 374 |
+
# Get vertex colors
|
| 375 |
+
rgb_vals = color_vol[verts_ind[:, 0], verts_ind[:, 1], verts_ind[:, 2]]
|
| 376 |
+
colors_b = np.floor(rgb_vals / self._color_const)
|
| 377 |
+
colors_g = np.floor((rgb_vals - colors_b * self._color_const) / 256)
|
| 378 |
+
colors_r = rgb_vals - colors_b * self._color_const - colors_g * 256
|
| 379 |
+
colors = np.floor(np.asarray([colors_r, colors_g, colors_b])).T
|
| 380 |
+
colors = colors.astype(np.uint8)
|
| 381 |
+
|
| 382 |
+
pc = np.hstack([verts, colors])
|
| 383 |
+
return pc
|
| 384 |
+
|
| 385 |
+
def get_mesh(self):
|
| 386 |
+
"""Compute a mesh from the voxel volume using marching cubes."""
|
| 387 |
+
tsdf_vol, color_vol = self.get_volume()
|
| 388 |
+
|
| 389 |
+
# Marching cubes
|
| 390 |
+
verts, faces, norms, vals = measure.marching_cubes_lewiner(tsdf_vol, level=0)
|
| 391 |
+
verts_ind = np.round(verts).astype(int)
|
| 392 |
+
verts = (
|
| 393 |
+
verts * self._voxel_size + self._vol_origin
|
| 394 |
+
) # voxel grid coordinates to world coordinates
|
| 395 |
+
|
| 396 |
+
# Get vertex colors
|
| 397 |
+
rgb_vals = color_vol[verts_ind[:, 0], verts_ind[:, 1], verts_ind[:, 2]]
|
| 398 |
+
colors_b = np.floor(rgb_vals / self._color_const)
|
| 399 |
+
colors_g = np.floor((rgb_vals - colors_b * self._color_const) / 256)
|
| 400 |
+
colors_r = rgb_vals - colors_b * self._color_const - colors_g * 256
|
| 401 |
+
colors = np.floor(np.asarray([colors_r, colors_g, colors_b])).T
|
| 402 |
+
colors = colors.astype(np.uint8)
|
| 403 |
+
return verts, faces, norms, colors
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
def rigid_transform(xyz, transform):
|
| 407 |
+
"""Applies a rigid transform to an (N, 3) pointcloud."""
|
| 408 |
+
xyz_h = np.hstack([xyz, np.ones((len(xyz), 1), dtype=np.float32)])
|
| 409 |
+
xyz_t_h = np.dot(transform, xyz_h.T).T
|
| 410 |
+
return xyz_t_h[:, :3]
|
| 411 |
+
|
| 412 |
+
|
| 413 |
+
def get_view_frustum(depth_im, cam_intr, cam_pose):
|
| 414 |
+
"""Get corners of 3D camera view frustum of depth image"""
|
| 415 |
+
im_h = depth_im.shape[0]
|
| 416 |
+
im_w = depth_im.shape[1]
|
| 417 |
+
max_depth = np.max(depth_im)
|
| 418 |
+
view_frust_pts = np.array(
|
| 419 |
+
[
|
| 420 |
+
(np.array([0, 0, 0, im_w, im_w]) - cam_intr[0, 2])
|
| 421 |
+
* np.array([0, max_depth, max_depth, max_depth, max_depth])
|
| 422 |
+
/ cam_intr[0, 0],
|
| 423 |
+
(np.array([0, 0, im_h, 0, im_h]) - cam_intr[1, 2])
|
| 424 |
+
* np.array([0, max_depth, max_depth, max_depth, max_depth])
|
| 425 |
+
/ cam_intr[1, 1],
|
| 426 |
+
np.array([0, max_depth, max_depth, max_depth, max_depth]),
|
| 427 |
+
]
|
| 428 |
+
)
|
| 429 |
+
view_frust_pts = rigid_transform(view_frust_pts.T, cam_pose).T
|
| 430 |
+
return view_frust_pts
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
def meshwrite(filename, verts, faces, norms, colors):
|
| 434 |
+
"""Save a 3D mesh to a polygon .ply file."""
|
| 435 |
+
# Write header
|
| 436 |
+
ply_file = open(filename, "w")
|
| 437 |
+
ply_file.write("ply\n")
|
| 438 |
+
ply_file.write("format ascii 1.0\n")
|
| 439 |
+
ply_file.write("element vertex %d\n" % (verts.shape[0]))
|
| 440 |
+
ply_file.write("property float x\n")
|
| 441 |
+
ply_file.write("property float y\n")
|
| 442 |
+
ply_file.write("property float z\n")
|
| 443 |
+
ply_file.write("property float nx\n")
|
| 444 |
+
ply_file.write("property float ny\n")
|
| 445 |
+
ply_file.write("property float nz\n")
|
| 446 |
+
ply_file.write("property uchar red\n")
|
| 447 |
+
ply_file.write("property uchar green\n")
|
| 448 |
+
ply_file.write("property uchar blue\n")
|
| 449 |
+
ply_file.write("element face %d\n" % (faces.shape[0]))
|
| 450 |
+
ply_file.write("property list uchar int vertex_index\n")
|
| 451 |
+
ply_file.write("end_header\n")
|
| 452 |
+
|
| 453 |
+
# Write vertex list
|
| 454 |
+
for i in range(verts.shape[0]):
|
| 455 |
+
ply_file.write(
|
| 456 |
+
"%f %f %f %f %f %f %d %d %d\n"
|
| 457 |
+
% (
|
| 458 |
+
verts[i, 0],
|
| 459 |
+
verts[i, 1],
|
| 460 |
+
verts[i, 2],
|
| 461 |
+
norms[i, 0],
|
| 462 |
+
norms[i, 1],
|
| 463 |
+
norms[i, 2],
|
| 464 |
+
colors[i, 0],
|
| 465 |
+
colors[i, 1],
|
| 466 |
+
colors[i, 2],
|
| 467 |
+
)
|
| 468 |
+
)
|
| 469 |
+
|
| 470 |
+
# Write face list
|
| 471 |
+
for i in range(faces.shape[0]):
|
| 472 |
+
ply_file.write("3 %d %d %d\n" % (faces[i, 0], faces[i, 1], faces[i, 2]))
|
| 473 |
+
|
| 474 |
+
ply_file.close()
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
def pcwrite(filename, xyzrgb):
|
| 478 |
+
"""Save a point cloud to a polygon .ply file."""
|
| 479 |
+
xyz = xyzrgb[:, :3]
|
| 480 |
+
rgb = xyzrgb[:, 3:].astype(np.uint8)
|
| 481 |
+
|
| 482 |
+
# Write header
|
| 483 |
+
ply_file = open(filename, "w")
|
| 484 |
+
ply_file.write("ply\n")
|
| 485 |
+
ply_file.write("format ascii 1.0\n")
|
| 486 |
+
ply_file.write("element vertex %d\n" % (xyz.shape[0]))
|
| 487 |
+
ply_file.write("property float x\n")
|
| 488 |
+
ply_file.write("property float y\n")
|
| 489 |
+
ply_file.write("property float z\n")
|
| 490 |
+
ply_file.write("property uchar red\n")
|
| 491 |
+
ply_file.write("property uchar green\n")
|
| 492 |
+
ply_file.write("property uchar blue\n")
|
| 493 |
+
ply_file.write("end_header\n")
|
| 494 |
+
|
| 495 |
+
# Write vertex list
|
| 496 |
+
for i in range(xyz.shape[0]):
|
| 497 |
+
ply_file.write(
|
| 498 |
+
"%f %f %f %d %d %d\n"
|
| 499 |
+
% (
|
| 500 |
+
xyz[i, 0],
|
| 501 |
+
xyz[i, 1],
|
| 502 |
+
xyz[i, 2],
|
| 503 |
+
rgb[i, 0],
|
| 504 |
+
rgb[i, 1],
|
| 505 |
+
rgb[i, 2],
|
| 506 |
+
)
|
| 507 |
+
)
|
helpers.py
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torch
|
| 3 |
+
import fusion
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
|
| 8 |
+
def read_calib(calib_path):
|
| 9 |
+
"""
|
| 10 |
+
Modify from https://github.com/utiasSTARS/pykitti/blob/d3e1bb81676e831886726cc5ed79ce1f049aef2c/pykitti/utils.py#L68
|
| 11 |
+
:param calib_path: Path to a calibration text file.
|
| 12 |
+
:return: dict with calibration matrices.
|
| 13 |
+
"""
|
| 14 |
+
calib_all = {}
|
| 15 |
+
with open(calib_path, "r") as f:
|
| 16 |
+
for line in f.readlines():
|
| 17 |
+
if line == "\n":
|
| 18 |
+
break
|
| 19 |
+
key, value = line.split(":", 1)
|
| 20 |
+
calib_all[key] = np.array([float(x) for x in value.split()])
|
| 21 |
+
|
| 22 |
+
# reshape matrices
|
| 23 |
+
calib_out = {}
|
| 24 |
+
# 3x4 projection matrix for left camera
|
| 25 |
+
calib_out["P2"] = calib_all["P2"].reshape(3, 4)
|
| 26 |
+
calib_out["Tr"] = np.identity(4) # 4x4 matrix
|
| 27 |
+
calib_out["Tr"][:3, :4] = calib_all["Tr"].reshape(3, 4)
|
| 28 |
+
return calib_out
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def vox2pix(cam_E, cam_k,
|
| 32 |
+
vox_origin, voxel_size,
|
| 33 |
+
img_W, img_H,
|
| 34 |
+
scene_size):
|
| 35 |
+
"""
|
| 36 |
+
compute the 2D projection of voxels centroids
|
| 37 |
+
|
| 38 |
+
Parameters:
|
| 39 |
+
----------
|
| 40 |
+
cam_E: 4x4
|
| 41 |
+
=camera pose in case of NYUv2 dataset
|
| 42 |
+
=Transformation from camera to lidar coordinate in case of SemKITTI
|
| 43 |
+
cam_k: 3x3
|
| 44 |
+
camera intrinsics
|
| 45 |
+
vox_origin: (3,)
|
| 46 |
+
world(NYU)/lidar(SemKITTI) cooridnates of the voxel at index (0, 0, 0)
|
| 47 |
+
img_W: int
|
| 48 |
+
image width
|
| 49 |
+
img_H: int
|
| 50 |
+
image height
|
| 51 |
+
scene_size: (3,)
|
| 52 |
+
scene size in meter: (51.2, 51.2, 6.4) for SemKITTI and (4.8, 4.8, 2.88) for NYUv2
|
| 53 |
+
|
| 54 |
+
Returns
|
| 55 |
+
-------
|
| 56 |
+
projected_pix: (N, 2)
|
| 57 |
+
Projected 2D positions of voxels
|
| 58 |
+
fov_mask: (N,)
|
| 59 |
+
Voxels mask indice voxels inside image's FOV
|
| 60 |
+
pix_z: (N,)
|
| 61 |
+
Voxels'distance to the sensor in meter
|
| 62 |
+
"""
|
| 63 |
+
# Compute the x, y, z bounding of the scene in meter
|
| 64 |
+
vol_bnds = np.zeros((3,2))
|
| 65 |
+
vol_bnds[:,0] = vox_origin
|
| 66 |
+
vol_bnds[:,1] = vox_origin + np.array(scene_size)
|
| 67 |
+
|
| 68 |
+
# Compute the voxels centroids in lidar cooridnates
|
| 69 |
+
vol_dim = np.ceil((vol_bnds[:,1]- vol_bnds[:,0])/ voxel_size).copy(order='C').astype(int)
|
| 70 |
+
xv, yv, zv = np.meshgrid(
|
| 71 |
+
range(vol_dim[0]),
|
| 72 |
+
range(vol_dim[1]),
|
| 73 |
+
range(vol_dim[2]),
|
| 74 |
+
indexing='ij'
|
| 75 |
+
)
|
| 76 |
+
vox_coords = np.concatenate([
|
| 77 |
+
xv.reshape(1,-1),
|
| 78 |
+
yv.reshape(1,-1),
|
| 79 |
+
zv.reshape(1,-1)
|
| 80 |
+
], axis=0).astype(int).T
|
| 81 |
+
|
| 82 |
+
# Project voxels'centroid from lidar coordinates to camera coordinates
|
| 83 |
+
cam_pts = fusion.TSDFVolume.vox2world(vox_origin, vox_coords, voxel_size)
|
| 84 |
+
cam_pts = fusion.rigid_transform(cam_pts, cam_E)
|
| 85 |
+
|
| 86 |
+
# Project camera coordinates to pixel positions
|
| 87 |
+
projected_pix = fusion.TSDFVolume.cam2pix(cam_pts, cam_k)
|
| 88 |
+
pix_x, pix_y = projected_pix[:, 0], projected_pix[:, 1]
|
| 89 |
+
|
| 90 |
+
# Eliminate pixels outside view frustum
|
| 91 |
+
pix_z = cam_pts[:, 2]
|
| 92 |
+
fov_mask = np.logical_and(pix_x >= 0,
|
| 93 |
+
np.logical_and(pix_x < img_W,
|
| 94 |
+
np.logical_and(pix_y >= 0,
|
| 95 |
+
np.logical_and(pix_y < img_H,
|
| 96 |
+
pix_z > 0))))
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
return torch.from_numpy(projected_pix), torch.from_numpy(fov_mask), torch.from_numpy(pix_z)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def get_grid_coords(dims, resolution):
|
| 104 |
+
"""
|
| 105 |
+
:param dims: the dimensions of the grid [x, y, z] (i.e. [256, 256, 32])
|
| 106 |
+
:return coords_grid: is the center coords of voxels in the grid
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
+
g_xx = np.arange(0, dims[0] + 1)
|
| 110 |
+
g_yy = np.arange(0, dims[1] + 1)
|
| 111 |
+
sensor_pose = 10
|
| 112 |
+
g_zz = np.arange(0, dims[2] + 1)
|
| 113 |
+
|
| 114 |
+
# Obtaining the grid with coords...
|
| 115 |
+
xx, yy, zz = np.meshgrid(g_xx[:-1], g_yy[:-1], g_zz[:-1])
|
| 116 |
+
coords_grid = np.array([xx.flatten(), yy.flatten(), zz.flatten()]).T
|
| 117 |
+
coords_grid = coords_grid.astype(np.float)
|
| 118 |
+
|
| 119 |
+
coords_grid = (coords_grid * resolution) + resolution / 2
|
| 120 |
+
|
| 121 |
+
temp = np.copy(coords_grid)
|
| 122 |
+
temp[:, 0] = coords_grid[:, 1]
|
| 123 |
+
temp[:, 1] = coords_grid[:, 0]
|
| 124 |
+
coords_grid = np.copy(temp)
|
| 125 |
+
|
| 126 |
+
return coords_grid
|
| 127 |
+
|
| 128 |
+
def get_projections(img_W, img_H):
|
| 129 |
+
scale_3ds = [1, 2]
|
| 130 |
+
data = {}
|
| 131 |
+
for scale_3d in scale_3ds:
|
| 132 |
+
scene_size = (51.2, 51.2, 6.4)
|
| 133 |
+
vox_origin = np.array([0, -25.6, -2])
|
| 134 |
+
voxel_size = 0.2
|
| 135 |
+
|
| 136 |
+
calib = read_calib("calib.txt")
|
| 137 |
+
cam_k = calib["P2"][:3, :3]
|
| 138 |
+
T_velo_2_cam = calib["Tr"]
|
| 139 |
+
|
| 140 |
+
# compute the 3D-2D mapping
|
| 141 |
+
projected_pix, fov_mask, pix_z = vox2pix(
|
| 142 |
+
T_velo_2_cam,
|
| 143 |
+
cam_k,
|
| 144 |
+
vox_origin,
|
| 145 |
+
voxel_size * scale_3d,
|
| 146 |
+
img_W,
|
| 147 |
+
img_H,
|
| 148 |
+
scene_size,
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
data["projected_pix_{}".format(scale_3d)] = projected_pix
|
| 152 |
+
data["pix_z_{}".format(scale_3d)] = pix_z
|
| 153 |
+
data["fov_mask_{}".format(scale_3d)] = fov_mask
|
| 154 |
+
return data
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def majority_pooling(grid, k_size=2):
|
| 158 |
+
result = np.zeros(
|
| 159 |
+
(grid.shape[0] // k_size, grid.shape[1] // k_size, grid.shape[2] // k_size)
|
| 160 |
+
)
|
| 161 |
+
for xx in range(0, int(np.floor(grid.shape[0] / k_size))):
|
| 162 |
+
for yy in range(0, int(np.floor(grid.shape[1] / k_size))):
|
| 163 |
+
for zz in range(0, int(np.floor(grid.shape[2] / k_size))):
|
| 164 |
+
|
| 165 |
+
sub_m = grid[
|
| 166 |
+
(xx * k_size) : (xx * k_size) + k_size,
|
| 167 |
+
(yy * k_size) : (yy * k_size) + k_size,
|
| 168 |
+
(zz * k_size) : (zz * k_size) + k_size,
|
| 169 |
+
]
|
| 170 |
+
unique, counts = np.unique(sub_m, return_counts=True)
|
| 171 |
+
if True in ((unique != 0) & (unique != 255)):
|
| 172 |
+
# Remove counts with 0 and 255
|
| 173 |
+
counts = counts[((unique != 0) & (unique != 255))]
|
| 174 |
+
unique = unique[((unique != 0) & (unique != 255))]
|
| 175 |
+
else:
|
| 176 |
+
if True in (unique == 0):
|
| 177 |
+
counts = counts[(unique != 255)]
|
| 178 |
+
unique = unique[(unique != 255)]
|
| 179 |
+
value = unique[np.argmax(counts)]
|
| 180 |
+
result[xx, yy, zz] = value
|
| 181 |
+
return result
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def draw(
|
| 185 |
+
voxels,
|
| 186 |
+
# T_velo_2_cam,
|
| 187 |
+
# vox_origin,
|
| 188 |
+
fov_mask,
|
| 189 |
+
# img_size,
|
| 190 |
+
# f,
|
| 191 |
+
voxel_size=0.4,
|
| 192 |
+
# d=7, # 7m - determine the size of the mesh representing the camera
|
| 193 |
+
):
|
| 194 |
+
|
| 195 |
+
fov_mask = fov_mask.reshape(-1)
|
| 196 |
+
# Compute the voxels coordinates
|
| 197 |
+
grid_coords = get_grid_coords(
|
| 198 |
+
[voxels.shape[0], voxels.shape[1], voxels.shape[2]], voxel_size
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
# Attach the predicted class to every voxel
|
| 203 |
+
grid_coords = np.vstack([grid_coords.T, voxels.reshape(-1)]).T
|
| 204 |
+
|
| 205 |
+
# Get the voxels inside FOV
|
| 206 |
+
fov_grid_coords = grid_coords[fov_mask, :]
|
| 207 |
+
|
| 208 |
+
# Get the voxels outside FOV
|
| 209 |
+
outfov_grid_coords = grid_coords[~fov_mask, :]
|
| 210 |
+
|
| 211 |
+
# Remove empty and unknown voxels
|
| 212 |
+
fov_voxels = fov_grid_coords[
|
| 213 |
+
(fov_grid_coords[:, 3] > 0) & (fov_grid_coords[:, 3] < 255), :
|
| 214 |
+
]
|
| 215 |
+
# print(np.unique(fov_voxels[:, 3], return_counts=True))
|
| 216 |
+
outfov_voxels = outfov_grid_coords[
|
| 217 |
+
(outfov_grid_coords[:, 3] > 0) & (outfov_grid_coords[:, 3] < 255), :
|
| 218 |
+
]
|
| 219 |
+
|
| 220 |
+
# figure = mlab.figure(size=(1400, 1400), bgcolor=(1, 1, 1))
|
| 221 |
+
colors = np.array(
|
| 222 |
+
[
|
| 223 |
+
[0,0,0],
|
| 224 |
+
[100, 150, 245],
|
| 225 |
+
[100, 230, 245],
|
| 226 |
+
[30, 60, 150],
|
| 227 |
+
[80, 30, 180],
|
| 228 |
+
[100, 80, 250],
|
| 229 |
+
[255, 30, 30],
|
| 230 |
+
[255, 40, 200],
|
| 231 |
+
[150, 30, 90],
|
| 232 |
+
[255, 0, 255],
|
| 233 |
+
[255, 150, 255],
|
| 234 |
+
[75, 0, 75],
|
| 235 |
+
[175, 0, 75],
|
| 236 |
+
[255, 200, 0],
|
| 237 |
+
[255, 120, 50],
|
| 238 |
+
[0, 175, 0],
|
| 239 |
+
[135, 60, 0],
|
| 240 |
+
[150, 240, 80],
|
| 241 |
+
[255, 240, 150],
|
| 242 |
+
[255, 0, 0],
|
| 243 |
+
]
|
| 244 |
+
).astype(np.uint8)
|
| 245 |
+
|
| 246 |
+
pts_colors = [f'rgb({colors[int(i)][0]}, {colors[int(i)][1]}, {colors[int(i)][2]})' for i in fov_voxels[:, 3]]
|
| 247 |
+
out_fov_colors = [f'rgb({colors[int(i)][0]//3*2}, {colors[int(i)][1]//3*2}, {colors[int(i)][2]//3*2})' for i in outfov_voxels[:, 3]]
|
| 248 |
+
pts_colors = pts_colors + out_fov_colors
|
| 249 |
+
|
| 250 |
+
fov_voxels = np.concatenate([fov_voxels, outfov_voxels], axis=0)
|
| 251 |
+
x = fov_voxels[:, 0].flatten()
|
| 252 |
+
y = fov_voxels[:, 1].flatten()
|
| 253 |
+
z = fov_voxels[:, 2].flatten()
|
| 254 |
+
# label = fov_voxels[:, 3].flatten()
|
| 255 |
+
fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z,mode='markers',
|
| 256 |
+
marker=dict(
|
| 257 |
+
size=3,
|
| 258 |
+
color=pts_colors, # set color to an array/list of desired values
|
| 259 |
+
# colorscale='Viridis', # choose a colorscale
|
| 260 |
+
opacity=1.0,
|
| 261 |
+
symbol='square'
|
| 262 |
+
))])
|
| 263 |
+
fig.update_layout(
|
| 264 |
+
scene = dict(
|
| 265 |
+
aspectmode='data',
|
| 266 |
+
xaxis = dict(
|
| 267 |
+
backgroundcolor="rgb(255, 255, 255)",
|
| 268 |
+
gridcolor="black",
|
| 269 |
+
showbackground=True,
|
| 270 |
+
zerolinecolor="black",
|
| 271 |
+
nticks=4,
|
| 272 |
+
visible=False,
|
| 273 |
+
range=[-1,55],),
|
| 274 |
+
yaxis = dict(
|
| 275 |
+
backgroundcolor="rgb(255, 255, 255)",
|
| 276 |
+
gridcolor="black",
|
| 277 |
+
showbackground=True,
|
| 278 |
+
zerolinecolor="black",
|
| 279 |
+
visible=False,
|
| 280 |
+
nticks=4, range=[-1,55],),
|
| 281 |
+
zaxis = dict(
|
| 282 |
+
backgroundcolor="rgb(255, 255, 255)",
|
| 283 |
+
gridcolor="black",
|
| 284 |
+
showbackground=True,
|
| 285 |
+
zerolinecolor="black",
|
| 286 |
+
visible=False,
|
| 287 |
+
nticks=4, range=[-1,7],),
|
| 288 |
+
bgcolor="black",
|
| 289 |
+
),
|
| 290 |
+
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
# fig = px.scatter_3d(
|
| 294 |
+
# fov_voxels,
|
| 295 |
+
# x=fov_voxels[:, 0], y="y", z="z", color="label")
|
| 296 |
+
# Draw occupied inside FOV voxels
|
| 297 |
+
# plt_plot_fov = mlab.points3d(
|
| 298 |
+
# fov_voxels[:, 0],
|
| 299 |
+
# fov_voxels[:, 1],
|
| 300 |
+
# fov_voxels[:, 2],
|
| 301 |
+
# fov_voxels[:, 3],
|
| 302 |
+
# colormap="viridis",
|
| 303 |
+
# scale_factor=voxel_size - 0.05 * voxel_size,
|
| 304 |
+
# mode="cube",
|
| 305 |
+
# opacity=1.0,
|
| 306 |
+
# vmin=1,
|
| 307 |
+
# vmax=19,
|
| 308 |
+
# )
|
| 309 |
+
|
| 310 |
+
# # Draw occupied outside FOV voxels
|
| 311 |
+
# plt_plot_outfov = mlab.points3d(
|
| 312 |
+
# outfov_voxels[:, 0],
|
| 313 |
+
# outfov_voxels[:, 1],
|
| 314 |
+
# outfov_voxels[:, 2],
|
| 315 |
+
# outfov_voxels[:, 3],
|
| 316 |
+
# colormap="viridis",
|
| 317 |
+
# scale_factor=voxel_size - 0.05 * voxel_size,
|
| 318 |
+
# mode="cube",
|
| 319 |
+
# opacity=1.0,
|
| 320 |
+
# vmin=1,
|
| 321 |
+
# vmax=19,
|
| 322 |
+
# )
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
# plt_plot_fov.glyph.scale_mode = "scale_by_vector"
|
| 327 |
+
# plt_plot_outfov.glyph.scale_mode = "scale_by_vector"
|
| 328 |
+
|
| 329 |
+
# plt_plot_fov.module_manager.scalar_lut_manager.lut.table = colors
|
| 330 |
+
|
| 331 |
+
# outfov_colors = colors
|
| 332 |
+
# outfov_colors[:, :3] = outfov_colors[:, :3] // 3 * 2
|
| 333 |
+
# plt_plot_outfov.module_manager.scalar_lut_manager.lut.table = outfov_colors
|
| 334 |
+
|
| 335 |
+
# mlab.show()
|
| 336 |
+
return fig
|
images/08/000010.jpg
ADDED
|
images/08/000085.jpg
ADDED
|
images/08/000103.jpg
ADDED
|
images/08/000187.jpg
ADDED
|
images/08/000234.jpg
ADDED
|
images/08/000290.jpg
ADDED
|
images/08/000465.jpg
ADDED
|
images/08/000511.jpg
ADDED
|
images/08/000618.jpg
ADDED
|
images/08/000790.jpg
ADDED
|
images/08/000822.jpg
ADDED
|
images/08/001005.jpg
ADDED
|
images/08/001122.jpg
ADDED
|
images/08/001380.jpg
ADDED
|
images/08/001446.jpg
ADDED
|
images/08/001530.jpg
ADDED
|
images/08/001687.jpg
ADDED
|
images/08/001842.jpg
ADDED
|
images/08/002010.jpg
ADDED
|
images/08/002128.jpg
ADDED
|
images/08/002272.jpg
ADDED
|
images/08/002360.jpg
ADDED
|
images/08/002505.jpg
ADDED
|
images/08/002716.jpg
ADDED
|
images/08/002944.jpg
ADDED
|
images/08/003149.jpg
ADDED
|
images/08/003365.jpg
ADDED
|
images/08/003533.jpg
ADDED
|
images/08/003790.jpg
ADDED
|
images/08/003929.jpg
ADDED
|
images/08/004059.jpg
ADDED
|
monoscene/.ipynb_checkpoints/CRP3D-checkpoint.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from monoscene.modules import (
|
| 4 |
+
Process,
|
| 5 |
+
ASPP,
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class CPMegaVoxels(nn.Module):
|
| 10 |
+
def __init__(self, feature, size, n_relations=4, bn_momentum=0.0003):
|
| 11 |
+
super().__init__()
|
| 12 |
+
self.size = size
|
| 13 |
+
self.n_relations = n_relations
|
| 14 |
+
print("n_relations", self.n_relations)
|
| 15 |
+
self.flatten_size = size[0] * size[1] * size[2]
|
| 16 |
+
self.feature = feature
|
| 17 |
+
self.context_feature = feature * 2
|
| 18 |
+
self.flatten_context_size = (size[0] // 2) * (size[1] // 2) * (size[2] // 2)
|
| 19 |
+
padding = ((size[0] + 1) % 2, (size[1] + 1) % 2, (size[2] + 1) % 2)
|
| 20 |
+
|
| 21 |
+
self.mega_context = nn.Sequential(
|
| 22 |
+
nn.Conv3d(
|
| 23 |
+
feature, self.context_feature, stride=2, padding=padding, kernel_size=3
|
| 24 |
+
),
|
| 25 |
+
)
|
| 26 |
+
self.flatten_context_size = (size[0] // 2) * (size[1] // 2) * (size[2] // 2)
|
| 27 |
+
|
| 28 |
+
self.context_prior_logits = nn.ModuleList(
|
| 29 |
+
[
|
| 30 |
+
nn.Sequential(
|
| 31 |
+
nn.Conv3d(
|
| 32 |
+
self.feature,
|
| 33 |
+
self.flatten_context_size,
|
| 34 |
+
padding=0,
|
| 35 |
+
kernel_size=1,
|
| 36 |
+
),
|
| 37 |
+
)
|
| 38 |
+
for i in range(n_relations)
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
self.aspp = ASPP(feature, [1, 2, 3])
|
| 42 |
+
|
| 43 |
+
self.resize = nn.Sequential(
|
| 44 |
+
nn.Conv3d(
|
| 45 |
+
self.context_feature * self.n_relations + feature,
|
| 46 |
+
feature,
|
| 47 |
+
kernel_size=1,
|
| 48 |
+
padding=0,
|
| 49 |
+
bias=False,
|
| 50 |
+
),
|
| 51 |
+
Process(feature, nn.BatchNorm3d, bn_momentum, dilations=[1]),
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
def forward(self, input):
|
| 55 |
+
ret = {}
|
| 56 |
+
bs = input.shape[0]
|
| 57 |
+
|
| 58 |
+
x_agg = self.aspp(input)
|
| 59 |
+
|
| 60 |
+
# get the mega context
|
| 61 |
+
x_mega_context_raw = self.mega_context(x_agg)
|
| 62 |
+
x_mega_context = x_mega_context_raw.reshape(bs, self.context_feature, -1)
|
| 63 |
+
x_mega_context = x_mega_context.permute(0, 2, 1)
|
| 64 |
+
|
| 65 |
+
# get context prior map
|
| 66 |
+
x_context_prior_logits = []
|
| 67 |
+
x_context_rels = []
|
| 68 |
+
for rel in range(self.n_relations):
|
| 69 |
+
|
| 70 |
+
# Compute the relation matrices
|
| 71 |
+
x_context_prior_logit = self.context_prior_logits[rel](x_agg)
|
| 72 |
+
x_context_prior_logit = x_context_prior_logit.reshape(
|
| 73 |
+
bs, self.flatten_context_size, self.flatten_size
|
| 74 |
+
)
|
| 75 |
+
x_context_prior_logits.append(x_context_prior_logit.unsqueeze(1))
|
| 76 |
+
|
| 77 |
+
x_context_prior_logit = x_context_prior_logit.permute(0, 2, 1)
|
| 78 |
+
x_context_prior = torch.sigmoid(x_context_prior_logit)
|
| 79 |
+
|
| 80 |
+
# Multiply the relation matrices with the mega context to gather context features
|
| 81 |
+
x_context_rel = torch.bmm(x_context_prior, x_mega_context) # bs, N, f
|
| 82 |
+
x_context_rels.append(x_context_rel)
|
| 83 |
+
|
| 84 |
+
x_context = torch.cat(x_context_rels, dim=2)
|
| 85 |
+
x_context = x_context.permute(0, 2, 1)
|
| 86 |
+
x_context = x_context.reshape(
|
| 87 |
+
bs, x_context.shape[1], self.size[0], self.size[1], self.size[2]
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
x = torch.cat([input, x_context], dim=1)
|
| 91 |
+
x = self.resize(x)
|
| 92 |
+
|
| 93 |
+
x_context_prior_logits = torch.cat(x_context_prior_logits, dim=1)
|
| 94 |
+
ret["P_logits"] = x_context_prior_logits
|
| 95 |
+
ret["x"] = x
|
| 96 |
+
|
| 97 |
+
return ret
|
monoscene/.ipynb_checkpoints/config-checkpoint.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
from typing import List
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class MonoSceneConfig(PretrainedConfig):
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
block_type="bottleneck",
|
| 10 |
+
layers: List[int] = [3, 4, 6, 3],
|
| 11 |
+
num_classes: int = 1000,
|
| 12 |
+
input_channels: int = 3,
|
| 13 |
+
cardinality: int = 1,
|
| 14 |
+
base_width: int = 64,
|
| 15 |
+
stem_width: int = 64,
|
| 16 |
+
stem_type: str = "",
|
| 17 |
+
avg_down: bool = False,
|
| 18 |
+
**kwargs,
|
| 19 |
+
):
|
| 20 |
+
self.block_type = block_type
|
| 21 |
+
self.layers = layers
|
| 22 |
+
self.num_classes = num_classes
|
| 23 |
+
self.input_channels = input_channels
|
| 24 |
+
self.cardinality = cardinality
|
| 25 |
+
self.base_width = base_width
|
| 26 |
+
self.stem_width = stem_width
|
| 27 |
+
self.stem_type = stem_type
|
| 28 |
+
self.avg_down = avg_down
|
| 29 |
+
super().__init__(**kwargs)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
monoscene/.ipynb_checkpoints/modules-checkpoint.py
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from monoscene.DDR import Bottleneck3D
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class ASPP(nn.Module):
|
| 7 |
+
"""
|
| 8 |
+
ASPP 3D
|
| 9 |
+
Adapt from https://github.com/cv-rits/LMSCNet/blob/main/LMSCNet/models/LMSCNet.py#L7
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
def __init__(self, planes, dilations_conv_list):
|
| 13 |
+
super().__init__()
|
| 14 |
+
|
| 15 |
+
# ASPP Block
|
| 16 |
+
self.conv_list = dilations_conv_list
|
| 17 |
+
self.conv1 = nn.ModuleList(
|
| 18 |
+
[
|
| 19 |
+
nn.Conv3d(
|
| 20 |
+
planes, planes, kernel_size=3, padding=dil, dilation=dil, bias=False
|
| 21 |
+
)
|
| 22 |
+
for dil in dilations_conv_list
|
| 23 |
+
]
|
| 24 |
+
)
|
| 25 |
+
self.bn1 = nn.ModuleList(
|
| 26 |
+
[nn.BatchNorm3d(planes) for dil in dilations_conv_list]
|
| 27 |
+
)
|
| 28 |
+
self.conv2 = nn.ModuleList(
|
| 29 |
+
[
|
| 30 |
+
nn.Conv3d(
|
| 31 |
+
planes, planes, kernel_size=3, padding=dil, dilation=dil, bias=False
|
| 32 |
+
)
|
| 33 |
+
for dil in dilations_conv_list
|
| 34 |
+
]
|
| 35 |
+
)
|
| 36 |
+
self.bn2 = nn.ModuleList(
|
| 37 |
+
[nn.BatchNorm3d(planes) for dil in dilations_conv_list]
|
| 38 |
+
)
|
| 39 |
+
self.relu = nn.ReLU()
|
| 40 |
+
|
| 41 |
+
def forward(self, x_in):
|
| 42 |
+
|
| 43 |
+
y = self.bn2[0](self.conv2[0](self.relu(self.bn1[0](self.conv1[0](x_in)))))
|
| 44 |
+
for i in range(1, len(self.conv_list)):
|
| 45 |
+
y += self.bn2[i](self.conv2[i](self.relu(self.bn1[i](self.conv1[i](x_in)))))
|
| 46 |
+
x_in = self.relu(y + x_in) # modified
|
| 47 |
+
|
| 48 |
+
return x_in
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class SegmentationHead(nn.Module):
|
| 52 |
+
"""
|
| 53 |
+
3D Segmentation heads to retrieve semantic segmentation at each scale.
|
| 54 |
+
Formed by Dim expansion, Conv3D, ASPP block, Conv3D.
|
| 55 |
+
Taken from https://github.com/cv-rits/LMSCNet/blob/main/LMSCNet/models/LMSCNet.py#L7
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
def __init__(self, inplanes, planes, nbr_classes, dilations_conv_list):
|
| 59 |
+
super().__init__()
|
| 60 |
+
|
| 61 |
+
# First convolution
|
| 62 |
+
self.conv0 = nn.Conv3d(inplanes, planes, kernel_size=3, padding=1, stride=1)
|
| 63 |
+
|
| 64 |
+
# ASPP Block
|
| 65 |
+
self.conv_list = dilations_conv_list
|
| 66 |
+
self.conv1 = nn.ModuleList(
|
| 67 |
+
[
|
| 68 |
+
nn.Conv3d(
|
| 69 |
+
planes, planes, kernel_size=3, padding=dil, dilation=dil, bias=False
|
| 70 |
+
)
|
| 71 |
+
for dil in dilations_conv_list
|
| 72 |
+
]
|
| 73 |
+
)
|
| 74 |
+
self.bn1 = nn.ModuleList(
|
| 75 |
+
[nn.BatchNorm3d(planes) for dil in dilations_conv_list]
|
| 76 |
+
)
|
| 77 |
+
self.conv2 = nn.ModuleList(
|
| 78 |
+
[
|
| 79 |
+
nn.Conv3d(
|
| 80 |
+
planes, planes, kernel_size=3, padding=dil, dilation=dil, bias=False
|
| 81 |
+
)
|
| 82 |
+
for dil in dilations_conv_list
|
| 83 |
+
]
|
| 84 |
+
)
|
| 85 |
+
self.bn2 = nn.ModuleList(
|
| 86 |
+
[nn.BatchNorm3d(planes) for dil in dilations_conv_list]
|
| 87 |
+
)
|
| 88 |
+
self.relu = nn.ReLU()
|
| 89 |
+
|
| 90 |
+
self.conv_classes = nn.Conv3d(
|
| 91 |
+
planes, nbr_classes, kernel_size=3, padding=1, stride=1
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
def forward(self, x_in):
|
| 95 |
+
|
| 96 |
+
# Convolution to go from inplanes to planes features...
|
| 97 |
+
x_in = self.relu(self.conv0(x_in))
|
| 98 |
+
|
| 99 |
+
y = self.bn2[0](self.conv2[0](self.relu(self.bn1[0](self.conv1[0](x_in)))))
|
| 100 |
+
for i in range(1, len(self.conv_list)):
|
| 101 |
+
y += self.bn2[i](self.conv2[i](self.relu(self.bn1[i](self.conv1[i](x_in)))))
|
| 102 |
+
x_in = self.relu(y + x_in) # modified
|
| 103 |
+
|
| 104 |
+
x_in = self.conv_classes(x_in)
|
| 105 |
+
|
| 106 |
+
return x_in
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class ProcessKitti(nn.Module):
|
| 110 |
+
def __init__(self, feature, norm_layer, bn_momentum, dilations=[1, 2, 3]):
|
| 111 |
+
super(Process, self).__init__()
|
| 112 |
+
self.main = nn.Sequential(
|
| 113 |
+
*[
|
| 114 |
+
Bottleneck3D(
|
| 115 |
+
feature,
|
| 116 |
+
feature // 4,
|
| 117 |
+
bn_momentum=bn_momentum,
|
| 118 |
+
norm_layer=norm_layer,
|
| 119 |
+
dilation=[i, i, i],
|
| 120 |
+
)
|
| 121 |
+
for i in dilations
|
| 122 |
+
]
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
def forward(self, x):
|
| 126 |
+
return self.main(x)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
class Process(nn.Module):
|
| 130 |
+
def __init__(self, feature, norm_layer, bn_momentum, dilations=[1, 2, 3]):
|
| 131 |
+
super(Process, self).__init__()
|
| 132 |
+
self.main = nn.Sequential(
|
| 133 |
+
*[
|
| 134 |
+
Bottleneck3D(
|
| 135 |
+
feature,
|
| 136 |
+
feature // 4,
|
| 137 |
+
bn_momentum=bn_momentum,
|
| 138 |
+
norm_layer=norm_layer,
|
| 139 |
+
dilation=[i, i, i],
|
| 140 |
+
)
|
| 141 |
+
for i in dilations
|
| 142 |
+
]
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
def forward(self, x):
|
| 146 |
+
return self.main(x)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
class Upsample(nn.Module):
|
| 150 |
+
def __init__(self, in_channels, out_channels, norm_layer, bn_momentum):
|
| 151 |
+
super(Upsample, self).__init__()
|
| 152 |
+
self.main = nn.Sequential(
|
| 153 |
+
nn.ConvTranspose3d(
|
| 154 |
+
in_channels,
|
| 155 |
+
out_channels,
|
| 156 |
+
kernel_size=3,
|
| 157 |
+
stride=2,
|
| 158 |
+
padding=1,
|
| 159 |
+
dilation=1,
|
| 160 |
+
output_padding=1,
|
| 161 |
+
),
|
| 162 |
+
norm_layer(out_channels, momentum=bn_momentum),
|
| 163 |
+
nn.ReLU(),
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
def forward(self, x):
|
| 167 |
+
return self.main(x)
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
class Downsample(nn.Module):
|
| 171 |
+
def __init__(self, feature, norm_layer, bn_momentum, expansion=8):
|
| 172 |
+
super(Downsample, self).__init__()
|
| 173 |
+
self.main = Bottleneck3D(
|
| 174 |
+
feature,
|
| 175 |
+
feature // 4,
|
| 176 |
+
bn_momentum=bn_momentum,
|
| 177 |
+
expansion=expansion,
|
| 178 |
+
stride=2,
|
| 179 |
+
downsample=nn.Sequential(
|
| 180 |
+
nn.AvgPool3d(kernel_size=2, stride=2),
|
| 181 |
+
nn.Conv3d(
|
| 182 |
+
feature,
|
| 183 |
+
int(feature * expansion / 4),
|
| 184 |
+
kernel_size=1,
|
| 185 |
+
stride=1,
|
| 186 |
+
bias=False,
|
| 187 |
+
),
|
| 188 |
+
norm_layer(int(feature * expansion / 4), momentum=bn_momentum),
|
| 189 |
+
),
|
| 190 |
+
norm_layer=norm_layer,
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
def forward(self, x):
|
| 194 |
+
return self.main(x)
|
monoscene/.ipynb_checkpoints/monoscene-checkpoint.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytorch_lightning as pl
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from monoscene.unet3d_nyu import UNet3D as UNet3DNYU
|
| 5 |
+
from monoscene.unet3d_kitti import UNet3D as UNet3DKitti
|
| 6 |
+
from monoscene.flosp import FLoSP
|
| 7 |
+
import numpy as np
|
| 8 |
+
import torch.nn.functional as F
|
| 9 |
+
from monoscene.unet2d import UNet2D
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class MonoScene(pl.LightningModule):
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
n_classes,
|
| 16 |
+
feature,
|
| 17 |
+
project_scale,
|
| 18 |
+
full_scene_size,
|
| 19 |
+
dataset,
|
| 20 |
+
n_relations=4,
|
| 21 |
+
context_prior=True,
|
| 22 |
+
fp_loss=True,
|
| 23 |
+
project_res=[],
|
| 24 |
+
frustum_size=4,
|
| 25 |
+
relation_loss=False,
|
| 26 |
+
CE_ssc_loss=True,
|
| 27 |
+
geo_scal_loss=True,
|
| 28 |
+
sem_scal_loss=True,
|
| 29 |
+
lr=1e-4,
|
| 30 |
+
weight_decay=1e-4,
|
| 31 |
+
):
|
| 32 |
+
super().__init__()
|
| 33 |
+
|
| 34 |
+
self.project_res = project_res
|
| 35 |
+
self.fp_loss = fp_loss
|
| 36 |
+
self.dataset = dataset
|
| 37 |
+
self.context_prior = context_prior
|
| 38 |
+
self.frustum_size = frustum_size
|
| 39 |
+
self.relation_loss = relation_loss
|
| 40 |
+
self.CE_ssc_loss = CE_ssc_loss
|
| 41 |
+
self.sem_scal_loss = sem_scal_loss
|
| 42 |
+
self.geo_scal_loss = geo_scal_loss
|
| 43 |
+
self.project_scale = project_scale
|
| 44 |
+
self.lr = lr
|
| 45 |
+
self.weight_decay = weight_decay
|
| 46 |
+
|
| 47 |
+
self.projects = {}
|
| 48 |
+
self.scale_2ds = [1, 2, 4, 8] # 2D scales
|
| 49 |
+
for scale_2d in self.scale_2ds:
|
| 50 |
+
self.projects[str(scale_2d)] = FLoSP(
|
| 51 |
+
full_scene_size, project_scale=self.project_scale, dataset=self.dataset
|
| 52 |
+
)
|
| 53 |
+
self.projects = nn.ModuleDict(self.projects)
|
| 54 |
+
|
| 55 |
+
self.n_classes = n_classes
|
| 56 |
+
if self.dataset == "NYU":
|
| 57 |
+
self.net_3d_decoder = UNet3DNYU(
|
| 58 |
+
self.n_classes,
|
| 59 |
+
nn.BatchNorm3d,
|
| 60 |
+
n_relations=n_relations,
|
| 61 |
+
feature=feature,
|
| 62 |
+
full_scene_size=full_scene_size,
|
| 63 |
+
context_prior=context_prior,
|
| 64 |
+
)
|
| 65 |
+
elif self.dataset == "kitti":
|
| 66 |
+
self.net_3d_decoder = UNet3DKitti(
|
| 67 |
+
self.n_classes,
|
| 68 |
+
nn.BatchNorm3d,
|
| 69 |
+
project_scale=project_scale,
|
| 70 |
+
feature=feature,
|
| 71 |
+
full_scene_size=full_scene_size,
|
| 72 |
+
context_prior=context_prior,
|
| 73 |
+
)
|
| 74 |
+
self.net_rgb = UNet2D.build(out_feature=feature, use_decoder=True)
|
| 75 |
+
|
| 76 |
+
def forward(self, batch):
|
| 77 |
+
|
| 78 |
+
img = batch["img"]
|
| 79 |
+
bs = len(img)
|
| 80 |
+
|
| 81 |
+
out = {}
|
| 82 |
+
|
| 83 |
+
x_rgb = self.net_rgb(img)
|
| 84 |
+
|
| 85 |
+
x3ds = []
|
| 86 |
+
for i in range(bs):
|
| 87 |
+
x3d = None
|
| 88 |
+
for scale_2d in self.project_res:
|
| 89 |
+
|
| 90 |
+
# project features at each 2D scale to target 3D scale
|
| 91 |
+
scale_2d = int(scale_2d)
|
| 92 |
+
projected_pix = batch["projected_pix_{}".format(self.project_scale)][i].cuda()
|
| 93 |
+
fov_mask = batch["fov_mask_{}".format(self.project_scale)][i].cuda()
|
| 94 |
+
|
| 95 |
+
# Sum all the 3D features
|
| 96 |
+
if x3d is None:
|
| 97 |
+
x3d = self.projects[str(scale_2d)](
|
| 98 |
+
x_rgb["1_" + str(scale_2d)][i],
|
| 99 |
+
projected_pix // scale_2d,
|
| 100 |
+
fov_mask,
|
| 101 |
+
)
|
| 102 |
+
else:
|
| 103 |
+
x3d += self.projects[str(scale_2d)](
|
| 104 |
+
x_rgb["1_" + str(scale_2d)][i],
|
| 105 |
+
projected_pix // scale_2d,
|
| 106 |
+
fov_mask,
|
| 107 |
+
)
|
| 108 |
+
x3ds.append(x3d)
|
| 109 |
+
|
| 110 |
+
input_dict = {
|
| 111 |
+
"x3d": torch.stack(x3ds),
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
out_dict = self.net_3d_decoder(input_dict)
|
| 115 |
+
|
| 116 |
+
ssc_pred = out_dict["ssc_logit"]
|
| 117 |
+
|
| 118 |
+
y_pred = ssc_pred.detach().cpu().numpy()
|
| 119 |
+
y_pred = np.argmax(y_pred, axis=1)
|
| 120 |
+
|
| 121 |
+
return y_pred
|
| 122 |
+
|
| 123 |
+
|
monoscene/.ipynb_checkpoints/monoscene_model-checkpoint.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PreTrainedModel
|
| 2 |
+
from .config import MonoSceneConfig
|
| 3 |
+
from monoscene.monoscene import MonoScene
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class MonoSceneModel(PreTrainedModel):
|
| 8 |
+
config_class = ResnetConfig
|
| 9 |
+
|
| 10 |
+
def __init__(self, config):
|
| 11 |
+
super().__init__(config)
|
| 12 |
+
self.model = MonoScene(
|
| 13 |
+
dataset=config.dataset,
|
| 14 |
+
n_classes=config.n_classes,
|
| 15 |
+
feature=config.feature,
|
| 16 |
+
project_scale=config.project_scale,
|
| 17 |
+
full_scene_size=config.full_scene_size
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def forward(self, tensor):
|
| 22 |
+
return self.model.forward(tensor)
|
monoscene/.ipynb_checkpoints/unet3d_kitti-checkpoint.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# encoding: utf-8
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from monoscene.modules import SegmentationHead
|
| 6 |
+
from monoscene.CRP3D import CPMegaVoxels
|
| 7 |
+
from monoscene.modules import Process, Upsample, Downsample
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class UNet3D(nn.Module):
|
| 11 |
+
def __init__(
|
| 12 |
+
self,
|
| 13 |
+
class_num,
|
| 14 |
+
norm_layer,
|
| 15 |
+
full_scene_size,
|
| 16 |
+
feature,
|
| 17 |
+
project_scale,
|
| 18 |
+
context_prior=None,
|
| 19 |
+
bn_momentum=0.1,
|
| 20 |
+
):
|
| 21 |
+
super(UNet3D, self).__init__()
|
| 22 |
+
self.business_layer = []
|
| 23 |
+
self.project_scale = project_scale
|
| 24 |
+
self.full_scene_size = full_scene_size
|
| 25 |
+
self.feature = feature
|
| 26 |
+
|
| 27 |
+
size_l1 = (
|
| 28 |
+
int(self.full_scene_size[0] / project_scale),
|
| 29 |
+
int(self.full_scene_size[1] / project_scale),
|
| 30 |
+
int(self.full_scene_size[2] / project_scale),
|
| 31 |
+
)
|
| 32 |
+
size_l2 = (size_l1[0] // 2, size_l1[1] // 2, size_l1[2] // 2)
|
| 33 |
+
size_l3 = (size_l2[0] // 2, size_l2[1] // 2, size_l2[2] // 2)
|
| 34 |
+
|
| 35 |
+
dilations = [1, 2, 3]
|
| 36 |
+
self.process_l1 = nn.Sequential(
|
| 37 |
+
Process(self.feature, norm_layer, bn_momentum, dilations=[1, 2, 3]),
|
| 38 |
+
Downsample(self.feature, norm_layer, bn_momentum),
|
| 39 |
+
)
|
| 40 |
+
self.process_l2 = nn.Sequential(
|
| 41 |
+
Process(self.feature * 2, norm_layer, bn_momentum, dilations=[1, 2, 3]),
|
| 42 |
+
Downsample(self.feature * 2, norm_layer, bn_momentum),
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
self.up_13_l2 = Upsample(
|
| 46 |
+
self.feature * 4, self.feature * 2, norm_layer, bn_momentum
|
| 47 |
+
)
|
| 48 |
+
self.up_12_l1 = Upsample(
|
| 49 |
+
self.feature * 2, self.feature, norm_layer, bn_momentum
|
| 50 |
+
)
|
| 51 |
+
self.up_l1_lfull = Upsample(
|
| 52 |
+
self.feature, self.feature // 2, norm_layer, bn_momentum
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
self.ssc_head = SegmentationHead(
|
| 56 |
+
self.feature // 2, self.feature // 2, class_num, dilations
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
self.context_prior = context_prior
|
| 60 |
+
if context_prior:
|
| 61 |
+
self.CP_mega_voxels = CPMegaVoxels(
|
| 62 |
+
self.feature * 4, size_l3, bn_momentum=bn_momentum
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
def forward(self, input_dict):
|
| 66 |
+
res = {}
|
| 67 |
+
|
| 68 |
+
x3d_l1 = input_dict["x3d"]
|
| 69 |
+
|
| 70 |
+
x3d_l2 = self.process_l1(x3d_l1)
|
| 71 |
+
|
| 72 |
+
x3d_l3 = self.process_l2(x3d_l2)
|
| 73 |
+
|
| 74 |
+
if self.context_prior:
|
| 75 |
+
ret = self.CP_mega_voxels(x3d_l3)
|
| 76 |
+
x3d_l3 = ret["x"]
|
| 77 |
+
for k in ret.keys():
|
| 78 |
+
res[k] = ret[k]
|
| 79 |
+
|
| 80 |
+
x3d_up_l2 = self.up_13_l2(x3d_l3) + x3d_l2
|
| 81 |
+
x3d_up_l1 = self.up_12_l1(x3d_up_l2) + x3d_l1
|
| 82 |
+
x3d_up_lfull = self.up_l1_lfull(x3d_up_l1)
|
| 83 |
+
|
| 84 |
+
ssc_logit_full = self.ssc_head(x3d_up_lfull)
|
| 85 |
+
|
| 86 |
+
res["ssc_logit"] = ssc_logit_full
|
| 87 |
+
|
| 88 |
+
return res
|
monoscene/.ipynb_checkpoints/unet3d_nyu-checkpoint.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# encoding: utf-8
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
import numpy as np
|
| 6 |
+
from monoscene.CRP3D import CPMegaVoxels
|
| 7 |
+
from monoscene.modules import (
|
| 8 |
+
Process,
|
| 9 |
+
Upsample,
|
| 10 |
+
Downsample,
|
| 11 |
+
SegmentationHead,
|
| 12 |
+
ASPP,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class UNet3D(nn.Module):
|
| 17 |
+
def __init__(
|
| 18 |
+
self,
|
| 19 |
+
class_num,
|
| 20 |
+
norm_layer,
|
| 21 |
+
feature,
|
| 22 |
+
full_scene_size,
|
| 23 |
+
n_relations=4,
|
| 24 |
+
project_res=[],
|
| 25 |
+
context_prior=True,
|
| 26 |
+
bn_momentum=0.1,
|
| 27 |
+
):
|
| 28 |
+
super(UNet3D, self).__init__()
|
| 29 |
+
self.business_layer = []
|
| 30 |
+
self.project_res = project_res
|
| 31 |
+
|
| 32 |
+
self.feature_1_4 = feature
|
| 33 |
+
self.feature_1_8 = feature * 2
|
| 34 |
+
self.feature_1_16 = feature * 4
|
| 35 |
+
|
| 36 |
+
self.feature_1_16_dec = self.feature_1_16
|
| 37 |
+
self.feature_1_8_dec = self.feature_1_8
|
| 38 |
+
self.feature_1_4_dec = self.feature_1_4
|
| 39 |
+
|
| 40 |
+
self.process_1_4 = nn.Sequential(
|
| 41 |
+
Process(self.feature_1_4, norm_layer, bn_momentum, dilations=[1, 2, 3]),
|
| 42 |
+
Downsample(self.feature_1_4, norm_layer, bn_momentum),
|
| 43 |
+
)
|
| 44 |
+
self.process_1_8 = nn.Sequential(
|
| 45 |
+
Process(self.feature_1_8, norm_layer, bn_momentum, dilations=[1, 2, 3]),
|
| 46 |
+
Downsample(self.feature_1_8, norm_layer, bn_momentum),
|
| 47 |
+
)
|
| 48 |
+
self.up_1_16_1_8 = Upsample(
|
| 49 |
+
self.feature_1_16_dec, self.feature_1_8_dec, norm_layer, bn_momentum
|
| 50 |
+
)
|
| 51 |
+
self.up_1_8_1_4 = Upsample(
|
| 52 |
+
self.feature_1_8_dec, self.feature_1_4_dec, norm_layer, bn_momentum
|
| 53 |
+
)
|
| 54 |
+
self.ssc_head_1_4 = SegmentationHead(
|
| 55 |
+
self.feature_1_4_dec, self.feature_1_4_dec, class_num, [1, 2, 3]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
self.context_prior = context_prior
|
| 59 |
+
size_1_16 = tuple(np.ceil(i / 4).astype(int) for i in full_scene_size)
|
| 60 |
+
|
| 61 |
+
if context_prior:
|
| 62 |
+
self.CP_mega_voxels = CPMegaVoxels(
|
| 63 |
+
self.feature_1_16,
|
| 64 |
+
size_1_16,
|
| 65 |
+
n_relations=n_relations,
|
| 66 |
+
bn_momentum=bn_momentum,
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
#
|
| 70 |
+
def forward(self, input_dict):
|
| 71 |
+
res = {}
|
| 72 |
+
|
| 73 |
+
x3d_1_4 = input_dict["x3d"]
|
| 74 |
+
x3d_1_8 = self.process_1_4(x3d_1_4)
|
| 75 |
+
x3d_1_16 = self.process_1_8(x3d_1_8)
|
| 76 |
+
|
| 77 |
+
if self.context_prior:
|
| 78 |
+
ret = self.CP_mega_voxels(x3d_1_16)
|
| 79 |
+
x3d_1_16 = ret["x"]
|
| 80 |
+
for k in ret.keys():
|
| 81 |
+
res[k] = ret[k]
|
| 82 |
+
|
| 83 |
+
x3d_up_1_8 = self.up_1_16_1_8(x3d_1_16) + x3d_1_8
|
| 84 |
+
x3d_up_1_4 = self.up_1_8_1_4(x3d_up_1_8) + x3d_1_4
|
| 85 |
+
|
| 86 |
+
ssc_logit_1_4 = self.ssc_head_1_4(x3d_up_1_4)
|
| 87 |
+
|
| 88 |
+
res["ssc_logit"] = ssc_logit_1_4
|
| 89 |
+
|
| 90 |
+
return res
|
monoscene/CRP3D.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from monoscene.modules import (
|
| 4 |
+
Process,
|
| 5 |
+
ASPP,
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class CPMegaVoxels(nn.Module):
|
| 10 |
+
def __init__(self, feature, size, n_relations=4, bn_momentum=0.0003):
|
| 11 |
+
super().__init__()
|
| 12 |
+
self.size = size
|
| 13 |
+
self.n_relations = n_relations
|
| 14 |
+
print("n_relations", self.n_relations)
|
| 15 |
+
self.flatten_size = size[0] * size[1] * size[2]
|
| 16 |
+
self.feature = feature
|
| 17 |
+
self.context_feature = feature * 2
|
| 18 |
+
self.flatten_context_size = (size[0] // 2) * (size[1] // 2) * (size[2] // 2)
|
| 19 |
+
padding = ((size[0] + 1) % 2, (size[1] + 1) % 2, (size[2] + 1) % 2)
|
| 20 |
+
|
| 21 |
+
self.mega_context = nn.Sequential(
|
| 22 |
+
nn.Conv3d(
|
| 23 |
+
feature, self.context_feature, stride=2, padding=padding, kernel_size=3
|
| 24 |
+
),
|
| 25 |
+
)
|
| 26 |
+
self.flatten_context_size = (size[0] // 2) * (size[1] // 2) * (size[2] // 2)
|
| 27 |
+
|
| 28 |
+
self.context_prior_logits = nn.ModuleList(
|
| 29 |
+
[
|
| 30 |
+
nn.Sequential(
|
| 31 |
+
nn.Conv3d(
|
| 32 |
+
self.feature,
|
| 33 |
+
self.flatten_context_size,
|
| 34 |
+
padding=0,
|
| 35 |
+
kernel_size=1,
|
| 36 |
+
),
|
| 37 |
+
)
|
| 38 |
+
for i in range(n_relations)
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
self.aspp = ASPP(feature, [1, 2, 3])
|
| 42 |
+
|
| 43 |
+
self.resize = nn.Sequential(
|
| 44 |
+
nn.Conv3d(
|
| 45 |
+
self.context_feature * self.n_relations + feature,
|
| 46 |
+
feature,
|
| 47 |
+
kernel_size=1,
|
| 48 |
+
padding=0,
|
| 49 |
+
bias=False,
|
| 50 |
+
),
|
| 51 |
+
Process(feature, nn.BatchNorm3d, bn_momentum, dilations=[1]),
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
def forward(self, input):
|
| 55 |
+
ret = {}
|
| 56 |
+
bs = input.shape[0]
|
| 57 |
+
|
| 58 |
+
x_agg = self.aspp(input)
|
| 59 |
+
|
| 60 |
+
# get the mega context
|
| 61 |
+
x_mega_context_raw = self.mega_context(x_agg)
|
| 62 |
+
x_mega_context = x_mega_context_raw.reshape(bs, self.context_feature, -1)
|
| 63 |
+
x_mega_context = x_mega_context.permute(0, 2, 1)
|
| 64 |
+
|
| 65 |
+
# get context prior map
|
| 66 |
+
x_context_prior_logits = []
|
| 67 |
+
x_context_rels = []
|
| 68 |
+
for rel in range(self.n_relations):
|
| 69 |
+
|
| 70 |
+
# Compute the relation matrices
|
| 71 |
+
x_context_prior_logit = self.context_prior_logits[rel](x_agg)
|
| 72 |
+
x_context_prior_logit = x_context_prior_logit.reshape(
|
| 73 |
+
bs, self.flatten_context_size, self.flatten_size
|
| 74 |
+
)
|
| 75 |
+
x_context_prior_logits.append(x_context_prior_logit.unsqueeze(1))
|
| 76 |
+
|
| 77 |
+
x_context_prior_logit = x_context_prior_logit.permute(0, 2, 1)
|
| 78 |
+
x_context_prior = torch.sigmoid(x_context_prior_logit)
|
| 79 |
+
|
| 80 |
+
# Multiply the relation matrices with the mega context to gather context features
|
| 81 |
+
x_context_rel = torch.bmm(x_context_prior, x_mega_context) # bs, N, f
|
| 82 |
+
x_context_rels.append(x_context_rel)
|
| 83 |
+
|
| 84 |
+
x_context = torch.cat(x_context_rels, dim=2)
|
| 85 |
+
x_context = x_context.permute(0, 2, 1)
|
| 86 |
+
x_context = x_context.reshape(
|
| 87 |
+
bs, x_context.shape[1], self.size[0], self.size[1], self.size[2]
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
x = torch.cat([input, x_context], dim=1)
|
| 91 |
+
x = self.resize(x)
|
| 92 |
+
|
| 93 |
+
x_context_prior_logits = torch.cat(x_context_prior_logits, dim=1)
|
| 94 |
+
ret["P_logits"] = x_context_prior_logits
|
| 95 |
+
ret["x"] = x
|
| 96 |
+
|
| 97 |
+
return ret
|
monoscene/DDR.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Most of the code in this file is taken from https://github.com/waterljwant/SSC/blob/master/models/DDR.py
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class SimpleRB(nn.Module):
|
| 11 |
+
def __init__(self, in_channel, norm_layer, bn_momentum):
|
| 12 |
+
super(SimpleRB, self).__init__()
|
| 13 |
+
self.path = nn.Sequential(
|
| 14 |
+
nn.Conv3d(in_channel, in_channel, kernel_size=3, padding=1, bias=False),
|
| 15 |
+
norm_layer(in_channel, momentum=bn_momentum),
|
| 16 |
+
nn.ReLU(),
|
| 17 |
+
nn.Conv3d(in_channel, in_channel, kernel_size=3, padding=1, bias=False),
|
| 18 |
+
norm_layer(in_channel, momentum=bn_momentum),
|
| 19 |
+
)
|
| 20 |
+
self.relu = nn.ReLU()
|
| 21 |
+
|
| 22 |
+
def forward(self, x):
|
| 23 |
+
residual = x
|
| 24 |
+
conv_path = self.path(x)
|
| 25 |
+
out = residual + conv_path
|
| 26 |
+
out = self.relu(out)
|
| 27 |
+
return out
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
"""
|
| 31 |
+
3D Residual Block,3x3x3 conv ==> 3 smaller 3D conv, refered from DDRNet
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class Bottleneck3D(nn.Module):
|
| 36 |
+
def __init__(
|
| 37 |
+
self,
|
| 38 |
+
inplanes,
|
| 39 |
+
planes,
|
| 40 |
+
norm_layer,
|
| 41 |
+
stride=1,
|
| 42 |
+
dilation=[1, 1, 1],
|
| 43 |
+
expansion=4,
|
| 44 |
+
downsample=None,
|
| 45 |
+
fist_dilation=1,
|
| 46 |
+
multi_grid=1,
|
| 47 |
+
bn_momentum=0.0003,
|
| 48 |
+
):
|
| 49 |
+
super(Bottleneck3D, self).__init__()
|
| 50 |
+
# often,planes = inplanes // 4
|
| 51 |
+
self.expansion = expansion
|
| 52 |
+
self.conv1 = nn.Conv3d(inplanes, planes, kernel_size=1, bias=False)
|
| 53 |
+
self.bn1 = norm_layer(planes, momentum=bn_momentum)
|
| 54 |
+
self.conv2 = nn.Conv3d(
|
| 55 |
+
planes,
|
| 56 |
+
planes,
|
| 57 |
+
kernel_size=(1, 1, 3),
|
| 58 |
+
stride=(1, 1, stride),
|
| 59 |
+
dilation=(1, 1, dilation[0]),
|
| 60 |
+
padding=(0, 0, dilation[0]),
|
| 61 |
+
bias=False,
|
| 62 |
+
)
|
| 63 |
+
self.bn2 = norm_layer(planes, momentum=bn_momentum)
|
| 64 |
+
self.conv3 = nn.Conv3d(
|
| 65 |
+
planes,
|
| 66 |
+
planes,
|
| 67 |
+
kernel_size=(1, 3, 1),
|
| 68 |
+
stride=(1, stride, 1),
|
| 69 |
+
dilation=(1, dilation[1], 1),
|
| 70 |
+
padding=(0, dilation[1], 0),
|
| 71 |
+
bias=False,
|
| 72 |
+
)
|
| 73 |
+
self.bn3 = norm_layer(planes, momentum=bn_momentum)
|
| 74 |
+
self.conv4 = nn.Conv3d(
|
| 75 |
+
planes,
|
| 76 |
+
planes,
|
| 77 |
+
kernel_size=(3, 1, 1),
|
| 78 |
+
stride=(stride, 1, 1),
|
| 79 |
+
dilation=(dilation[2], 1, 1),
|
| 80 |
+
padding=(dilation[2], 0, 0),
|
| 81 |
+
bias=False,
|
| 82 |
+
)
|
| 83 |
+
self.bn4 = norm_layer(planes, momentum=bn_momentum)
|
| 84 |
+
self.conv5 = nn.Conv3d(
|
| 85 |
+
planes, planes * self.expansion, kernel_size=(1, 1, 1), bias=False
|
| 86 |
+
)
|
| 87 |
+
self.bn5 = norm_layer(planes * self.expansion, momentum=bn_momentum)
|
| 88 |
+
|
| 89 |
+
self.relu = nn.ReLU(inplace=False)
|
| 90 |
+
self.relu_inplace = nn.ReLU(inplace=True)
|
| 91 |
+
self.downsample = downsample
|
| 92 |
+
self.dilation = dilation
|
| 93 |
+
self.stride = stride
|
| 94 |
+
|
| 95 |
+
self.downsample2 = nn.Sequential(
|
| 96 |
+
nn.AvgPool3d(kernel_size=(1, stride, 1), stride=(1, stride, 1)),
|
| 97 |
+
nn.Conv3d(planes, planes, kernel_size=1, stride=1, bias=False),
|
| 98 |
+
norm_layer(planes, momentum=bn_momentum),
|
| 99 |
+
)
|
| 100 |
+
self.downsample3 = nn.Sequential(
|
| 101 |
+
nn.AvgPool3d(kernel_size=(stride, 1, 1), stride=(stride, 1, 1)),
|
| 102 |
+
nn.Conv3d(planes, planes, kernel_size=1, stride=1, bias=False),
|
| 103 |
+
norm_layer(planes, momentum=bn_momentum),
|
| 104 |
+
)
|
| 105 |
+
self.downsample4 = nn.Sequential(
|
| 106 |
+
nn.AvgPool3d(kernel_size=(stride, 1, 1), stride=(stride, 1, 1)),
|
| 107 |
+
nn.Conv3d(planes, planes, kernel_size=1, stride=1, bias=False),
|
| 108 |
+
norm_layer(planes, momentum=bn_momentum),
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
def forward(self, x):
|
| 112 |
+
residual = x
|
| 113 |
+
|
| 114 |
+
out1 = self.relu(self.bn1(self.conv1(x)))
|
| 115 |
+
out2 = self.bn2(self.conv2(out1))
|
| 116 |
+
out2_relu = self.relu(out2)
|
| 117 |
+
|
| 118 |
+
out3 = self.bn3(self.conv3(out2_relu))
|
| 119 |
+
if self.stride != 1:
|
| 120 |
+
out2 = self.downsample2(out2)
|
| 121 |
+
out3 = out3 + out2
|
| 122 |
+
out3_relu = self.relu(out3)
|
| 123 |
+
|
| 124 |
+
out4 = self.bn4(self.conv4(out3_relu))
|
| 125 |
+
if self.stride != 1:
|
| 126 |
+
out2 = self.downsample3(out2)
|
| 127 |
+
out3 = self.downsample4(out3)
|
| 128 |
+
out4 = out4 + out2 + out3
|
| 129 |
+
|
| 130 |
+
out4_relu = self.relu(out4)
|
| 131 |
+
out5 = self.bn5(self.conv5(out4_relu))
|
| 132 |
+
|
| 133 |
+
if self.downsample is not None:
|
| 134 |
+
residual = self.downsample(x)
|
| 135 |
+
|
| 136 |
+
out = out5 + residual
|
| 137 |
+
out_relu = self.relu(out)
|
| 138 |
+
|
| 139 |
+
return out_relu
|
monoscene/__init__.py
ADDED
|
File without changes
|
monoscene/__pycache__/CRP3D.cpython-37.pyc
ADDED
|
Binary file (2.34 kB). View file
|
|
|