Skip to content

Commit 411e809

Browse files
authored
Download tutorial-dependent datasets at build-time. (#71)
* Fetch tutorial-dependent datasets at build-time. * Update docker entrypoint to support target build-choice when running container [ci skip] * Update README with additional tutorial building instructions [ci skip]
1 parent f9d1c59 commit 411e809

File tree

34 files changed

+403
-314
lines changed

34 files changed

+403
-314
lines changed

CMakeLists.txt

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
22

33
project(pytorch-cpp VERSION 1.0.0 LANGUAGES CXX)
44

5-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
5+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
66

7-
option(DOWNLOAD_DATASETS "Download datasets used in the tutorials." ON)
8-
option(CREATE_SCRIPTMODULES "Create all necessary scriptmodule files." OFF)
7+
option(DOWNLOAD_DATASETS "Automatically download required datasets at build-time." ON)
8+
option(CREATE_SCRIPTMODULES "Automatically create all required scriptmodule files at build-time (requires python3)." OFF)
99

1010
set(PYTORCH_VERSION "1.6.0")
1111

1212
find_package(Torch ${PYTORCH_VERSION} EXACT QUIET PATHS "${CMAKE_SOURCE_DIR}/libtorch")
1313

1414
if(NOT Torch_FOUND)
1515
unset(Torch_FOUND)
16-
include(download_libtorch)
17-
endif()
18-
19-
if(DOWNLOAD_DATASETS)
20-
include(download_datasets)
16+
include(fetch_libtorch)
2117
endif()
2218

2319
if(CREATE_SCRIPTMODULES)
@@ -43,33 +39,84 @@ add_subdirectory("extern")
4339
# Utils
4440
add_subdirectory("utils/image_io")
4541

42+
# Dataset fetching
43+
if(DOWNLOAD_DATASETS)
44+
set(DATA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/data CACHE PATH "Dataset download directory")
45+
file(MAKE_DIRECTORY ${DATA_DIR})
46+
47+
add_custom_target(mnist COMMAND ${CMAKE_COMMAND}
48+
-D DATA_DIR=${DATA_DIR}
49+
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/fetch_mnist.cmake)
50+
add_custom_target(cifar10 COMMAND ${CMAKE_COMMAND}
51+
-D DATA_DIR=${DATA_DIR}
52+
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/fetch_cifar10.cmake)
53+
add_custom_target(penntreebank COMMAND ${CMAKE_COMMAND}
54+
-D DATA_DIR=${DATA_DIR}
55+
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/fetch_penntreebank.cmake)
56+
add_custom_target(neural_style_transfer_images COMMAND ${CMAKE_COMMAND}
57+
-D DATA_DIR=${DATA_DIR}
58+
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/fetch_neural_style_transfer_images.cmake)
59+
add_custom_target(flickr8k COMMAND ${CMAKE_COMMAND}
60+
-D DATA_DIR=${DATA_DIR}
61+
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/fetch_flickr8k.cmake)
62+
endif()
63+
4664
# Add tutorial sub-projects:
4765

48-
# Basic
66+
# Basics
4967
add_subdirectory("tutorials/basics/feedforward_neural_network")
5068
add_subdirectory("tutorials/basics/linear_regression")
5169
add_subdirectory("tutorials/basics/logistic_regression")
5270
add_subdirectory("tutorials/basics/pytorch_basics")
5371

72+
add_custom_target(basics)
73+
add_dependencies(basics
74+
feedforward-neural-network
75+
linear-regression
76+
logistic-regression
77+
pytorch-basics)
78+
5479
# Intermediate
5580
add_subdirectory("tutorials/intermediate/convolutional_neural_network")
5681
add_subdirectory("tutorials/intermediate/deep_residual_network")
5782
add_subdirectory("tutorials/intermediate/recurrent_neural_network")
5883
add_subdirectory("tutorials/intermediate/bidirectional_recurrent_neural_network")
5984
add_subdirectory("tutorials/intermediate/language_model")
6085

86+
add_custom_target(intermediate)
87+
add_dependencies(intermediate
88+
convolutional-neural-network
89+
deep-residual-network
90+
recurrent-neural-network
91+
bidirectional-recurrent-neural-network
92+
language-model)
93+
6194
# Advanced
6295
add_subdirectory("tutorials/advanced/generative_adversarial_network")
6396
add_subdirectory("tutorials/advanced/variational_autoencoder")
6497
add_subdirectory("tutorials/advanced/neural_style_transfer")
6598
add_subdirectory("tutorials/advanced/image_captioning")
6699

100+
add_custom_target(advanced)
101+
add_dependencies(advanced
102+
generative-adversarial-network
103+
variational-autoencoder
104+
neural-style-transfer
105+
image-captioning)
106+
67107
# Popular
68108
add_subdirectory("tutorials/popular/blitz/tensors")
69109
add_subdirectory("tutorials/popular/blitz/autograd")
70110
add_subdirectory("tutorials/popular/blitz/neural_networks")
71111
add_subdirectory("tutorials/popular/blitz/training_a_classifier")
72112

113+
add_custom_target(popular)
114+
add_dependencies(popular
115+
tensors
116+
autograd
117+
neural-networks
118+
training-a-classifier)
119+
73120
if(MSVC)
74121
include(copy_torch_dlls)
75122
copy_torch_dlls(${EXECUTABLE_NAME})

Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,5 @@ COPY --from=conda-installs /opt/conda /opt/conda
5353
COPY --chown=pytorch:pytorch ./docker/docker-entrypoint.sh /
5454
RUN chmod +x /docker-entrypoint.sh
5555
ENTRYPOINT [ "/docker-entrypoint.sh" ]
56-
CMD [ "bash" ]
5756

58-
57+

README.md

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ This repository provides tutorial code in C++ for deep learning researchers to l
5757
## Requirements
5858

5959
1. [C++](http://www.cplusplus.com/doc/tutorial/introduction/)
60-
2. [CMake](https://cmake.org/download/)
60+
2. [CMake](https://cmake.org/download/) (minimum version 3.14)
6161
3. [LibTorch v1.6.0](https://pytorch.org/cppdocs/installing.html)
6262
4. [Conda](https://docs.conda.io/projects/conda/en/latest/user-guide/install/download.html)
6363

@@ -96,10 +96,10 @@ Some useful options:
9696
| Option | Default | Description |
9797
| :------------- |:------------|-----:|
9898
| `-D CUDA_V=(9.2 [Linux only]\|10.1\|10.2\|none)` | `none` | Download LibTorch for a CUDA version (`none` = download CPU version). |
99-
| `-D DOWNLOAD_DATASETS=(OFF\|ON)` | `ON` | Download all datasets used in the tutorials. |
99+
| `-D DOWNLOAD_DATASETS=(OFF\|ON)` | `ON` | Download required datasets during build (only if they do not already exist in `pytorch-cpp/data`). |
100+
|`-D CREATE_SCRIPTMODULES=(OFF\|ON)` | `OFF` | Create all required scriptmodule files for prelearned models / weights during build. Requires installed python3 with pytorch and torchvision. |
100101
| `-D CMAKE_PREFIX_PATH=path/to/libtorch/share/cmake/Torch` | `<empty>` | Skip the downloading of LibTorch and use your own local version (see [Requirements](#requirements)) instead. |
101102
| `-D CMAKE_BUILD_TYPE=(Release\|Debug)` | `<empty>` (`Release` when downloading LibTorch on Windows) | Set the build type (`Release` = compile with optimizations).|
102-
|`-D CREATE_SCRIPTMODULES=(OFF\|ON)` | `OFF` | Create all needed scriptmodule files for prelearned models / weights. Requires installed python3 with pytorch and torchvision. |
103103

104104
<details>
105105
<summary><b>Example Linux</b></summary>
@@ -133,21 +133,45 @@ cmake -B build \
133133
</details>
134134

135135
#### Build
136+
>**_Note for Windows (Visual Studio) users:_** <br>
137+
>The CMake script downloads the *Release* version of LibTorch, so `--config Release` has to be appended to the build command.
136138
139+
**How dataset download and scriptmodule creation work:**
140+
* If `DOWNLOAD_DATASETS` is `ON`, the datasets required by the tutorials you choose to build will be downloaded to `pytorch-cpp/data` (if they do not already exist there).
141+
* If `CREATE_SCRIPTMODULES` is `ON`, the scriptmodule files for the prelearned models / weights required by the tutorials you choose to build will be created in the `model` folder of the respective tutorial's source folder (if they do not already exist).
142+
#### All tutorials
143+
To build all tutorials use
137144
```bash
138145
cmake --build build
139146
```
140-
>**_Note for Windows users:_** <br>
141-
>The CMake script downloads the *Release* version of LibTorch, so `--config Release` has to be appended to the build command.
142-
>
143-
>**_General Note:_** <br>
144-
>By default all tutorials will be built. If you only want to build one specific tutorial, specify the `target` parameter for the build command. For example to only build the language model tutorial, append `--target language-model` (target name = tutorial foldername with all underscores replaced with hyphens).
147+
148+
#### All tutorials in a category
149+
You can choose to only build tutorials in one of the categories `basics`, `intermediate`, `advanced` or `popular`. For example, if you are only interested in the `basics` tutorials:
150+
```bash
151+
cmake --build build --target basics
152+
# In general: cmake --build build --target {category}
153+
```
154+
155+
#### Single tutorial
156+
You can also choose to only build a single tutorial. For example to build the language model tutorial only:
157+
```bash
158+
cmake --build build --target language-model
159+
# In general: cmake --build build --target {tutorial-name}
160+
```
161+
>**_Note_**:
162+
> The target argument is the tutorial's foldername with all underscores replaced by hyphens.
163+
164+
>**_Tip for users of CMake version >= 3.15_**:
165+
> You can specify several targets separated by spaces, for example:
166+
> ```bash
167+
> cmake --build build --target language-model image-captioning
168+
> ```
145169
146170
#### Run Tutorials
147171
1. (**IMPORTANT!**) First change into the tutorial's directory within `build/tutorials`. For example, assuming you are in the `pytorch-cpp` directory and want to change to the pytorch basics tutorial folder:
148172
```bash
149173
cd build/tutorials/basics/pytorch_basics
150-
# In general: cd build/tutorials/{basics|intermediate|advanced}/{tutorial_name}
174+
# In general: cd build/tutorials/{basics|intermediate|advanced|popular/blitz}/{tutorial_name}
151175
```
152176
2. Run the executable. Note that the executable's name is the tutorial's foldername with all underscores replaced with hyphens (e.g. for tutorial folder: `pytorch_basics` -> executable name: `pytorch-basics` (or `pytorch-basics.exe` on Windows)). For example, to run the pytorch basics tutorial:<br><br>
153177
**Linux/Mac**
@@ -176,13 +200,19 @@ You can build and run the tutorials (on CPU) in a Docker container using the pro
176200
```bash
177201
docker-compose run --rm pytorch-cpp
178202
```
179-
This fetches all necessary dependencies and builds the tutorials. After the build is done, by default the container starts `bash` in interactive mode in the `build/tutorials` folder.
180-
As an alternative, you can also directly run a tutorial by instead invoking the above command with the tutorial as additional argument, for example:
203+
This fetches all necessary dependencies and builds all tutorials.
204+
After the build is done, by default the container starts `bash` in interactive mode in the `build/tutorials` folder.
205+
As with the local build, you can choose to only build tutorials of a category (`basics`, `intermediate`, `advanced`, `popular`):
206+
```bash
207+
docker-compose run --rm pytorch-cpp {category}
208+
```
209+
In this case the container is started in the chosen category's base build directory.
210+
Alternatively, you can also directly run a tutorial by instead invoking the run command with a tutorial name as additional argument, for example:
181211
```bash
182212
docker-compose run --rm pytorch-cpp pytorch-basics
183213
# In general: docker-compose run --rm pytorch-cpp {tutorial-name}
184214
```
185-
This will - if necessary - build all tutorials and then start the provided tutorial in a container.
215+
This will - if necessary - build the pytorch-basics tutorial and then start the executable in a container.
186216
187217
## License
188218
This repository is licensed under MIT as given in [LICENSE](LICENSE).

cmake/download_cifar10.cmake

Lines changed: 0 additions & 40 deletions
This file was deleted.

cmake/download_datasets.cmake

Lines changed: 0 additions & 9 deletions
This file was deleted.

cmake/download_flickr8k.cmake

Lines changed: 0 additions & 43 deletions
This file was deleted.

cmake/download_mnist.cmake

Lines changed: 0 additions & 46 deletions
This file was deleted.

cmake/download_neural_style_transfer_images.cmake

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)