Sanaxen / cpp_torch

It is tiny-dnn based on libtorch. Only headers without dependencies other than libtorch, deep learning framework
MIT License
34 stars 11 forks source link

How to test a single OpenCV image? #12

Open zouwen198317 opened 6 months ago

zouwen198317 commented 6 months ago

How to test a single OpenCV image?

zouwen198317 commented 6 months ago

The expected result cannot be obtained. void CTinyDnnGpuDlg::OnBnClickedButton5() { // TODO: 在此添加控件通知处理程序代码

CString edit_text_w;
(static_cast<CEdit*>(GetDlgItem(IDC_EDIT3)))->GetWindowText(edit_text_w);
CString edit_text_h;
(static_cast<CEdit*>(GetDlgItem(IDC_EDIT4)))->GetWindowText(edit_text_h);
int w = _ttoi(edit_text_w);
int h = _ttoi(edit_text_h);

CString edit_text_cls;
(static_cast<CEdit*>(GetDlgItem(IDC_EDIT6)))->GetWindowText(edit_text_cls);
int cls = _ttoi(edit_text_cls);

// 获取IDC_EDIT5中的文本(图像文件路径)
CString imagePathCString;
GetDlgItemText(IDC_EDIT5, imagePathCString);

// 将CString转换为std::string
std::string imagePathString = CT2A(imagePathCString);

// 使用imread函数读取图像
cv::Mat image = cv::imread(imagePathString, 1);

// 检查图像是否成功加载
if (image.empty())
{
    MessageBox(_T("Failed to load image."));
    return;
}

torch::manual_seed(1);

torch::DeviceType device_type;

ifdef USE_CUDA

if (torch::cuda::is_available()) {
    std::cout << "CUDA available! Training on GPU." << std::endl;
    device_type = torch::kCUDA;
}
else

endif

{
    std::cout << "Training on CPU." << std::endl;
    device_type = torch::kCPU;
}
torch::Device device(device_type);

cpp_torch::Net model;

model.get()->setInput(3, w, h);

model.get()->add_conv2d(3, 32, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_conv2d(32, 32, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_maxpool2d(2);
model.get()->add_conv_drop(0.2);

model.get()->add_conv2d(32, 64, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_conv2d(64, 64, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_maxpool2d(2);
model.get()->add_conv_drop(0.3);

model.get()->add_conv2d(64, 128, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_conv2d(128, 128, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_maxpool2d(2);
model.get()->add_conv_drop(0.4);

model.get()->add_fc(cls);

model.get()->add_LogSoftmax(1);

cpp_torch::network_torch<cpp_torch::Net> nn(model, device);

nn.input_dim(3, w, h);
nn.output_dim(1, 1, cls);
nn.classification = true;
nn.batch_shuffle = true;
nn.load(std::string("model1.pt"));

tiny_dnn::vec_t vimg = pre_img_fn(image, w, h);
tiny_dnn::vec_t re = nn.predict(vimg);
tiny_dnn::label_t re_l = nn.predict_label(vimg);

for (const auto& key : re)
{
    std::cout << key << std::endl;
}

std::cout << "label_t:" << re_l << std::endl;

//tiny_dnn::result res2 = nn2.test(test_images, test_labels);
//cpp_torch::print_ConfusionMatrix(res2);

//Net model2;
//cpp_torch::network_torch<Net> nn2(model2, device);
//nn2 = nn;

//nn2.load(std::string("model1.pt"));

}

zouwen198317 commented 6 months ago

done! void CTinyDnnGpuDlg::OnBnClickedButton5() { // TODO: 在此添加控件通知处理程序代码

CString edit_text_w;
(static_cast<CEdit*>(GetDlgItem(IDC_EDIT3)))->GetWindowText(edit_text_w);
CString edit_text_h;
(static_cast<CEdit*>(GetDlgItem(IDC_EDIT4)))->GetWindowText(edit_text_h);
int w = _ttoi(edit_text_w);
int h = _ttoi(edit_text_h);

CString edit_text_cls;
(static_cast<CEdit*>(GetDlgItem(IDC_EDIT6)))->GetWindowText(edit_text_cls);
int cls = _ttoi(edit_text_cls);

// 获取IDC_EDIT5中的文本(图像文件路径)
CString imagePathCString;
GetDlgItemText(IDC_EDIT5, imagePathCString);

// 将CString转换为std::string
std::string imagePathString = CT2A(imagePathCString);

// 使用imread函数读取图像
cv::Mat image = cv::imread(imagePathString, 1);

// 检查图像是否成功加载
if (image.empty())
{
    MessageBox(_T("Failed to load image."));
    return;
}

torch::DeviceType device_type;

ifdef USE_CUDA

if (torch::cuda::is_available()) {
    std::cout << "CUDA available! Training on GPU." << std::endl;
    device_type = torch::kCUDA;
}
else

endif

{
    std::cout << "Training on CPU." << std::endl;
    device_type = torch::kCPU;
}
torch::Device device(device_type);

//cpp_torch::Net model;
//cpp_torch::network_torch<cpp_torch::Net> nn(model, device);

cpp_torch::Net model;

model.get()->setInput(3, w, h);

if USING_MY_NET

else

model.get()->add_conv2d(3, 32, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_conv2d(32, 32, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_maxpool2d(2);
model.get()->add_conv_drop(0.2);

model.get()->add_conv2d(32, 64, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_conv2d(64, 64, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_maxpool2d(2);
model.get()->add_conv_drop(0.3);

model.get()->add_conv2d(64, 128, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_conv2d(128, 128, 3, 3, 3);
model.get()->add_ReLU();
model.get()->add_bn2d();
model.get()->add_maxpool2d(2);
model.get()->add_conv_drop(0.4);

model.get()->add_fc(cls);

endif

model.get()->add_LogSoftmax(1);

cpp_torch::network_torch<cpp_torch::Net> nn(model, device);

nn.input_dim(3, w, h);
std::cout << w << " " << h << std::endl;
nn.output_dim(1, 1, cls);
nn.load(std::string("model1.pt"));
// 设置图像文件路径
// 读取图像并进行预处理
tiny_dnn::vec_t preprocessed_image = pre_img_fn(image, w, h);

// 使用模型进行推理
tiny_dnn::vec_t output = nn.predict(preprocessed_image);
std::cout << "3" << std::endl;
int in = nn.vec_max_index(output);

// 输出每个类别的分值
/*std::cout << "Class probabilities:" << std::endl;
for (int i = 0; i < output.size(); ++i)
{
    std::cout << "Class " << i << ": " << output[i] << std::endl;
}*/

std::cout << "ok" << in << std::endl;

return;

}