- This repository contains various examples to perform inference using PyTorch C++ API.
- Run
git clone https://github.com/Wizaron/pytorch-cpp-inference
in order to clone this repository.
- Dockerfile can be found at
docker
directory. In order to build docker image, you should go todocker
directory and rundocker build -t <docker-image-name> .
. - After creation of the docker image, you should create a docker container via
docker run -v <directory-that-this-repository-resides>:<target-directory-in-docker-container> -it <docker-image-name>
. - Inside docker container, go to the directory that this repository resides.
- Download
libtorch
from PyTorch Website or usingwget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip
. - Unzip it via
unzip libtorch-shared-with-deps-latest.zip
. This will createlibtorch
directory that contains torch shared libraries and headers.
- In order to export
torch.jit.ScriptModule
of ResNet18 to perform C++ inference, go tomodels/resnet
directory and runpython resnet.py
. It will download pretrained ResNet18 model on ImageNet and createmodels/resnet_model.pth
which we will use in C++ inference.
-
libtorch
directory stores C++ torch headers and shared libraries to link the model against PyTorch. -
utils
directory stores various utility function to perform inference in C++. -
inference-cpp
directory stores codes to perform inference. -
In order to build executable:
- Go to
inference-cpp/cnn-classification
directory. - Run
./build.sh
in order to build executable, named aspredict
. - Run the executable via
./predict <path-to-image> <path-to-exported-script-module> <path-to-labels-file>
. - Example:
./predict image.jpeg ../../models/resnet/resnet_model.pth labels.txt
- Go to