Last active
October 17, 2019 19:00
-
-
Save bfortuner/c99a32dbdeb0f4f560d0080118f15af8 to your computer and use it in GitHub Desktop.
Example libtorch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <torch/script.h> // One-stop header. | |
#include <torch/torch.h> | |
#include <iostream> | |
#include <memory> | |
int main(int argc, const char* argv[]) { | |
if (argc != 2) { | |
std::cerr << "usage: example-app <path-to-exported-script-module>\n"; | |
return -1; | |
} | |
// Create the device we pass around based on whether CUDA is available. | |
torch::Device device(torch::kCPU); | |
if (torch::cuda::is_available()) { | |
std::cout << "CUDA is available! Training on GPU." << std::endl; | |
device = torch::Device(torch::kCUDA); | |
} else { | |
std::cout << "CUDA not available! Training on CPU." << std::endl; | |
} | |
torch::jit::script::Module module; | |
try { | |
// Deserialize the ScriptModule from a file using torch::jit::load(). | |
module = torch::jit::load(argv[1]); | |
} | |
catch (const c10::Error& e) { | |
std::cerr << "error loading the model\n"; | |
return -1; | |
} | |
std::cout << "ok\n"; | |
// Move to GPU | |
// module.to(at::kCUDA); | |
// Create a vector of inputs. | |
std::vector<torch::jit::IValue> inputs; | |
inputs.push_back(torch::ones({1, 3, 224, 224})); //#.to(at::kCUDA)); | |
// Execute the model and turn its output into a tensor. | |
at::Tensor output = module.forward(inputs).toTensor(); | |
std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment