<jupyter_start><jupyter_text>Export ONNX<jupyter_code>example = next(iter(dataloader))
example_torch = example_convert_to_torch(example, float_dtype)
batch_size = example["anchors"].shape[0]
example_tuple = list(example_torch.values())

pillar_x = example_tuple[0][:,:,0].unsqueeze(0).unsqueeze(0)
pillar_y = example_tuple[0][:,:,1].unsqueeze(0).unsqueeze(0)
pillar_z = example_tuple[0][:,:,2].unsqueeze(0).unsqueeze(0)
#             pillar_i = example_tuple[0][:,:,3].unsqueeze(0).unsqueeze(0)
pillar_i = torch.ones(pillar_x.shape,dtype=torch.float32, device=pillar_x.device )

num_points_per_pillar = example_tuple[1].float().unsqueeze(0)

# Find distance of x, y, and z from pillar center
# assuming xyres_16.proto
coors_x = example_tuple[2][:, 3].float()
coors_y = example_tuple[2][:, 2].float()
vx, vy = voxel_generator.voxel_size[0], voxel_generator.voxel_size[1]
x_offset = vx/2 + voxel_generator.point_cloud_range[0]
y_offset = vy/2 + voxel_generator.point_cloud_range[1]
# self.x_offset = self.vx / 2 + pc_range[0]
# self.y_offset = self.vy / 2 + pc_range[1]
# this assumes xyres 20
# x_sub = coors_x.unsqueeze(1) * 0.16 + 0.1
# y_sub = coors_y.unsqueeze(1) * 0.16 + -39.9
# here assumes xyres 16
x_sub = coors_x.unsqueeze(1)*vx + x_offset
y_sub = coors_y.unsqueeze(1)*vy + y_offset
# x_sub = coors_x.unsqueeze(1)*0.28 + 0.14
# y_sub = coors_y.unsqueeze(1)*0.28 - 20.0
ones = torch.ones([1,voxel_generator._max_num_points], dtype = torch.float32, device = pillar_x.device)
x_sub_shaped = torch.mm(x_sub, ones).unsqueeze(0).unsqueeze(0)
y_sub_shaped = torch.mm(y_sub, ones).unsqueeze(0).unsqueeze(0)

num_points_for_a_pillar = pillar_x.size()[3]
mask = get_paddings_indicator(num_points_per_pillar, num_points_for_a_pillar, axis=0)
mask = mask.permute(0, 2, 1)
mask = mask.unsqueeze(1)
mask = mask.type_as(pillar_x)

coors   = example_tuple[2]
print("pillar_x.size()",pillar_x.size())
print("pillar_y.size()",pillar_y.size())
print("pillar_z.size()",pillar_z.size())
print("pillar_i.size()",pillar_i.size())
print("num_points_per_pillar.size()",num_points_per_pillar.size())
print("x_sub_shaped.size()",x_sub_shaped.size())
print("y_sub_shaped.size()",y_sub_shaped.size())
print("mask.size()",mask.size())
pillar_x = torch.ones([1, 1, 70000, 100], dtype=torch.float32, device=pillar_x.device)
pillar_y = torch.ones([1, 1, 70000, 100], dtype=torch.float32, device=pillar_x.device)
pillar_z = torch.ones([1, 1, 70000, 100], dtype=torch.float32, device=pillar_x.device)
pillar_i = torch.ones([1, 1, 70000, 100], dtype=torch.float32, device=pillar_x.device)
num_points_per_pillar = torch.ones([1, 70000], dtype=torch.float32, device=pillar_x.device)
x_sub_shaped = torch.ones([1, 1, 70000, 100], dtype=torch.float32, device=pillar_x.device)
y_sub_shaped = torch.ones([1, 1, 70000, 100], dtype=torch.float32, device=pillar_x.device)
mask = torch.ones([1, 1, 70000, 100], dtype=torch.float32, device=pillar_x.device)
example1 = [pillar_x, pillar_y, pillar_z, pillar_i,
         num_points_per_pillar, x_sub_shaped, y_sub_shaped, mask]
input_names = ['pillar_x', 'pillar_y', 'pillar_z', 'pillar_i',
         'num_points_per_pillar', 'x_sub_shaped', 'y_sub_shaped', 'mask']

torch.onnx.export(net, example1, "pfe.onnx", verbose=False, input_names=input_names)
rpn_input = torch.ones([1, 64, 400, 600], dtype=torch.float32, device=pillar_x.device)
torch.onnx.export(net.rpn, rpn_input, "rpn.onnx", verbose=False)<jupyter_output><empty_output><jupyter_text>Plot eval once<jupyter_code>net.eval()
example = next(iter(dataloader))

example_torch = example_convert_to_torch(example, float_dtype)
batch_size = example["anchors"].shape[0]
example_tuple = list(example_torch.values())

pillar_x = example_tuple[0][:,:,0].unsqueeze(0).unsqueeze(0)
pillar_y = example_tuple[0][:,:,1].unsqueeze(0).unsqueeze(0)
pillar_z = example_tuple[0][:,:,2].unsqueeze(0).unsqueeze(0)
# pillar_i = example_tuple[0][:,:,3].unsqueeze(0).unsqueeze(0)
pillar_i = torch.ones(pillar_x.shape,dtype=torch.float32, device=pillar_x.device )

num_points_per_pillar = example_tuple[1].float().unsqueeze(0)

# Find distance of x, y, and z from pillar center
# assuming xyres_16.proto
coors_x = example_tuple[2][:, 3].float()
coors_y = example_tuple[2][:, 2].float()
vx, vy = voxel_generator.voxel_size[0], voxel_generator.voxel_size[1]
x_offset = vx/2 + voxel_generator.point_cloud_range[0]
y_offset = vy/2 + voxel_generator.point_cloud_range[1]
# self.x_offset = self.vx / 2 + pc_range[0]
# self.y_offset = self.vy / 2 + pc_range[1]
# this assumes xyres 20
# x_sub = coors_x.unsqueeze(1) * 0.16 + 0.1
# y_sub = coors_y.unsqueeze(1) * 0.16 + -39.9
# here assumes xyres 16
x_sub = coors_x.unsqueeze(1)*vx + x_offset
y_sub = coors_y.unsqueeze(1)*vy + y_offset
# x_sub = coors_x.unsqueeze(1)*0.28 + 0.14
# y_sub = coors_y.unsqueeze(1)*0.28 - 20.0
ones = torch.ones([1,voxel_generator._max_num_points], dtype = torch.float32, device = pillar_x.device)
x_sub_shaped = torch.mm(x_sub, ones)
y_sub_shaped = torch.mm(y_sub, ones)

num_points_for_a_pillar = pillar_x.size()[3]
mask = get_paddings_indicator(num_points_per_pillar, num_points_for_a_pillar, axis=0)
mask = mask.permute(0, 2, 1)
mask = mask.unsqueeze(1)
mask = mask.type_as(pillar_x)

coors   = example_tuple[2]
anchors = example_tuple[3]
labels  = example_tuple[4]
reg_targets = example_tuple[5]

inputs = [pillar_x, pillar_y, pillar_z, pillar_i,
         num_points_per_pillar, x_sub_shaped, y_sub_shaped, mask, coors,
         anchors, labels, reg_targets]

ret_dict = net(inputs) 
ret_dict

# example_token = example["metadata"][0]['token']
# filtered_sample_tokens = dataset.dataset.filtered_sample_tokens
# index = filtered_sample_tokens.index(example["metadata"][0]['token'])
# gt_example = dataset.dataset.get_sensor_data(index)
points = example['points'][0]
pc_range = model_cfg.voxel_generator.point_cloud_range
points = np.array(
    [p for p in points if (pc_range[0] < p[0] < pc_range[3]) & (pc_range[1] < p[1] < pc_range[4]) & (
            pc_range[2] < p[2] < pc_range[5])])
gt_boxes = example['gt_boxes']
gt_labels = example['gt_names'][0]
c = np.zeros(points[:, 3].shape[0]).reshape(-1, 1)
c = np.concatenate([c, c, c], axis=1)
points = points[:, 0:3]
pc = o3d.geometry.PointCloud()
pc.points = o3d.utility.Vector3dVector(points)
pc.colors = o3d.utility.Vector3dVector(c)
mesh_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=2.0,
                                                               origin=[-0, -0, -0])
geo = [pc, mesh_frame]
detection = [{'box3d_lidar': ret_dict[0][0], "label_preds": ret_dict[0][2], 'scores': ret_dict[0][1]}]
geo = add_prediction_per_class(dataset.dataset.nusc,
                               detection, gt_boxes, gt_labels,
                               ["traffic_cone"], geo)
o3d.visualization.draw_geometries(geo)

import pandas as pd
pd.set_option('display.max_rows', 5000)
import matplotlib.pyplot as plt
plt.plot(np.array(range(len(mean_losses)))*50,mean_losses,"r.")
mean_losses<jupyter_output><empty_output><jupyter_text>Eval<jupyter_code>from second.data.preprocess import merge_second_batch, prep_pointcloud

# pc_file_name = "/media/starlet/LdTho/data/sets/1614053547.551486.pcd"
# pc_file_name = "/media/starlet/LdTho/data/sets/1612769433360603.pcd"
pc_file_name = "/media/starlet/LdTho/data/sets/baraja_lidar/1612769435143246.pcd"
pcd = o3d.io.read_point_cloud(pc_file_name)
points = np.asarray(pcd.points)
pc_range = model_cfg.voxel_generator.point_cloud_range
points = np.array([p for p in points if (pc_range[0] < p[0] < pc_range[3])
                   & (pc_range[1] < p[1] < pc_range[4])
                   & (pc_range[2] < p[2] < pc_range[5])])

points = np.concatenate([points.transpose(), 
                         np.array([np.ones(points.shape[0])]),],axis = 0).transpose()
points = points[~np.isnan(points).any(axis=1)]

points

input_dict = {
    'lidar': {
        'type': 'lidar',
        'points': points
    },
    'metadata': {
        'token': pc_file_name
    }
}

out_size_factor = model_cfg.rpn.layer_strides[0] // model_cfg.rpn.upsample_strides[0]
example = prep_pointcloud(input_dict=input_dict,
                          root_path= None ,
                          voxel_generator= voxel_generator,
                          target_assigner= target_assigner,
                          max_voxels= 70000,
                          training= False,
                          create_targets=False,
                          shuffle_points=False,
                          num_point_features=model_cfg.num_point_features,
                          remove_outside_points=False,
                          anchor_cache=None,
                          anchor_area_threshold=-1,
                          out_size_factor=out_size_factor,
                          out_dtype=np.float32
                          )
# example["points"] = points
example["metadata"] = input_dict["metadata"]
example = [example]
print(example)
eval_dataloader = torch.utils.data.DataLoader(
        example,
        batch_size=input_cfg.batch_size,
        shuffle=True,
        num_workers=4,
        pin_memory=False,
        collate_fn=merge_second_batch)
net.eval()
example = next(iter(eval_dataloader))
example_torch = example_convert_to_torch(example, float_dtype)
batch_size = example["anchors"].shape[0]
example_tuple = list(example_torch.values())

pillar_x = example_tuple[0][:,:,0].unsqueeze(0).unsqueeze(0)
pillar_y = example_tuple[0][:,:,1].unsqueeze(0).unsqueeze(0)
pillar_z = example_tuple[0][:,:,2].unsqueeze(0).unsqueeze(0)
# pillar_i = example_tuple[0][:,:,3].unsqueeze(0).unsqueeze(0)
pillar_i = torch.ones(pillar_x.shape,dtype=torch.float32, device=pillar_x.device )

num_points_per_pillar = example_tuple[1].float().unsqueeze(0)

# Find distance of x, y, and z from pillar center
# assuming xyres_16.proto
coors_x = example_tuple[2][:, 3].float()
coors_y = example_tuple[2][:, 2].float()
vx, vy = voxel_generator.voxel_size[0], voxel_generator.voxel_size[1]
x_offset = vx/2 + voxel_generator.point_cloud_range[0]
y_offset = vy/2 + voxel_generator.point_cloud_range[1]
# self.x_offset = self.vx / 2 + pc_range[0]
# self.y_offset = self.vy / 2 + pc_range[1]
# this assumes xyres 20
# x_sub = coors_x.unsqueeze(1) * 0.16 + 0.1
# y_sub = coors_y.unsqueeze(1) * 0.16 + -39.9
# here assumes xyres 16
x_sub = coors_x.unsqueeze(1)*vx + x_offset
y_sub = coors_y.unsqueeze(1)*vy + y_offset
# x_sub = coors_x.unsqueeze(1)*0.28 + 0.14
# y_sub = coors_y.unsqueeze(1)*0.28 - 20.0
ones = torch.ones([1,voxel_generator._max_num_points], dtype = torch.float32, device = pillar_x.device)
x_sub_shaped = torch.mm(x_sub, ones)
y_sub_shaped = torch.mm(y_sub, ones)

num_points_for_a_pillar = pillar_x.size()[3]
mask = get_paddings_indicator(num_points_per_pillar, num_points_for_a_pillar, axis=0)
mask = mask.permute(0, 2, 1)
mask = mask.unsqueeze(1)
mask = mask.type_as(pillar_x)

coors   = example_tuple[2]
anchors = example_tuple[3]
labels  = example_tuple[4]
# reg_targets = example_tuple[5]

inputs = [pillar_x, pillar_y, pillar_z, pillar_i,
         num_points_per_pillar, x_sub_shaped, y_sub_shaped, mask, coors,
         anchors, labels]

ret_dict = net(inputs)
ret_dict
points = np.asarray(pcd.points)
points = np.array(
    [p for p in points if (pc_range[0] < p[0] < pc_range[3]) & (pc_range[1] < p[1] < pc_range[4]) & (
           pc_range[2]  < p[2] < pc_range[5])])
points = np.concatenate([points.transpose(), 
                         np.array([np.repeat(0.0, points.shape[0])]),
                         np.array([np.repeat(0.0, points.shape[0])])],axis = 0).transpose()
points = points[~np.isnan(points).any(axis=1)]

c = points[:, 3].reshape(-1, 1)
c = np.concatenate([c, c, c], axis=1)
points = points[:, 0:3]

pc = o3d.geometry.PointCloud()
pc.points = o3d.utility.Vector3dVector(points)
pc.colors = o3d.utility.Vector3dVector(c)
mesh_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=2.0,
                                                               origin=[-0, -0, -0])


geo = [pc, mesh_frame]
detection = [{'box3d_lidar': ret_dict[0][0], "label_preds": ret_dict[0][2], 'scores': ret_dict[0][1]}]
det_boxes = detection[0]['box3d_lidar'].cpu().detach().numpy()
det_labels = detection[0]['label_preds'].cpu().detach().numpy()
det_scores = detection[0]['scores'].cpu().detach().numpy()
class_names = ['traffic_cone']
color = {
"traffic_cone": (1,0,0),
"gt_traffic_cone": (0,1,0),
"pedestrian": (1,1,0),
"gt_pedestrian": (0,0,1)
}
for i, class_name in enumerate(class_names):
    mask = np.logical_and(det_labels == i, det_scores > 0.21)
    class_det_boxes = det_boxes[mask]
    class_det_scores = det_scores[mask]
    class_det_labels = det_labels[mask]
    print(len(class_det_boxes),len(class_det_scores),len(class_det_labels))
    print(class_det_scores)
    rbbox_corners = box_np_ops.center_to_corner_box3d(class_det_boxes[:, :3],
                                                      class_det_boxes[:, 3:6],
                                                      class_det_boxes[:, 6],
                                                      origin=(0.5, 0.5, 0.5), axis=2)

    for j in range(len(rbbox_corners)):
        geo.append(buildBBox(rbbox_corners[j],
                                    color=color[class_name]))

o3d.visualization.draw_geometries(geo)

pillar_z
v_path = "/media/starlet/LdTho/data/sets/KITTI/training/velodyne/000000.bin"
points = np.fromfile(
        str(v_path), dtype=np.float32,
        count=-1).reshape([-1, 4])
points<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup