Skip to content

Commit

Permalink
Fixing hello-world and removing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
datumbox committed Sep 6, 2021
1 parent 4279288 commit f486437
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 155 deletions.
16 changes: 14 additions & 2 deletions examples/cpp/hello_world/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
#include <iostream>
#include <torch/script.h>
#include <torch/torch.h>
#include <torchvision/vision.h>
#include <torchvision/models/resnet.h>

int main()
{
auto model = vision::models::ResNet18();
torch::jit::script::Module model;
try {
std::cout << "Loading model\n";
// Deserialize the ScriptModule from a file using torch::jit::load().
model = torch::jit::load("resnet18.pt");
std::cout << "Model loaded\n";
} catch (const torch::Error& e) {
std::cout << "error loading the model\n";
return -1;
} catch (const std::exception& e) {
std::cout << "Other error: " << e.what() << "\n";
return -1;
}
model->eval();

// Create a random input tensor and run it through the model.
Expand Down
13 changes: 13 additions & 0 deletions examples/cpp/hello_world/trace_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os.path as osp

import torch
import torchvision

HERE = osp.dirname(osp.abspath(__file__))
ASSETS = osp.dirname(osp.dirname(HERE))

model = torchvision.models.resnet18(pretrained=False)
model.eval()

traced_model = torch.jit.script(model)
traced_model.save("resnet18.pt")
6 changes: 5 additions & 1 deletion packaging/build_cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@ fi
# Compile and run the CPP example
popd
cd examples/cpp/hello_world

mkdir build

# Trace model
python trace_model.py
cp resnet18.pt build

cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch

Expand Down
152 changes: 0 additions & 152 deletions test/test_cpp_models.py

This file was deleted.

0 comments on commit f486437

Please sign in to comment.