RandLA-Net导出onnx模型并使用onnxruntime推理

首先下载RandLA-Net工程:https://github.com/tsunghan-wu/RandLA-Net-pytorch

导出onnx模型

import torch
from utils.config import ConfigSemanticKITTI as cfg
from network.RandLANet import Networkmodel = Network(cfg)
checkpoint = torch.load("./pretrain_model/checkpoint.tar")
model.load_state_dict(checkpoint['model_state_dict'])input = {}
input['xyz'] = [torch.zeros([1, 45056, 3]), torch.zeros([1, 11264, 3]), torch.zeros([1, 2816, 3]), torch.zeros([1, 704, 3])]
input['neigh_idx'] = [torch.zeros([1, 45056, 16], dtype=torch.int64), torch.zeros([1, 11264, 16], dtype=torch.int64), torch.zeros([1, 2816, 16], dtype=torch.int64), torch.zeros([1, 704, 16], dtype=torch.int64)]
input['sub_idx'] = [torch.zeros([1, 11264, 16], dtype=torch.int64), torch.zeros([1, 2816, 16], dtype=torch.int64), torch.zeros([1, 704, 16], dtype=torch.int64), torch.zeros([1, 176, 16], dtype=torch.int64)]
input['interp_idx'] = [torch.zeros([1, 45056, 1], dtype=torch.int64), torch.zeros([1, 11264, 1], dtype=torch.int64), torch.zeros([1, 2816, 1], dtype=torch.int64), torch.zeros([1, 704, 1], dtype=torch.int64)]
input['features'] = torch.zeros([1, 3, 45056])
input['labels'] = torch.zeros([1, 45056], dtype=torch.int64)
input['logits'] = torch.zeros([1, 19, 45056])torch.onnx.export(model, input, "randla-net.onnx", opset_version=13)

onnx模型结构如下:
在这里插入图片描述

onnxruntime推理

python代码

import pickle
import numpy as np
import torch
from network.RandLANet import Network
from utils.data_process import DataProcessing as DP
from utils.config import ConfigSemanticKITTI as cfgnp.random.seed(0)
k_n = 16 
num_points = 4096 * 11 
num_layers = 4
num_classes = 19
sub_sampling_ratio = [4, 4, 4, 4]  if __name__ == '__main__':net = Network(cfg).to(torch.device("cpu"))checkpoint = torch.load("pretrain_model/checkpoint.tar", map_location=torch.device('cpu'))net.load_state_dict(checkpoint['model_state_dict'])points = np.load('./data/08/velodyne/000000.npy') possibility = np.zeros(points.shape[0]) * 1e-3 #[np.random.rand(points.shape[0]) * 1e-3]min_possibility = [float(np.min(possibility[-1]))]probs = [np.zeros(shape=[points.shape[0], num_classes], dtype=np.float32)]test_probs = probstest_smooth = 0.98import onnxruntime     onnx_session = onnxruntime.InferenceSession("randla-net.onnx", providers=['CPUExecutionProvider'])input_name = []for node in onnx_session.get_inputs():input_name.append(node.name)output_name = []for node in onnx_session.get_outputs():output_name.append(node.name)net.eval()with torch.no_grad():with open('./data/08/KDTree/000000.pkl', 'rb') as f:tree = pickle.load(f)pc = np.array(tree.data, copy=False)labels = np.zeros(np.shape(pc)[0])    while np.min(min_possibility) <= 0.5:cloud_ind = int(np.argmin(min_possibility))pick_idx = np.argmin(possibility)        center_point = pc[pick_idx, :].reshape(1, -1)selected_idx = tree.query(center_point, num_points)[1][0]selected_pc = pc[selected_idx]selected_labels = labels[selected_idx]   dists = np.sum(np.square((selected_pc - pc[pick_idx])), axis=1)delta = np.square(1 - dists / np.max(dists))possibility[selected_idx] += delta  # possibility[193] += delta[1], possibility[20283] += delta[45055]min_possibility[cloud_ind] = np.min(possibility)batch_pc = np.expand_dims(selected_pc, 0)batch_label = np.expand_dims(selected_labels, 0)batch_pc_idx = np.expand_dims(selected_idx, 0)batch_cloud_idx = np.expand_dims(np.array([cloud_ind], dtype=np.int32), 0)features = batch_pcinput_points, input_neighbors, input_pools, input_up_samples = [], [], [], []for i in range(num_layers):neighbour_idx = DP.knn_search(batch_pc, batch_pc, k_n)sub_points = batch_pc[:, :batch_pc.shape[1] // sub_sampling_ratio[i], :]pool_i = neighbour_idx[:, :batch_pc.shape[1] // sub_sampling_ratio[i], :]   up_i = DP.knn_search(sub_points, batch_pc, 1)input_points.append(batch_pc)input_neighbors.append(neighbour_idx)input_pools.append(pool_i)input_up_samples.append(up_i)batch_pc = sub_pointsflat_inputs = input_points + input_neighbors + input_pools + input_up_samplesflat_inputs += [features, batch_label, batch_pc_idx, batch_cloud_idx]batch_data, inputs = {}, {}    batch_data['xyz'] = []for tmp in flat_inputs[:num_layers]:batch_data['xyz'].append(torch.from_numpy(tmp).float())inputs['xyz.1'] = flat_inputs[:num_layers][0].astype(np.float32)inputs['xyz.2'] = flat_inputs[:num_layers][1].astype(np.float32)inputs['xyz.3'] = flat_inputs[:num_layers][2].astype(np.float32)inputs['xyz'] = flat_inputs[:num_layers][3].astype(np.float32)batch_data['neigh_idx'] = []for tmp in flat_inputs[num_layers: 2 * num_layers]:batch_data['neigh_idx'].append(torch.from_numpy(tmp).long())inputs['neigh_idx.1'] = flat_inputs[num_layers: 2 * num_layers][0].astype(np.int64)inputs['neigh_idx.2'] = flat_inputs[num_layers: 2 * num_layers][1].astype(np.int64)inputs['neigh_idx.3'] = flat_inputs[num_layers: 2 * num_layers][2].astype(np.int64)inputs['neigh_idx'] = flat_inputs[num_layers: 2 * num_layers][3].astype(np.int64)batch_data['sub_idx'] = []for tmp in flat_inputs[2 * num_layers:3 * num_layers]:batch_data['sub_idx'].append(torch.from_numpy(tmp).long())inputs['8'] = flat_inputs[2 * num_layers:3 * num_layers][0].astype(np.int64)inputs['9'] = flat_inputs[2 * num_layers:3 * num_layers][1].astype(np.int64)inputs['10'] = flat_inputs[2 * num_layers:3 * num_layers][2].astype(np.int64)inputs['11'] = flat_inputs[2 * num_layers:3 * num_layers][3].astype(np.int64)batch_data['interp_idx'] = []for tmp in flat_inputs[3 * num_layers:4 * num_layers]:batch_data['interp_idx'].append(torch.from_numpy(tmp).long())inputs['12'] = flat_inputs[3 * num_layers:4 * num_layers][0].astype(np.int64)inputs['13'] = flat_inputs[3 * num_layers:4 * num_layers][1].astype(np.int64)inputs['14'] = flat_inputs[3 * num_layers:4 * num_layers][2].astype(np.int64)inputs['15'] = flat_inputs[3 * num_layers:4 * num_layers][3].astype(np.int64)batch_data['features'] = torch.from_numpy(flat_inputs[4 * num_layers]).transpose(1, 2).float()inputs['input.1'] = np.swapaxes(flat_inputs[4 * num_layers], 1, 2).astype(np.float32)batch_data['labels'] = torch.from_numpy(flat_inputs[4 * num_layers + 1]).long()inputs['17'] = flat_inputs[4 * num_layers + 1].astype(np.int64)input_inds = flat_inputs[4 * num_layers + 2]cloud_inds = flat_inputs[4 * num_layers + 3]for key in batch_data:if type(batch_data[key]) is list:for i in range(num_layers):batch_data[key][i] = batch_data[key][i]else:batch_data[key] = batch_data[key]end_points = net(batch_data)outputs = onnx_session.run(None, inputs)      end_points['logits'] = end_points['logits'].transpose(1, 2).cpu().numpy()for j in range(end_points['logits'].shape[0]):probs = end_points['logits'][j]inds = input_inds[j]c_i = cloud_inds[j][0]test_probs[c_i][inds] = test_smooth * test_probs[c_i][inds] + (1 - test_smooth) * probs #19  (45056, 19)for j in range(len(test_probs)): pred = np.argmax(test_probs[j], 1).astype(np.uint32) + 1output = np.concatenate((points, pred.reshape(-1, 1)), axis=1)np.savetxt('./result/output.txt', output)

C++代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/search/kdtree.h> 
#include <pcl/common/distances.h>
#include <onnxruntime_cxx_api.h>#include "knn_.h"const int k_n = 16;
const int num_classes = 19;
const int num_points = 4096 * 11;
const int num_layers = 4;
const float test_smooth = 0.98;std::vector<std::vector<long>> knn_search(pcl::PointCloud<pcl::PointXYZ>::Ptr& support_pts, pcl::PointCloud<pcl::PointXYZ>::Ptr& query_pts, int k)
{float* points = new float[support_pts->size() * 3];for (size_t i = 0; i < support_pts->size(); i++){points[3 * i + 0] = support_pts->points[i].x;points[3 * i + 1] = support_pts->points[i].y;points[3 * i + 2] = support_pts->points[i].z;}float* queries = new float[query_pts->size() * 3];for (size_t i = 0; i < query_pts->size(); i++){queries[3 * i + 0] = query_pts->points[i].x;queries[3 * i + 1] = query_pts->points[i].y;queries[3 * i + 2] = query_pts->points[i].z;}long* indices = new long[query_pts->size() * k];cpp_knn_omp(points, support_pts->size(), 3, queries, query_pts->size(), k, indices);std::vector<std::vector<long>> neighbour_idx(query_pts->size(), std::vector<long>(k));for (size_t i = 0; i < query_pts->size(); i++){for (size_t j = 0; j < k; j++){neighbour_idx[i][j] = indices[k * i + j];}}return neighbour_idx;
}int main()
{Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "randla-net");Ort::SessionOptions session_options;session_options.SetIntraOpNumThreads(1);session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);const wchar_t* model_path = L"randla-net.onnx";Ort::Session session(env, model_path, session_options);Ort::AllocatorWithDefaultOptions allocator;std::vector<const char*>  input_node_names;for (size_t i = 0; i < session.GetInputCount(); i++){input_node_names.push_back(session.GetInputName(i, allocator));}std::vector<const char*> output_node_names;for (size_t i = 0; i < session.GetOutputCount(); i++){output_node_names.push_back(session.GetOutputName(i, allocator));}float x, y, z;pcl::PointCloud<pcl::PointXYZ>::Ptr points(new pcl::PointCloud<pcl::PointXYZ>);std::ifstream infile_points("000000.txt");while (infile_points >> x >> y >> z){points->push_back(pcl::PointXYZ(x, y, z));}std::vector<float> possibility(points->size(), 0);std::vector<float> min_possibility = { 0 };std::vector<std::vector<float>> test_probs(points->size(), std::vector<float>(num_classes, 0));pcl::PointCloud<pcl::PointXYZ>::Ptr pc(new pcl::PointCloud<pcl::PointXYZ>);std::ifstream infile_pc("000000.pkl", std::ios::binary);while (infile_pc >> x >> y >> z){pc->push_back(pcl::PointXYZ(x, y, z));}std::vector<float> labels(pc->size(), 0);while (*std::min_element(min_possibility.begin(), min_possibility.end()) < 0.5){int cloud_ind = std::min_element(min_possibility.begin(), min_possibility.end()) - min_possibility.begin();int pick_idx = std::min_element(possibility.begin(), possibility.end()) - possibility.begin();pcl::PointXYZ center_point = pc->points[pick_idx];pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree(new pcl::search::KdTree<pcl::PointXYZ>);kdtree->setInputCloud(pc);std::vector<int> selected_idx(num_points);std::vector<float> distances(num_points);kdtree->nearestKSearch(center_point, num_points, selected_idx, distances);pcl::PointCloud<pcl::PointXYZ>::Ptr selected_pc(new pcl::PointCloud<pcl::PointXYZ>);pcl::copyPointCloud(*pc, selected_idx, *selected_pc);std::vector<float> selected_labels(num_points);for (size_t i = 0; i < num_points; i++){selected_labels[i] = labels[selected_idx[i]];}std::vector<float> dists(num_points);for (size_t i = 0; i < num_points; i++){dists[i] = pcl::squaredEuclideanDistance(selected_pc->points[i], pc->points[pick_idx]);}float max_dists = *std::max_element(dists.begin(), dists.end());std::vector<float> delta(num_points);for (size_t i = 0; i < num_points; i++){delta[i] = pow(1 - dists[i] / max_dists, 2);possibility[selected_idx[i]] += delta[i];}min_possibility[cloud_ind] = *std::min_element(possibility.begin(), possibility.end());pcl::PointCloud<pcl::PointXYZ>::Ptr features(new pcl::PointCloud<pcl::PointXYZ>);pcl::copyPointCloud(*selected_pc, *features);std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> input_points;std::vector<std::vector<std::vector<long>>> input_neighbors, input_pools, input_up_samples;for (size_t i = 0; i < num_layers; i++){std::vector<std::vector<long>> neighbour_idx = knn_search(selected_pc, selected_pc, k_n);pcl::PointCloud<pcl::PointXYZ>::Ptr sub_points(new pcl::PointCloud<pcl::PointXYZ>);std::vector<int> index(selected_pc->size() / 4);std::iota(index.begin(), index.end(), 0);pcl::copyPointCloud(*selected_pc, index, *sub_points);std::vector<std::vector<long>> pool_i(selected_pc->size() / 4);std::copy(neighbour_idx.begin(), neighbour_idx.begin() + selected_pc->size() / 4, pool_i.begin());std::vector<std::vector<long>> up_i = knn_search(sub_points, selected_pc, 1);input_points.push_back(selected_pc);input_neighbors.push_back(neighbour_idx);input_pools.push_back(pool_i);input_up_samples.push_back(up_i);selected_pc = sub_points;}const size_t xyz1_size = 1 * input_points[0]->size() * 3;std::vector<float> xyz1_values(xyz1_size);for (size_t i = 0; i < input_points[0]->size(); i++){xyz1_values[3 * i + 0] = input_points[0]->points[i].x;xyz1_values[3 * i + 1] = input_points[0]->points[i].y;xyz1_values[3 * i + 2] = input_points[0]->points[i].z;}std::vector<int64_t> xyz1_dims = { 1, (int64_t)input_points[0]->size(), 3 };auto xyz1_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value xyz1_tensor = Ort::Value::CreateTensor<float>(xyz1_memory, xyz1_values.data(), xyz1_size, xyz1_dims.data(), xyz1_dims.size());const size_t xyz2_size = 1 * input_points[1]->size() * 3;std::vector<float> xyz2_values(xyz2_size);for (size_t i = 0; i < input_points[1]->size(); i++){xyz2_values[3 * i + 0] = input_points[1]->points[i].x;xyz2_values[3 * i + 1] = input_points[1]->points[i].y;xyz2_values[3 * i + 2] = input_points[1]->points[i].z;}std::vector<int64_t> xyz2_dims = { 1, (int64_t)input_points[1]->size(), 3 };auto xyz2_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value xyz2_tensor = Ort::Value::CreateTensor<float>(xyz2_memory, xyz2_values.data(), xyz2_size, xyz2_dims.data(), xyz2_dims.size());const size_t xyz3_size = 1 * input_points[2]->size() * 3;std::vector<float> xyz3_values(xyz3_size);for (size_t i = 0; i < input_points[2]->size(); i++){xyz3_values[3 * i + 0] = input_points[2]->points[i].x;xyz3_values[3 * i + 1] = input_points[2]->points[i].y;xyz3_values[3 * i + 2] = input_points[2]->points[i].z;}std::vector<int64_t> xyz3_dims = { 1, (int64_t)input_points[2]->size(), 3 };auto xyz3_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value xyz3_tensor = Ort::Value::CreateTensor<float>(xyz3_memory, xyz3_values.data(), xyz3_size, xyz3_dims.data(), xyz3_dims.size());const size_t xyz_size = 1 * input_points[3]->size() * 3;std::vector<float> xyz_values(xyz_size);for (size_t i = 0; i < input_points[3]->size(); i++){xyz_values[3 * i + 0] = input_points[3]->points[i].x;xyz_values[3 * i + 1] = input_points[3]->points[i].y;xyz_values[3 * i + 2] = input_points[3]->points[i].z;}std::vector<int64_t> xyz_dims = { 1, (int64_t)input_points[3]->size(), 3 };auto xyz_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value xyz_tensor = Ort::Value::CreateTensor<float>(xyz_memory, xyz_values.data(), xyz_size, xyz_dims.data(), xyz_dims.size());const size_t neigh_idx1_size = 1 * input_neighbors[0].size() * 16;std::vector<int64_t> neigh_idx1_values(neigh_idx1_size);for (size_t i = 0; i < input_neighbors[0].size(); i++){for (size_t j = 0; j < 16; j++){neigh_idx1_values[16 * i + j] = input_neighbors[0][i][j];}}std::vector<int64_t> neigh_idx1_dims = { 1, (int64_t)input_neighbors[0].size(), 16 };auto neigh_idx1_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value neigh_idx1_tensor = Ort::Value::CreateTensor<int64_t>(neigh_idx1_memory, neigh_idx1_values.data(), neigh_idx1_size, neigh_idx1_dims.data(), neigh_idx1_dims.size());const size_t neigh_idx2_size = 1 * input_neighbors[1].size() * 16;std::vector<int64_t> neigh_idx2_values(neigh_idx2_size);for (size_t i = 0; i < input_neighbors[1].size(); i++){for (size_t j = 0; j < 16; j++){neigh_idx2_values[16 * i + j] = input_neighbors[1][i][j];}}std::vector<int64_t> neigh_idx2_dims = { 1, (int64_t)input_neighbors[1].size(), 16 };auto neigh_idx2_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value neigh_idx2_tensor = Ort::Value::CreateTensor<int64_t>(neigh_idx2_memory, neigh_idx2_values.data(), neigh_idx2_size, neigh_idx2_dims.data(), neigh_idx2_dims.size());const size_t neigh_idx3_size = 1 * input_neighbors[2].size() * 16;std::vector<int64_t> neigh_idx3_values(neigh_idx3_size);for (size_t i = 0; i < input_neighbors[2].size(); i++){for (size_t j = 0; j < 16; j++){neigh_idx3_values[16 * i + j] = input_neighbors[2][i][j];}}std::vector<int64_t> neigh_idx3_dims = { 1, (int64_t)input_neighbors[2].size(), 16 };auto neigh_idx3_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value neigh_idx3_tensor = Ort::Value::CreateTensor<int64_t>(neigh_idx3_memory, neigh_idx3_values.data(), neigh_idx3_size, neigh_idx3_dims.data(), neigh_idx3_dims.size());const size_t neigh_idx_size = 1 * input_neighbors[3].size() * 16;std::vector<int64_t> neigh_idx_values(neigh_idx_size);for (size_t i = 0; i < input_neighbors[3].size(); i++){for (size_t j = 0; j < 16; j++){neigh_idx_values[16 * i + j] = input_neighbors[3][i][j];}}std::vector<int64_t> neigh_idx_dims = { 1, (int64_t)input_neighbors[3].size(), 16 };auto neigh_idx_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value neigh_idx_tensor = Ort::Value::CreateTensor<int64_t>(neigh_idx_memory, neigh_idx_values.data(), neigh_idx_size, neigh_idx_dims.data(), neigh_idx_dims.size());const size_t sub_idx8_size = 1 * input_pools[0].size() * 16;std::vector<int64_t> sub_idx8_values(sub_idx8_size);for (size_t i = 0; i < input_pools[0].size(); i++){for (size_t j = 0; j < 16; j++){sub_idx8_values[16 * i + j] = input_pools[0][i][j];}}std::vector<int64_t> sub_idx8_dims = { 1, (int64_t)input_pools[0].size(), 16 };auto sub_idx8_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value sub_idx8_tensor = Ort::Value::CreateTensor<int64_t>(sub_idx8_memory, sub_idx8_values.data(), sub_idx8_size, sub_idx8_dims.data(), sub_idx8_dims.size());const size_t sub_idx9_size = 1 * input_pools[1].size() * 16;std::vector<int64_t> sub_idx9_values(sub_idx9_size);for (size_t i = 0; i < input_pools[1].size(); i++){for (size_t j = 0; j < 16; j++){sub_idx9_values[16 * i + j] = input_pools[1][i][j];}}std::vector<int64_t> sub_idx9_dims = { 1, (int64_t)input_pools[1].size(), 16 };auto sub_idx9_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value sub_idx9_tensor = Ort::Value::CreateTensor<int64_t>(sub_idx9_memory, sub_idx9_values.data(), sub_idx9_size, sub_idx9_dims.data(), sub_idx9_dims.size());const size_t sub_idx10_size = 1 * input_pools[2].size() * 16;std::vector<int64_t> sub_idx10_values(sub_idx10_size);for (size_t i = 0; i < input_pools[2].size(); i++){for (size_t j = 0; j < 16; j++){sub_idx10_values[16 * i + j] = input_pools[2][i][j];}}std::vector<int64_t> sub_idx10_dims = { 1, (int64_t)input_pools[2].size(), 16 };auto sub_idx10_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value sub_idx10_tensor = Ort::Value::CreateTensor<int64_t>(sub_idx10_memory, sub_idx10_values.data(), sub_idx10_size, sub_idx10_dims.data(), sub_idx10_dims.size());const size_t sub_idx11_size = 1 * input_pools[3].size() * 16;std::vector<int64_t> sub_idx11_values(sub_idx11_size);for (size_t i = 0; i < input_pools[3].size(); i++){for (size_t j = 0; j < 16; j++){sub_idx11_values[16 * i + j] = input_pools[3][i][j];}}std::vector<int64_t> sub_idx11_dims = { 1, (int64_t)input_pools[3].size(), 16 };auto sub_idx11_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value sub_idx11_tensor = Ort::Value::CreateTensor<int64_t>(sub_idx11_memory, sub_idx11_values.data(), sub_idx11_size, sub_idx11_dims.data(), sub_idx11_dims.size());const size_t interp_idx12_size = 1 * input_up_samples[0].size() * 1;std::vector<int64_t> interp_idx12_values(interp_idx12_size);for (size_t i = 0; i < input_up_samples[0].size(); i++){interp_idx12_values[i] = input_up_samples[0][i][0];}std::vector<int64_t> interp_idx12_dims = { 1, (int64_t)input_up_samples[0].size(), 1 };auto interp_idx12_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value interp_idx12_tensor = Ort::Value::CreateTensor<int64_t>(interp_idx12_memory, interp_idx12_values.data(), interp_idx12_size, interp_idx12_dims.data(), interp_idx12_dims.size());const size_t interp_idx13_size = 1 * input_up_samples[1].size() * 1;std::vector<int64_t> interp_idx13_values(interp_idx13_size);for (size_t i = 0; i < input_up_samples[1].size(); i++){interp_idx13_values[i] = input_up_samples[1][i][0];}std::vector<int64_t> interp_idx13_dims = { 1, (int64_t)input_up_samples[1].size(), 1 };auto interp_idx13_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value interp_idx13_tensor = Ort::Value::CreateTensor<int64_t>(interp_idx13_memory, interp_idx13_values.data(), interp_idx13_size, interp_idx13_dims.data(), interp_idx13_dims.size());const size_t interp_idx14_size = 1 * input_up_samples[2].size() * 1;std::vector<int64_t> interp_idx14_values(interp_idx14_size);for (size_t i = 0; i < input_up_samples[2].size(); i++){interp_idx14_values[i] = input_up_samples[2][i][0];}std::vector<int64_t> interp_idx14_dims = { 1, (int64_t)input_up_samples[2].size(), 1 };auto interp_idx14_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value interp_idx14_tensor = Ort::Value::CreateTensor<int64_t>(interp_idx14_memory, interp_idx14_values.data(), interp_idx14_size, interp_idx14_dims.data(), interp_idx14_dims.size());const size_t interp_idx15_size = 1 * input_up_samples[3].size() * 1;std::vector<int64_t> interp_idx15_values(interp_idx15_size);for (size_t i = 0; i < input_up_samples[3].size(); i++){interp_idx15_values[i] = input_up_samples[3][i][0];}std::vector<int64_t> interp_idx15_dims = { 1, (int64_t)input_up_samples[3].size(), 1 };auto interp_idx15_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value interp_idx15_tensor = Ort::Value::CreateTensor<int64_t>(interp_idx15_memory, interp_idx15_values.data(), interp_idx15_size, interp_idx15_dims.data(), interp_idx15_dims.size());const size_t features_size = 1 * 3 * features->size();std::vector<float> features_values(features_size);for (size_t i = 0; i < features->size(); i++){features_values[features->size() * 0 + i] = features->points[i].x;features_values[features->size() * 1 + i] = features->points[i].y;features_values[features->size() * 2 + i] = features->points[i].z;}std::vector<int64_t> features_dims = { 1, 3, (int64_t)features->size() };auto features_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value features_tensor = Ort::Value::CreateTensor<float>(features_memory, features_values.data(), features_size, features_dims.data(), features_dims.size());const size_t labels_size = 1 * selected_labels.size();std::vector<int64_t> labels_values(labels_size);for (size_t i = 0; i < selected_labels.size(); i++){labels_values[i] = selected_labels[i];}std::vector<int64_t> labels_dims = { 1, (int64_t)selected_labels.size() };auto labels_memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);Ort::Value labels_tensor = Ort::Value::CreateTensor<int64_t>(labels_memory, labels_values.data(), labels_size, labels_dims.data(), labels_dims.size());std::vector<Ort::Value> inputs;inputs.push_back(std::move(xyz1_tensor));inputs.push_back(std::move(xyz2_tensor));inputs.push_back(std::move(xyz3_tensor));inputs.push_back(std::move(xyz_tensor));inputs.push_back(std::move(neigh_idx1_tensor));inputs.push_back(std::move(neigh_idx2_tensor));inputs.push_back(std::move(neigh_idx3_tensor));inputs.push_back(std::move(neigh_idx_tensor));inputs.push_back(std::move(sub_idx8_tensor));inputs.push_back(std::move(sub_idx9_tensor));inputs.push_back(std::move(sub_idx10_tensor));inputs.push_back(std::move(sub_idx11_tensor));inputs.push_back(std::move(interp_idx12_tensor));inputs.push_back(std::move(interp_idx13_tensor));inputs.push_back(std::move(interp_idx14_tensor));inputs.push_back(std::move(interp_idx15_tensor));inputs.push_back(std::move(features_tensor));inputs.push_back(std::move(labels_tensor));std::vector<Ort::Value> outputs = session.Run(Ort::RunOptions{ nullptr }, input_node_names.data(), inputs.data(), input_node_names.size(), output_node_names.data(), output_node_names.size());const float* output = outputs[18].GetTensorData<float>();std::vector<int64_t> output_dims = outputs[18].GetTensorTypeAndShapeInfo().GetShape(); //1*19*45056size_t count = outputs[18].GetTensorTypeAndShapeInfo().GetElementCount();std::vector<float> pred(output, output + count);std::vector<std::vector<float>> probs(output_dims[2], std::vector<float>(output_dims[1])); //45056*19for (size_t i = 0; i < output_dims[2]; i++){for (size_t j = 0; j < output_dims[1]; j++){probs[i][j] = pred[j * output_dims[2] + i];}}std::vector<int> inds = selected_idx;int c_i = cloud_ind;for (size_t i = 0; i < inds.size(); i++){for (size_t j = 0; j < num_classes; j++){test_probs[inds[i]][j] = test_smooth * test_probs[inds[i]][j] + (1 - test_smooth) * probs[i][j];}}}std::vector<int> pred(test_probs.size());std::fstream output("output.txt", 'w');for (size_t i = 0; i < test_probs.size(); i++){pred[i] = max_element(test_probs[i].begin(), test_probs[i].end()) - test_probs[i].begin() + 1;output << points->points[i].x << " " << points->points[i].y << " " << points->points[i].z << " " << pred[i] << std::endl;}return 0;
}

预测结果:在这里插入图片描述

完整的工程可见:https://github.com/taifyang/RandLA-Net-onnxruntime

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/621938.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Pandas实战100例 | 案例 13: 数据分类 - 使用 `cut` 对数值进行分箱

案例 13: 数据分类 - 使用 cut 对数值进行分箱 知识点讲解 在数据分析中&#xff0c;将连续的数值数据分类成不同的区间&#xff08;或“分箱”&#xff09;是一种常见的做法。Pandas 提供了 cut 函数&#xff0c;它可以根据你指定的分箱边界将数值数据分配到不同的类别中。 …

力扣(leetcode)第709题转成小写字母(Python)

709.转成小写字母 题目链接&#xff1a;709.转成小写字母 给你一个字符串 s &#xff0c;将该字符串中的大写字母转换成相同的小写字母&#xff0c;返回新的字符串。 示例 1&#xff1a; 输入&#xff1a;s “Hello” 输出&#xff1a;“hello” 示例 2&#xff1a; 输入&am…

浏览器进程模型和JS的事件循环

一、浏览器的进程模型 1、什么是进程&#xff1f; 程序运行所需要的专属内存空间 2、什么是线程&#xff1f; ​​​​​运行​代码的称为线程&#xff08;同一个进程中的线程共享进程的资源&#xff09; ⼀个进程⾄少有⼀个线程&#xff0c;所以在进程开启后会⾃动创建⼀个线…

ubuntu在使用su - root时提示认证失败

原因&#xff1a; 在ubuntu中&#xff0c;默认情况下&#xff0c;是没有开启root账户的&#xff0c;因此在输入密码的时候会显示认证失败。 解决方法&#xff1a; 输入sudo passwd root设置密码来激活root权限 参考链接&#xff1a;ubuntu出现su:Authentication failure解决方…

《剑指 Offer》专项突破版 - 面试题 7 : 数组中和为 0 的 3 个数字(C++ 实现)

题目链接&#xff1a;15. 三数之和 - 力扣&#xff08;LeetCode&#xff09; 题目&#xff1a; 输入一个数组&#xff0c;如何找出数组中所有和为 0 的 3 个数字的三元组&#xff1f;需要注意的是&#xff0c;返回值中不得包含重复的三元组。例如&#xff0c;在数组 [-1, 0, …

【数值分析】数值微分

1. 基于Taylor公式的数值微分公式 f ′ ( x ) ≈ f ( x h ) − f ( x ) h , 截断误差 − f ′ ′ ( ξ ) 2 h f(x)\approx \frac{f(xh)-f(x)}{h}\,\,,\,\, 截断误差 \,\,\, - \frac{f(\xi)}{2}h f′(x)≈hf(xh)−f(x)​,截断误差−2f′′(ξ)​h f ′ ( x ) ≈ f ( x ) − f …

【JUC进阶】14. TransmittableThreadLocal

目录 1、前言 2、TransmittableThreadLocal 2.1、使用场景 2.2、基本使用 3、实现原理 4、小结 1、前言 书接上回《【JUC进阶】13. InheritableThreadLocal》&#xff0c;提到了InheritableThreadLocal虽然能进行父子线程的值传递&#xff0c;但是如果在线程池中&#x…

spring-mvc(1):Hello World

虽然目前大多数都是使用springboot来开发java程序&#xff0c;或者使用其来为其他端提供接口&#xff0c;而为其他端提供接口&#xff0c;这些功能都是依靠springmvc实现的&#xff0c;所以有必要学习一下spring-mvc&#xff0c;这样才能更好的学习springboot。 一&#xff0c…

c语言题目之九九乘法表的打印

文章目录 题目一、题目分析二&#xff0c;代码编写三&#xff0c;拓展 题目 用c语言打印九九乘法表 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、题目分析 在上面图我们假设一个乘法为一个单位&#xff0c;在这里我们可以看到第一行有一行一列&…

Python 3 字符串的基本使用

介绍 字符串是一个或多个字符&#xff08;字母、数字、符号&#xff09;的序列&#xff0c;可以是常量&#xff0c;也可以是变量。字符串由 Unicode 组成&#xff0c;是不可变的序列&#xff0c;这意味着它们是不变的。 由于文本是我们在日常生活中使用的一种常见数据形式&am…

掌握WPF控件:熟练常用属性(二)

WPF布局常用控件&#xff08;二&#xff09; Calendar 用于日期选择的控件。它提供了一个可视化的界面&#xff0c;可以通过它来选择特定的日期。 常用属性描述DisplayMode用来设置Calendar的显示模式&#xff0c;有三种可选值&#xff1a;默认Month&#xff08;月&#xff…

自编C++题目——输入程序

预估难度 简单 题目描述 小明编了一个输入程序&#xff0c;当用户的输入之中有<时&#xff0c;光标移动到最右边&#xff1b;当输入有>时&#xff0c;光标移动到最左边&#xff0c;当输入有^时&#xff0c;光标移动到前一个字符&#xff0c;当输入为#时&#xff0c;清…

华纳云:在Conda中环境迁移有哪些步骤

在Conda中&#xff0c;环境的迁移通常涉及两个方面&#xff1a;导出环境配置和导出环境中的包。这可以通过以下步骤来完成&#xff1a; 导出环境配置&#xff1a; 在源环境中运行以下命令&#xff0c;导出环境配置到一个 environment.yml 文件中&#xff1a; conda env expo…

SLAM第十四讲

基础知识 四元数 先将三维空间的点p(x,y,z) 变成四元数的表示q(0,x,y,z) 其中0为四元数的实部&#xff0c;x,y,z为四元数的虚部。 实部为0的四元数也叫纯虚四元数。 通过 左乘四元数&#xff…

YOLOv8 Ultralytics:使用Ultralytics框架进行SAM图像分割

YOLOv8 Ultralytics&#xff1a;使用Ultralytics框架进行SAM图像分割 前言相关介绍前提条件实验环境安装环境项目地址LinuxWindows 使用Ultralytics框架进行SAM图像分割参考文献 前言 由于本人水平有限&#xff0c;难免出现错漏&#xff0c;敬请批评改正。更多精彩内容&#xf…

Rust的第一个程序

从C系出发感觉长相怪异程度只比Erlang柔和那么一点点。 教材里的猜数字字符界面游戏&#xff1a; use rand::Rng; use std::cmp::Ordering; use std::io;fn main() {println!("Guess the number!");let secret_number rand::thread_rng().gen_range(1..100);//pri…

TypeScript进阶(四)声明文件

✨ 专栏介绍 TypeScript是一种由微软开发的开源编程语言&#xff0c;它是JavaScript的超集&#xff0c;意味着任何有效的JavaScript代码都是有效的TypeScript代码。TypeScript通过添加静态类型和其他特性来增强JavaScript&#xff0c;使其更适合大型项目和团队开发。 在TypeS…

P1643 完美数 题解

完美数 首先&#xff0c;介绍一下这篇题解的特邀嘉宾&#xff1a;ChatGPT4.0 传送门 题目描述 考古队员小星在一次考察中意外跌入深渊&#xff0c;穿越到了一个神秘的荒漠。这里有许多超越他认识的事物存在&#xff0c;例如许多漂浮在空中的建筑&#xff0c;例如各种奇怪的…

长亭科技-雷池WAF的安装与使用

目录 1、安装雷池 2、登录雷池 3、简单配置 4、防护测试 5、其他补充 1、安装雷池 在Linux系统上执行如下命令 &#xff08;需要docker环境&#xff0c;提前把docker、docker-compose 装好&#xff09; bash -c "$(curl -fsSLk https://waf-ce.chaitin.cn/release…

PHP常用符号和函数

// 单行注解 /* */ 多行注解 引号的使用 ’ ’ 单引号,没有任何意义,不经任何处理直接拿过来; ” “双引号,php动态处理然后输出,一般用于变量. 变量形态: 一种是True 即 真的; 另一种是False 即假的 常见变量形态: string 字串(数字\汉字\等等) integer 整数(1、2、…