{"id":13438767,"url":"https://github.com/BIGBALLON/PyTorch-CPP","last_synced_at":"2025-03-20T06:31:20.480Z","repository":{"id":46539313,"uuid":"166348597","full_name":"BIGBALLON/PyTorch-CPP","owner":"BIGBALLON","description":"PyTorch C++ inference with LibTorch","archived":false,"fork":false,"pushed_at":"2020-11-27T12:33:43.000Z","size":543,"stargazers_count":330,"open_issues_count":0,"forks_count":69,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-10-28T00:23:45.158Z","etag":null,"topics":["cpp","demo","imagenet","inference","libtorch","opencv","pytorch","pytorch-cpp"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BIGBALLON.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-18T05:22:25.000Z","updated_at":"2024-10-23T11:44:59.000Z","dependencies_parsed_at":"2022-08-12T12:51:07.156Z","dependency_job_id":null,"html_url":"https://github.com/BIGBALLON/PyTorch-CPP","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BIGBALLON%2FPyTorch-CPP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BIGBALLON%2FPyTorch-CPP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BIGBALLON%2FPyTorch-CPP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BIGBALLON%2FPyTorch-CPP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BIGBALLON","download_url":"https://codeload.github.com/BIGBALLON/PyTorch-CPP/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244565382,"owners_count":20473266,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","demo","imagenet","inference","libtorch","opencv","pytorch","pytorch-cpp"],"created_at":"2024-07-31T03:01:08.264Z","updated_at":"2025-03-20T06:31:15.472Z","avatar_url":"https://github.com/BIGBALLON.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"\n\n\u003cimg src=\"./pic/pytorch-cpp.jpg\" width=25% align=\"right\" /\u003e \n\nThis demo will demonstrate how to use LibTorch to build your C++ application.\n\n**[UPDATE 2019/01/18]** : Init the repo, test with PyTorch1.0.  \n**[UPDATE 2020/02/22]** : Thanks for [Ageliss](https://github.com/Ageliss) and his [PR](https://github.com/BIGBALLON/PyTorch-CPP/pull/4), which update this demo to fit LibTorch1.4.0 and OpenCV4.0.  \n**[UPDATE 2020/04/15]** : Retest this tutorial with **OpenCV4.3**/**PyTorch1.4**/**LibTorch1.4**, update readme for beginner.  \n**[UPDATE 2020/04/25]** : Update ``CMakeLists.txt`` to fit **C++14,** retest with **CUDA10.0**/**PyTorch1.5**/**LibTorch1.5**.  \n**[UPDATE 2020/11/08]** : Improve readme, retest with **PyTorch1.7**/ **CUDA10.2+cuDNNv7.6.5** and **CUDA11.0+cuDNNv8.0.4**\n\n## Contents\n\n- [Contents](#contents)\n- [Preparation](#preparation)\n  - [Step 0: Dependencies](#step-0-dependencies)\n  - [Step 1: JIT Model](#step-1-jit-model)\n  - [Step 2: Cpp Program](#step-2-cpp-program)\n  - [Step 3: CMakeLists](#step-3-cmakelists)\n- [Build](#build)\n- [Usage](#usage)\n\n\n## Preparation\n\n\n### Step 0: Dependencies\n\n**Make sure** LibTorch and OpenCV have been installed correctly.\n\n- **Install OpenCV**: for [Linux](https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html), for [Mac OS](https://docs.opencv.org/master/d0/db2/tutorial_macos_install.html)\n- **Get LibTorch**: download LibTorch package from the official [website](https://pytorch.org/get-started/locally/), then unpack it, for example:\n\n```bash\ncd path_to_your_workspace\nwget https://download.pytorch.org/libtorch/cu102/libtorch-cxx11-abi-shared-with-deps-1.7.0.zip\nunzip libtorch-cxx11-abi-shared-with-deps-1.7.0.zip\n```\n\n### Step 1: JIT Model\n\nExport torch script file, we use ``resnet18``/``resnet50`` in this demo. (see [model_trace.py](./model_trace.py))\n\n### Step 2: Cpp Program\n\nWrite C++ application program. (see [prediction.cpp](./prediction.cpp))\n\n**PS**: ``module-\u003eto(at::kCUDA)`` and ``input_tensor.to(at::kCUDA)`` will switch your model \u0026 tensors to GPU mode, comment out them if you just want to use CPU. \n\n\n### Step 3: CMakeLists\n\nWrite a [CMakeLists.txt](./CMakeLists.txt). (check [cppdocs](https://pytorch.org/cppdocs/) for more details)\n\n## Build\n\n- run ``model_trace.py``, you will get a converted model ``resnet50.pt``.\n- compile your cpp program, you need to use ``-DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch``, for example:\n\n```bash\nmkdir build\ncd build\n# change \"/home/bigballon/libtorch\" to your libtorch path\ncmake -DCMAKE_PREFIX_PATH=/home/bigballon/libtorch ..\nmake\n```\n\n\n**PS**: If you get the compile error: ``error: undefined reference to `cv::imread(std::string const\u0026, int)'``, check [issues 14684](https://github.com/pytorch/pytorch/issues/14684) and [issues 14620](https://github.com/pytorch/pytorch/issues/14620) for more details.\n\n## Usage\n\n\n```bash\nclassifier \u003cpath-to-exported-script-module\u003e \u003cpath-to-lable-file\u003e\n# example:\n# ./classifier ../resnet18.pt ../label.txt\n```\n\n![video](./pic/video.gif)\n\n```\n\u003e ./classifier ../resnet18.pt ../label\n== Switch to GPU mode\n== Model [../resnet18.pt] loaded!\n== Label loaded! Let's try it\n== Input image path: [enter Q to exit]\n../pic/dog.jpg\n== image size: [976 x 549] ==\n== simply resize: [224 x 224] ==\n    ============= Top-1 =============\n    Label:  beagle\n    With Probability:  97.0629%\n    ============= Top-2 =============\n    Label:  Walker hound, Walker foxhound\n    With Probability:  1.30952%\n    ============= Top-3 =============\n    Label:  English foxhound\n    With Probability:  0.434456%\n```\n![dog](./pic/dog.jpg)\n\n```\n../pic/shark.jpg\n== image size: [800 x 500] ==\n== simply resize: [224 x 224] ==\n    ============= Top-1 =============\n    Label:  tiger shark, Galeocerdo cuvieri\n    With Probability:  67.672%\n    ============= Top-2 =============\n    Label:  hammerhead, hammerhead shark\n    With Probability:  16.4908%\n    ============= Top-3 =============\n    Label:  great white shark, white shark, man-eater, man-eating shark\n    With Probability:  15.7808%\n== Input image path: [enter Q to exit]\nQ\n```\n![shark](./pic/shark.jpg)\n\n```\n\u003e ./classifier ../resnet50.pt ../label\n== Switch to GPU mode\n== Model [../resnet50.pt] loaded!\n== Label loaded! Let's try it\n== Input image path: [enter Q to exit]\n../pic/dog.jpg\n== image size: [976 x 549] ==\n== simply resize: [224 x 224] ==\n    ============= Top-1 =============\n    Label:  beagle\n    With Probability:  99.1227%\n    ============= Top-2 =============\n    Label:  Walker hound, Walker foxhound\n    With Probability:  0.469356%\n    ============= Top-3 =============\n    Label:  English foxhound\n    With Probability:  0.110916%\n== Input image path: [enter Q to exit]\n../pic/shark.jpg\n== image size: [800 x 500] ==\n== simply resize: [224 x 224] ==\n    ============= Top-1 =============\n    Label:  tiger shark, Galeocerdo cuvieri\n    With Probability:  92.2599%\n    ============= Top-2 =============\n    Label:  great white shark, white shark, man-eater, man-eating shark\n    With Probability:  5.94252%\n    ============= Top-3 =============\n    Label:  hammerhead, hammerhead shark\n    With Probability:  1.77417%\n== Input image path: [enter Q to exit]\nQ\n```\n\nTake it easy!! :love_letter:\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBIGBALLON%2FPyTorch-CPP","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBIGBALLON%2FPyTorch-CPP","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBIGBALLON%2FPyTorch-CPP/lists"}