Skip to content

Deep Learning sample programs using PyTorch in C++

License

Notifications You must be signed in to change notification settings

stjordanis/pytorch_cpp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyTorch C++ Samples

These are Deep Learning sample programs of PyTorch written in C++.

Description

PyTorch is famous as a kind of Deep Learning Frameworks.
Among them, Python source code is overflowing on the Web, so we can easily write the source code of Deep Learning in Python.
However, there is very little source code written in C++ of compiler language.
Therefore, I hope this repository will help many programmers by providing PyTorch sample programs written in C++.
In addition, I might adapt programs to the latest version.

Requirement

1. PyTorch C++

Please select the environment to use as follows on PyTorch official.
PyTorch official : https://pytorch.org/


PyTorch Build : Stable (1.6.0)
Your OS : Linux
Package : LibTorch
CUDA : 10.2
Run this Command : Download here (cxx11 ABI)
GPU : https://download.pytorch.org/libtorch/cu102/libtorch-cxx11-abi-shared-with-deps-1.6.0.zip
CPU : https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.6.0%2Bcpu.zip


2. OpenCV

version : 3.0.0 or more
This is used for pre-processing and post-processing.
Please refer to other sites for more detailed installation method.

3. OpenMP

version : 4.5 (It works on other versions.)
This is used to load data in parallel.
It may be installed on standard Linux OS.

4. Boost

version : 1.65.1 (It works on other versions.)
This is used for command line arguments, etc.

$ sudo apt install libboost-dev libboost-all-dev

5. Gnuplot

version : 5.2 (It works on other versions.)
This is used to display loss graph.

$ sudo apt install gnuplot

6. libpng

version : 0.2 (It works on other versions.)
This is used to load and save index-color image in semantic segmentation.

$ sudo apt install libpng++-dev

Preparation

1. Git Clone

$ git clone https://github.com/koba-jon/pytorch_cpp.git
$ cd pytorch_cpp

2. Path Setting

$ vi utils/CMakeLists.txt

Please change the 4th line of "CMakeLists.txt" according to the path of the directory "libtorch".
The following is an example where the directory "libtorch" is located directly under the directory "HOME".

3: # LibTorch
4: set(LIBTORCH_DIR $ENV{HOME}/libtorch)
5: list(APPEND CMAKE_PREFIX_PATH ${LIBTORCH_DIR})

3. Execution

Please move to the directory of each model and refer to "README.md".

Utility

1. Making Original Dataset

Please create a link for the original dataset.
The following is an example of "AE2d" using "celebA" Dataset.

$ cd AE2d/datasets
$ ln -s <dataset_path> ./

You should substitute the path of dataset for "<dataset_path>".
Please make sure you have training or test data directly under "<dataset_path>".

$ vi hold_out.sh

Please edit the file for original dataset.

#!/bin/bash

python3 hold_out.py \
    --input_dir celebA_org \
    --output_dir celebA \
    --train_rate 9 \
    --valid_rate 1

By running this file, you can split it into training and validation data.

$ sudo apt install python3 python3-pip
$ pip3 install natsort
$ sh hold_out.sh
$ cd ..

2. Data Input System

There are transform, dataset and dataloader for data input in this repository.
It corresponds to the following source code in the directory, and we can add new function to the source code below.

  • transforms.cpp
  • transforms.hpp
  • datasets.cpp
  • datasets.hpp
  • dataloader.cpp
  • dataloader.hpp

3. Check Progress

There are a feature to check progress for training in this repository.
We can watch number of epoch, loss, time and speed in training.
util1
It corresponds to the following source code in the directory.

  • progress.cpp
  • progress.hpp

4. Monitoring System

There are monitoring system for training in this repository.
We can watch output image and loss graph.
The feature to watch output image is in the "samples" in the directory "checkpoints" created during training.
The feature to watch loss graph is in the "graph" in the directory "checkpoints" created during training.
util2
It corresponds to the following source code in the directory.

  • visualizer.cpp
  • visualizer.hpp

Conclusion

I hope this repository will help many programmers by providing PyTorch sample programs written in C++.
If you have any problems with the source code of this repository, please feel free to "issue".
Let's have a good development and research life!

About

Deep Learning sample programs using PyTorch in C++

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 92.0%
  • Python 5.6%
  • Shell 1.2%
  • CMake 1.2%