File size: 9,385 Bytes
d2410ba a5ddf83 a13fd6e d2410ba c5a4ee1 d2410ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# -*- coding: utf-8 -*-
import os
import torch
import argparse
import numpy as np
import open3d as o3d
from huggingface_hub import hf_hub_download, HfFolder
from segment import seg_point, seg_box, seg_mask
import sam2point.dataset as dataset
import sam2point.configs as configs
from sam2point.voxelizer import Voxelizer
from sam2point.utils import cal
import matplotlib.pyplot as plt
import plotly.graph_objects as go
print("Torch CUDA:", torch.cuda.is_available())
# use bfloat16 for the entire notebook
torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()
# if torch.cuda.get_device_properties(0).major >= 8:
# # turn on tfloat32 for Ampere GPUs (https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices)
# torch.backends.cuda.matmul.allow_tf32 = True
# torch.backends.cudnn.allow_tf32 = True
def run_demo(dataset_name, prompt_type, sample_idx, prompt_idx, voxel_size, theta, mode, ret_prompt):
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', choices=['S3DIS', 'ScanNet', 'Objaverse', 'KITTI', 'Semantic3D'], default='Objaverse', help='dataset selected')
parser.add_argument('--prompt_type', choices=['point', 'box', 'mask'], default='point', help='prompt type selected')
parser.add_argument('--sample_idx', type=int, default=2, help='the index of the scene or object')
parser.add_argument('--prompt_idx', type=int, default=0, help='the index of the prompt')
parser.add_argument('--voxel_size', type=float, default=0.02, help='voxel size')
parser.add_argument('--theta', type=float, default=0.5) # indoor NOTE
parser.add_argument('--mode', type=str, default='bilinear') # indoor NOTE
parser.add_argument("--ret_prompt", action="store_true")
args = parser.parse_args()
args.dataset, args.prompt_type, args.sample_idx, args.prompt_idx = dataset_name, prompt_type, sample_idx, prompt_idx
args.voxel_size, args.theta, args.mode, args.ret_prompt = voxel_size, theta, mode, ret_prompt
print(args)
#cache
name_list = [args.dataset, "sample" + str(args.sample_idx), args.prompt_type + "-prompt" + str(args.prompt_idx)]
name = '_'.join(name_list)
# hf
repo_id = "ZiyuG/Cache"
result_name = "cache_results/" + name + '.npy'
prompt_name = "cache_prompt/" + name + '.npy'
token = os.getenv('HF_TOKEN')
try:
result_file = hf_hub_download(repo_id=repo_id, filename=result_name, use_auth_token=token, repo_type='dataset')
prompt_file = hf_hub_download(repo_id=repo_id, filename=prompt_name, use_auth_token=token, repo_type='dataset')
new_color = np.load(result_file)
PROMPT = np.load(prompt_file)
if not args.ret_prompt: return new_color, PROMPT
else: return PROMPT
except Exception as e:
if os.path.exists("./cache_results/" + name + '.npy') and os.path.exists("./cache_prompt/" + name + '.npy'):
new_color = np.load("./cache_results/" + name + '.npy')
PROMPT = np.load("./cache_prompt/" + name + '.npy')
if not args.ret_prompt: return new_color, PROMPT
else: return PROMPT
#########
if args.dataset == 'S3DIS':
info = configs.S3DIS_samples[args.sample_idx]
# early return
if args.prompt_type == 'point' and args.ret_prompt: return list(np.array(info['point_prompts'])[args.prompt_idx])
elif args.prompt_type == 'box' and args.ret_prompt: return list(np.array(info['box_prompts'])[args.prompt_idx])
point, color = dataset.load_S3DIS_sample(info['path'])
elif args.dataset == 'ScanNet':
info = configs.ScanNet_samples[args.sample_idx]
# early return
if args.prompt_type == 'point' and args.ret_prompt: return list(np.array(info['point_prompts'])[args.prompt_idx])
elif args.prompt_type == 'box' and args.ret_prompt: return list(np.array(info['box_prompts'])[args.prompt_idx])
point, color = dataset.load_ScanNet_sample(info['path'])
elif args.dataset == 'Objaverse':
info = configs.Objaverse_samples[args.sample_idx]
# early return
if args.prompt_type == 'point' and args.ret_prompt: return list(np.array(info['point_prompts'])[args.prompt_idx])
elif args.prompt_type == 'box' and args.ret_prompt: return list(np.array(info['box_prompts'])[args.prompt_idx])
point, color = dataset.load_Objaverse_sample(info['path'])
args.voxel_size = info[configs.VOXEL[args.prompt_type]][args.prompt_idx]
elif args.dataset == 'KITTI':
info = configs.KITTI_samples[args.sample_idx]
# early return
if args.prompt_type == 'point' and args.ret_prompt: return list(np.array(info['point_prompts'])[args.prompt_idx])
elif args.prompt_type == 'box' and args.ret_prompt: return list(np.array(info['box_prompts'])[args.prompt_idx])
point, color = dataset.load_KITTI_sample(info['path'])
args.voxel_size = info[configs.VOXEL[args.prompt_type]][args.prompt_idx]
elif args.dataset == 'Semantic3D':
info = configs.Semantic3D_samples[args.sample_idx]
# early return
if args.prompt_type == 'point' and args.ret_prompt: return list(np.array(info['point_prompts'])[args.prompt_idx])
elif args.prompt_type == 'box' and args.ret_prompt: return list(np.array(info['box_prompts'])[args.prompt_idx])
point, color = dataset.load_Semantic3D_sample(info['path'], args.sample_idx)
args.voxel_size = info[configs.VOXEL[args.prompt_type]][args.prompt_idx]
point_color = np.concatenate([point, color], axis=1)
voxelizer = Voxelizer(voxel_size=args.voxel_size, clip_bound=None)
labels_in = point[:, :1].astype(int)
locs, feats, labels, inds_reconstruct = voxelizer.voxelize(point, color, labels_in)
if args.prompt_type == 'point':
if args.ret_prompt: return list(np.array(info['point_prompts'])[args.prompt_idx])
mask = seg_point(locs, feats, info['point_prompts'], args)
point_prompts = np.array(info['point_prompts'])
prompt_point = list(point_prompts[args.prompt_idx])
prompt_box = None
PROMPT = prompt_point
elif args.prompt_type == 'box':
if args.ret_prompt: return list(np.array(info['box_prompts'])[args.prompt_idx])
mask = seg_box(locs, feats, info['box_prompts'], args)
point_prompts = np.array(info['box_prompts'])
prompt_point = None
prompt_box = list(point_prompts[args.prompt_idx])
PROMPT = prompt_box
elif args.prompt_type == 'mask':
if 'mask_prompts' not in info: info['mask_prompts'] = info['point_prompts']
mask, prompt_mask = seg_mask(locs, feats, info['mask_prompts'], args)
prompt_point, prompt_box = None, None
point_locs = locs[inds_reconstruct]
point_prompt_mask = prompt_mask[point_locs[:, 0], point_locs[:, 1], point_locs[:, 2]]
point_prompt_mask = point_prompt_mask.unsqueeze(-1)
point_prompt_mask_not = ~point_prompt_mask
color_prompt_mask = color * point_prompt_mask_not.numpy() + (color * 0 + np.array([[1., 0., 0.]])) * point_prompt_mask.numpy()
PROMPT = color_prompt_mask
if args.ret_prompt:
return color_prompt_mask
point_locs = locs[inds_reconstruct]
point_mask = mask[point_locs[:, 0], point_locs[:, 1], point_locs[:, 2]]
point_mask = point_mask.unsqueeze(-1)
point_mask_not = ~point_mask
point, color = point_color[:, :3], point_color[:, 3:]
new_color = color * point_mask_not.numpy() + (color * 0 + np.array([[0., 1., 0.]])) * point_mask.numpy()
name_list = [args.dataset, "sample" + str(args.sample_idx), args.prompt_type + "-prompt" + str(args.prompt_idx)]
name = '_'.join(name_list) + 'frames'
# os.system('rm -rf ' + name)
#cache
name_list = [args.dataset, "sample" + str(args.sample_idx), args.prompt_type + "-prompt" + str(args.prompt_idx)]
name = '_'.join(name_list)
os.makedirs("cache_results", exist_ok=True)
os.makedirs("cache_prompt", exist_ok=True)
np.save("./cache_results/" + name + '.npy', new_color)
np.save("./cache_prompt/" + name + '.npy', PROMPT)
return new_color, PROMPT
def create_box(prompt):
x_min, y_min, z_min, x_max, y_max, z_max = tuple(prompt)
bbox_points = np.array([
[x_min, y_min, z_min],
[x_max, y_min, z_min],
[x_max, y_max, z_min],
[x_min, y_max, z_min],
[x_min, y_min, z_max],
[x_max, y_min, z_max],
[x_max, y_max, z_max],
[x_min, y_max, z_max]
])
edges = [
(0, 1), (1, 2), (2, 3), (3, 0), # Bottom face
(4, 5), (5, 6), (6, 7), (7, 4), # Top face
(0, 4), (1, 5), (2, 6), (3, 7) # Vertical edges
]
bbox_lines = []
f = 1
for start, end in edges:
bbox_lines.append(go.Scatter3d(
x=[bbox_points[start, 0], bbox_points[end, 0]],
y=[bbox_points[start, 1], bbox_points[end, 1]],
z=[bbox_points[start, 2], bbox_points[end, 2]],
mode='lines',
# line=dict(color='red', width=2), # Customize color and width
# line=dict(color='rgb(255, 140, 0)', width=4), # Customize color and width
line=dict(color='rgb(220, 20, 60)', width=6), # Customize color and width
name="Box Prompt" if f == 1 else "",
showlegend=True if f == 1 else False
))
f = 0
return bbox_lines
|