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 Network


model = 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 cfg


np.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 = probs
    test_smooth = 0.98
    
    import 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_pc
            input_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_points
            flat_inputs = input_points + input_neighbors + input_pools + input_up_samples
            flat_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) + 1
        output = 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*45056
		size_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*19
		for (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

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

使用 GitHub 远程仓库

使用 GitHub 远程仓库 GitHub 是最大的 Git 版本库托管商&#xff0c;是成千上万的开发者和项目能够合作进行的中心。 大部分 Git 版本库都托管在 GitHub&#xff0c;很多开源项目使用 GitHub 实现 Git 托管、问题追踪、代码审查以及其它事情。本篇文章主要带大家上手 GitHub …

html+css+Jquery 实现 文本域 文字数量限制、根据输入字数自适应高度

先看效果&#xff1a;初始的效果&#xff0c;样式多少有点问题&#xff0c;不重要&#xff01;重要的是功能&#xff01; 输入后&#xff1a; 根据文字长度&#xff0c;决定文本域长度 限制文字数量 话不多说&#xff0c;直接上代码&#xff01; <!DOCTYPE html> <h…

科研绘图(四)火山图

火山图是生物信息学中常用的一种图表&#xff0c;用来显示基因表达数据的变化。它通常将每个点表示为一个基因&#xff0c;x轴显示对数比率&#xff08;log ratio&#xff09;&#xff0c;表示基因表达的变化大小&#xff1b;y轴显示-log10(p-value)&#xff0c;表示变化的统计…

手把手Docker部署Gitblit服务器

1拉取镜像 docker pull jacekkow/gitblit:v1.9.1 2.启动 docker run -d --name gitblit --restart always -p 10006:8080 -p 18443:8443 -p 19418:9418 -p 29418:29418 -v /data/gitblit/data:/opt/gitblit-data jacekkow/gitblit:v1.9.1 3.查看 默认账户/密码:admin/adm…

Trie字符串统计

题目传送门&#xff1a;835.Trie字符串统计 维护一个字符串集合&#xff0c;支持两种操作&#xff1a; I x 向集合中插入一个字符串 x&#xff1b;Q x 询问一个字符串在集合中出现了多少次。 共有 N 个操作&#xff0c;所有输入的字符串总长度不超过 105105&#xff0c;字符串仅…

您与此网站之间建立的连接不安全

连接不安全的主要原因之一是使用不安全的通信协议。在互联网传输中&#xff0c;如果使用的协议不加密&#xff0c;那么数据就容易受到窃听和篡改。另一个可能的原因是网站没有正确配置其安全证书&#xff0c;使得用户的连接没有得到适当的加密保护。 解决方法&#xff1a; 采用…

Jeson nano--安装使用摄像头csi/usb

Jeson nano--安装使用摄像头 一、 安装使用摄像头二、vscode调用摄像头总结 一、 安装使用摄像头 列出与视频设备相关的设备文件 ls /dev/video*显示与 /dev/video0 设备关联的摄像头支持的所有格式及其详细信息。可以查看输出以了解所支持的分辨率、帧率和像素格式等信息。请…

centos docker-compose安装教程-2024最新版 亲测可用

目录 长时间不安装,生疏了,再次记录下 1.下载 2.修改名称 3.提权 4.测试验证 长时间不安装,生疏了,再次记录下 1.下载 官网地址 docker-compose官网地址&#xff1a;https://docs.docker.com/compose/compose-file/compose-file-v3/ #进入目录 cd /usr/local/bin#下载 wg…

LeetCode讲解篇之216. 组合总和 III

文章目录 题目描述题解思路题解代码 题目描述 题解思路 使用递归回溯算法&#xff0c;当选择数字num后&#xff0c;在去选择大于num的合法数字&#xff0c;计算过程中的数字和&#xff0c;直到选择了k次&#xff0c;如果数组和等于n则加入结果集 从1开始选择数字&#xff0c;直…

jmeter如何做接口测试?

Jmeter介绍&测试准备&#xff1a; Jmeter介绍&#xff1a;Jmeter是软件行业里面比较常用的接口、性能测试工具&#xff0c;下面介绍下如何用Jmeter做接口测试以及如何用它连接MySQL数据库。 前期准备&#xff1a;测试前&#xff0c;需要安装好Jmeter以及jdk并配置好jdk环…

基于时域有限差分法的FDTD的计算电磁学算法-YEE网格下的更新公式推导

基于时域有限差分法的FDTD的计算电磁学算法&#xff08;含Matlab代码&#xff09;-YEE网格下的更新公式推导 参考书籍&#xff1a;The finite-difference time-domain method for electromagnetics with MATLAB simulations&#xff08;国内翻译版本&#xff1a;MATLAB模拟的电…

LLM之RAG理论(五)| 使用知识图谱增强RAG

知识图谱&#xff08;KG&#xff09;或任何图都包括节点和边&#xff0c;其中每个节点表示一个概念&#xff0c;每个边表示一对概念之间的关系。本文介绍一种将任何文本语料库转换为知识图谱的技术&#xff0c;本文演示的知识图谱可以替换其他专业知识图谱。 一、知识图谱 知识…

Postman接口测试之断言,全网最细教程没有之一!

一、断言 在 postman 中我们是在Tests标签中编写断言&#xff0c;同时右侧封装了常用的断言&#xff0c;当然 Tests 除了可以作为断言&#xff0c;还可以当做后置处理器来编写一些后置处理代码&#xff0c;经常应用于&#xff1a; 【1】获取当前接口的响应&#xff0c;传递给…

JavaScript的一道题型

这道题我觉得考察的知识点蛮多的&#xff0c;就单独拿出来了&#x1f92d; <html> <head><title>Title</title> </head> <body> <input type"button" value"全选" id"qx"> <input type"butto…

关于编程的一些小小记录

这里记录一些关于编程的小技巧吧&#xff0c;算是个记录 1&#xff0c;vs同时有多个cpp文件怎么办&#xff1f; 我们只想运行第一个cpp文件&#xff0c;那么怎么做呢&#xff1f; 其实很简单&#xff0c;单击你不想让之运行的文件&#xff0c;点击最下面的属性 最后设置为这样…

强化学习应用(五):基于Q-learning算法的无人车配送路径规划(通过Python代码)

一、Q-learning算法介绍 Q-learning是一种强化学习算法&#xff0c;用于解决基于环境的决策问题。它通过学习一个Q-table来指导智能体在不同状态下采取最优动作。下面是Q-learning算法的基本步骤&#xff1a; 1. 定义环境&#xff1a;确定问题的状态和动作空间&#xff0c;并…

最小花费-银行转账-图的最短路-超详细解析注释

最小花费-银行转账-图的最短路-超详细解析注释 【题目描述】 在n个人中&#xff0c;某些人的银行账号之间可以互相转账。这些人之间转账的手续费各不相同。给定这些人之间转账时需要从转账金额里扣除百分之几的手续费&#xff0c;请问A最少需要多少钱使得转账后B收到100元。 …

office办公技能|word中的常见通配符使用

一、删除Word中含有指定内容的整行 操作方法&#xff1a; 1、快捷键 CtrlH&#xff0c;打开Word的查找替换窗口&#xff0c;单击【更多】按钮&#xff0c;勾选“使用通配符”。 2、在查找内容处&#xff0c;输入“替换内容*^13”&#xff0c;替换为处什么都不填。 3、单击【…

现阶段Python和Java哪个更吃香?

现阶段Python和Java哪个更吃香&#xff1f; 在开始前我有一些资料&#xff0c;是我根据网友给的问题精心整理了一份「Java的资料从专业入门到高级教程」&#xff0c; 点个关注在评论区回复“888”之后私信回复“888”&#xff0c;全部无偿共享给大家&#xff01;&#xff01;&…