libtorch_examples
libtorch_examples copied to clipboard
opencv to torch tensor doesn't look quite right
Hi,
Thanks for providing these examples. I'm wondering if the yolo3.cpp is working because I'm looking at the conversion from the opencv image to torch tensor and it doesn't quite look right. The snipit of code is below (which is lines 56 to 67 of yolo3.cpp). Looks like you use opencv to convert the data to float, but then the torch::from_blob
call is expecting byte data (and the subsequent step converts to float). Maybe just change the torch::from_blob
call to take float and then skip the subsequent conversion?
Thanks,
Marc
origin_image = cv::imread(argv[1]);
cv::cvtColor(origin_image, resized_image, cv::COLOR_RGB2BGR);
cv::resize(resized_image, resized_image, cv::Size(input_image_size, input_image_size));
cv::Mat img_float;
resized_image.convertTo(img_float, CV_32F, 1.0/255);
// auto img_tensor = torch::CPU(torch::kFloat32).tensorFromBlob(img_float.data, {1, input_image_size, input_image_size, 3});
auto img_tensor = torch::from_blob(img_float.data, {1, input_image_size, input_image_size, 3}, at::kByte);
img_tensor = img_tensor.to(torch::kFloat32);
img_tensor = img_tensor.permute({0,3,1,2});