Using CUDA 10 on linux
Segv occurs at line ‘int idx = top_idxs_a[i];’ in the code below
#include <torch/script.h>
#include "utils.hpp"
#include <opencv2/opencv.hpp>

int main(int argc, char** argv)
{
    if (argc != 4) {
    std::cerr << "usage: test_PyTorch_Cpp_Prediction_GPU <path-to-image> <path-to-exported-script-module> <path-to-labels-file> \n";
    return -1;
  } 

  std::string image_path = argv[1];
  std::string model_path = argv[2];
  std::string labels_path = argv[3];

  int image_height = 224;
  int image_width = 224;
  int n_channels = 3;

  // Read labels
  std::vector<std::string> labels;
  std::string label;
  std::ifstream labelsfile (labels_path);
  if (labelsfile.is_open())
  {
    while (getline(labelsfile, label))
    {
      labels.push_back(label);
    }
    labelsfile.close();
  }

  std::vector<double> mean = {0.485, 0.456, 0.406};
  std::vector<double> std = {0.229, 0.224, 0.225};

  cv::Mat image = cv::imread(image_path, 1);

  // Deserialize the ScriptModule from a file using torch::jit::load().
  std::shared_ptr<torch::jit::script::Module> module = torch::jit::load(model_path);
  assert(module != nullptr);

  module->to(torch::kCUDA);

  // Preprocess image
  image = preprocess(image, image_height, image_width, mean, std);

  std::vector<int64_t> dims = {1, image_height, image_width, n_channels};
  std::vector<int64_t> permute_dims = {0, 3, 1, 2};
  torch::TensorOptions options(torch::kFloat32);
  torch::Tensor image_as_tensor = torch::from_blob(image.data, torch::IntList(dims), options).clone();
  image_as_tensor = image_as_tensor.permute(torch::IntList(permute_dims));
  image_as_tensor = image_as_tensor.toType(torch::kFloat32);
  
  // image_as_tensor = image_as_tensor.to(torch::kCUDA);
  std::vector<torch::jit::IValue> inputs;
  inputs.push_back((image_as_tensor).cuda());
  torch::Tensor output = module->forward(inputs).toTensor();
  // torch::Tensor output = module->forward({image_as_tensor}).toTensor();
  output = output.to(torch::kCPU, false);
  std::cout << output.sizes() << "\n";

  // out tensor sort, print the first 2 category
  std::tuple<torch::Tensor,torch::Tensor> result = output.sort(-1, true);
  torch::Tensor top_scores = std::get<0>(result)[0];
  torch::Tensor top_idxs = std::get<1>(result)[0].toType(torch::kInt32);

  auto top_scores_a = top_scores.accessor<float,1>();
  auto top_idxs_a = top_idxs.accessor<int,1>();
  // std::cout << top_scores_a << "\n";
  // std::cout << top_idxs_a << "\n";
  std::cout << labels.size() << "\n";
  for (int i = 0; i < 5; ++i) {
    int idx = top_idxs_a[i];
    std::cout <<idx << labels[idx] <<"\n";
    std::cout << "top " << i+1 << ", label: "<< labels[idx] << " ";
    std::cout << ", score: " << top_scores_a[i] << std::endl;
    }
}