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); vector<String> files; glob("E:/prj/超分样本/1", files, true); size_t num = files.size(); bool Moos = true; cout << "共读取了" << num << "张图片" << endl; cout << "--------------------------------" << endl; for (int i = 0; i < num; i++) { Mat srcimg = imread(files[i]); Mat SR_image = net.Detect(srcimg, Moos); imshow("input", srcimg); imshow("result", SR_image); imwrite("./output/" + to_string(i + 1) + ".png", SR_image); waitKey(0); } },在这段代码中,我如何把SR_net net("E:/prj/SR_C/onnx_file/rrdb_full.onnx", input_shape, true, true);这一行写到主函数的外面?
时间: 2024-01-31 16:04:11 浏览: 160
可以在 SR_net 类的定义中增加一个默认构造函数,并在这个构造函数中调用 SR_net(string path, vector<int> input_size, bool fp32, bool cuda) 构造函数,如下所示:
```c++
class SR_net {
public:
SR_net() : SR_net("E:/prj/SR_C/onnx_file/rrdb_full.onnx", {}, true, true) {}
SR_net(string path, vector<int> input_size, bool fp32, bool cuda = true);
// 其他成员函数和成员变量
};
int main() {
SR_net net;
// 其他代码
return 0;
}
```
这样,在主函数中就可以直接使用 SR_net net; 创建对象了。默认构造函数中的参数可以根据实际情况进行修改。
相关问题
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);这一行写到主函数的外面?
你可以将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操作符释放内存。
阅读全文