class SR_net { public: SR_net(string path, vector<int> input_size, bool fp32, bool cuda = true); private: vector<int64_t> Gdims; int Gfp32; Env env = Env(ORT_LOGGING_LEVEL_ERROR, "RRDB"); SessionOptions session_options = SessionOptions(); Session* Gsession = nullptr; vector<const char*> Ginput_names; vector<const char*> Goutput_names; vector<int> Ginput_size = {}; }; SR_net::SR_net(string path, vector<int> input_size, bool fp32, bool cuda) { this->Ginput_size = input_size; this->Gfp32 = fp32; clock_t startTime_, endTime_; startTime_ = clock(); session_options.SetIntraOpNumThreads(6); if (cuda) { OrtCUDAProviderOptions cuda_option; cuda_option.device_id = 0; cuda_option.arena_extend_strategy = 0; cuda_option.cudnn_conv_algo_search = OrtCudnnConvAlgoSearchExhaustive; cuda_option.gpu_mem_limit = SIZE_MAX; cuda_option.do_copy_in_default_stream = 1; session_options.AppendExecutionProvider_CUDA(cuda_option); } wstring widestr = wstring(path.begin(), path.end()); this->Gsession = new Session(env, widestr.c_str(), this->session_options); this->session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL); AllocatorWithDefaultOptions allocator; this->Ginput_names = { "input" }; this->Goutput_names = { "output" }; endTime_ = clock(); cout << " The model loading time is:" << (double)(endTime_ - startTime_) / CLOCKS_PER_SEC << "s" << endl; } int main() { vector<int> input_shape = {}; SR_net net("E:/prj/SR_C/onnx_file/rrdb_full.onnx", input_shape, true, true); },在这段代码中,我如何把SR_net net("E:/prj/SR_C/onnx_file/rrdb_full.onnx", input_shape, true, true);这一行写到主函数的外面?
时间: 2024-02-27 18:56:36 浏览: 378
MFC.rar_MFC bool_True
你可以将SR_net的构造函数定义为一个静态函数,然后在主函数外部进行调用。具体代码如下:
```c++
class SR_net {
public:
SR_net(string path, vector<int> input_size, bool fp32, bool cuda = true);
static SR_net* createSRNet();
private:
vector<int64_t> Gdims;
int Gfp32;
Env env = Env(ORT_LOGGING_LEVEL_ERROR, "RRDB");
SessionOptions session_options = SessionOptions();
Session* Gsession = nullptr;
vector<const char*> Ginput_names;
vector<const char*> Goutput_names;
vector<int> Ginput_size = {};
};
SR_net::SR_net(string path, vector<int> input_size, bool fp32, bool cuda) {
// 构造函数的代码不变
}
SR_net* SR_net::createSRNet() {
vector<int> input_shape = {};
SR_net* net = new SR_net("E:/prj/SR_C/onnx_file/rrdb_full.onnx", input_shape, true, true);
return net;
}
int main() {
SR_net* net = SR_net::createSRNet();
// 其他代码
delete net;
return 0;
}
```
在主函数中,我们通过SR_net::createSRNet()函数创建SR_net对象,这个函数会返回一个指向SR_net对象的指针。当我们使用完SR_net对象后,需要调用delete操作符释放内存。
阅读全文