Runtime 的用法

Runtime 的用法

1.该类主要代表了应用程序的运行环境。一个RunTime就代表一个运行环境。

2.RunTime类常用的方法:

(1) getRuntime():该方法用于返回当前应用程序的运行环境对象。

(2) exec(String command):该方法用于根据指定的路径执行对应的可执行文件。

代码的实现方式

import java.io.*;
public class Example10_3 {
public static void main(String args[]) {
try{
Runtime ce=Runtime.getRuntime();//配置运行环境;
ce.exec("C:\Program Files\Internet Explorer\iexplore.exe "); 调出该文件运行程序
// File file=new File(“c:/windows”,“Notepad.exe”);
// ce.exec(file.getAbsolutePath());
// ce.exec(file.getAbsolutePath());
// file=new File(“C:\Program Files\Internet Explorer”,"IEXPLORE www.baidu.com ");
// ce.exec(file.getAbsolutePath());
}
catch(Exception e) {
System.out.println(e);
}
}
}

](https://img-blog.csdnimg.cn/20190307125741114.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4NTU2Ng==,size_16,color_FFFFFF,t_70)

### 关于C++中使用ONNX Runtime的教程 #### C++ ONNX Runtime初始化与模型加载 为了在C++环境中利用ONNX Runtime执行推理操作,开发者需先引入`onnxruntime_cxx_api.h`头文件来访问必要的API函数。通过这些API可以实现会话创建、模型加载以及输入输出张量的操作等功能[^1]。 ```cpp #include "onnxruntime_cxx_api.h" ``` 接着定义一个简单的类用于封装ONNX Runtime的核心逻辑: ```cpp class OrtSessionWrapper { public: Ort::Env env; Ort::Session* session; OrtSessionWrapper(const char* model_path, const Ort::SessionOptions& options) : env(ORT_LOGGING_LEVEL_WARNING, "test"), session(nullptr){ session = new Ort::Session(env, model_path, options); } ~OrtSessionWrapper(){ delete session; } }; ``` 上述代码展示了如何基于给定路径加载指定的ONNX模型并设置环境参数以控制日志级别等行为[^2]。 #### 输入数据准备及推理过程 当完成模型加载之后,则需要准备好待处理的数据作为网络输入,并调用相应的接口来进行前向传播计算得到预测结果。这里给出一段示范性的代码片段说明这一流程: ```cpp // 创建session option对象,默认配置即可满足大多数需求 Ort::SessionOptions so; // 构造器传入model path 和 SessionOption实例化OrtSessionWrapper 对象 std::unique_ptr<OrtSessionWrapper> ort_session(new OrtSessionWrapper("path/to/model.onnx", so)); // 准备好要送入神经网络中的测试样本(此处仅为示意) float input_data[] = { /* ... */ }; // 将input data转换成适合OnnxRuntime使用的Tensor格式 auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault); Ort::Value input_tensor = Ort::Value::CreateTensor<float>(memory_info, input_data, sizeof(input_data)/sizeof(float), {/* dims info here */}); // 获取模型所需全部输入节点名称列表 char** input_names = {"data"}; size_t num_input_nodes = 1; // 执行一次inference call auto output_tensors = ort_session->session->Run( Ort::RunOptions{nullptr}, input_names, &input_tensor, num_input_nodes, nullptr, 0); // 处理解码后的output tensor... for (int i=0;i<output_tensors.size();++i){ float *p_output = output_tensors[i].GetTensorMutableData<float>(); } ``` 此部分实现了从构建运行选项到实际调用`Run()`方法进行推理的过程描述,同时也包含了对于输出结果初步解析的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值