Spaces:
Runtime error
Runtime error
Update inference.py
Browse files- inference.py +257 -261
inference.py
CHANGED
|
@@ -1,261 +1,257 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
import
|
| 13 |
-
|
| 14 |
-
from
|
| 15 |
-
from
|
| 16 |
-
from
|
| 17 |
-
from
|
| 18 |
-
from
|
| 19 |
-
from
|
| 20 |
-
from
|
| 21 |
-
from
|
| 22 |
-
from
|
| 23 |
-
from
|
| 24 |
-
from
|
| 25 |
-
from utils.
|
| 26 |
-
from
|
| 27 |
-
from
|
| 28 |
-
from
|
| 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 |
-
vis_merge =
|
| 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 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
json_data
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
# dataset =
|
| 254 |
-
#
|
| 255 |
-
#
|
| 256 |
-
# ])
|
| 257 |
-
#
|
| 258 |
-
# '1169_pano_21',
|
| 259 |
-
# '0583_pano_59',
|
| 260 |
-
# ], vp_align=True)
|
| 261 |
-
# inference_dataset(dataset)
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import argparse
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
import torch
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import glob
|
| 9 |
+
|
| 10 |
+
from tqdm import tqdm
|
| 11 |
+
from PIL import Image
|
| 12 |
+
from config.defaults import merge_from_file, get_config
|
| 13 |
+
from dataset.mp3d_dataset import MP3DDataset
|
| 14 |
+
from dataset.zind_dataset import ZindDataset
|
| 15 |
+
from models.build import build_model
|
| 16 |
+
from loss import GradLoss
|
| 17 |
+
from postprocessing.post_process import post_process
|
| 18 |
+
from preprocessing.pano_lsd_align import panoEdgeDetection, rotatePanorama
|
| 19 |
+
from utils.boundary import corners2boundaries, layout2depth
|
| 20 |
+
from utils.conversion import depth2xyz
|
| 21 |
+
from utils.logger import get_logger
|
| 22 |
+
from utils.misc import tensor2np_d, tensor2np
|
| 23 |
+
from evaluation.accuracy import show_grad
|
| 24 |
+
from models.lgt_net import LGT_Net
|
| 25 |
+
from utils.writer import xyz2json
|
| 26 |
+
from visualization.boundary import draw_boundaries
|
| 27 |
+
from visualization.floorplan import draw_floorplan, draw_iou_floorplan
|
| 28 |
+
from visualization.obj3d import create_3d_obj
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def parse_option():
|
| 32 |
+
parser = argparse.ArgumentParser(description='Panorama Layout Transformer training and evaluation script')
|
| 33 |
+
parser.add_argument('--img_glob',
|
| 34 |
+
type=str,
|
| 35 |
+
required=True,
|
| 36 |
+
help='image glob path')
|
| 37 |
+
|
| 38 |
+
parser.add_argument('--cfg',
|
| 39 |
+
type=str,
|
| 40 |
+
required=True,
|
| 41 |
+
metavar='FILE',
|
| 42 |
+
help='path of config file')
|
| 43 |
+
|
| 44 |
+
parser.add_argument('--post_processing',
|
| 45 |
+
type=str,
|
| 46 |
+
default='manhattan',
|
| 47 |
+
choices=['manhattan', 'atalanta', 'original'],
|
| 48 |
+
help='post-processing type')
|
| 49 |
+
|
| 50 |
+
parser.add_argument('--output_dir',
|
| 51 |
+
type=str,
|
| 52 |
+
default='src/output',
|
| 53 |
+
help='path of output')
|
| 54 |
+
|
| 55 |
+
parser.add_argument('--visualize_3d', action='store_true',
|
| 56 |
+
help='visualize_3d')
|
| 57 |
+
|
| 58 |
+
parser.add_argument('--output_3d', action='store_true',
|
| 59 |
+
help='output_3d')
|
| 60 |
+
|
| 61 |
+
parser.add_argument('--device',
|
| 62 |
+
type=str,
|
| 63 |
+
default='cuda',
|
| 64 |
+
help='device')
|
| 65 |
+
|
| 66 |
+
args = parser.parse_args()
|
| 67 |
+
args.mode = 'test'
|
| 68 |
+
|
| 69 |
+
print("arguments:")
|
| 70 |
+
for arg in vars(args):
|
| 71 |
+
print(arg, ":", getattr(args, arg))
|
| 72 |
+
print("-" * 50)
|
| 73 |
+
return args
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def visualize_2d(img, dt, show_depth=True, show_floorplan=True, show=False, save_path=None):
|
| 77 |
+
dt_np = tensor2np_d(dt)
|
| 78 |
+
dt_depth = dt_np['depth'][0]
|
| 79 |
+
dt_xyz = depth2xyz(np.abs(dt_depth))
|
| 80 |
+
dt_ratio = dt_np['ratio'][0][0]
|
| 81 |
+
dt_boundaries = corners2boundaries(dt_ratio, corners_xyz=dt_xyz, step=None, visible=False, length=img.shape[1])
|
| 82 |
+
vis_img = draw_boundaries(img, boundary_list=dt_boundaries, boundary_color=[0, 1, 0])
|
| 83 |
+
|
| 84 |
+
if 'processed_xyz' in dt:
|
| 85 |
+
dt_boundaries = corners2boundaries(dt_ratio, corners_xyz=dt['processed_xyz'][0], step=None, visible=False,
|
| 86 |
+
length=img.shape[1])
|
| 87 |
+
vis_img = draw_boundaries(vis_img, boundary_list=dt_boundaries, boundary_color=[1, 0, 0])
|
| 88 |
+
|
| 89 |
+
if show_depth:
|
| 90 |
+
dt_grad_img = show_depth_normal_grad(dt)
|
| 91 |
+
grad_h = dt_grad_img.shape[0]
|
| 92 |
+
vis_merge = [
|
| 93 |
+
vis_img[0:-grad_h, :, :],
|
| 94 |
+
dt_grad_img,
|
| 95 |
+
]
|
| 96 |
+
vis_img = np.concatenate(vis_merge, axis=0)
|
| 97 |
+
# vis_img = dt_grad_img.transpose(1, 2, 0)[100:]
|
| 98 |
+
|
| 99 |
+
if show_floorplan:
|
| 100 |
+
if 'processed_xyz' in dt:
|
| 101 |
+
floorplan = draw_iou_floorplan(dt['processed_xyz'][0][..., ::2], dt_xyz[..., ::2],
|
| 102 |
+
dt_board_color=[1, 0, 0, 1], gt_board_color=[0, 1, 0, 1])
|
| 103 |
+
else:
|
| 104 |
+
floorplan = show_alpha_floorplan(dt_xyz, border_color=[0, 1, 0, 1])
|
| 105 |
+
|
| 106 |
+
vis_img = np.concatenate([vis_img, floorplan[:, 60:-60, :]], axis=1)
|
| 107 |
+
if show:
|
| 108 |
+
plt.imshow(vis_img)
|
| 109 |
+
plt.show()
|
| 110 |
+
if save_path:
|
| 111 |
+
result = Image.fromarray((vis_img * 255).astype(np.uint8))
|
| 112 |
+
result.save(save_path)
|
| 113 |
+
return vis_img
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def preprocess(img_ori, q_error=0.7, refine_iter=3, vp_cache_path=None):
|
| 117 |
+
# Align images with VP
|
| 118 |
+
if os.path.exists(vp_cache_path):
|
| 119 |
+
with open(vp_cache_path) as f:
|
| 120 |
+
vp = [[float(v) for v in line.rstrip().split(' ')] for line in f.readlines()]
|
| 121 |
+
vp = np.array(vp)
|
| 122 |
+
else:
|
| 123 |
+
# VP detection and line segment extraction
|
| 124 |
+
_, vp, _, _, _, _, _ = panoEdgeDetection(img_ori,
|
| 125 |
+
qError=q_error,
|
| 126 |
+
refineIter=refine_iter)
|
| 127 |
+
i_img = rotatePanorama(img_ori, vp[2::-1])
|
| 128 |
+
|
| 129 |
+
if vp_cache_path is not None:
|
| 130 |
+
with open(vp_cache_path, 'w') as f:
|
| 131 |
+
for i in range(3):
|
| 132 |
+
f.write('%.6f %.6f %.6f\n' % (vp[i, 0], vp[i, 1], vp[i, 2]))
|
| 133 |
+
|
| 134 |
+
return i_img, vp
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def show_depth_normal_grad(dt):
|
| 138 |
+
grad_conv = GradLoss().to(dt['depth'].device).grad_conv
|
| 139 |
+
dt_grad_img = show_grad(dt['depth'][0], grad_conv, 50)
|
| 140 |
+
dt_grad_img = cv2.resize(dt_grad_img, (1024, 60), interpolation=cv2.INTER_NEAREST)
|
| 141 |
+
return dt_grad_img
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def show_alpha_floorplan(dt_xyz, side_l=512, border_color=None):
|
| 145 |
+
if border_color is None:
|
| 146 |
+
border_color = [1, 0, 0, 1]
|
| 147 |
+
fill_color = [0.2, 0.2, 0.2, 0.2]
|
| 148 |
+
dt_floorplan = draw_floorplan(xz=dt_xyz[..., ::2], fill_color=fill_color,
|
| 149 |
+
border_color=border_color, side_l=side_l, show=False, center_color=[1, 0, 0, 1])
|
| 150 |
+
dt_floorplan = Image.fromarray((dt_floorplan * 255).astype(np.uint8), mode='RGBA')
|
| 151 |
+
back = np.zeros([side_l, side_l, len(fill_color)], dtype=np.float32)
|
| 152 |
+
back[..., :] = [0.8, 0.8, 0.8, 1]
|
| 153 |
+
back = Image.fromarray((back * 255).astype(np.uint8), mode='RGBA')
|
| 154 |
+
iou_floorplan = Image.alpha_composite(back, dt_floorplan).convert("RGB")
|
| 155 |
+
dt_floorplan = np.array(iou_floorplan) / 255.0
|
| 156 |
+
return dt_floorplan
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def save_pred_json(xyz, ration, save_path):
|
| 160 |
+
# xyz[..., -1] = -xyz[..., -1]
|
| 161 |
+
json_data = xyz2json(xyz, ration)
|
| 162 |
+
with open(save_path, 'w') as f:
|
| 163 |
+
f.write(json.dumps(json_data, indent=4) + '\n')
|
| 164 |
+
return json_data
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def inference():
|
| 168 |
+
if len(img_paths) == 0:
|
| 169 |
+
logger.error('No images found')
|
| 170 |
+
return
|
| 171 |
+
|
| 172 |
+
bar = tqdm(img_paths, ncols=100)
|
| 173 |
+
for img_path in bar:
|
| 174 |
+
if not os.path.isfile(img_path):
|
| 175 |
+
logger.error(f'The {img_path} not is file')
|
| 176 |
+
continue
|
| 177 |
+
name = os.path.basename(img_path).split('.')[0]
|
| 178 |
+
bar.set_description(name)
|
| 179 |
+
img = np.array(Image.open(img_path).resize((1024, 512), Image.Resampling.BICUBIC))[..., :3]
|
| 180 |
+
if args.post_processing is not None and 'manhattan' in args.post_processing:
|
| 181 |
+
bar.set_description("Preprocessing")
|
| 182 |
+
img, vp = preprocess(img, vp_cache_path=os.path.join(args.output_dir, f"{name}_vp.txt"))
|
| 183 |
+
|
| 184 |
+
img = (img / 255.0).astype(np.float32)
|
| 185 |
+
run_one_inference(img, model, args, name)
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def inference_dataset(dataset):
|
| 189 |
+
bar = tqdm(dataset, ncols=100)
|
| 190 |
+
for data in bar:
|
| 191 |
+
bar.set_description(data['id'])
|
| 192 |
+
run_one_inference(data['image'].transpose(1, 2, 0), model, args, name=data['id'], logger=logger)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
@torch.no_grad()
|
| 196 |
+
def run_one_inference(img, model, args, name, logger, show=True, show_depth=True,
|
| 197 |
+
show_floorplan=True, mesh_format='.gltf', mesh_resolution=512):
|
| 198 |
+
model.eval()
|
| 199 |
+
logger.info("model inference...")
|
| 200 |
+
dt = model(torch.from_numpy(img.transpose(2, 0, 1)[None]).to(args.device))
|
| 201 |
+
if args.post_processing != 'original':
|
| 202 |
+
logger.info(f"post-processing, type:{args.post_processing}...")
|
| 203 |
+
dt['processed_xyz'] = post_process(tensor2np(dt['depth']), type_name=args.post_processing)
|
| 204 |
+
|
| 205 |
+
visualize_2d(img, dt,
|
| 206 |
+
show_depth=show_depth,
|
| 207 |
+
show_floorplan=show_floorplan,
|
| 208 |
+
show=show,
|
| 209 |
+
save_path=os.path.join(args.output_dir, f"{name}_pred.png"))
|
| 210 |
+
output_xyz = dt['processed_xyz'][0] if 'processed_xyz' in dt else depth2xyz(tensor2np(dt['depth'][0]))
|
| 211 |
+
|
| 212 |
+
logger.info(f"saving predicted layout json...")
|
| 213 |
+
json_data = save_pred_json(output_xyz, tensor2np(dt['ratio'][0])[0],
|
| 214 |
+
save_path=os.path.join(args.output_dir, f"{name}_pred.json"))
|
| 215 |
+
# if args.visualize_3d:
|
| 216 |
+
# from visualization.visualizer.visualizer import visualize_3d
|
| 217 |
+
# visualize_3d(json_data, (img * 255).astype(np.uint8))
|
| 218 |
+
|
| 219 |
+
if args.visualize_3d or args.output_3d:
|
| 220 |
+
dt_boundaries = corners2boundaries(tensor2np(dt['ratio'][0])[0], corners_xyz=output_xyz, step=None,
|
| 221 |
+
length=mesh_resolution if 'processed_xyz' in dt else None,
|
| 222 |
+
visible=True if 'processed_xyz' in dt else False)
|
| 223 |
+
dt_layout_depth = layout2depth(dt_boundaries, show=False)
|
| 224 |
+
|
| 225 |
+
logger.info(f"creating 3d mesh ...")
|
| 226 |
+
create_3d_obj(cv2.resize(img, dt_layout_depth.shape[::-1]), dt_layout_depth,
|
| 227 |
+
save_path=os.path.join(args.output_dir, f"{name}_3d{mesh_format}") if args.output_3d else None,
|
| 228 |
+
mesh=True, show=args.visualize_3d)
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
if __name__ == '__main__':
|
| 232 |
+
logger = get_logger()
|
| 233 |
+
args = parse_option()
|
| 234 |
+
config = get_config(args)
|
| 235 |
+
|
| 236 |
+
if ('cuda' in args.device or 'cuda' in config.TRAIN.DEVICE) and not torch.cuda.is_available():
|
| 237 |
+
logger.info(f'The {args.device} is not available, will use cpu ...')
|
| 238 |
+
config.defrost()
|
| 239 |
+
args.device = "cpu"
|
| 240 |
+
config.TRAIN.DEVICE = "cpu"
|
| 241 |
+
config.freeze()
|
| 242 |
+
|
| 243 |
+
model, _, _, _ = build_model(config, logger)
|
| 244 |
+
os.makedirs(args.output_dir, exist_ok=True)
|
| 245 |
+
img_paths = sorted(glob.glob(args.img_glob))
|
| 246 |
+
|
| 247 |
+
inference()
|
| 248 |
+
|
| 249 |
+
# dataset = MP3DDataset(root_dir='./src/dataset/mp3d', mode='test', split_list=[
|
| 250 |
+
# ['7y3sRwLe3Va', '155fac2d50764bf09feb6c8f33e8fb76'],
|
| 251 |
+
# ['e9zR4mvMWw7', 'c904c55a5d0e420bbd6e4e030b9fe5b4'],
|
| 252 |
+
# ])
|
| 253 |
+
# dataset = ZindDataset(root_dir='./src/dataset/zind', mode='test', split_list=[
|
| 254 |
+
# '1169_pano_21',
|
| 255 |
+
# '0583_pano_59',
|
| 256 |
+
# ], vp_align=True)
|
| 257 |
+
# inference_dataset(dataset)
|
|
|
|
|
|
|
|
|
|
|
|