From 53dc36825cfed07d7e4a1bfc0a62e62ca0bf68a3 Mon Sep 17 00:00:00 2001 From: Matt Popovich Date: Wed, 14 Jul 2021 14:29:26 -0600 Subject: [PATCH 1/2] Added more descriptive error logging --- deployment/libtorch/main.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/deployment/libtorch/main.cpp b/deployment/libtorch/main.cpp index 43332650..2e415377 100644 --- a/deployment/libtorch/main.cpp +++ b/deployment/libtorch/main.cpp @@ -21,7 +21,7 @@ std::vector LoadNames(const std::string& path) { // load class names std::vector class_names; std::ifstream infile(path); - if (infile.is_open()) { + if (infile.good()) { std::string line; while (getline (infile, line)) { class_names.emplace_back(line); @@ -29,7 +29,8 @@ std::vector LoadNames(const std::string& path) { infile.close(); } else { - std::cerr << "Error loading the class names!\n"; + std::cerr << ">>> ERROR: Failed to access class name path: " << path + << "\n>>>\tDoes the file exist? Permission to read it?\n"; } return class_names; @@ -123,18 +124,29 @@ int main(int argc, char* argv[]) { // load input image std::string image_path = cmd.get("input_source"); + if (std::ifstream(image_path).fail()) { + std::cerr << ">>> ERROR: Failed to access image file path: " << image_path + << "\n>>>\tDoes the file exist? Permission to read it?\n"; + return -1; + } torch::jit::script::Module module; try { std::cout << ">>> Loading model" << std::endl; // Deserialize the ScriptModule from a file using torch::jit::load(). std::string weights = cmd.get("checkpoint"); + if (std::ifstream(weights).fail()) { + std::cerr << ">>> ERROR: Failed to access checkpoint file path: " << weights + << "\n>>>\tDoes the file exist? Permission to read it?\n"; + return -1; + } + module = torch::jit::load(weights); module.to(device_type); module.eval(); std::cout << ">>> Model loaded" << std::endl; } catch (const torch::Error& e) { - std::cout << ">>> error loading the model" << std::endl; + std::cout << ">>> Error loading the model" << std::endl; return -1; } catch (const std::exception& e) { std::cout << ">>> Other error: " << e.what() << std::endl; From 4504af593c792b1c2171de93707a4593f7f8536a Mon Sep 17 00:00:00 2001 From: Matt Popovich Date: Wed, 14 Jul 2021 16:59:13 -0600 Subject: [PATCH 2/2] Print error if model fails to load --- deployment/libtorch/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deployment/libtorch/main.cpp b/deployment/libtorch/main.cpp index 2e415377..aefbcdf3 100644 --- a/deployment/libtorch/main.cpp +++ b/deployment/libtorch/main.cpp @@ -146,7 +146,7 @@ int main(int argc, char* argv[]) { module.eval(); std::cout << ">>> Model loaded" << std::endl; } catch (const torch::Error& e) { - std::cout << ">>> Error loading the model" << std::endl; + std::cout << ">>> Error loading the model: " << e.what() << std::endl; return -1; } catch (const std::exception& e) { std::cout << ">>> Other error: " << e.what() << std::endl;