├── .clang-format ├── datasets └── swissroll.zip ├── Chapter06 ├── data │ └── photo.png ├── tapkee │ ├── util.h │ ├── CMakeLists.txt │ └── util.cc └── dlib │ └── CMakeLists.txt ├── Chapter14 └── android_detection │ ├── app │ ├── src │ │ └── main │ │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── themes.xml │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── layout │ │ │ │ └── activity_main.xml │ │ │ ├── xml │ │ │ │ ├── backup_rules.xml │ │ │ │ └── data_extraction_rules.xml │ │ │ ├── values-night │ │ │ │ └── themes.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── cpp │ │ │ ├── log.h │ │ │ ├── yolo.h │ │ │ ├── CMakeLists.txt │ │ │ ├── detector.h │ │ │ └── native-lib.cpp │ │ │ ├── assets │ │ │ └── classes.txt │ │ │ ├── AndroidManifest.xml │ │ │ └── java │ │ │ └── com │ │ │ └── example │ │ │ └── objectdetection │ │ │ └── MainActivity.kt │ ├── proguard-rules.pro │ └── build.gradle.kts │ ├── gradle │ ├── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ └── libs.versions.toml │ ├── build.gradle.kts │ ├── settings.gradle.kts │ ├── gradle.properties │ └── gradlew.bat ├── env_scripts ├── run_container.sh ├── checkout_lib.sh ├── install_lib_custom.sh ├── install_lib.sh ├── install_android.sh ├── install_env.sh └── dockerfile ├── Chapter02 ├── json │ ├── reviewsreader.h │ ├── paper.h │ ├── review.h │ ├── CMakeLists.txt │ └── json.cc ├── csv │ ├── dlib │ │ ├── CMakeLists.txt │ │ └── csv_dlib.cc │ ├── mlpack │ │ ├── CMakeLists.txt │ │ └── csv_mlpack.cc │ ├── eigen │ │ ├── CMakeLists.txt │ │ └── csv.cc │ └── flashlight │ │ ├── CMakeLists.txt │ │ └── csv_fl.cc ├── img │ ├── dlib │ │ ├── CMakeLists.txt │ │ └── img_dlib.cc │ └── opencv │ │ ├── CMakeLists.txt │ │ └── ocv.cc └── hdf5 │ ├── CMakeLists.txt │ └── hdf5.cc ├── Chapter12 ├── onnxruntime │ ├── model │ │ └── refs.txt │ └── CMakeLists.txt ├── dlib │ ├── CMakeLists.txt │ └── dlib-save.cc ├── flashlight │ ├── CMakeLists.txt │ └── fl_save.cc ├── pytorch │ └── CMakeLists.txt └── mlpack │ ├── CMakeLists.txt │ ├── cmake │ └── Findmlpack.cmake │ └── mlpack-save.cc ├── Chapter09 ├── mlpack │ ├── stacking.h │ ├── CMakeLists.txt │ └── cmake │ │ └── Findmlpack.cmake └── dlib │ ├── CMakeLists.txt │ └── dlib_ensemble.cc ├── Chapter10 ├── pytorch │ ├── lenet5.h │ ├── CMakeLists.txt │ ├── mnistdataset.h │ ├── lenet5.cpp │ └── mnistdataset.cpp ├── dlib │ ├── CMakeLists.txt │ └── mlp-dlib.cc ├── flashlight │ ├── CMakeLists.txt │ └── mlp_fl.cc └── mlpack │ ├── CMakeLists.txt │ ├── cmake │ └── Findmlpack.cmake │ └── mlp_mlpack.cc ├── Chapter11 └── pytorch │ ├── tokenizer.h │ ├── model.h │ ├── imdbreader.h │ ├── imdbdataset.h │ ├── imdbdataset.cc │ ├── CMakeLists.txt │ ├── model.cpp │ ├── imdbreader.cc │ ├── tokenizer.cc │ ├── export.py │ └── main.cpp ├── .gitignore ├── Chapter08 ├── eigen │ ├── CMakeLists.txt │ └── data_loader.h └── mlpack │ ├── CMakeLists.txt │ ├── cmake │ └── Findmlpack.cmake │ └── mlpack_recommender.cc ├── Chapter01 ├── arrayfire_samples │ ├── CMakeLists.txt │ ├── linreg_af.cc │ └── linalg_af.cc ├── dlib_samples │ ├── CMakeLists.txt │ ├── linreg_dlib.cc │ └── linalg_dlib.cc ├── blaze_samples │ ├── CMakeLists.txt │ ├── linreg_blaze.cc │ └── linalg_blaze.cc ├── xtensor_samples │ ├── CMakeLists.txt │ └── linalg_xtensor.cc └── eigen_samples │ ├── CMakeLists.txt │ ├── linreg_eigen.cc │ └── linalg_eigen.cc ├── Chapter13 └── flashlight │ ├── CMakeLists.txt │ ├── mlflow.h │ ├── mlflow.cc │ └── fl_track.cc ├── Chapter03 ├── dlib │ └── CMakeLists.txt ├── flashlight │ ├── CMakeLists.txt │ └── search_optuna.py └── mlpack │ ├── CMakeLists.txt │ ├── cmake │ └── Findmlpack.cmake │ └── grid-mlpack.cc ├── Chapter04 ├── dlib │ └── CMakeLists.txt └── mlpack │ ├── CMakeLists.txt │ └── cmake │ └── Findmlpack.cmake ├── Chapter05 ├── dlib │ └── CMakeLists.txt ├── mlpack │ ├── CMakeLists.txt │ └── cmake │ │ └── Findmlpack.cmake └── data │ ├── multivar.csv │ └── univar.csv ├── Chapter07 ├── dlib │ └── CMakeLists.txt ├── mlpack │ ├── CMakeLists.txt │ └── cmake │ │ └── Findmlpack.cmake └── flashlight │ └── CMakeLists.txt ├── LICENSE └── README.md /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: Chromium 2 | IndentWidth: 2 3 | ColumnLimit: 0 4 | -------------------------------------------------------------------------------- /datasets/swissroll.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/datasets/swissroll.zip -------------------------------------------------------------------------------- /Chapter06/data/photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter06/data/photo.png -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ObjectDetection 3 | -------------------------------------------------------------------------------- /Chapter06/tapkee/util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | tapkee::DenseMatrix read_data(const std::string& file_name, char delimiter); 6 | -------------------------------------------------------------------------------- /env_scripts/run_container.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | xhost +local:root 3 | docker run --net=host -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -it -v $PWD/..:/samples buildenv:1.0 bash 4 | 5 | -------------------------------------------------------------------------------- /Chapter02/json/reviewsreader.h: -------------------------------------------------------------------------------- 1 | #ifndef REVIEWSREADER_H 2 | #define REVIEWSREADER_H 3 | 4 | #include "paper.h" 5 | 6 | Papers ReadPapersReviews(const std::string& filename); 7 | 8 | #endif // REVIEWSREADER_H 9 | -------------------------------------------------------------------------------- /Chapter14/android_detection/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter14/android_detection/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter12/onnxruntime/model/refs.txt: -------------------------------------------------------------------------------- 1 | https://github.com/onnx/models/blob/main/validated/vision/classification/synset.txt 2 | https://github.com/onnx/models/blob/main/validated/vision/classification/resnet/model/resnet50-v1-7.onnx 3 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter14/android_detection/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter14/android_detection/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter14/android_detection/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter14/android_detection/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter14/android_detection/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter14/android_detection/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter14/android_detection/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter14/android_detection/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter14/android_detection/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-on-Machine-learning-with-C-Second-Edition/HEAD/Chapter14/android_detection/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /Chapter14/android_detection/build.gradle.kts: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | plugins { 3 | alias(libs.plugins.android.application) apply false 4 | alias(libs.plugins.jetbrains.kotlin.android) apply false 5 | } -------------------------------------------------------------------------------- /Chapter14/android_detection/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Jul 25 22:16:55 EEST 2024 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip 5 | zipStoreBase=GRADLE_USER_HOME 6 | zipStorePath=wrapper/dists 7 | -------------------------------------------------------------------------------- /Chapter02/json/paper.h: -------------------------------------------------------------------------------- 1 | #ifndef PAPER_H 2 | #define PAPER_H 3 | 4 | #include "review.h" 5 | 6 | #include 7 | 8 | struct Paper { 9 | uint32_t id{0}; 10 | std::string preliminary_decision; 11 | std::vector reviews; 12 | }; 13 | 14 | using Papers = std::vector; 15 | 16 | #endif // PAPER_H 17 | -------------------------------------------------------------------------------- /env_scripts/checkout_lib.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -x 3 | set -e 4 | 5 | START_DIR=$(pwd) 6 | REPOSITORY=$1 7 | COMMIT_HASH=$2 8 | 9 | cd $START_DIR/libs/sources 10 | git clone $REPOSITORY 11 | cd "$(basename "$REPOSITORY" .git)" 12 | git checkout $COMMIT_HASH 13 | git submodule update --init --recursive 14 | cd $START_DIR 15 | -------------------------------------------------------------------------------- /Chapter02/json/review.h: -------------------------------------------------------------------------------- 1 | #ifndef REVIEW_H 2 | #define REVIEW_H 3 | 4 | #include 5 | 6 | struct Review { 7 | std::string confidence; 8 | std::string evaluation; 9 | uint32_t id{0}; 10 | std::string language; 11 | std::string orientation; 12 | std::string remarks; 13 | std::string text; 14 | std::string timespan; 15 | }; 16 | 17 | #endif // REVIEW_H 18 | -------------------------------------------------------------------------------- /Chapter09/mlpack/stacking.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | void StackingClassification(size_t num_classes, 6 | const arma::mat& train_input, 7 | const arma::Row& train_labels, 8 | const arma::mat& test_input, 9 | const arma::Row& test_labels); -------------------------------------------------------------------------------- /Chapter10/pytorch/lenet5.h: -------------------------------------------------------------------------------- 1 | #ifndef LENET5_H 2 | #define LENET5_H 3 | 4 | #include 5 | 6 | class LeNet5Impl : public torch::nn::Module { 7 | public: 8 | LeNet5Impl(); 9 | 10 | torch::Tensor forward(torch::Tensor x); 11 | 12 | private: 13 | torch::nn::Sequential conv_; 14 | torch::nn::Sequential full_; 15 | }; 16 | 17 | TORCH_MODULE(LeNet5); 18 | 19 | #endif // LENET5_H 20 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter11/pytorch/tokenizer.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | class Tokenizer { 10 | public: 11 | Tokenizer(const std::string& vocab_file_path, int max_len = 128); 12 | 13 | std::pair tokenize(const std::string text); 14 | 15 | private: 16 | std::unordered_map vocab_; 17 | int max_len_{0}; 18 | }; 19 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | -------------------------------------------------------------------------------- /Chapter11/pytorch/model.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | class ModelImpl : public torch::nn::Module { 7 | public: 8 | ModelImpl() = delete; 9 | ModelImpl(const std::string& bert_model_path); 10 | 11 | torch::Tensor forward(at::Tensor input_ids, at::Tensor attention_masks); 12 | 13 | private: 14 | torch::jit::script::Module bert_; 15 | torch::nn::Dropout dropout_; 16 | torch::nn::Linear fc1_; 17 | torch::nn::Linear fc2_; 18 | }; 19 | 20 | TORCH_MODULE(Model); 21 | -------------------------------------------------------------------------------- /env_scripts/install_lib_custom.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -x 3 | set -e 4 | 5 | START_DIR=$(pwd) 6 | REPOSITORY=$1 7 | COMMIT_HASH=$2 8 | shift; shift; 9 | EXTRA_COMMAND=$@ 10 | 11 | cd $START_DIR/libs/sources 12 | git clone $REPOSITORY 13 | cd "$(basename "$REPOSITORY" .git)" 14 | git checkout $COMMIT_HASH 15 | 16 | if [ -f ".gitmodules" ];then 17 | sed -i 's/git:\/\//https:\/\//g' ".gitmodules" 18 | fi 19 | 20 | git submodule update --init --recursive 21 | $EXTRA_COMMAND 22 | cd .. 23 | rm -rf build 24 | cd $START_DIR 25 | 26 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/xml/backup_rules.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 13 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/cpp/log.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #define LOG_TAG "OBJECT-DETECTION" 5 | #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) 6 | #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) 7 | #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) 8 | #define ASSERT(cond, fmt, ...) \ 9 | if (!(cond)) { \ 10 | __android_log_assert(#cond, LOG_TAG, fmt, ##__VA_ARGS__); \ 11 | } 12 | -------------------------------------------------------------------------------- /Chapter11/pytorch/imdbreader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | class ImdbReader { 7 | public: 8 | ImdbReader(const std::string& root_path); 9 | 10 | size_t get_pos_size() const; 11 | size_t get_neg_size() const; 12 | 13 | const std::string& get_pos(size_t index) const; 14 | const std::string& get_neg(size_t index) const; 15 | 16 | private: 17 | using Reviews = std::vector; 18 | 19 | void read_directory(const std::string& path, Reviews& reviews); 20 | 21 | private: 22 | Reviews pos_samples_; 23 | Reviews neg_samples_; 24 | size_t max_size_{0}; 25 | }; 26 | -------------------------------------------------------------------------------- /Chapter02/csv/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(csv-dlib) 3 | 4 | find_package(dlib 19.24 REQUIRED) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | add_executable(csv-dlib "csv_dlib.cc") 24 | target_link_libraries(csv-dlib dlib::dlib) 25 | -------------------------------------------------------------------------------- /Chapter02/img/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(img-dlib) 3 | 4 | find_package(dlib 19.24 REQUIRED) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | add_executable(img-dlib "img_dlib.cc") 24 | target_link_libraries(img-dlib dlib::dlib) 25 | -------------------------------------------------------------------------------- /env_scripts/install_lib.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -x 3 | set -e 4 | 5 | START_DIR=$(pwd) 6 | REPOSITORY=$1 7 | COMMIT_HASH=$2 8 | shift; shift; 9 | EXTRA_CMAKE_PARAMS=$@ 10 | 11 | cd $START_DIR/libs/sources 12 | git clone $REPOSITORY 13 | cd "$(basename "$REPOSITORY" .git)" 14 | git checkout $COMMIT_HASH 15 | 16 | if [ -f ".gitmodules" ];then 17 | sed -i 's/git:\/\//https:\/\//g' ".gitmodules" 18 | fi 19 | 20 | git submodule update --init --recursive 21 | mkdir build 22 | cd build 23 | cmake -DCMAKE_INSTALL_PREFIX=$START_DIR/libs $EXTRA_CMAKE_PARAMS .. 24 | cmake --build . --target install -- -j8 25 | cd .. 26 | rm -rf build 27 | cd $START_DIR 28 | -------------------------------------------------------------------------------- /Chapter14/android_detection/settings.gradle.kts: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | google { 4 | content { 5 | includeGroupByRegex("com\\.android.*") 6 | includeGroupByRegex("com\\.google.*") 7 | includeGroupByRegex("androidx.*") 8 | } 9 | } 10 | mavenCentral() 11 | gradlePluginPortal() 12 | } 13 | } 14 | dependencyResolutionManagement { 15 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 16 | repositories { 17 | google() 18 | mavenCentral() 19 | } 20 | } 21 | 22 | rootProject.name = "ObjectDetection" 23 | include(":app") 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # VS Code settings 2 | .vscode/* 3 | 4 | # Local History for Visual Studio Code 5 | .history/ 6 | 7 | # Built Visual Studio Code Extensions 8 | *.vsix 9 | *.vscode 10 | 11 | # Build folders 12 | build 13 | 14 | # data folders 15 | data 16 | 17 | # Prerequisites 18 | *.d 19 | 20 | # Compiled Object files 21 | *.slo 22 | *.lo 23 | *.o 24 | *.obj 25 | 26 | # Precompiled Headers 27 | *.gch 28 | *.pch 29 | 30 | # Compiled Dynamic libraries 31 | *.so 32 | *.dylib 33 | *.dll 34 | 35 | # Fortran module files 36 | *.mod 37 | *.smod 38 | 39 | # Compiled Static libraries 40 | *.lai 41 | *.la 42 | *.a 43 | *.lib 44 | 45 | # Executables 46 | *.exe 47 | *.out 48 | *.app 49 | 50 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/xml/data_extraction_rules.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 12 | 13 | 19 | -------------------------------------------------------------------------------- /Chapter12/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(dlib-save) 3 | 4 | find_package(dlib 19.24 REQUIRED) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | set(SOURCES 24 | dlib-save.cc 25 | ) 26 | 27 | add_executable(dlib-save ${SOURCES}) 28 | target_link_libraries(dlib-save dlib::dlib) 29 | 30 | -------------------------------------------------------------------------------- /Chapter02/img/opencv/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(img-opencv) 3 | 4 | find_package(OpenCV 4.5 REQUIRED COMPONENTS core imgproc highgui) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | add_executable(img-opencv "ocv.cc") 24 | target_link_libraries(img-opencv opencv_core opencv_imgproc opencv_highgui) 25 | -------------------------------------------------------------------------------- /Chapter10/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(dlib-mlp) 3 | 4 | find_package(dlib 19.24 REQUIRED) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | set(SOURCES 24 | mlp-dlib.cc 25 | ../data/data.h 26 | ../data/data.cc 27 | ) 28 | 29 | add_executable(dlib-mlp ${SOURCES}) 30 | target_link_libraries(dlib-mlp dlib::dlib) 31 | -------------------------------------------------------------------------------- /Chapter08/eigen/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(eigen_recommender) 3 | 4 | find_package(Eigen3 3.4.0 REQUIRED) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | include_directories(${CSV_LIB_PATH}) 24 | 25 | add_executable(eigen_recommender "eigen_recommender.cc") 26 | target_link_libraries (eigen_recommender Eigen3::Eigen gomp) 27 | 28 | 29 | -------------------------------------------------------------------------------- /Chapter12/flashlight/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(fl_save) 3 | 4 | find_package(flashlight 0.4.0 REQUIRED) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | include_directories(${PLOTCPP_PATH}) 24 | 25 | set(SOURCES 26 | fl_save.cc 27 | ) 28 | 29 | add_executable(fl_save ${SOURCES}) 30 | target_link_libraries(fl_save flashlight::flashlight) 31 | -------------------------------------------------------------------------------- /Chapter01/arrayfire_samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(arrayfire_samples) 3 | 4 | find_package(ArrayFire) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | add_executable(linalg_af "linalg_af.cc") 24 | target_link_libraries(linalg_af ArrayFire::afcpu) 25 | 26 | add_executable(linreg_af "linreg_af.cc") 27 | target_link_libraries(linreg_af ArrayFire::afcpu) 28 | 29 | -------------------------------------------------------------------------------- /Chapter01/dlib_samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(dlib-samples) 3 | 4 | find_package(dlib 19.24 REQUIRED) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | add_executable(linalg-dlib "linalg_dlib.cc") 24 | target_link_libraries(linalg-dlib dlib::dlib) 25 | 26 | add_executable(linreg-dlib "linreg_dlib.cc") 27 | target_link_libraries(linreg-dlib dlib::dlib) 28 | 29 | -------------------------------------------------------------------------------- /Chapter12/pytorch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(pytorch-save) 3 | 4 | find_package(Torch REQUIRED) 5 | find_package(OpenCV REQUIRED) 6 | 7 | set(CMAKE_VERBOSE_MAKEFILE ON) 8 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 9 | set(CMAKE_CXX_STANDARD 20) 10 | set(CMAKE_CXX_EXTENSIONS OFF) 11 | 12 | 13 | include_directories(${TORCH_INCLUDE_DIRS}) 14 | 15 | add_compile_options( 16 | -Wall -Wextra -msse3 -fopenmp 17 | $<$:-Ofast> 18 | $<$:-O0> 19 | $<$:-ggdb3> 20 | ) 21 | 22 | set(SOURCE_FILES main.cc) 23 | 24 | add_executable("${CMAKE_PROJECT_NAME}" ${SOURCE_FILES}) 25 | target_link_libraries("${CMAKE_PROJECT_NAME}" ${TORCH_LIBRARIES} ${OpenCV_LIBS}) 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Chapter13/flashlight/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(fl_track) 3 | 4 | find_package(flashlight 0.4.0 REQUIRED) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | include_directories(${PLOTCPP_PATH}) 24 | 25 | set(SOURCES 26 | fl_track.cc 27 | mlflow.cc 28 | ) 29 | 30 | add_executable(fl_track ${SOURCES}) 31 | target_link_libraries(fl_track flashlight::flashlight) 32 | -------------------------------------------------------------------------------- /Chapter01/blaze_samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(blaze_samples) 3 | 4 | find_package(blaze 3.8.2 REQUIRED) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | add_executable(linalg_blaze "linalg_blaze.cc") 24 | target_link_libraries(linalg_blaze blas lapack blaze::blaze) 25 | 26 | add_executable(linreg_blaze "linreg_blaze.cc") 27 | target_link_libraries(linreg_blaze blas lapack blaze::blaze) 28 | 29 | -------------------------------------------------------------------------------- /Chapter01/xtensor_samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(xtensor_samples) 3 | 4 | find_package(xtensor 0.24.5 REQUIRED) 5 | find_package(xtensor-blas 0.20.0 REQUIRED) 6 | 7 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | set(CMAKE_CXX_STANDARD 20) 9 | set(CMAKE_CXX_EXTENSIONS OFF) 10 | 11 | set(CMAKE_VERBOSE_MAKEFILE ON) 12 | 13 | add_compile_options( 14 | -Wall -Wextra -msse3 15 | $<$:-Ofast> 16 | $<$:-O0> 17 | $<$:-ggdb3> 18 | ) 19 | 20 | add_compile_definitions( 21 | $<$:NDEBUG> 22 | ) 23 | 24 | add_executable(linalg_xtensor "linalg_xtensor.cc") 25 | target_include_directories(linalg_xtensor PRIVATE ${xtensor_INCLUDE_DIRS}) 26 | target_link_libraries(linalg_xtensor blas) 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Chapter10/flashlight/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(mlp-flashlight) 3 | 4 | find_package(flashlight 0.4.0 REQUIRED) 5 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_STANDARD 20) 8 | set(CMAKE_CXX_EXTENSIONS OFF) 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | 12 | add_compile_options( 13 | -Wall -Wextra -msse3 -fopenmp 14 | $<$:-Ofast> 15 | $<$:-O0> 16 | $<$:-ggdb3> 17 | ) 18 | 19 | add_compile_definitions( 20 | $<$:NDEBUG> 21 | ) 22 | 23 | include_directories(${PLOTCPP_PATH}) 24 | 25 | set(SOURCES 26 | mlp_fl.cc 27 | ../data/data.h 28 | ../data/data.cc 29 | ) 30 | 31 | add_executable(mlp_fl ${SOURCES}) 32 | target_link_libraries(mlp_fl flashlight::flashlight) 33 | -------------------------------------------------------------------------------- /Chapter11/pytorch/imdbdataset.h: -------------------------------------------------------------------------------- 1 | #ifndef MNISTDATASET_H 2 | #define MNISTDATASET_H 3 | 4 | #include "imdbreader.h" 5 | #include "tokenizer.h" 6 | 7 | #include 8 | 9 | #include 10 | 11 | using ImdbData = std::pair; 12 | using ImdbExample = torch::data::Example; 13 | 14 | class ImdbDataset : public torch::data::Dataset { 15 | public: 16 | ImdbDataset(const std::string& dataset_path, 17 | std::shared_ptr tokenizer); 18 | 19 | // torch::data::Dataset implementation 20 | ImdbExample get(size_t index) override; 21 | torch::optional size() const override; 22 | 23 | private: 24 | ImdbReader reader_; 25 | std::shared_ptr tokenizer_; 26 | }; 27 | 28 | #endif // MNISTDATASET_H 29 | -------------------------------------------------------------------------------- /Chapter01/eigen_samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(eigen_samples) 3 | 4 | find_package(OpenMP) 5 | find_package(Eigen3 3.4.0 REQUIRED) 6 | 7 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | set(CMAKE_CXX_STANDARD 20) 9 | set(CMAKE_CXX_EXTENSIONS OFF) 10 | 11 | set(CMAKE_VERBOSE_MAKEFILE ON) 12 | 13 | add_compile_options( 14 | -Wall -Wextra -msse3 15 | $<$:-Ofast> 16 | $<$:-O0> 17 | $<$:-ggdb3> 18 | ) 19 | 20 | add_compile_definitions( 21 | $<$:NDEBUG> 22 | ) 23 | 24 | add_executable(linalg_eigen "linalg_eigen.cc") 25 | target_link_libraries(linalg_eigen Eigen3::Eigen OpenMP::OpenMP_CXX) 26 | 27 | add_executable(linreg_eigen "linreg_eigen.cc") 28 | target_link_libraries(linreg_eigen Eigen3::Eigen OpenMP::OpenMP_CXX) 29 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /Chapter12/onnxruntime/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(onnx-load) 3 | 4 | find_package(onnxruntime REQUIRED) 5 | find_package(OpenCV 4.5 REQUIRED COMPONENTS core imgproc imgcodecs) 6 | 7 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | set(CMAKE_CXX_STANDARD 20) 9 | set(CMAKE_CXX_EXTENSIONS OFF) 10 | 11 | set(CMAKE_VERBOSE_MAKEFILE ON) 12 | 13 | add_compile_options( 14 | -Wall -Wextra -msse3 -fopenmp 15 | $<$:-Ofast> 16 | $<$:-O0> 17 | $<$:-ggdb3> 18 | ) 19 | 20 | add_compile_definitions( 21 | $<$:NDEBUG> 22 | ) 23 | 24 | include_directories(${PLOTCPP_PATH}) 25 | 26 | set(SOURCES 27 | main.cc 28 | ) 29 | 30 | add_executable(onnx-load ${SOURCES}) 31 | target_link_libraries(onnx-load onnxruntime::onnxruntime opencv_core opencv_imgproc opencv_imgcodecs) 32 | -------------------------------------------------------------------------------- /Chapter02/json/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(json_sample) 3 | 4 | find_package(nlohmann_json 3.11.2 REQUIRED) 5 | find_package(Eigen3 3.4.0 REQUIRED) 6 | 7 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | set(CMAKE_CXX_STANDARD 20) 9 | set(CMAKE_CXX_EXTENSIONS OFF) 10 | 11 | set(CMAKE_VERBOSE_MAKEFILE ON) 12 | 13 | add_compile_options( 14 | -Wall -Wextra -msse3 -fopenmp 15 | $<$:-Ofast> 16 | $<$:-O0> 17 | $<$:-ggdb3> 18 | ) 19 | 20 | add_compile_definitions( 21 | $<$:NDEBUG> 22 | ) 23 | 24 | set(SOURCES json.cc 25 | review.h 26 | paper.h 27 | reviewsreader.h 28 | reviewsreader.cpp) 29 | 30 | add_executable(json_sample ${SOURCES}) 31 | target_link_libraries(json_sample Eigen3::Eigen nlohmann_json::nlohmann_json) 32 | -------------------------------------------------------------------------------- /Chapter10/pytorch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(lenet-pytorch) 3 | 4 | find_package(Torch REQUIRED) 5 | find_package(OpenCV REQUIRED) 6 | 7 | set(CMAKE_VERBOSE_MAKEFILE ON) 8 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 9 | set(CMAKE_CXX_STANDARD 20) 10 | set(CMAKE_CXX_EXTENSIONS OFF) 11 | 12 | 13 | include_directories(${TORCH_INCLUDE_DIRS}) 14 | 15 | add_compile_options( 16 | -Wall -Wextra -msse3 -fopenmp 17 | $<$:-Ofast> 18 | $<$:-O0> 19 | $<$:-ggdb3> 20 | ) 21 | 22 | set(SOURCE_FILES main.cpp 23 | mnistdataset.h 24 | mnistdataset.cpp 25 | lenet5.h 26 | lenet5.cpp) 27 | 28 | add_executable("${CMAKE_PROJECT_NAME}" ${SOURCE_FILES}) 29 | target_link_libraries("${CMAKE_PROJECT_NAME}" ${TORCH_LIBRARIES} ${OpenCV_LIBS}) 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/mlpack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(mlpack-ensemble) 3 | 4 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 5 | find_package(mlpack 4.0.1 REQUIRED) 6 | 7 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | set(CMAKE_CXX_STANDARD 20) 9 | set(CMAKE_CXX_EXTENSIONS OFF) 10 | 11 | add_compile_options( 12 | -Wall -Wextra -msse3 -fopenmp 13 | $<$:-Ofast> 14 | $<$:-O0> 15 | $<$:-ggdb3> 16 | ) 17 | 18 | add_link_options(-fopenmp) 19 | 20 | add_compile_definitions( 21 | $<$:NDEBUG> 22 | ) 23 | 24 | include_directories(${MLPACK_INCLUDE_DIR}) 25 | 26 | add_executable(mlpack-ensemble mlpack_ensemble.cc stacking.cc) 27 | target_link_directories(mlpack-ensemble PRIVATE ${CMAKE_PREFIX_PATH}/lib) 28 | target_link_libraries(mlpack-ensemble ${MLPACK_LIBRARIES} armadillo) 29 | -------------------------------------------------------------------------------- /Chapter06/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-dr) 3 | 4 | find_package(dlib 19.24 REQUIRED) 5 | 6 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 7 | 8 | if (NOT PLOTCPP_PATH) 9 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 10 | else() 11 | message("plotcpp path is ${PLOTCPP_PATH}") 12 | endif() 13 | 14 | 15 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 16 | set(CMAKE_CXX_STANDARD 20) 17 | set(CMAKE_CXX_EXTENSIONS OFF) 18 | 19 | add_compile_options( 20 | -Wall -Wextra -msse3 -fopenmp 21 | $<$:-Ofast> 22 | $<$:-O0> 23 | $<$:-ggdb3> 24 | ) 25 | 26 | add_compile_definitions( 27 | $<$:NDEBUG> 28 | ) 29 | 30 | include_directories(${PLOTCPP_PATH}) 31 | 32 | add_executable(dlib-dr "dlib-dr.cc") 33 | target_link_libraries(dlib-dr dlib::dlib) 34 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /Chapter03/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-sample) 3 | 4 | find_package(dlib 19.24 REQUIRED) 5 | 6 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 7 | 8 | if (NOT PLOTCPP_PATH) 9 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 10 | else() 11 | message("plotcpp path is ${PLOTCPP_PATH}") 12 | endif() 13 | 14 | 15 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 16 | set(CMAKE_CXX_STANDARD 20) 17 | set(CMAKE_CXX_EXTENSIONS OFF) 18 | 19 | add_compile_options( 20 | -Wall -Wextra -msse3 -fopenmp 21 | $<$:-Ofast> 22 | $<$:-O0> 23 | $<$:-ggdb3> 24 | ) 25 | 26 | add_compile_definitions( 27 | $<$:NDEBUG> 28 | ) 29 | 30 | include_directories(${PLOTCPP_PATH}) 31 | 32 | add_executable(grid-dlib "grid-dlib.cc") 33 | target_link_libraries(grid-dlib dlib::dlib) 34 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /Chapter04/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-cluster) 3 | 4 | find_package(dlib 19.24 REQUIRED) 5 | 6 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 7 | 8 | if (NOT PLOTCPP_PATH) 9 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 10 | else() 11 | message("plotcpp path is ${PLOTCPP_PATH}") 12 | endif() 13 | 14 | 15 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 16 | set(CMAKE_CXX_STANDARD 20) 17 | set(CMAKE_CXX_EXTENSIONS OFF) 18 | 19 | add_compile_options( 20 | -Wall -Wextra -msse3 -fopenmp 21 | $<$:-Ofast> 22 | $<$:-O0> 23 | $<$:-ggdb3> 24 | ) 25 | 26 | add_compile_definitions( 27 | $<$:NDEBUG> 28 | ) 29 | 30 | include_directories(${PLOTCPP_PATH}) 31 | 32 | add_executable(dlib-cluster "dlib-cluster.cc") 33 | target_link_libraries(dlib-cluster dlib::dlib) 34 | -------------------------------------------------------------------------------- /Chapter05/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-anomaly) 3 | 4 | find_package(dlib 19.24 REQUIRED) 5 | 6 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 7 | 8 | if (NOT PLOTCPP_PATH) 9 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 10 | else() 11 | message("plotcpp path is ${PLOTCPP_PATH}") 12 | endif() 13 | 14 | 15 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 16 | set(CMAKE_CXX_STANDARD 20) 17 | set(CMAKE_CXX_EXTENSIONS OFF) 18 | 19 | add_compile_options( 20 | -Wall -Wextra -msse3 -fopenmp 21 | $<$:-Ofast> 22 | $<$:-O0> 23 | $<$:-ggdb3> 24 | ) 25 | 26 | add_compile_definitions( 27 | $<$:NDEBUG> 28 | ) 29 | 30 | include_directories(${PLOTCPP_PATH}) 31 | 32 | add_executable(dlib-anomaly "dlib-anomaly.cc") 33 | target_link_libraries(dlib-anomaly dlib::dlib) 34 | -------------------------------------------------------------------------------- /Chapter07/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dlib-classify) 3 | 4 | find_package(dlib 19.24 REQUIRED) 5 | 6 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 7 | 8 | if (NOT PLOTCPP_PATH) 9 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 10 | else() 11 | message("plotcpp path is ${PLOTCPP_PATH}") 12 | endif() 13 | 14 | 15 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 16 | set(CMAKE_CXX_STANDARD 20) 17 | set(CMAKE_CXX_EXTENSIONS OFF) 18 | 19 | add_compile_options( 20 | -Wall -Wextra -msse3 -fopenmp 21 | $<$:-Ofast> 22 | $<$:-O0> 23 | $<$:-ggdb3> 24 | ) 25 | 26 | add_compile_definitions( 27 | $<$:NDEBUG> 28 | ) 29 | 30 | include_directories(${PLOTCPP_PATH}) 31 | 32 | add_executable(dlib-classify "dlib-classify.cc") 33 | target_link_libraries(dlib-classify dlib::dlib) 34 | -------------------------------------------------------------------------------- /Chapter02/csv/mlpack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(mlpack-csv-sample) 3 | 4 | include(FindPackageHandleStandardArgs) 5 | 6 | find_package(OpenMP REQUIRED) 7 | 8 | find_path(MLPACK_INCLUDE_DIR 9 | NAMES mlpack/core.hpp mlpack/prereqs.hpp 10 | ) 11 | 12 | find_package_handle_standard_args(mlpack 13 | REQUIRED_VARS MLPACK_INCLUDE_DIR 14 | ) 15 | 16 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 17 | set(CMAKE_CXX_STANDARD 20) 18 | set(CMAKE_CXX_EXTENSIONS OFF) 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | add_compile_options( 23 | -Wall -Wextra -msse3 -fopenmp 24 | $<$:-Ofast> 25 | $<$:-O0> 26 | $<$:-ggdb3> 27 | ) 28 | 29 | add_compile_definitions( 30 | $<$:NDEBUG> 31 | ) 32 | 33 | include_directories(${MLPACK_INCLUDE_DIR}) 34 | 35 | add_executable(csv_mlpack csv_mlpack.cc) 36 | target_link_libraries(csv_mlpack OpenMP::OpenMP_CXX) -------------------------------------------------------------------------------- /Chapter02/hdf5/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(hdf5_sample) 3 | 4 | find_package(HDF5 1.10.7 REQUIRED) 5 | find_package(HighFive 2.7.0 REQUIRED) 6 | find_package(nlohmann_json 3.11.2 REQUIRED) 7 | 8 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 9 | set(CMAKE_CXX_STANDARD 20) 10 | set(CMAKE_CXX_EXTENSIONS OFF) 11 | 12 | set(CMAKE_VERBOSE_MAKEFILE ON) 13 | 14 | add_compile_options( 15 | -Wall -Wextra -msse3 -fopenmp 16 | $<$:-Ofast> 17 | $<$:-O0> 18 | $<$:-ggdb3> 19 | ) 20 | 21 | add_compile_definitions( 22 | $<$:NDEBUG> 23 | ) 24 | 25 | set(SOURCES hdf5.cc 26 | ../../json/paper.h 27 | ../../json/review.h 28 | ../../json/reviewsreader.h 29 | ../../json/reviewsreader.cpp) 30 | 31 | add_executable(hdf5_sample ${SOURCES}) 32 | target_link_libraries(hdf5_sample nlohmann_json::nlohmann_json HighFive) 33 | -------------------------------------------------------------------------------- /Chapter02/csv/eigen/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(csv_sample) 3 | 4 | set(CSV_LIB_PATH "" CACHE PATH "Path to csv library include dir") 5 | 6 | if (NOT CSV_LIB_PATH) 7 | message(FATAL_ERROR "Missigng CSV lib install path, please specify CSV_LIB_PATH") 8 | else() 9 | message("CSV lib path is ${CSV_LIB_PATH}") 10 | endif() 11 | 12 | find_package(Eigen3 3.4.0 REQUIRED) 13 | 14 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 15 | set(CMAKE_CXX_STANDARD 20) 16 | set(CMAKE_CXX_EXTENSIONS OFF) 17 | 18 | set(CMAKE_VERBOSE_MAKEFILE ON) 19 | 20 | add_compile_options( 21 | -Wall -Wextra -msse3 -fopenmp 22 | $<$:-Ofast> 23 | $<$:-O0> 24 | $<$:-ggdb3> 25 | ) 26 | 27 | add_compile_definitions( 28 | $<$:NDEBUG> 29 | ) 30 | 31 | include_directories(${CSV_LIB_PATH}) 32 | 33 | add_executable(csv_sample "csv.cc") 34 | target_link_libraries (csv_sample Eigen3::Eigen) 35 | -------------------------------------------------------------------------------- /Chapter09/dlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(dlib-ensemble) 3 | 4 | find_package(dlib 19.24 REQUIRED) 5 | 6 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 7 | 8 | if (NOT PLOTCPP_PATH) 9 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 10 | else() 11 | message("plotcpp path is ${PLOTCPP_PATH}") 12 | endif() 13 | 14 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 15 | set(CMAKE_CXX_STANDARD 20) 16 | set(CMAKE_CXX_EXTENSIONS OFF) 17 | 18 | set(CMAKE_VERBOSE_MAKEFILE ON) 19 | 20 | add_compile_options( 21 | -Wall -Wextra -msse3 -fopenmp 22 | $<$:-Ofast> 23 | $<$:-O0> 24 | $<$:-ggdb3> 25 | ) 26 | 27 | add_compile_definitions( 28 | $<$:NDEBUG> 29 | ) 30 | 31 | include_directories(${PLOTCPP_PATH}) 32 | 33 | add_executable(dlib-ensemble "dlib_ensemble.cc") 34 | target_link_libraries(dlib-ensemble dlib::dlib) 35 | -------------------------------------------------------------------------------- /Chapter03/flashlight/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(grid-flashlight) 3 | 4 | find_package(flashlight 0.4.0 REQUIRED) 5 | 6 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 7 | 8 | if (NOT PLOTCPP_PATH) 9 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 10 | else() 11 | message("plotcpp path is ${PLOTCPP_PATH}") 12 | endif() 13 | 14 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 15 | set(CMAKE_CXX_STANDARD 20) 16 | set(CMAKE_CXX_EXTENSIONS OFF) 17 | 18 | set(CMAKE_VERBOSE_MAKEFILE ON) 19 | 20 | add_compile_options( 21 | -Wall -Wextra -msse3 -fopenmp 22 | $<$:-Ofast> 23 | $<$:-O0> 24 | $<$:-ggdb3> 25 | ) 26 | 27 | add_compile_definitions( 28 | $<$:NDEBUG> 29 | ) 30 | 31 | include_directories(${PLOTCPP_PATH}) 32 | 33 | add_executable(grid_fl grid_fl.cc) 34 | target_link_libraries(grid_fl flashlight::flashlight) 35 | -------------------------------------------------------------------------------- /Chapter02/csv/flashlight/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(flashlight-csv-sample) 3 | 4 | set(CSV_LIB_PATH "" CACHE PATH "Path to csv library include dir") 5 | 6 | if (NOT CSV_LIB_PATH) 7 | message(FATAL_ERROR "Missigng CSV lib install path, please specify CSV_LIB_PATH") 8 | else() 9 | message("CSV lib path is ${CSV_LIB_PATH}") 10 | endif() 11 | 12 | find_package(flashlight 0.4.0 REQUIRED) 13 | 14 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 15 | set(CMAKE_CXX_STANDARD 20) 16 | set(CMAKE_CXX_EXTENSIONS OFF) 17 | 18 | set(CMAKE_VERBOSE_MAKEFILE ON) 19 | 20 | add_compile_options( 21 | -Wall -Wextra -msse3 -fopenmp 22 | $<$:-Ofast> 23 | $<$:-O0> 24 | $<$:-ggdb3> 25 | ) 26 | 27 | add_compile_definitions( 28 | $<$:NDEBUG> 29 | ) 30 | 31 | include_directories(${CSV_LIB_PATH}) 32 | 33 | add_executable(csv_fl csv_fl.cc) 34 | target_link_libraries(csv_fl flashlight::flashlight) 35 | -------------------------------------------------------------------------------- /Chapter08/mlpack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(mlpack-recommender) 3 | 4 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 5 | find_package(mlpack 4.0.1 REQUIRED) 6 | 7 | find_package(Boost REQUIRED serialization) 8 | 9 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 10 | set(CMAKE_CXX_STANDARD 20) 11 | set(CMAKE_CXX_EXTENSIONS OFF) 12 | 13 | add_compile_options( 14 | -Wall -Wextra -msse3 -fopenmp 15 | $<$:-Ofast> 16 | $<$:-O0> 17 | $<$:-ggdb3> 18 | ) 19 | 20 | add_link_options(-fopenmp) 21 | 22 | add_compile_definitions( 23 | $<$:NDEBUG> 24 | ) 25 | 26 | include_directories(${MLPACK_INCLUDE_DIR}) 27 | 28 | add_executable(mlpack-recommender "mlpack_recommender.cc") 29 | target_link_directories(mlpack-recommender PRIVATE ${CMAKE_PREFIX_PATH}/lib) 30 | target_link_libraries(mlpack-recommender ${MLPACK_LIBRARIES} ${Boost_LIBRARIES} armadillo) 31 | -------------------------------------------------------------------------------- /Chapter11/pytorch/imdbdataset.cc: -------------------------------------------------------------------------------- 1 | #include "imdbdataset.h" 2 | #include 3 | #include 4 | 5 | ImdbDataset::ImdbDataset(const std::string& dataset_path, 6 | std::shared_ptr tokenizer) 7 | : reader_(dataset_path), tokenizer_(std::move(tokenizer)) {} 8 | 9 | ImdbExample ImdbDataset::get(size_t index) { 10 | torch::Tensor target; 11 | const std::string* review{nullptr}; 12 | if (index < reader_.get_pos_size()) { 13 | review = &reader_.get_pos(index); 14 | target = torch::tensor(1, torch::dtype(torch::kLong)); 15 | } else { 16 | review = &reader_.get_neg(index - reader_.get_pos_size()); 17 | target = torch::tensor(0, torch::dtype(torch::kLong)); 18 | } 19 | // encode text 20 | auto tokenizer_out = tokenizer_->tokenize(*review); 21 | 22 | return {tokenizer_out, target.squeeze()}; 23 | } 24 | 25 | torch::optional ImdbDataset::size() const { 26 | return reader_.get_pos_size() + reader_.get_neg_size(); 27 | } 28 | -------------------------------------------------------------------------------- /Chapter10/pytorch/mnistdataset.h: -------------------------------------------------------------------------------- 1 | #ifndef MNISTDATASET_H 2 | #define MNISTDATASET_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class MNISTDataset : public torch::data::Dataset { 10 | public: 11 | MNISTDataset(const std::string& images_file_name, 12 | const std::string& labels_file_name, 13 | torch::DeviceType device); 14 | 15 | // test only method 16 | void ShowItem(size_t index) const; 17 | 18 | // torch::data::Dataset implementation 19 | torch::data::Example<> get(size_t index) override; 20 | torch::optional size() const override; 21 | 22 | private: 23 | void ReadLabels(const std::string& labels_file_name); 24 | void ReadImages(const std::string& images_file_name); 25 | 26 | uint32_t rows_ = 0; 27 | uint32_t columns_ = 0; 28 | std::vector labels_; 29 | std::vector images_; 30 | torch::DeviceType device_; 31 | }; 32 | 33 | #endif // MNISTDATASET_H 34 | -------------------------------------------------------------------------------- /Chapter11/pytorch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(sentiment-pytorch) 3 | 4 | find_package(Torch REQUIRED) 5 | find_package(OpenCV REQUIRED) 6 | 7 | set(CMAKE_VERBOSE_MAKEFILE ON) 8 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 9 | set(CMAKE_CXX_STANDARD 20) 10 | set(CMAKE_CXX_EXTENSIONS OFF) 11 | 12 | 13 | include_directories(${TORCH_INCLUDE_DIRS}) 14 | 15 | add_compile_options( 16 | -Wall -Wextra -msse3 -fopenmp 17 | $<$:-Ofast> 18 | $<$:-O0> 19 | $<$:-ggdb3> 20 | ) 21 | 22 | set(SOURCE_FILES main.cpp 23 | tokenizer.h 24 | tokenizer.cc 25 | imdbdataset.h 26 | imdbdataset.cc 27 | imdbreader.cc 28 | imdbreader.h 29 | model.h 30 | model.cpp) 31 | 32 | add_executable("${CMAKE_PROJECT_NAME}" ${SOURCE_FILES}) 33 | target_link_libraries("${CMAKE_PROJECT_NAME}" ${TORCH_LIBRARIES} ${OpenCV_LIBS}) 34 | 35 | -------------------------------------------------------------------------------- /Chapter12/mlpack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(mlpack-save) 3 | 4 | include(FindPackageHandleStandardArgs) 5 | 6 | find_package(OpenMP REQUIRED) 7 | 8 | find_path(MLPACK_INCLUDE_DIR 9 | NAMES mlpack/core.hpp mlpack/prereqs.hpp 10 | ) 11 | 12 | find_package_handle_standard_args(mlpack 13 | REQUIRED_VARS MLPACK_INCLUDE_DIR 14 | ) 15 | 16 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 17 | set(CMAKE_CXX_STANDARD 20) 18 | set(CMAKE_CXX_EXTENSIONS OFF) 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | add_compile_options( 23 | -Wall -Wextra -msse3 -fopenmp 24 | $<$:-Ofast> 25 | $<$:-O0> 26 | $<$:-ggdb3> 27 | ) 28 | 29 | add_compile_definitions( 30 | $<$:NDEBUG> 31 | ARMA_DONT_USE_WRAPPER 32 | ) 33 | 34 | include_directories(${MLPACK_INCLUDE_DIR}) 35 | 36 | set(SOURCES 37 | mlpack-save.cc 38 | ) 39 | 40 | add_executable(mlpack-save ${SOURCES}) 41 | target_link_libraries(mlpack-save OpenMP::OpenMP_CXX blas lapack) 42 | -------------------------------------------------------------------------------- /Chapter10/mlpack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(mlp-mlpack) 3 | 4 | include(FindPackageHandleStandardArgs) 5 | 6 | find_package(OpenMP REQUIRED) 7 | 8 | find_path(MLPACK_INCLUDE_DIR 9 | NAMES mlpack/core.hpp mlpack/prereqs.hpp 10 | ) 11 | 12 | find_package_handle_standard_args(mlpack 13 | REQUIRED_VARS MLPACK_INCLUDE_DIR 14 | ) 15 | 16 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 17 | set(CMAKE_CXX_STANDARD 20) 18 | set(CMAKE_CXX_EXTENSIONS OFF) 19 | 20 | set(CMAKE_VERBOSE_MAKEFILE ON) 21 | 22 | add_compile_options( 23 | -Wall -Wextra -msse3 -fopenmp 24 | $<$:-Ofast> 25 | $<$:-O0> 26 | $<$:-ggdb3> 27 | ) 28 | 29 | add_compile_definitions( 30 | $<$:NDEBUG> 31 | ARMA_DONT_USE_WRAPPER 32 | ) 33 | 34 | include_directories(${MLPACK_INCLUDE_DIR}) 35 | 36 | set(SOURCES 37 | mlp_mlpack.cc 38 | ../data/data.h 39 | ../data/data.cc 40 | ) 41 | 42 | add_executable(mlp-mlpack ${SOURCES}) 43 | target_link_libraries(mlp-mlpack OpenMP::OpenMP_CXX blas lapack) -------------------------------------------------------------------------------- /Chapter13/flashlight/mlflow.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include 5 | 6 | #include 7 | 8 | // The mlflow REST API use implementation https://mlflow.org/docs/latest/rest-api.html 9 | // 1. start a server with command: mlflow server --backend-store-uri file:///some_directory/mlruns 10 | 11 | class MLFlow { 12 | public: 13 | MLFlow(); 14 | MLFlow(const std::string& host, size_t port); 15 | 16 | ~MLFlow() = default; 17 | MLFlow(const MLFlow&) = delete; 18 | MLFlow& operator=(const MLFlow&) = delete; 19 | MLFlow(MLFlow&&) = delete; 20 | MLFlow& operator=(MLFlow&&) = delete; 21 | 22 | void set_experiment(const std::string& name); 23 | void start_run(); 24 | void end_run(); 25 | void log_metric(const std::string& name, float value, size_t epoch); 26 | void log_param(const std::string& name, const std::string& value); 27 | 28 | template 29 | void log_param(const std::string& name, T value) { 30 | log_param(name, std::to_string(value)); 31 | } 32 | 33 | private: 34 | httplib::Client http_client_; 35 | std::string experiment_id_; 36 | std::string run_id_; 37 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Chapter03/mlpack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(mlpack-sample) 3 | 4 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 5 | find_package(mlpack 4.0.1 REQUIRED) 6 | 7 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 8 | 9 | if (NOT PLOTCPP_PATH) 10 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 11 | else() 12 | message("plotcpp path is ${PLOTCPP_PATH}") 13 | endif() 14 | 15 | 16 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 17 | set(CMAKE_CXX_STANDARD 20) 18 | set(CMAKE_CXX_EXTENSIONS OFF) 19 | 20 | add_compile_options( 21 | -Wall -Wextra -msse3 -fopenmp 22 | $<$:-Ofast> 23 | $<$:-O0> 24 | $<$:-ggdb3> 25 | ) 26 | 27 | add_link_options(-fopenmp) 28 | 29 | add_compile_definitions( 30 | $<$:NDEBUG> 31 | ) 32 | 33 | include_directories(${PLOTCPP_PATH}) 34 | include_directories(${MLPACK_INCLUDE_DIR}) 35 | 36 | add_executable(grid-mlpack "grid-mlpack.cc") 37 | target_link_directories(grid-mlpack PRIVATE ${CMAKE_PREFIX_PATH}/lib) 38 | target_link_libraries(grid-mlpack ${MLPACK_LIBRARIES} armadillo) 39 | -------------------------------------------------------------------------------- /Chapter04/mlpack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(mlpack-cluster) 3 | 4 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 5 | find_package(mlpack 4.0.1 REQUIRED) 6 | 7 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 8 | 9 | if (NOT PLOTCPP_PATH) 10 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 11 | else() 12 | message("plotcpp path is ${PLOTCPP_PATH}") 13 | endif() 14 | 15 | 16 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 17 | set(CMAKE_CXX_STANDARD 20) 18 | set(CMAKE_CXX_EXTENSIONS OFF) 19 | 20 | add_compile_options( 21 | -Wall -Wextra -msse3 -fopenmp 22 | $<$:-Ofast> 23 | $<$:-O0> 24 | $<$:-ggdb3> 25 | ) 26 | 27 | add_link_options(-fopenmp) 28 | 29 | add_compile_definitions( 30 | $<$:NDEBUG> 31 | ) 32 | 33 | include_directories(${PLOTCPP_PATH}) 34 | include_directories(${MLPACK_INCLUDE_DIR}) 35 | 36 | add_executable(mlpack-cluster "mlpack-cluster.cc") 37 | target_link_directories(mlpack-cluster PRIVATE ${CMAKE_PREFIX_PATH}/lib) 38 | target_link_libraries(mlpack-cluster ${MLPACK_LIBRARIES} armadillo) 39 | -------------------------------------------------------------------------------- /Chapter05/mlpack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(mlpack-anomaly) 3 | 4 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 5 | find_package(mlpack 4.0.1 REQUIRED) 6 | 7 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 8 | 9 | if (NOT PLOTCPP_PATH) 10 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 11 | else() 12 | message("plotcpp path is ${PLOTCPP_PATH}") 13 | endif() 14 | 15 | 16 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 17 | set(CMAKE_CXX_STANDARD 20) 18 | set(CMAKE_CXX_EXTENSIONS OFF) 19 | 20 | add_compile_options( 21 | -Wall -Wextra -msse3 -fopenmp 22 | $<$:-Ofast> 23 | $<$:-O0> 24 | $<$:-ggdb3> 25 | ) 26 | 27 | add_link_options(-fopenmp) 28 | 29 | add_compile_definitions( 30 | $<$:NDEBUG> 31 | ) 32 | 33 | include_directories(${PLOTCPP_PATH}) 34 | include_directories(${MLPACK_INCLUDE_DIR}) 35 | 36 | add_executable(mlpack-anomaly "mlpack-anomaly.cc") 37 | target_link_directories(mlpack-anomaly PRIVATE ${CMAKE_PREFIX_PATH}/lib) 38 | target_link_libraries(mlpack-anomaly ${MLPACK_LIBRARIES} armadillo) 39 | -------------------------------------------------------------------------------- /Chapter07/mlpack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(mlpack-classify) 3 | 4 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 5 | find_package(mlpack 4.0.1 REQUIRED) 6 | 7 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 8 | 9 | if (NOT PLOTCPP_PATH) 10 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 11 | else() 12 | message("plotcpp path is ${PLOTCPP_PATH}") 13 | endif() 14 | 15 | 16 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 17 | set(CMAKE_CXX_STANDARD 20) 18 | set(CMAKE_CXX_EXTENSIONS OFF) 19 | 20 | add_compile_options( 21 | -Wall -Wextra -msse3 -fopenmp 22 | $<$:-Ofast> 23 | $<$:-O0> 24 | $<$:-ggdb3> 25 | ) 26 | 27 | add_link_options(-fopenmp) 28 | 29 | add_compile_definitions( 30 | $<$:NDEBUG> 31 | ) 32 | 33 | include_directories(${PLOTCPP_PATH}) 34 | include_directories(${MLPACK_INCLUDE_DIR}) 35 | 36 | add_executable(mlpack-classify "mlpack-classify.cc") 37 | target_link_directories(mlpack-classify PRIVATE ${CMAKE_PREFIX_PATH}/lib) 38 | target_link_libraries(mlpack-classify ${MLPACK_LIBRARIES} armadillo) 39 | -------------------------------------------------------------------------------- /Chapter14/android_detection/gradle/libs.versions.toml: -------------------------------------------------------------------------------- 1 | [versions] 2 | agp = "8.5.1" 3 | kotlin = "1.9.0" 4 | coreKtx = "1.13.1" 5 | junit = "4.13.2" 6 | junitVersion = "1.2.1" 7 | espressoCore = "3.6.1" 8 | appcompat = "1.7.0" 9 | material = "1.12.0" 10 | constraintlayout = "2.1.4" 11 | 12 | [libraries] 13 | androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } 14 | junit = { group = "junit", name = "junit", version.ref = "junit" } 15 | androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } 16 | androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } 17 | androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } 18 | material = { group = "com.google.android.material", name = "material", version.ref = "material" } 19 | androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" } 20 | 21 | [plugins] 22 | android-application = { id = "com.android.application", version.ref = "agp" } 23 | jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } 24 | 25 | -------------------------------------------------------------------------------- /Chapter11/pytorch/model.cpp: -------------------------------------------------------------------------------- 1 | #include "model.h" 2 | 3 | ModelImpl::ModelImpl(const std::string& bert_model_path) 4 | : dropout_(register_module("dropout", torch::nn::Dropout(torch::nn::DropoutOptions().p(0.2)))), 5 | fc1_(register_module("fc1", torch::nn::Linear(torch::nn::LinearOptions(768, 512)))), 6 | fc2_(register_module("fc2", torch::nn::Linear(torch::nn::LinearOptions(512, 2)))) { 7 | bert_ = torch::jit::load(bert_model_path); 8 | } 9 | 10 | torch::Tensor ModelImpl::forward(at::Tensor input_ids, at::Tensor attention_masks) { 11 | std::vector inputs = {input_ids, attention_masks}; 12 | auto bert_output = bert_.forward(inputs); 13 | // Pooled output is the embedding of the [CLS] token (from Sequence output), 14 | // further processed by a Linear layer and a Tanh activation function. 15 | // The Linear layer weights are trained from the next sentence prediction 16 | // (classification) objective during pretraining. 17 | auto pooler_output = bert_output.toTuple()->elements()[1].toTensor(); 18 | auto x = fc1_(pooler_output); 19 | x = torch::nn::functional::relu(x); 20 | x = dropout_(x); 21 | x = fc2_(x); 22 | x = torch::softmax(x, /*dim=*/1); 23 | return x; 24 | } 25 | -------------------------------------------------------------------------------- /Chapter03/mlpack/cmake/Findmlpack.cmake: -------------------------------------------------------------------------------- 1 | #.rst: 2 | # Findmlpack 3 | # ------------- 4 | # 5 | # Find mlpack 6 | # 7 | # Find the mlpack C++ library 8 | # 9 | # Using mlpack:: 10 | # 11 | # find_package(mlpack REQUIRED) 12 | # include_directories(${MLPACK_INCLUDE_DIRS}) 13 | # add_executable(foo foo.cc) 14 | # 15 | # This module sets the following variables:: 16 | # 17 | # mlpack_FOUND - set to true if the library is found 18 | # MLPACK_INCLUDE_DIRS - list of required include directories 19 | # MLPACK_VERSION_MAJOR - major version number 20 | # MLPACK_VERSION_MINOR - minor version number 21 | # MLPACK_VERSION_PATCH - patch version number 22 | # MLPACK_VERSION_STRING - version number as a string (ex: "1.0.4") 23 | 24 | include(FindPackageHandleStandardArgs) 25 | 26 | message(INFO "Looking mlpack in ${CMAKE_PREFIX_PATH}") 27 | find_path(MLPACK_INCLUDE_DIR 28 | NAMES mlpack/core.hpp mlpack/prereqs.hpp 29 | PATHS "${CMAKE_PREFIX_PATH}/include/" 30 | ) 31 | 32 | find_package_handle_standard_args(mlpack 33 | REQUIRED_VARS MLPACK_INCLUDE_DIR 34 | ) 35 | 36 | if(mlpack_FOUND) 37 | set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIR}) 38 | endif() 39 | 40 | # Hide internal variables 41 | mark_as_advanced(MLPACK_INCLUDE_DIR) -------------------------------------------------------------------------------- /Chapter04/mlpack/cmake/Findmlpack.cmake: -------------------------------------------------------------------------------- 1 | #.rst: 2 | # Findmlpack 3 | # ------------- 4 | # 5 | # Find mlpack 6 | # 7 | # Find the mlpack C++ library 8 | # 9 | # Using mlpack:: 10 | # 11 | # find_package(mlpack REQUIRED) 12 | # include_directories(${MLPACK_INCLUDE_DIRS}) 13 | # add_executable(foo foo.cc) 14 | # 15 | # This module sets the following variables:: 16 | # 17 | # mlpack_FOUND - set to true if the library is found 18 | # MLPACK_INCLUDE_DIRS - list of required include directories 19 | # MLPACK_VERSION_MAJOR - major version number 20 | # MLPACK_VERSION_MINOR - minor version number 21 | # MLPACK_VERSION_PATCH - patch version number 22 | # MLPACK_VERSION_STRING - version number as a string (ex: "1.0.4") 23 | 24 | include(FindPackageHandleStandardArgs) 25 | 26 | message(INFO "Looking mlpack in ${CMAKE_PREFIX_PATH}") 27 | find_path(MLPACK_INCLUDE_DIR 28 | NAMES mlpack/core.hpp mlpack/prereqs.hpp 29 | PATHS "${CMAKE_PREFIX_PATH}/include/" 30 | ) 31 | 32 | find_package_handle_standard_args(mlpack 33 | REQUIRED_VARS MLPACK_INCLUDE_DIR 34 | ) 35 | 36 | if(mlpack_FOUND) 37 | set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIR}) 38 | endif() 39 | 40 | # Hide internal variables 41 | mark_as_advanced(MLPACK_INCLUDE_DIR) -------------------------------------------------------------------------------- /Chapter05/mlpack/cmake/Findmlpack.cmake: -------------------------------------------------------------------------------- 1 | #.rst: 2 | # Findmlpack 3 | # ------------- 4 | # 5 | # Find mlpack 6 | # 7 | # Find the mlpack C++ library 8 | # 9 | # Using mlpack:: 10 | # 11 | # find_package(mlpack REQUIRED) 12 | # include_directories(${MLPACK_INCLUDE_DIRS}) 13 | # add_executable(foo foo.cc) 14 | # 15 | # This module sets the following variables:: 16 | # 17 | # mlpack_FOUND - set to true if the library is found 18 | # MLPACK_INCLUDE_DIRS - list of required include directories 19 | # MLPACK_VERSION_MAJOR - major version number 20 | # MLPACK_VERSION_MINOR - minor version number 21 | # MLPACK_VERSION_PATCH - patch version number 22 | # MLPACK_VERSION_STRING - version number as a string (ex: "1.0.4") 23 | 24 | include(FindPackageHandleStandardArgs) 25 | 26 | message(INFO "Looking mlpack in ${CMAKE_PREFIX_PATH}") 27 | find_path(MLPACK_INCLUDE_DIR 28 | NAMES mlpack/core.hpp mlpack/prereqs.hpp 29 | PATHS "${CMAKE_PREFIX_PATH}/include/" 30 | ) 31 | 32 | find_package_handle_standard_args(mlpack 33 | REQUIRED_VARS MLPACK_INCLUDE_DIR 34 | ) 35 | 36 | if(mlpack_FOUND) 37 | set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIR}) 38 | endif() 39 | 40 | # Hide internal variables 41 | mark_as_advanced(MLPACK_INCLUDE_DIR) -------------------------------------------------------------------------------- /Chapter07/mlpack/cmake/Findmlpack.cmake: -------------------------------------------------------------------------------- 1 | #.rst: 2 | # Findmlpack 3 | # ------------- 4 | # 5 | # Find mlpack 6 | # 7 | # Find the mlpack C++ library 8 | # 9 | # Using mlpack:: 10 | # 11 | # find_package(mlpack REQUIRED) 12 | # include_directories(${MLPACK_INCLUDE_DIRS}) 13 | # add_executable(foo foo.cc) 14 | # 15 | # This module sets the following variables:: 16 | # 17 | # mlpack_FOUND - set to true if the library is found 18 | # MLPACK_INCLUDE_DIRS - list of required include directories 19 | # MLPACK_VERSION_MAJOR - major version number 20 | # MLPACK_VERSION_MINOR - minor version number 21 | # MLPACK_VERSION_PATCH - patch version number 22 | # MLPACK_VERSION_STRING - version number as a string (ex: "1.0.4") 23 | 24 | include(FindPackageHandleStandardArgs) 25 | 26 | message(INFO "Looking mlpack in ${CMAKE_PREFIX_PATH}") 27 | find_path(MLPACK_INCLUDE_DIR 28 | NAMES mlpack/core.hpp mlpack/prereqs.hpp 29 | PATHS "${CMAKE_PREFIX_PATH}/include/" 30 | ) 31 | 32 | find_package_handle_standard_args(mlpack 33 | REQUIRED_VARS MLPACK_INCLUDE_DIR 34 | ) 35 | 36 | if(mlpack_FOUND) 37 | set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIR}) 38 | endif() 39 | 40 | # Hide internal variables 41 | mark_as_advanced(MLPACK_INCLUDE_DIR) -------------------------------------------------------------------------------- /Chapter08/mlpack/cmake/Findmlpack.cmake: -------------------------------------------------------------------------------- 1 | #.rst: 2 | # Findmlpack 3 | # ------------- 4 | # 5 | # Find mlpack 6 | # 7 | # Find the mlpack C++ library 8 | # 9 | # Using mlpack:: 10 | # 11 | # find_package(mlpack REQUIRED) 12 | # include_directories(${MLPACK_INCLUDE_DIRS}) 13 | # add_executable(foo foo.cc) 14 | # 15 | # This module sets the following variables:: 16 | # 17 | # mlpack_FOUND - set to true if the library is found 18 | # MLPACK_INCLUDE_DIRS - list of required include directories 19 | # MLPACK_VERSION_MAJOR - major version number 20 | # MLPACK_VERSION_MINOR - minor version number 21 | # MLPACK_VERSION_PATCH - patch version number 22 | # MLPACK_VERSION_STRING - version number as a string (ex: "1.0.4") 23 | 24 | include(FindPackageHandleStandardArgs) 25 | 26 | message(INFO "Looking mlpack in ${CMAKE_PREFIX_PATH}") 27 | find_path(MLPACK_INCLUDE_DIR 28 | NAMES mlpack/core.hpp mlpack/prereqs.hpp 29 | PATHS "${CMAKE_PREFIX_PATH}/include/" 30 | ) 31 | 32 | find_package_handle_standard_args(mlpack 33 | REQUIRED_VARS MLPACK_INCLUDE_DIR 34 | ) 35 | 36 | if(mlpack_FOUND) 37 | set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIR}) 38 | endif() 39 | 40 | # Hide internal variables 41 | mark_as_advanced(MLPACK_INCLUDE_DIR) -------------------------------------------------------------------------------- /Chapter09/mlpack/cmake/Findmlpack.cmake: -------------------------------------------------------------------------------- 1 | #.rst: 2 | # Findmlpack 3 | # ------------- 4 | # 5 | # Find mlpack 6 | # 7 | # Find the mlpack C++ library 8 | # 9 | # Using mlpack:: 10 | # 11 | # find_package(mlpack REQUIRED) 12 | # include_directories(${MLPACK_INCLUDE_DIRS}) 13 | # add_executable(foo foo.cc) 14 | # 15 | # This module sets the following variables:: 16 | # 17 | # mlpack_FOUND - set to true if the library is found 18 | # MLPACK_INCLUDE_DIRS - list of required include directories 19 | # MLPACK_VERSION_MAJOR - major version number 20 | # MLPACK_VERSION_MINOR - minor version number 21 | # MLPACK_VERSION_PATCH - patch version number 22 | # MLPACK_VERSION_STRING - version number as a string (ex: "1.0.4") 23 | 24 | include(FindPackageHandleStandardArgs) 25 | 26 | message(INFO "Looking mlpack in ${CMAKE_PREFIX_PATH}") 27 | find_path(MLPACK_INCLUDE_DIR 28 | NAMES mlpack/core.hpp mlpack/prereqs.hpp 29 | PATHS "${CMAKE_PREFIX_PATH}/include/" 30 | ) 31 | 32 | find_package_handle_standard_args(mlpack 33 | REQUIRED_VARS MLPACK_INCLUDE_DIR 34 | ) 35 | 36 | if(mlpack_FOUND) 37 | set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIR}) 38 | endif() 39 | 40 | # Hide internal variables 41 | mark_as_advanced(MLPACK_INCLUDE_DIR) -------------------------------------------------------------------------------- /Chapter10/mlpack/cmake/Findmlpack.cmake: -------------------------------------------------------------------------------- 1 | #.rst: 2 | # Findmlpack 3 | # ------------- 4 | # 5 | # Find mlpack 6 | # 7 | # Find the mlpack C++ library 8 | # 9 | # Using mlpack:: 10 | # 11 | # find_package(mlpack REQUIRED) 12 | # include_directories(${MLPACK_INCLUDE_DIRS}) 13 | # add_executable(foo foo.cc) 14 | # 15 | # This module sets the following variables:: 16 | # 17 | # mlpack_FOUND - set to true if the library is found 18 | # MLPACK_INCLUDE_DIRS - list of required include directories 19 | # MLPACK_VERSION_MAJOR - major version number 20 | # MLPACK_VERSION_MINOR - minor version number 21 | # MLPACK_VERSION_PATCH - patch version number 22 | # MLPACK_VERSION_STRING - version number as a string (ex: "1.0.4") 23 | 24 | include(FindPackageHandleStandardArgs) 25 | 26 | message(INFO "Looking mlpack in ${CMAKE_PREFIX_PATH}") 27 | find_path(MLPACK_INCLUDE_DIR 28 | NAMES mlpack/core.hpp mlpack/prereqs.hpp 29 | PATHS "${CMAKE_PREFIX_PATH}/include/" 30 | ) 31 | 32 | find_package_handle_standard_args(mlpack 33 | REQUIRED_VARS MLPACK_INCLUDE_DIR 34 | ) 35 | 36 | if(mlpack_FOUND) 37 | set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIR}) 38 | endif() 39 | 40 | # Hide internal variables 41 | mark_as_advanced(MLPACK_INCLUDE_DIR) -------------------------------------------------------------------------------- /Chapter12/mlpack/cmake/Findmlpack.cmake: -------------------------------------------------------------------------------- 1 | #.rst: 2 | # Findmlpack 3 | # ------------- 4 | # 5 | # Find mlpack 6 | # 7 | # Find the mlpack C++ library 8 | # 9 | # Using mlpack:: 10 | # 11 | # find_package(mlpack REQUIRED) 12 | # include_directories(${MLPACK_INCLUDE_DIRS}) 13 | # add_executable(foo foo.cc) 14 | # 15 | # This module sets the following variables:: 16 | # 17 | # mlpack_FOUND - set to true if the library is found 18 | # MLPACK_INCLUDE_DIRS - list of required include directories 19 | # MLPACK_VERSION_MAJOR - major version number 20 | # MLPACK_VERSION_MINOR - minor version number 21 | # MLPACK_VERSION_PATCH - patch version number 22 | # MLPACK_VERSION_STRING - version number as a string (ex: "1.0.4") 23 | 24 | include(FindPackageHandleStandardArgs) 25 | 26 | message(INFO "Looking mlpack in ${CMAKE_PREFIX_PATH}") 27 | find_path(MLPACK_INCLUDE_DIR 28 | NAMES mlpack/core.hpp mlpack/prereqs.hpp 29 | PATHS "${CMAKE_PREFIX_PATH}/include/" 30 | ) 31 | 32 | find_package_handle_standard_args(mlpack 33 | REQUIRED_VARS MLPACK_INCLUDE_DIR 34 | ) 35 | 36 | if(mlpack_FOUND) 37 | set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIR}) 38 | endif() 39 | 40 | # Hide internal variables 41 | mark_as_advanced(MLPACK_INCLUDE_DIR) -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/assets/classes.txt: -------------------------------------------------------------------------------- 1 | 0:person 2 | 1:bicycle 3 | 2:car 4 | 3:motorcycle 5 | 4:airplane 6 | 5:bus 7 | 6:train 8 | 7:truck 9 | 8:boat 10 | 9:traffic-light 11 | 10:fire-hydrant 12 | 11:stop-sign 13 | 12:parking-meter 14 | 13:bench 15 | 14:bird 16 | 15:cat 17 | 16:dog 18 | 17:horse 19 | 18:sheep 20 | 19:cow 21 | 20:elephant 22 | 21:bear 23 | 22:zebra 24 | 23:giraffe 25 | 24:backpack 26 | 25:umbrella 27 | 26:handbag 28 | 27:tie 29 | 28:suitcase 30 | 29:frisbee 31 | 30:skis 32 | 31:snowboard 33 | 32:sports-ball 34 | 33:kite 35 | 34:baseball-bat 36 | 35:baseball-glove 37 | 36:skateboard 38 | 37:surfboard 39 | 38:tennis-racket 40 | 39:bottle 41 | 40:wine-glass 42 | 41:cup 43 | 42:fork 44 | 43:knife 45 | 44:spoon 46 | 45:bowl 47 | 46:banana 48 | 47:apple 49 | 48:sandwich 50 | 49:orange 51 | 50:broccoli 52 | 51:carrot 53 | 52:hot-dog 54 | 53:pizza 55 | 54:donut 56 | 55:cake 57 | 56:chair 58 | 57:couch 59 | 58:potted-plant 60 | 59:bed 61 | 60:dining-table 62 | 61:toilet 63 | 62:tv 64 | 63:laptop 65 | 64:mouse 66 | 65:remote 67 | 66:keyboard 68 | 67:cell-phone 69 | 68:microwave 70 | 69:oven 71 | 70:toaster 72 | 71:sink 73 | 72:refrigerator 74 | 73:book 75 | 74:clock 76 | 75:vase 77 | 76:scissors 78 | 77:teddy-bear 79 | 78:hair-drier 80 | 79:toothbrush -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/cpp/yolo.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | struct YOLOResult { 12 | int class_index; 13 | std::string class_name; 14 | float score; 15 | cv::Rect rect; 16 | }; 17 | 18 | class YOLO { 19 | public: 20 | explicit YOLO(AAssetManager *asset_manager); 21 | 22 | ~YOLO() = default; 23 | 24 | YOLO(const YOLO &) = delete; 25 | 26 | YOLO &operator=(const YOLO &) = delete; 27 | 28 | YOLO(YOLO &&) = delete; 29 | 30 | YOLO &operator==(YOLO &&) = delete; 31 | 32 | std::vector detect(const cv::Mat &image); 33 | 34 | private: 35 | void load_classes(std::istream &stream); 36 | 37 | void output2results(const torch::Tensor &output, 38 | float img_scale_x, 39 | float img_scale_y); 40 | 41 | std::vector non_max_suppression(); 42 | 43 | private: 44 | using Classes = std::map; 45 | Classes classes_; 46 | torch::jit::mobile::Module model_; 47 | cv::Mat rgb_img_; 48 | std::vector results_; 49 | }; 50 | 51 | 52 | -------------------------------------------------------------------------------- /Chapter06/tapkee/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(tapkee-dr) 3 | 4 | find_package(OpenMP) 5 | find_package(Eigen3 3.4.0 REQUIRED) 6 | find_package(fmt) 7 | 8 | set(TAPKEE_PATH "" CACHE PATH "path to tapkee install dir") 9 | if (NOT TAPKEE_PATH) 10 | message(FATAL_ERROR "Missing tapkee include path, please specify TAPKEE_PATH") 11 | else() 12 | message("tapkee path is ${TAPKEE_PATH}") 13 | endif() 14 | 15 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 16 | if (NOT PLOTCPP_PATH) 17 | message(FATAL_ERROR "Missing plotcpp include path, please specify PLOTCPP_PATH") 18 | else() 19 | message("plotcpp path is ${PLOTCPP_PATH}") 20 | endif() 21 | 22 | 23 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 24 | set(CMAKE_CXX_STANDARD 20) 25 | set(CMAKE_CXX_EXTENSIONS OFF) 26 | 27 | add_compile_options( 28 | -Wall -Wextra -msse3 -fopenmp 29 | $<$:-Ofast> 30 | $<$:-O0> 31 | $<$:-ggdb3> 32 | ) 33 | 34 | add_compile_definitions( 35 | $<$:NDEBUG> 36 | ) 37 | 38 | include_directories(${PLOTCPP_PATH}) 39 | include_directories(${TAPKEE_PATH}) 40 | 41 | add_executable(tapkee-dr tapkee-dr.cc util.cc) 42 | target_link_libraries (tapkee-dr Eigen3::Eigen OpenMP::OpenMP_CXX fmt::fmt) 43 | -------------------------------------------------------------------------------- /Chapter07/flashlight/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(fl-classify) 3 | 4 | find_package(flashlight 0.4.0 REQUIRED) 5 | 6 | set(CSV_LIB_PATH "" CACHE PATH "Path to csv library include dir") 7 | 8 | if (NOT CSV_LIB_PATH) 9 | message(FATAL_ERROR "Missigng CSV lib install path, please specify CSV_LIB_PATH") 10 | else() 11 | message("CSV lib path is ${CSV_LIB_PATH}") 12 | endif() 13 | 14 | set(PLOTCPP_PATH "" CACHE PATH "path to poltcpp install dir") 15 | 16 | if (NOT PLOTCPP_PATH) 17 | message(FATAL_ERROR "Missigng plotcpp include path, please specify PLOTCPP_PATH") 18 | else() 19 | message("plotcpp path is ${PLOTCPP_PATH}") 20 | endif() 21 | 22 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 23 | set(CMAKE_CXX_STANDARD 20) 24 | set(CMAKE_CXX_EXTENSIONS OFF) 25 | 26 | set(CMAKE_VERBOSE_MAKEFILE ON) 27 | 28 | add_compile_options( 29 | -Wall -Wextra -msse3 -fopenmp 30 | $<$:-Ofast> 31 | $<$:-O0> 32 | $<$:-ggdb3> 33 | ) 34 | 35 | add_compile_definitions( 36 | $<$:NDEBUG> 37 | ) 38 | 39 | include_directories(${PLOTCPP_PATH}) 40 | include_directories(${CSV_LIB_PATH}) 41 | 42 | add_executable(fl-classify fl_classify.cc) 43 | target_link_libraries(fl-classify flashlight::flashlight) 44 | -------------------------------------------------------------------------------- /Chapter12/mlpack/mlpack-save.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace mlpack; 8 | 9 | using ModelType = FFN; 10 | 11 | ModelType make_model() { 12 | MeanSquaredError loss; 13 | ConstInitialization init(0.); 14 | ModelType model(loss, init); 15 | model.Add(8); 16 | model.Add(); 17 | model.Add(16); 18 | model.Add(); 19 | model.Add(32); 20 | model.Add(); 21 | model.Add(1); 22 | return model; 23 | } 24 | 25 | int main() { 26 | size_t n = 10000; 27 | arma::mat x = arma::randn(n).t(); 28 | arma::mat y = x * 0.3f + 0.4f; 29 | 30 | // Define optimizer 31 | ens::Adam optimizer; 32 | 33 | auto model = make_model(); 34 | 35 | model.Train(x, y, optimizer, ens::ProgressBar()); 36 | 37 | data::Save("model.bin", model.Parameters(), true); 38 | 39 | auto new_model = make_model(); 40 | data::Load("model.bin", new_model.Parameters(), true); 41 | 42 | arma::mat predictions; 43 | new_model.Predict(x, predictions); 44 | 45 | auto mse = SquaredEuclideanDistance::Evaluate(predictions, y) / (y.n_elem); 46 | std::cout << "Final MSE: " << mse << std::endl; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Chapter03/flashlight/search_optuna.py: -------------------------------------------------------------------------------- 1 | import optuna 2 | import subprocess 3 | import argparse 4 | 5 | 6 | if __name__ == "__main__": 7 | parser = argparse.ArgumentParser(description="PolyFit") 8 | parser.add_argument( 9 | "binary_path", type=str, help="path to the optimization process binary" 10 | ) 11 | args = parser.parse_args() 12 | 13 | search_space = { 14 | "learning_rate": [0.01, 0.025, 0.045], 15 | "polynomial_degree": [8, 14, 16], 16 | "batch_size": [16, 32, 64], 17 | } 18 | study = optuna.create_study( 19 | study_name="PolyFit", 20 | direction="minimize", 21 | sampler=optuna.samplers.GridSampler(search_space), 22 | ) 23 | 24 | def objective(trial: optuna.trial.Trial): 25 | lr = trial.suggest_float("learning_rate", low=0.01, high=0.05) 26 | d = trial.suggest_int("polynomial_degree", low=8, high=16) 27 | bs = trial.suggest_int("batch_size", low=16, high=64) 28 | 29 | print( 30 | f"Start training with: learning_rate=f{lr}, polynomial_degree={d}, batch_size={bs}\n" 31 | ) 32 | 33 | result = subprocess.run( 34 | [args.binary_path, str(d), str(lr), str(bs)], stdout=subprocess.PIPE 35 | ) 36 | mse = float(result.stdout) 37 | return mse 38 | 39 | study.optimize(objective) 40 | print(f"Best value: {study.best_value} (params: {study.best_params})\n") 41 | -------------------------------------------------------------------------------- /Chapter06/tapkee/util.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "util.h" 3 | 4 | // The code was taken from the CLI util of the Tapkee library 5 | 6 | tapkee::DenseMatrix read_data(const std::string& file_name, char delimiter) { 7 | using namespace std; 8 | string str; 9 | vector> input_data; 10 | ifstream ifs(file_name); 11 | while (ifs) { 12 | getline(ifs, str); 13 | 14 | istringstream ss(str); 15 | if (str.size()) { 16 | vector row; 17 | while (ss) { 18 | string value_string; 19 | if (!getline(ss, value_string, delimiter)) 20 | break; 21 | istringstream value_stream(value_string); 22 | tapkee::ScalarType value; 23 | if (value_stream >> value) 24 | row.push_back(value); 25 | } 26 | input_data.push_back(row); 27 | } 28 | } 29 | 30 | if (!input_data.empty()) { 31 | tapkee::DenseMatrix fm(input_data.size(), input_data[0].size()); 32 | for (int i = 0; i < fm.rows(); i++) { 33 | if (static_cast(input_data[i].size()) != fm.cols()) { 34 | stringstream ss; 35 | ss << "Wrong data at line " << i; 36 | throw std::runtime_error(ss.str()); 37 | } 38 | for (int j = 0; j < fm.cols(); j++) 39 | fm(i, j) = input_data[i][j]; 40 | } 41 | return fm; 42 | } else { 43 | return tapkee::DenseMatrix(0, 0); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 17 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Chapter14/android_detection/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. For more details, visit 12 | # https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Kotlin code style for this project: "official" or "obsolete": 19 | kotlin.code.style=official 20 | # Enables namespacing of each library's R class so that its R class includes only the 21 | # resources declared in the library itself and none from the library's dependencies, 22 | # thereby reducing the size of the R class for that library 23 | android.nonTransitiveRClass=true -------------------------------------------------------------------------------- /Chapter11/pytorch/imdbreader.cc: -------------------------------------------------------------------------------- 1 | #include "imdbreader.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace fs = std::filesystem; 9 | 10 | ImdbReader::ImdbReader(const std::string& root_path) { 11 | auto root = fs::path(root_path); 12 | auto neg_path = root / "neg"; 13 | auto pos_path = root / "pos"; 14 | if (fs::exists(neg_path) && fs::exists(pos_path)) { 15 | auto neg = std::async(std::launch::async, 16 | [&]() { read_directory(neg_path, neg_samples_); }); 17 | auto pos = std::async(std::launch::async, 18 | [&]() { read_directory(pos_path, pos_samples_); }); 19 | neg.get(); 20 | pos.get(); 21 | } else { 22 | throw std::invalid_argument("ImdbReader incorrect path"); 23 | } 24 | } 25 | 26 | const std::string& ImdbReader::get_pos(size_t index) const { 27 | return pos_samples_.at(index); 28 | } 29 | 30 | const std::string& ImdbReader::get_neg(size_t index) const { 31 | return neg_samples_.at(index); 32 | } 33 | 34 | size_t ImdbReader::get_pos_size() const { 35 | return pos_samples_.size(); 36 | } 37 | 38 | size_t ImdbReader::get_neg_size() const { 39 | return neg_samples_.size(); 40 | } 41 | 42 | void ImdbReader::read_directory(const std::string& path, Reviews& reviews) { 43 | for (auto& entry : fs::directory_iterator(path)) { 44 | if (fs::is_regular_file(entry)) { 45 | std::ifstream file(entry.path()); 46 | if (file) { 47 | std::stringstream buffer; 48 | buffer << file.rdbuf(); 49 | reviews.push_back(buffer.str()); 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22.1) 2 | project("object-detection") 3 | 4 | set(CMAKE_VERBOSE_MAKEFILE on) 5 | set(CMAKE_CXX_STANDARD 20) 6 | 7 | # find_package can be used only if you have protobuf installed 8 | # find_package(Torch REQUIRED) 9 | 10 | set(Torch_DIR CACHE STRING "") 11 | set(Torch_LIBS_DIR ${Torch_DIR}/lib/) 12 | set(Torch_LIBS ${Torch_DIR}/lib/libtorch_cpu.so ${Torch_DIR}/lib/libc10.so) 13 | set(Torch_INCLUDE_DIRS ${Torch_DIR}/include) 14 | 15 | set(OpenCV_DIR CACHE STRING "") 16 | find_package(OpenCV REQUIRED) 17 | 18 | add_compile_options( 19 | -Wall -Wextra 20 | $<$:-Ofast> 21 | $<$:-O0> 22 | $<$:-ggdb3> 23 | ) 24 | 25 | add_compile_definitions( 26 | $<$:NDEBUG> 27 | ) 28 | 29 | include_directories(${ANDROID_NDK}/sources/android/native_app_glue 30 | ${COMMON_SOURCE_DIR} 31 | ${OpenCV_INCLUDE_DIRS} 32 | ${Torch_INCLUDE_DIRS}) 33 | 34 | # build native_app_glue as a static lib 35 | add_library(app_glue STATIC 36 | ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c) 37 | 38 | set(CMAKE_SHARED_LINKER_FLAGS 39 | "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate") 40 | 41 | set(SOURCES 42 | native-lib.cpp 43 | detector.cpp 44 | yolo.cpp 45 | ) 46 | 47 | add_library(${CMAKE_PROJECT_NAME} SHARED ${SOURCES}) 48 | 49 | target_link_libraries(${CMAKE_PROJECT_NAME} 50 | android 51 | m 52 | log 53 | app_glue 54 | camera2ndk 55 | mediandk 56 | opencv_java 57 | ${Torch_LIBS}) -------------------------------------------------------------------------------- /Chapter01/dlib_samples/linreg_dlib.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | float func(float x) { 8 | return 4.f + 0.3f * x; // line coeficients 9 | } 10 | 11 | using SampleType = dlib::matrix; 12 | using KernelType = dlib::linear_kernel; 13 | 14 | int main() { 15 | using namespace dlib; 16 | size_t n = 1000; 17 | std::vector> x(n); 18 | std::vector y(n); 19 | 20 | std::random_device rd; 21 | std::mt19937 re(rd()); 22 | std::uniform_real_distribution dist(-1.5, 1.5); 23 | 24 | // generate data 25 | for (size_t i = 0; i < n; ++i) { 26 | x[i].set_size(1, 1); 27 | x[i](0, 0) = i; 28 | 29 | y[i] = func(i) + dist(re); 30 | } 31 | 32 | // // normalize data 33 | vector_normalizer> normalizer_x; 34 | // let the normalizer learn the mean and standard deviation of the samples 35 | normalizer_x.train(x); 36 | // now normalize each sample 37 | for (size_t i = 0; i < x.size(); ++i) { 38 | x[i] = normalizer_x(x[i]); 39 | } 40 | 41 | krr_trainer trainer; 42 | trainer.set_kernel(KernelType()); 43 | decision_function df = trainer.train(x, y); 44 | 45 | // Generate new data 46 | std::cout << "Original data \n"; 47 | std::vector> new_x(5); 48 | for (size_t i = 0; i < 5; ++i) { 49 | new_x[i].set_size(1, 1); 50 | new_x[i](0, 0) = i; 51 | new_x[i] = normalizer_x(new_x[i]); 52 | std::cout << func(i) << std::endl; 53 | } 54 | 55 | std::cout << "Predictions \n"; 56 | for (auto& v : new_x) { 57 | auto prediction = df(v); 58 | std::cout << prediction << std::endl; 59 | } 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Chapter08/eigen/data_loader.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_LOADER_H 2 | #define DATA_LOADER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using Movies = std::map; 11 | 12 | using UserRate = std::pair; 13 | using Ratings = std::unordered_map>; 14 | 15 | Movies LoadMovies(const std::string& path) { 16 | Movies movies; 17 | int32_t row = 0; 18 | std::ifstream indata(path); 19 | if (indata) { 20 | std::string line; 21 | 22 | while (std::getline(indata, line)) { 23 | if (row != 0) { 24 | std::stringstream line_stream(line); 25 | std::string cell; 26 | std::getline(line_stream, cell, ','); 27 | auto id = std::stoi(cell); 28 | std::getline(line_stream, cell, ','); 29 | movies.insert({id, cell}); 30 | } 31 | ++row; 32 | } 33 | } 34 | return movies; 35 | } 36 | 37 | Ratings LoadRatings(const std::string& path) { 38 | Ratings ratings; 39 | std::ifstream indata(path); 40 | if (indata) { 41 | std::string line; 42 | int32_t row = 0; 43 | while (std::getline(indata, line)) { 44 | if (row != 0) { 45 | std::stringstream line_stream(line); 46 | std::string cell; 47 | std::getline(line_stream, cell, ','); 48 | auto user_id = std::stoi(cell); 49 | std::getline(line_stream, cell, ','); 50 | auto movie_id = std::stoi(cell); 51 | std::getline(line_stream, cell, ','); 52 | auto rating = std::stof(cell); 53 | ratings[user_id].push_back({movie_id, rating}); 54 | } 55 | ++row; 56 | } 57 | } 58 | return ratings; 59 | } 60 | 61 | #endif // DATA_LOADER_H 62 | -------------------------------------------------------------------------------- /Chapter01/arrayfire_samples/linreg_af.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int main() { 13 | size_t n = 1000; 14 | 15 | auto predict = [](auto& v, auto& w) { 16 | return af::batchFunc(v, w, [](const auto& a, const auto& b) { return af::sum(a * b, /*dim*/ 1); }); 17 | }; 18 | 19 | // generate training data 20 | auto x = af::iota(af::dim4(n)) / n; 21 | x += af::randu(n); // add some noise 22 | x = af::join(1, af::constant(1, n), x); // add a bias that is always 1 23 | 24 | af::dim4 weights_dim(1, 2); 25 | auto line_weights = af::array(weights_dim, {4.f, 0.3f}); 26 | auto y = predict(x, line_weights); 27 | 28 | // train classifier 29 | auto train_weights = af::constant(0.f, weights_dim, af::dtype::f32); 30 | af::array j, dj; // cost value and its gradient 31 | float lr = 0.1f; // learning rate 32 | int n_iter = 300; 33 | for (int i = 0; i < n_iter; ++i) { 34 | std::cout << "Iteration " << i << ":\n"; 35 | // get the cost 36 | auto h = predict(x, train_weights); 37 | auto diff = (y - h); 38 | 39 | auto j = af::sum(diff * diff) / n; 40 | af_print(j); 41 | 42 | // find the gradient of cost 43 | auto dm = (-2.f / n) * af::sum(x.col(1) * diff); 44 | auto dc = (-2.f / n) * af::sum(diff); 45 | auto dj = af::join(1, dc, dm); 46 | 47 | // update the parameters via gradient descent 48 | train_weights = train_weights - lr * dj; 49 | } 50 | 51 | std::cout << "Learned line weights:\n"; 52 | af_print(train_weights); 53 | 54 | return 0; 55 | }; 56 | -------------------------------------------------------------------------------- /Chapter02/csv/mlpack/csv_mlpack.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace fs = std::filesystem; 8 | 9 | using namespace mlpack; 10 | 11 | int main(int argc, char** argv) { 12 | try { 13 | if (argc > 1) { 14 | if (fs::exists(argv[1])) { 15 | arma::mat dataset; 16 | mlpack::data::DatasetInfo info; 17 | data::Load(argv[1], dataset, info, /*fail with error*/ true); 18 | std::cout << "Number of dimensions: " << info.Dimensionality() << std::endl; 19 | std::cout << "Number of classes: " << info.NumMappings(4) << std::endl; 20 | 21 | arma::Row labels; 22 | labels = arma::conv_to>::from(dataset.row(dataset.n_rows - 1)); 23 | dataset.shed_row(dataset.n_rows - 1); 24 | 25 | data::MinMaxScaler min_max_scaler; 26 | min_max_scaler.Fit(dataset); 27 | 28 | arma::mat scaled_dataset; 29 | min_max_scaler.Transform(dataset, scaled_dataset); 30 | 31 | std::cout << scaled_dataset << std::endl; 32 | 33 | min_max_scaler.InverseTransform(scaled_dataset, dataset); 34 | 35 | data::StandardScaler standard_scaler; 36 | standard_scaler.Fit(dataset); 37 | 38 | standard_scaler.Transform(dataset, scaled_dataset); 39 | 40 | std::cout << scaled_dataset << std::endl; 41 | 42 | standard_scaler.InverseTransform(scaled_dataset, dataset); 43 | } else { 44 | std::cerr << "Invalid file path " << argv[1] << "\n"; 45 | } 46 | } else { 47 | std::cerr << "Please specify path to the dataset\n"; 48 | } 49 | } catch (const std::exception& err) { 50 | std::cerr << err.what() << std::endl; 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Chapter02/json/json.cc: -------------------------------------------------------------------------------- 1 | #include "reviewsreader.h" 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | namespace fs = std::filesystem; 10 | 11 | int main(int argc, char** argv) { 12 | if (argc > 1) { 13 | auto file_path = fs::path(argv[1]); 14 | if (fs::exists(file_path)) { 15 | auto papers = ReadPapersReviews(file_path); 16 | // create matrices 17 | Eigen::MatrixXi x_data(papers.size(), 3); 18 | Eigen::MatrixXi y_data(papers.size(), 1); 19 | Eigen::Index i = 0; 20 | for (const auto& p : papers) { 21 | if (p.preliminary_decision == "accept") 22 | y_data(i, 0) = 1; 23 | else 24 | y_data(i, 0) = 0; 25 | 26 | if (!p.reviews.empty()) { 27 | int64_t confidence_avg = 0; 28 | int64_t evaluation_avg = 0; 29 | int64_t orientation_avg = 0; 30 | for (const auto& r : p.reviews) { 31 | confidence_avg += std::stoi(r.confidence); 32 | evaluation_avg += std::stoi(r.evaluation); 33 | orientation_avg += std::stoi(r.orientation); 34 | } 35 | int64_t reviews_num = static_cast(p.reviews.size()); 36 | x_data(i, 0) = static_cast(confidence_avg / reviews_num); 37 | x_data(i, 1) = static_cast(evaluation_avg / reviews_num); 38 | x_data(i, 2) = static_cast(orientation_avg / reviews_num); 39 | } 40 | ++i; 41 | } 42 | std::cout << x_data << std::endl; 43 | std::cout << y_data << std::endl; 44 | 45 | } else { 46 | std::cout << "File path is incorrect " << file_path << "\n"; 47 | } 48 | } else { 49 | std::cout << "Please provide a path to a dataset file\n"; 50 | } 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Chapter10/dlib/mlp-dlib.cc: -------------------------------------------------------------------------------- 1 | #include "../data/data.h" 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | int main() { 10 | using namespace dlib; 11 | 12 | size_t n = 10000; 13 | size_t seed = 45345; 14 | auto data = GenerateData(-1.5, 1.5, n, seed, false); 15 | 16 | std::vector> x(n); 17 | std::vector> y_data(n); 18 | 19 | for (size_t i = 0; i < n; ++i) { 20 | x[i].set_size(1, 1); 21 | x[i](0, 0) = data.first[i]; 22 | 23 | y_data[i].set_size(1, 1); 24 | y_data[i](0, 0) = data.second[i]; 25 | } 26 | 27 | // normalize data 28 | vector_normalizer> normalizer_x; 29 | vector_normalizer> normalizer_y; 30 | 31 | // let the normalizer learn the mean and standard deviation of the samples 32 | normalizer_x.train(x); 33 | normalizer_y.train(y_data); 34 | 35 | std::vector y(n); 36 | 37 | // now normalize each sample 38 | for (size_t i = 0; i < x.size(); ++i) { 39 | x[i] = normalizer_x(x[i]); 40 | y_data[i] = normalizer_y(y_data[i]); 41 | y[i] = static_cast(y_data[i](0, 0)); 42 | } 43 | 44 | using NetworkType = loss_mean_squared< 45 | fc<1, htan>>>>>>>>>; 46 | NetworkType network; 47 | float weight_decay = 0.0001f; 48 | float momentum = 0.5f; 49 | sgd solver(weight_decay, momentum); 50 | dnn_trainer trainer(network, solver); 51 | trainer.set_learning_rate(0.01); 52 | trainer.set_learning_rate_shrink_factor(1); // disable learning rate changes 53 | trainer.set_mini_batch_size(64); 54 | trainer.set_max_num_epochs(500); 55 | trainer.be_verbose(); 56 | trainer.train(x, y); 57 | network.clean(); 58 | 59 | // auto predictions = network(new_x); 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/res/drawable/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /env_scripts/install_android.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -x 3 | set -e 4 | 5 | START_DIR=$(pwd) 6 | mkdir $START_DIR/android 7 | cd $START_DIR/android 8 | 9 | wget https://github.com/opencv/opencv/releases/download/4.10.0/opencv-4.10.0-android-sdk.zip 10 | unzip opencv-4.10.0-android-sdk.zip 11 | 12 | wget https://dl.google.com/android/repository/commandlinetools-linux-9477386_latest.zip 13 | unzip commandlinetools-linux-9477386_latest.zip 14 | 15 | 16 | # Set export JAVA_HOME variable to point where java SDK is installed for example: 17 | # export JAVA_HOME=~/android-studio/jbr 18 | 19 | yes | ./cmdline-tools/bin/sdkmanager --sdk_root=$ANDROID_SDK_ROOT "cmdline-tools;latest" 20 | yes | ./cmdline-tools/latest/bin/sdkmanager --licenses 21 | yes | ./cmdline-tools/latest/bin/sdkmanager "platform-tools" "tools" 22 | yes | ./cmdline-tools/latest/bin/sdkmanager "platforms;android-35" 23 | yes | ./cmdline-tools/latest/bin/sdkmanager "build-tools;35.0.0" 24 | yes | ./cmdline-tools/latest/bin/sdkmanager "system-images;android-35;google_apis;arm64-v8a" 25 | yes | ./cmdline-tools/latest/bin/sdkmanager --install "ndk;26.1.10909125" 26 | 27 | git clone https://github.com/pytorch/pytorch.git 28 | cd pytorch/ 29 | git checkout v2.3.1 30 | git submodule update --init --recursive 31 | 32 | export ANDROID_NDK=$START_DIR/android/ndk/26.1.10909125 33 | export ANDROID_ABI='arm64-v8a' 34 | export ANDROID_STL_SHARED=1 35 | 36 | $START_DIR/android/pytorch/scripts/build_android.sh \ 37 | -DBUILD_SHARED_LIBS=ON \ 38 | -DUSE_VULKAN=OFF \ 39 | -DCMAKE_PREFIX_PATH=$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())') \ 40 | -DPYTHON_EXECUTABLE=$(python -c 'import sys; print(sys.executable)') \ 41 | 42 | # don't forget to upadte android project buils.gradle.kts file with next variables: 43 | # val pytorchDir = "$START_DIR/android/pytorch/build_android/install/" 44 | # val opencvDir = "$START_DIR/android/OpenCV-android-sdk/sdk/native/jni/" 45 | 46 | 47 | -------------------------------------------------------------------------------- /Chapter10/mlpack/mlp_mlpack.cc: -------------------------------------------------------------------------------- 1 | #include "../data/data.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace mlpack; 10 | 11 | int main() { 12 | 13 | size_t n = 10000; 14 | size_t seed = 45345; 15 | auto data = GenerateData(-1.5, 1.5, n, seed, false); 16 | 17 | arma::mat x = arma::mat(data.first).t(); 18 | arma::mat y = arma::mat(data.second).t(); 19 | 20 | // Normalize data 21 | arma::mat scaled_x; 22 | arma::mat scaled_y; 23 | 24 | data::MinMaxScaler x_scaler; 25 | x_scaler.Fit(x); 26 | x_scaler.Transform(x, scaled_x); 27 | 28 | data::MinMaxScaler y_scaler; 29 | y_scaler.Fit(y); 30 | y_scaler.Transform(y, scaled_y); 31 | 32 | // Define model 33 | MeanSquaredError loss; 34 | ConstInitialization init(0.); 35 | FFN model(loss, init); 36 | model.Add(8); 37 | model.Add(); 38 | model.Add(16); 39 | model.Add(); 40 | model.Add(32); 41 | model.Add(); 42 | model.Add(1); 43 | 44 | // Define optimizer 45 | size_t epochs = 100; 46 | ens::MomentumSGD optimizer(/*stepSize=*/0.01, 47 | /*batchSize= */ 64, 48 | /*maxIterations= */ epochs * x.n_cols, 49 | /*tolerance=*/1e-10, 50 | /*shuffle=*/false); 51 | 52 | 53 | ens::StoreBestCoordinates best_params; 54 | 55 | model.Train(scaled_x, scaled_y, optimizer, ens::ProgressBar(), ens::EarlyStopAtMinLoss(20), best_params); 56 | 57 | // model.Parameters() = best_params.BestCoordinates(); 58 | 59 | arma::mat predictions; 60 | model.Predict(scaled_x, predictions); 61 | 62 | auto mse = SquaredEuclideanDistance::Evaluate(predictions, scaled_y) / (scaled_y.n_elem); 63 | std::cout << "Final MSE: " << mse << std::endl; 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/cpp/detector.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "yolo.h" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | class ObjectDetector { 15 | public: 16 | explicit ObjectDetector(android_app *app); 17 | 18 | ~ObjectDetector(); 19 | 20 | ObjectDetector(const ObjectDetector &) = delete; 21 | 22 | ObjectDetector &operator=(const ObjectDetector &) = delete; 23 | 24 | ObjectDetector(ObjectDetector &&) = delete; 25 | 26 | ObjectDetector &operator=(ObjectDetector &&) = delete; 27 | 28 | void configure_resources(); 29 | 30 | // should be called when permissions were granted and there is suitable camera device 31 | void allow_camera_session(std::string_view camera_id); 32 | 33 | void release_resources(); 34 | 35 | void draw_frame(); 36 | 37 | private: 38 | [[nodiscard]] bool is_session_allowed() const; 39 | 40 | void create_camera(); 41 | 42 | void delete_camera(); 43 | 44 | // camera capture properties are configured here 45 | void create_image_reader(); 46 | 47 | void delete_image_reader(); 48 | 49 | void create_session(); 50 | 51 | void delete_session(); 52 | 53 | void process_image(ANativeWindow_Buffer *buf, AImage *image); 54 | 55 | private: 56 | android_app *android_app_{nullptr}; 57 | std::string camera_id_; 58 | ACameraManager *camera_mgr_{nullptr}; 59 | ACameraDevice *camera_device_{nullptr}; 60 | AImageReader *image_reader_{nullptr}; 61 | 62 | ACaptureSessionOutput *session_output_{nullptr}; 63 | ACaptureSessionOutputContainer *output_container_{nullptr}; 64 | ACameraOutputTarget *output_target_{nullptr}; 65 | ACaptureRequest *capture_request_{nullptr}; 66 | ACameraCaptureSession *capture_session_{nullptr}; 67 | 68 | int32_t width_{800}; 69 | int32_t height_{600}; 70 | int32_t orientation_{0}; 71 | 72 | cv::Mat rgba_img_; 73 | std::shared_ptr yolo_; 74 | }; 75 | -------------------------------------------------------------------------------- /Chapter10/pytorch/lenet5.cpp: -------------------------------------------------------------------------------- 1 | #include "lenet5.h" 2 | 3 | static std::vector k_size = {2, 2}; 4 | static std::vector p_size = {0, 0}; 5 | static c10::optional divisor_override; 6 | 7 | LeNet5Impl::LeNet5Impl() { 8 | conv_ = torch::nn::Sequential( 9 | torch::nn::Conv2d(torch::nn::Conv2dOptions(1, 6, 5)), 10 | torch::nn::Functional(torch::tanh), 11 | torch::nn::Functional(torch::avg_pool2d, 12 | /*kernel_size*/ torch::IntArrayRef(k_size), 13 | /*stride*/ torch::IntArrayRef(k_size), 14 | /*padding*/ torch::IntArrayRef(p_size), 15 | /*ceil_mode*/ false, 16 | /*count_include_pad*/ false, 17 | divisor_override), 18 | torch::nn::Conv2d(torch::nn::Conv2dOptions(6, 16, 5)), 19 | torch::nn::Functional(torch::tanh), 20 | torch::nn::Functional(torch::avg_pool2d, 21 | /*kernel_size*/ torch::IntArrayRef(k_size), 22 | /*stride*/ torch::IntArrayRef(k_size), 23 | /*padding*/ torch::IntArrayRef(p_size), 24 | /*ceil_mode*/ false, 25 | /*count_include_pad*/ false, 26 | divisor_override), 27 | torch::nn::Conv2d(torch::nn::Conv2dOptions(16, 120, 5)), 28 | torch::nn::Functional(torch::tanh)); 29 | register_module("conv", conv_); 30 | 31 | full_ = torch::nn::Sequential( 32 | torch::nn::Linear(torch::nn::LinearOptions(120, 84)), 33 | torch::nn::Functional(torch::tanh), 34 | torch::nn::Linear(torch::nn::LinearOptions(84, 10))); 35 | register_module("full", full_); 36 | } 37 | 38 | torch::Tensor LeNet5Impl::forward(at::Tensor x) { 39 | auto output = conv_->forward(x); 40 | output = output.view({x.size(0), -1}); 41 | output = full_->forward(output); 42 | // not all of the functions can be used with torch::nn::Functional class 43 | // because of same names 44 | output = torch::log_softmax(output, -1); 45 | return output; 46 | } 47 | -------------------------------------------------------------------------------- /Chapter02/csv/dlib/csv_dlib.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | namespace fs = std::filesystem; 10 | 11 | int main(int argc, char** argv) { 12 | using namespace dlib; 13 | if (argc > 1) { 14 | if (fs::exists(argv[1])) { 15 | std::stringstream str_stream; 16 | { 17 | // replace categorial values with numeric ones 18 | std::ifstream data_stream(argv[1]); 19 | std::string data_string((std::istreambuf_iterator(data_stream)), 20 | std::istreambuf_iterator()); 21 | 22 | // replace string labels, because SharkML parssr can't handle strings 23 | data_string = 24 | std::regex_replace(data_string, std::regex("Iris-setosa"), "1"); 25 | data_string = 26 | std::regex_replace(data_string, std::regex("Iris-versicolor"), "2"); 27 | data_string = 28 | std::regex_replace(data_string, std::regex("Iris-virginica"), "3"); 29 | 30 | str_stream << data_string; 31 | } 32 | 33 | matrix data; 34 | str_stream >> data; 35 | 36 | std::cout << data << std::endl; 37 | 38 | matrix x_data = subm(data, 0, 0, data.nr(), data.nc() - 1); 39 | 40 | std::vector> samples; 41 | for (int r = 0; r < x_data.nr(); ++r) { 42 | samples.push_back(rowm(x_data, r)); 43 | } 44 | 45 | // Normalization 46 | // Standardization 47 | matrix m(mean(mat(samples))); 48 | matrix sd(reciprocal(stddev(mat(samples)))); 49 | for (size_t i = 0; i < samples.size(); ++i) 50 | samples[i] = pointwise_multiply(samples[i] - m, sd); 51 | std::cout << mat(samples) << std::endl; 52 | 53 | // Another approach 54 | // vector_normalizer> normalizer; 55 | // samples normalizer.train(samples); 56 | // samples = normalizer(samples); 57 | } else { 58 | std::cerr << "Invalid file path " << argv[1] << std::endl; 59 | } 60 | } else { 61 | std::cerr << "Please provide a path to a dataset\n"; 62 | } 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Chapter01/eigen_samples/linreg_eigen.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | std::pair GenerateData(size_t n) { 10 | std::vector x_data(n); 11 | std::iota(x_data.begin(), x_data.end(), 0); 12 | std::vector y_data(n); 13 | std::iota(y_data.begin(), y_data.end(), 0); 14 | 15 | // mutate data 16 | std::random_device rd; 17 | std::mt19937 re(rd()); 18 | std::uniform_real_distribution dist(-1.5f, 1.5f); 19 | 20 | for (auto& x : x_data) { 21 | x += dist(re); // add noise 22 | } 23 | 24 | for (auto& y : y_data) { 25 | y += dist(re); // add noise 26 | } 27 | 28 | // Make result 29 | Eigen::Map x(x_data.data(), static_cast(n), 1); 30 | Eigen::Map y(y_data.data(), static_cast(n), 1); 31 | 32 | return {x, y}; 33 | } 34 | 35 | int main() { 36 | size_t n = 1000; 37 | // generate training data 38 | Eigen::MatrixXf x1, y; 39 | std::tie(x1, y) = GenerateData(n); 40 | Eigen::MatrixXf x0 = Eigen::MatrixXf::Ones(n, 1); 41 | // setup line coeficients y = b(4) + k(0.3)*x 42 | y.array() *= 0.3f; 43 | y.array() += 4.f; 44 | Eigen::MatrixXf x(n, 2); 45 | x << x0, x1; 46 | 47 | // train estimator 48 | Eigen::LeastSquaresConjugateGradient gd; 49 | gd.setMaxIterations(100); 50 | gd.setTolerance(0.001f); 51 | gd.compute(x); 52 | Eigen::VectorXf b = gd.solve(y); 53 | std::cout << "Estimated parameters vector : \n" << b << std::endl; 54 | 55 | // normal equations 56 | Eigen::VectorXf b_norm = (x.transpose() * x).ldlt().solve(x.transpose() * y); 57 | std::cout << "Estimated with normal equation parameters vector : " << b_norm 58 | << std::endl; 59 | 60 | // predict 61 | Eigen::MatrixXf new_x(5, 2); 62 | new_x << 1, 1, 1, 2, 1, 3, 1, 4, 1, 5; 63 | auto new_y = new_x.array().rowwise() * b.transpose().array(); 64 | std::cout << "Predicted values : \n" << new_y << std::endl; 65 | 66 | auto new_y_norm = new_x.array().rowwise() * b_norm.transpose().array(); 67 | std::cout << "Predicted(norm) values : \n" << new_y_norm << std::endl; 68 | 69 | return 0; 70 | }; 71 | -------------------------------------------------------------------------------- /Chapter11/pytorch/tokenizer.cc: -------------------------------------------------------------------------------- 1 | #include "tokenizer.h" 2 | 3 | #include 4 | 5 | Tokenizer::Tokenizer(const std::string& vocab_file_path, int max_len) 6 | : max_len_{max_len} { 7 | auto file = std::ifstream(vocab_file_path); 8 | std::string line; 9 | while (std::getline(file, line)) { 10 | auto sep_pos = line.find_first_of(' '); 11 | auto token = line.substr(0, sep_pos); 12 | auto id = std::stoi(line.substr(sep_pos + 1)); 13 | vocab_.insert({token, id}); 14 | } 15 | } 16 | 17 | std::pair Tokenizer::tokenize(const std::string text) { 18 | std::string pad_token = "[PAD]"; 19 | std::string start_token = "[CLS]"; 20 | std::string end_token = "[SEP]"; 21 | auto pad_token_id = vocab_[pad_token]; 22 | auto start_token_id = vocab_[start_token]; 23 | auto end_token_id = vocab_[end_token]; 24 | 25 | std::vector input_ids(max_len_, pad_token_id); 26 | std::vector attention_mask(max_len_, 0); 27 | input_ids[0] = start_token_id; 28 | attention_mask[0] = 1; 29 | 30 | std::string word; 31 | std::istringstream ss(text); 32 | 33 | int input_id = 1; 34 | while (getline(ss, word, ' ')) { 35 | size_t start = 0; 36 | while (start < word.size()) { 37 | size_t end = word.size(); 38 | std::string token; 39 | bool has_token = false; 40 | while (start < end) { 41 | auto token = word.substr(start, end - start); 42 | if (start > 0) 43 | token = "##" + token; 44 | auto token_iter = vocab_.find(token); 45 | if (token_iter != vocab_.end()) { 46 | attention_mask[input_id] = 1; 47 | input_ids[input_id] = token_iter->second; 48 | ++input_id; 49 | has_token = true; 50 | break; 51 | } 52 | end--; 53 | } 54 | if (input_id == max_len_ - 1) { 55 | break; 56 | } 57 | if (!has_token) { 58 | break; 59 | } 60 | start = end; 61 | } 62 | if (input_id == max_len_ - 1) { 63 | break; 64 | } 65 | } 66 | attention_mask[input_id] = 1; 67 | input_ids[input_id] = end_token_id; 68 | 69 | auto input_ids_tensor = torch::tensor(input_ids).unsqueeze(0); 70 | auto attention_masks_tensor = torch::tensor(attention_mask).unsqueeze(0); 71 | return std::make_pair(input_ids_tensor, attention_masks_tensor); 72 | } -------------------------------------------------------------------------------- /Chapter14/android_detection/app/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | alias(libs.plugins.android.application) 3 | alias(libs.plugins.jetbrains.kotlin.android) 4 | } 5 | 6 | val pytorchDir = "/development/android/pytorch/build_android/install/" 7 | val opencvDir = "/development/android/OpenCV-android-sdk/sdk/native/jni/" 8 | 9 | 10 | android { 11 | namespace = "com.example.objectdetection" 12 | compileSdk = 34 13 | 14 | defaultConfig { 15 | applicationId = "com.example.objectdetection" 16 | minSdk = 24 17 | targetSdk = 34 18 | versionCode = 1 19 | versionName = "1.0" 20 | 21 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" 22 | 23 | externalNativeBuild { 24 | cmake { 25 | arguments += "-DANDROID_ARM_NEON=TRUE" 26 | arguments += "-DANDROID_STL=c++_shared" 27 | arguments += "-DTorch_DIR=${pytorchDir}" 28 | arguments += "-DOpenCV_DIR=${opencvDir}" 29 | } 30 | } 31 | ndk { 32 | abiFilters.add("arm64-v8a") 33 | } 34 | } 35 | 36 | 37 | buildTypes { 38 | release { 39 | isMinifyEnabled = false 40 | proguardFiles( 41 | getDefaultProguardFile("proguard-android-optimize.txt"), 42 | "proguard-rules.pro" 43 | ) 44 | signingConfig = signingConfigs.getByName("debug") 45 | } 46 | } 47 | compileOptions { 48 | sourceCompatibility = JavaVersion.VERSION_1_8 49 | targetCompatibility = JavaVersion.VERSION_1_8 50 | } 51 | kotlinOptions { 52 | jvmTarget = "1.8" 53 | } 54 | externalNativeBuild { 55 | cmake { 56 | path = file("src/main/cpp/CMakeLists.txt") 57 | version = "3.22.1" 58 | } 59 | } 60 | buildFeatures { 61 | viewBinding = true 62 | } 63 | ndkVersion = "26.1.10909125" 64 | } 65 | 66 | dependencies { 67 | 68 | implementation(libs.androidx.core.ktx) 69 | implementation(libs.androidx.appcompat) 70 | implementation(libs.material) 71 | implementation(libs.androidx.constraintlayout) 72 | testImplementation(libs.junit) 73 | androidTestImplementation(libs.androidx.junit) 74 | androidTestImplementation(libs.androidx.espresso.core) 75 | } 76 | -------------------------------------------------------------------------------- /Chapter01/blaze_samples/linreg_blaze.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "blaze/math/StorageOrder.h" 9 | #include "blaze/math/dense/DynamicVector.h" 10 | 11 | typedef blaze::DynamicMatrix Matrix; 12 | typedef blaze::DynamicVector Vector; 13 | 14 | std::pair GenerateData(size_t n) { 15 | std::vector x_data(n); 16 | std::iota(x_data.begin(), x_data.end(), 0); 17 | std::vector y_data(n); 18 | std::iota(y_data.begin(), y_data.end(), 0); 19 | 20 | // mutate data 21 | std::random_device rd; 22 | std::mt19937 re(rd()); 23 | std::uniform_real_distribution dist(-1.5f, 1.5f); 24 | 25 | for (auto& x : x_data) { 26 | x += dist(re); // add noise 27 | } 28 | 29 | for (auto& y : y_data) { 30 | y += dist(re); // add noise 31 | } 32 | 33 | // Make result 34 | Vector x(n, x_data.data()); 35 | Matrix y(n, 1UL, y_data.data()); 36 | 37 | return {x, y}; 38 | } 39 | 40 | int main() { 41 | size_t n = 1000; 42 | // generate training data 43 | Vector x0 = blaze::uniform(n, 1.f); 44 | Vector x1; 45 | Matrix y; 46 | std::tie(x1, y) = GenerateData(n); 47 | 48 | // setup line coeficients y = b(4) + k(0.3)*x 49 | y *= 0.3f; 50 | y += 4.f; 51 | 52 | // combine X matrix 53 | Matrix x(n, 2UL); 54 | blaze::column<0UL>(x) = x0; 55 | blaze::column<1UL>(x) = x1; 56 | 57 | // solve normal equation 58 | // calculate X^T*X 59 | auto xtx = blaze::trans(x) * x; 60 | 61 | // calculate the inverse of X^T*X 62 | auto inv_xtx = blaze::inv(xtx); 63 | 64 | // calculate X^T*y 65 | auto xty = blaze::trans(x) * y; 66 | 67 | // calculate the coefficients of the linear regression 68 | Matrix beta = inv_xtx * xty; 69 | 70 | std::cout << "Estimated line coefficients: \n" 71 | << beta << "\n"; 72 | 73 | // predict 74 | Matrix new_x = {{1, 1}, 75 | {1, 2}, 76 | {1, 3}, 77 | {1, 4}, 78 | {1, 5}}; 79 | 80 | auto line_coeffs = blaze::expand(blaze::row<0UL>(blaze::trans(beta)), new_x.rows()); 81 | auto new_y_norm = new_x % line_coeffs; 82 | std::cout << "Predicted(norm) values : \n" 83 | << new_y_norm << std::endl; 84 | 85 | return 0; 86 | }; 87 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/cpp/native-lib.cpp: -------------------------------------------------------------------------------- 1 | #include "log.h" 2 | #include "detector.h" 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | std::shared_ptr object_detector_; 16 | 17 | extern "C" JNIEXPORT void JNICALL 18 | Java_com_example_objectdetection_MainActivity_initObjectDetection( 19 | JNIEnv *env, 20 | jobject /* this */, 21 | jstring camId) { 22 | auto camera_id = env->GetStringUTFChars(camId, nullptr); 23 | LOGI("Camera ID: %s", camera_id); 24 | if (object_detector_) { 25 | object_detector_->allow_camera_session(camera_id); 26 | object_detector_->configure_resources(); 27 | } else 28 | LOGE("Object Detector object is missed!"); 29 | } 30 | 31 | extern "C" JNIEXPORT void JNICALL 32 | Java_com_example_objectdetection_MainActivity_stopObjectDetection( 33 | JNIEnv *, 34 | jobject /* this */) { 35 | object_detector_->release_resources(); 36 | } 37 | 38 | static void ProcessAndroidCmd(struct android_app */*app*/, int32_t cmd) { 39 | if (object_detector_) { 40 | switch (cmd) { 41 | case APP_CMD_INIT_WINDOW: 42 | object_detector_->configure_resources(); 43 | break; 44 | case APP_CMD_TERM_WINDOW: 45 | object_detector_->release_resources(); 46 | break; 47 | } 48 | } 49 | } 50 | 51 | extern "C" void android_main(struct android_app *app) { 52 | LOGI("Native entry point"); 53 | object_detector_ = std::make_shared(app); 54 | app->onAppCmd = ProcessAndroidCmd; 55 | 56 | while (!app->destroyRequested) { 57 | struct android_poll_source *source = nullptr; 58 | auto result = ALooper_pollOnce(0, nullptr, nullptr, (void **) &source); 59 | ASSERT(result != ALOOPER_POLL_ERROR, "ALooper_pollOnce returned an error"); 60 | if (source != nullptr) { 61 | source->process(app, source); 62 | } 63 | if (object_detector_) 64 | object_detector_->draw_frame(); 65 | } 66 | 67 | // application is closing ... 68 | object_detector_.reset(); 69 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Machine Learning Summit 2025

2 | 3 | ## Machine Learning Summit 2025 4 | **Bridging Theory and Practice: ML Solutions for Today’s Challenges** 5 | 6 | 3 days, 20+ experts, and 25+ tech sessions and talks covering critical aspects of: 7 | - **Agentic and Generative AI** 8 | - **Applied Machine Learning in the Real World** 9 | - **ML Engineering and Optimization** 10 | 11 | 👉 [Book your ticket now >>](https://packt.link/mlsumgh) 12 | 13 | --- 14 | 15 | ## Join Our Newsletters 📬 16 | 17 | ### DataPro 18 | *The future of AI is unfolding. Don’t fall behind.* 19 | 20 |

DataPro QR

21 | 22 | Stay ahead with [**DataPro**](https://landing.packtpub.com/subscribe-datapronewsletter/?link_from_packtlink=yes), the free weekly newsletter for data scientists, AI/ML researchers, and data engineers. 23 | From trending tools like **PyTorch**, **scikit-learn**, **XGBoost**, and **BentoML** to hands-on insights on **database optimization** and real-world **ML workflows**, you’ll get what matters, fast. 24 | 25 | > Stay sharp with [DataPro](https://landing.packtpub.com/subscribe-datapronewsletter/?link_from_packtlink=yes). Join **115K+ data professionals** who never miss a beat. 26 | 27 | --- 28 | 29 | ### BIPro 30 | *Business runs on data. Make sure yours tells the right story.* 31 | 32 |

BIPro QR

33 | 34 | [**BIPro**](https://landing.packtpub.com/subscribe-bipro-newsletter/?link_from_packtlink=yes) is your free weekly newsletter for BI professionals, analysts, and data leaders. 35 | Get practical tips on **dashboarding**, **data visualization**, and **analytics strategy** with tools like **Power BI**, **Tableau**, **Looker**, **SQL**, and **dbt**. 36 | 37 | > Get smarter with [BIPro](https://landing.packtpub.com/subscribe-bipro-newsletter/?link_from_packtlink=yes). Trusted by **35K+ BI professionals**, see what you’re missing. 38 | 39 | # Hands-on Machine learning with C++, Second Edition 40 | Hands-on Machine learning with C++, Second Edition, published by Packt 41 | -------------------------------------------------------------------------------- /Chapter10/flashlight/mlp_fl.cc: -------------------------------------------------------------------------------- 1 | #include "../data/data.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | fl::init(); 9 | 10 | size_t n = 10000; 11 | size_t seed = 45345; 12 | auto data = GenerateData(-1.5, 1.5, n, seed, false); 13 | 14 | fl::Tensor x = fl::Tensor::fromVector(data.first); 15 | fl::Tensor y = fl::Tensor::fromVector(data.second); 16 | 17 | // Normalize data 18 | auto x_mean = fl::mean(x, /*reduction axis*/ {0}, /*keep_dims*/ true); 19 | auto x_std = fl::std(x, /*reduction axis*/ {0}, /*keep_dims*/ true); 20 | x = (x - x_mean) / x_std; 21 | 22 | // Define dataset 23 | std::vector fields{x, y}; 24 | auto dataset = std::make_shared(fields); 25 | fl::BatchDataset batch_dataset(dataset, /*batch_size=*/64); 26 | 27 | // Deifine model 28 | fl::Sequential model; 29 | model.add(fl::View({1, 1, 1, -1})); // to process a batch 30 | model.add(fl::Linear(1, 8)); 31 | model.add(fl::ReLU()); 32 | model.add(fl::Linear(8, 16)); 33 | model.add(fl::ReLU()); 34 | model.add(fl::Linear(16, 32)); 35 | model.add(fl::ReLU()); 36 | model.add(fl::Linear(32, 1)); 37 | 38 | // define MSE loss 39 | auto loss = fl::MeanSquaredError(); 40 | 41 | // Define optimizer 42 | float learning_rate = 0.01; 43 | float momentum = 0.5; 44 | auto sgd = fl::SGDOptimizer(model.params(), learning_rate, momentum); 45 | 46 | // Define epoch average MSE meter 47 | fl::AverageValueMeter meter; 48 | 49 | const int epochs = 500; 50 | for (int epoch_i = 0; epoch_i < epochs; ++epoch_i) { 51 | meter.reset(); 52 | for (auto& batch : batch_dataset) { 53 | sgd.zeroGrad(); 54 | 55 | // Forward propagation 56 | auto predicted = model(fl::input(batch[0])); 57 | 58 | // Calculate loss 59 | auto local_batch_size = batch[0].shape().dim(0); 60 | auto target = fl::reshape(batch[1], {1, 1, 1, local_batch_size}); 61 | auto loss_value = loss(predicted, fl::noGrad(target)); 62 | 63 | // Backward propagation 64 | loss_value.backward(); 65 | 66 | // Update parameters 67 | sgd.step(); 68 | 69 | meter.add(loss_value.scalar()); 70 | } 71 | std::cout << "Epoch: " << epoch_i << " MSE: " << meter.value()[0] 72 | << std::endl; 73 | } 74 | 75 | auto predicted = model(fl::noGrad(x)); 76 | auto target = fl::reshape(y, {1, 1, 1, x.shape().dim(0)}); 77 | auto mse = loss(predicted, fl::noGrad(target)); 78 | std::cout << "Final MSE: " << mse.scalar() << std::endl; 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /env_scripts/install_env.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -x 3 | set -e 4 | 5 | DEV_DIR=$(pwd) 6 | 7 | mkdir -p libs 8 | mkdir -p libs/sources 9 | 10 | # Blaze 11 | . ./install_lib.sh https://bitbucket.org/blaze-lib/blaze.git v3.8.2 12 | 13 | # Firearray 14 | . ./install_lib.sh https://github.com/arrayfire/arrayfire v3.8.3 -DBUILD_TESTS=OFF -DAF_BUILD_EXAMPLES=OFF 15 | 16 | # Flashlight 17 | . ./install_lib.sh https://github.com/flashlight/flashlight.git v0.4.0 -DFL_BUILD_TESTS=OFF -DFL_BUILD_EXAMPLES=OFF -DFL_LIBRARIES_USE_MKL=OFF -DFL_USE_CPU=ON -DFL_USE_ONEDNN=OFF -DArrayFire_DIR=/development/libs/share/ArrayFire/cmake/ -DFL_ARRAYFIRE_USE_CPU=ON -DFL_BUILD_DISTRIBUTED=OFF 18 | 19 | # DLib 20 | . ./install_lib.sh https://github.com/davisking/dlib v19.24.6 21 | 22 | # Armadillo 23 | . ./install_lib.sh https://gitlab.com/conradsnicta/armadillo-code 14.0.x 24 | 25 | # xtl 26 | . ./install_lib.sh https://github.com/xtensor-stack/xtl 0.7.7 27 | 28 | # xtensor 29 | . ./install_lib.sh https://github.com/xtensor-stack/xtensor 0.25.0 30 | 31 | # xtensor-blas 32 | . ./install_lib.sh https://github.com/xtensor-stack/xtensor-blas 0.21.0 33 | 34 | # NlohmanJson 35 | . ./install_lib.sh https://github.com/nlohmann/json.git v3.11.3 -DJSON_BuildTests=OFF 36 | 37 | # mlpack 38 | . ./install_lib.sh https://github.com/mlpack/mlpack 4.5.0 -DBUILD_PYTHON_BINDINGS=OFF -DBUILD_TESTS=OFF -DDOWNLOAD_DEPENDENCIES=ON 39 | 40 | # Eigen 41 | . ./install_lib.sh https://gitlab.com/libeigen/eigen.git 3.4.0 42 | 43 | # HighFive 44 | . ./install_lib.sh https://github.com/BlueBrain/HighFive v2.10.0 45 | 46 | # cpp-httplib 47 | . ./install_lib.sh https://github.com/yhirose/cpp-httplib v0.18.1 48 | 49 | # PlotCpp 50 | . ./checkout_lib.sh https://github.com/Kolkir/plotcpp c86bd4f5d9029986f0d5f368450d79f0dd32c7e4 51 | 52 | # fast-cpp-csv-parser 53 | . ./checkout_lib.sh https://github.com/ben-strasser/fast-cpp-csv-parser 4ade42d5f8c454c6c57b3dce9c51c6dd02182a66 54 | 55 | # tapkee 56 | . ./install_lib.sh https://github.com/lisitsyn/tapkee ba5f052d2548ec03dcc6a4ac0ed8deeb79f1d43a -DBUILD_TESTS=OFF 57 | 58 | # onnxruntime 59 | . ./install_lib_custom.sh https://github.com/Microsoft/onnxruntime.git v1.19.2 "./build.sh --config RelWithDebInfo --build_shared_lib --parallel --compile_no_warning_as_error --skip_submodule_sync --allow_running_as_root --skip_tests --cmake_extra_defines CMAKE_INSTALL_PREFIX=/development/libs/ --cmake_extra_defines nnxruntime_BUILD_UNIT_TESTS=OFF --target install" 60 | 61 | # PyTorch - install after onnxruntime to fix protobuf conflict 62 | . ./install_lib.sh https://github.com/pytorch/pytorch v2.3.1 -DBUILD_PYTHON=OFF 63 | 64 | # return back 65 | cd $DEV_DIR 66 | 67 | 68 | -------------------------------------------------------------------------------- /env_scripts/dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:22.04 2 | MAINTAINER Kirill kirill.tomorrow@gmail.com 3 | 4 | ENV DEBIAN_FRONTEND=noninteractive 5 | 6 | RUN apt-get -y update 7 | RUN apt-get -y upgrade 8 | 9 | RUN apt-get install -y unzip 10 | RUN apt-get install -y build-essential 11 | RUN apt-get install -y gdb 12 | RUN apt-get install -y git 13 | RUN apt-get install -y libfmt-dev 14 | RUN apt-get install -y wget 15 | 16 | RUN wget https://github.com/Kitware/CMake/releases/download/v3.27.5/cmake-3.27.5-Linux-x86_64.sh \ 17 | -q -O /tmp/cmake-install.sh \ 18 | && chmod u+x /tmp/cmake-install.sh \ 19 | && mkdir /usr/bin/cmake \ 20 | && /tmp/cmake-install.sh --skip-license --prefix=/usr/bin/cmake \ 21 | && rm /tmp/cmake-install.sh 22 | 23 | ENV PATH="/usr/bin/cmake/bin:${PATH}" 24 | 25 | RUN apt-get install -y python3 26 | RUN apt-get install -y python3-pip 27 | RUN apt-get install -y python-is-python3 28 | RUN apt-get install -y libblas-dev 29 | RUN apt-get install -y libopenblas-dev 30 | RUN apt-get install -y libfftw3-dev 31 | RUN apt-get install -y libatlas-base-dev 32 | RUN apt-get install -y liblapacke-dev 33 | RUN apt-get install -y liblapack-dev 34 | RUN apt-get install -y libboost-all-dev 35 | RUN apt-get install -y libopencv-core4.5d 36 | RUN apt-get install -y libopencv-imgproc4.5d 37 | RUN apt-get install -q -y libopencv-dev 38 | RUN apt-get install -y libopencv-highgui4.5d 39 | RUN apt-get install -y libopencv-highgui-dev 40 | RUN apt-get install -y libhdf5-dev 41 | RUN apt-get install -y libjson-c-dev 42 | RUN apt-get install -y libx11-dev 43 | RUN apt-get install -y openjdk-8-jdk 44 | RUN apt-get install -y openjdk-17-jdk 45 | RUN apt-get install -y ninja-build 46 | RUN apt-get install -y gnuplot 47 | RUN apt-get install -y vim 48 | RUN apt-get install -y python3-venv 49 | RUN apt-get install -y libcpuinfo-dev 50 | RUN apt-get install -y libspdlog-dev 51 | 52 | 53 | RUN pip install pyyaml 54 | RUN pip install typing 55 | RUN pip install typing_extensions 56 | RUN pip install optuna 57 | RUN pip install torch==2.3.1 --index-url https://download.pytorch.org/whl/cpu 58 | RUN pip install transformers 59 | RUN pip install mlflow==2.15.0 60 | 61 | 62 | RUN mkdir development 63 | ADD checkout_lib.sh /development 64 | RUN chmod 777 /development/checkout_lib.sh 65 | ADD install_lib.sh /development 66 | RUN chmod 777 /development/install_lib.sh 67 | ADD install_env.sh /development 68 | RUN chmod 777 /development/install_env.sh 69 | ADD install_android.sh /development 70 | RUN chmod 777 /development/install_android.sh 71 | ADD install_lib_custom.sh /development 72 | RUN chmod 777 /development/install_lib_custom.sh 73 | 74 | WORKDIR /development 75 | RUN ./install_env.sh 76 | RUN ./install_android.sh 77 | 78 | -------------------------------------------------------------------------------- /Chapter02/csv/flashlight/csv_fl.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | namespace fs = std::filesystem; 13 | 14 | std::tuple load_dataset(const std::string& file_path) { 15 | if (fs::exists(file_path)) { 16 | constexpr int columns_num = 5; 17 | io::CSVReader csv_reader(file_path); 18 | 19 | fl::Tensor x; 20 | fl::Tensor y; 21 | std::array features; 22 | std::string class_id; 23 | while (csv_reader.read_row(features[0], features[1], features[2], features[3], class_id)) { 24 | auto sample_x = fl::Tensor::fromBuffer({1, 4}, features.data(), fl::MemoryLocation::Host); 25 | x = x.isEmpty() ? sample_x : fl::concatenate({x, sample_x}, 0); 26 | fl::Tensor sample_y; 27 | if (class_id == "Iris-virginica") 28 | sample_y = fl::reshape(fl::fromScalar(2.f), {1, 1}); 29 | else if (class_id == "Iris-versicolor") 30 | sample_y = fl::reshape(fl::fromScalar(1.f), {1, 1}); 31 | else if (class_id == "Iris-setosa") 32 | sample_y = fl::reshape(fl::fromScalar(0.f), {1, 1}); 33 | y = y.isEmpty() ? sample_y : fl::concatenate({y, sample_y}, 0); 34 | } 35 | return std::make_tuple(fl::transpose(x), fl::transpose(y)); 36 | } else { 37 | throw std::runtime_error("Invalid dataset file path"); 38 | } 39 | } 40 | 41 | int main(int argc, char** argv) { 42 | try { 43 | if (argc > 1) { 44 | fl::init(); 45 | auto [x, y] = load_dataset(argv[1]); 46 | 47 | // min-max scaling 48 | auto x_min = fl::amin(x, {1}); 49 | auto x_max = fl::amax(x, {1}); 50 | auto x_min_max = (x - x_min) / (x_max - x_min); 51 | // std::cout << x_min_max << std::endl; 52 | 53 | // normalization(z-score) 54 | auto x_mean = fl::mean(x, {1}); 55 | auto x_std = fl::std(x, {1}); 56 | auto x_norm = (x - x_mean) / x_std; 57 | // std::cout << x_norm << std::endl; 58 | 59 | auto dataset = std::make_shared(std::vector{x_norm, y}); 60 | for (auto& sample : fl::ShuffleDataset(dataset)) { 61 | std::cout << "X:\n" 62 | << sample[0] << std::endl; 63 | std::cout << "Y:\n" 64 | << sample[1] << std::endl; 65 | } 66 | } else { 67 | std::cerr << "Please specify path to the dataset\n"; 68 | } 69 | } catch (const std::exception& err) { 70 | std::cerr << err.what() << std::endl; 71 | } 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Chapter05/data/multivar.csv: -------------------------------------------------------------------------------- 1 | -2.366071741736619050e+00,8.650395628888157828e-01 2 | -2.155754253582252922e+00,2.132222203711148456e+00 3 | -1.945436765427887016e+00,2.015619142427458765e+00 4 | -1.735119277273520666e+00,1.117873018462888446e+00 5 | -1.524801789119154538e+00,2.172147469944559095e+00 6 | -1.314484300964788410e+00,1.649987573729479573e+00 7 | -1.104166812810422282e+00,1.121810591207495378e+00 8 | -8.938493246560559324e-01,5.186163482186424700e-02 9 | -6.835318365016899156e-01,1.426654324999584167e-01 10 | -4.732143483473238432e-01,7.018454311790772726e-01 11 | -4.732143483473238432e-01,1.022930364234008183e+00 12 | -4.405788760475083388e-01,2.354185565669291713e-01 13 | -4.079434037476928898e-01,1.226449814389925841e+00 14 | -3.753079314478774409e-01,3.868910032502616558e-01 15 | -3.426724591480619919e-01,-2.544523328121584549e-01 16 | -3.100369868482465430e-01,3.657503327330084986e-01 17 | -2.774015145484310385e-01,8.435027789268647602e-01 18 | -2.447660422486156173e-01,-1.046225942801146225e+00 19 | -2.121305699488001406e-01,1.108090305823731025e+00 20 | -1.794950976489846917e-01,-8.619055461521095696e-01 21 | -1.468596253491695758e-01,9.392878543658147450e-01 22 | -1.142241530493541130e-01,-4.656391608976957475e-01 23 | -8.158868074953865013e-02,-1.408916465526846573e-01 24 | -4.895320844972318730e-02,-6.511466492714065302e-01 25 | -1.631773614990772794e-02,-2.215014870058659435e-01 26 | 1.631773614990772794e-02,-3.007114269538465612e-01 27 | 4.895320844972318730e-02,5.165426704147627612e-01 28 | 8.158868074953865013e-02,-1.825263761954630179e-02 29 | 1.142241530493541130e-01,1.923576893053128289e-01 30 | 1.468596253491695758e-01,-2.260784287348586441e-01 31 | 1.794950976489850247e-01,-1.496801548082859687e-02 32 | 2.121305699488004737e-01,-1.215400566124676812e-01 33 | 2.447660422486156173e-01,-2.063076696355736206e-01 34 | 2.774015145484310385e-01,2.397131895238458765e-01 35 | 3.100369868482465430e-01,-1.984560675759159398e-01 36 | 3.426724591480619919e-01,-5.755975250449898439e-01 37 | 3.753079314478774409e-01,-1.223317702674122653e-01 38 | 4.079434037476928898e-01,-8.655580590524503126e-01 39 | 4.405788760475083388e-01,-7.779034381553371347e-01 40 | 4.732143483473238432e-01,1.659989657811084818e-01 41 | 4.732143483473238432e-01,-7.717732003953987618e-01 42 | 6.835318365016899156e-01,9.560850339548961074e-02 43 | 8.938493246560559324e-01,-3.996008610269475292e-01 44 | 1.104166812810422060e+00,-1.280067860233096377e+00 45 | 1.314484300964788188e+00,-1.248099544067293287e+00 46 | 1.524801789119154760e+00,-2.532438696316576809e+00 47 | 1.735119277273520888e+00,-1.624757223026498609e+00 48 | 1.945436765427887016e+00,-1.426747826902983407e+00 49 | 2.155754253582252922e+00,-1.709137301946266119e+00 50 | 2.366071741736619050e+00,-1.247523715042455184e+00 51 | -1.500000000000000000e+00,-1.500000000000000000e+00 52 | 1.500000000000000000e+00,1.500000000000000000e+00 53 | -------------------------------------------------------------------------------- /Chapter05/data/univar.csv: -------------------------------------------------------------------------------- 1 | 1.300000000000000000e+01,9.000000000000000000e+00 2 | 8.000000000000000000e+00,8.000000000000000000e+00 3 | 1.400000000000000000e+01,9.000000000000000000e+00 4 | 8.000000000000000000e+00,6.000000000000000000e+00 5 | 6.000000000000000000e+00,6.000000000000000000e+00 6 | 9.000000000000000000e+00,1.000000000000000000e+01 7 | 7.000000000000000000e+00,1.200000000000000000e+01 8 | 9.000000000000000000e+00,1.200000000000000000e+01 9 | 5.000000000000000000e+00,9.000000000000000000e+00 10 | 1.100000000000000000e+01,6.000000000000000000e+00 11 | 1.400000000000000000e+01,1.100000000000000000e+01 12 | 1.200000000000000000e+01,1.300000000000000000e+01 13 | 1.100000000000000000e+01,9.000000000000000000e+00 14 | 7.000000000000000000e+00,1.200000000000000000e+01 15 | 8.000000000000000000e+00,1.400000000000000000e+01 16 | 8.000000000000000000e+00,7.000000000000000000e+00 17 | 9.000000000000000000e+00,5.000000000000000000e+00 18 | 8.000000000000000000e+00,7.000000000000000000e+00 19 | 6.000000000000000000e+00,6.000000000000000000e+00 20 | 5.000000000000000000e+00,1.000000000000000000e+01 21 | 1.400000000000000000e+01,6.000000000000000000e+00 22 | 1.400000000000000000e+01,1.000000000000000000e+01 23 | 1.100000000000000000e+01,1.200000000000000000e+01 24 | 6.000000000000000000e+00,7.000000000000000000e+00 25 | 5.000000000000000000e+00,9.000000000000000000e+00 26 | 5.000000000000000000e+00,6.000000000000000000e+00 27 | 1.100000000000000000e+01,1.300000000000000000e+01 28 | 1.000000000000000000e+01,1.300000000000000000e+01 29 | 5.000000000000000000e+00,8.000000000000000000e+00 30 | 1.400000000000000000e+01,6.000000000000000000e+00 31 | 9.000000000000000000e+00,8.000000000000000000e+00 32 | 1.300000000000000000e+01,6.000000000000000000e+00 33 | 6.000000000000000000e+00,1.000000000000000000e+01 34 | 1.100000000000000000e+01,1.200000000000000000e+01 35 | 8.000000000000000000e+00,1.400000000000000000e+01 36 | 1.200000000000000000e+01,1.000000000000000000e+01 37 | 1.100000000000000000e+01,8.000000000000000000e+00 38 | 1.300000000000000000e+01,1.400000000000000000e+01 39 | 1.200000000000000000e+01,9.000000000000000000e+00 40 | 9.000000000000000000e+00,1.300000000000000000e+01 41 | 9.000000000000000000e+00,1.000000000000000000e+01 42 | 6.000000000000000000e+00,7.000000000000000000e+00 43 | 5.000000000000000000e+00,1.000000000000000000e+01 44 | 1.300000000000000000e+01,8.000000000000000000e+00 45 | 6.000000000000000000e+00,5.000000000000000000e+00 46 | 1.300000000000000000e+01,5.000000000000000000e+00 47 | 6.000000000000000000e+00,1.200000000000000000e+01 48 | 6.000000000000000000e+00,7.000000000000000000e+00 49 | 9.000000000000000000e+00,9.000000000000000000e+00 50 | 7.000000000000000000e+00,1.100000000000000000e+01 51 | 7.000000000000000000e+00,2.500000000000000000e+01 52 | 1.400000000000000000e+01,1.600000000000000000e+01 53 | 1.300000000000000000e+01,2.200000000000000000e+01 54 | 1.500000000000000000e+01,2.200000000000000000e+01 55 | -------------------------------------------------------------------------------- /Chapter01/dlib_samples/linalg_dlib.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // definitions 6 | { 7 | // compile time sized matrix 8 | dlib::matrix y; 9 | // dynamically sized matrix 10 | dlib::matrix m(3, 3); 11 | // later we can change size of this matrix 12 | m.set_size(6, 6); 13 | } 14 | // initializations 15 | { 16 | // comma operator 17 | dlib::matrix m(3, 3); 18 | m = 1., 2., 3., 4., 5., 6., 7., 8., 9.; 19 | std::cout << "Matix from comma operator\n" << m << std::endl; 20 | 21 | // wrap array 22 | double data[] = {1, 2, 3, 4, 5, 6}; 23 | auto m2 = dlib::mat(data, 2, 3); // create matrix with size 2x3 24 | std::cout << "Matix from array\n" << m2 << std::endl; 25 | 26 | // Matrix elements can be accessed with () operator 27 | m(1, 2) = 300; 28 | std::cout << "Matix element updated\n" << m << std::endl; 29 | 30 | // Also you can initialize matrix with some predefined values 31 | auto a = dlib::identity_matrix(3); 32 | std::cout << "Identity matix \n" << a << std::endl; 33 | 34 | auto b = dlib::ones_matrix(3, 4); 35 | std::cout << "Ones matix \n" << b << std::endl; 36 | 37 | auto c = dlib::randm(3, 4); // matrix with random values with size 3x3 38 | std::cout << "Random matix \n" << c << std::endl; 39 | } 40 | // arithmetic operations 41 | { 42 | dlib::matrix a(2, 2); 43 | a = 1, 1, 1, 1; 44 | dlib::matrix b(2, 2); 45 | b = 2, 2, 2, 2; 46 | 47 | auto c = a + b; 48 | std::cout << "c = a + b \n" << c << std::endl; 49 | 50 | auto e = a * b; // real matrix multiplication 51 | std::cout << "e = a dot b \n" << e << std::endl; 52 | 53 | a += 5; 54 | std::cout << "a += 5 \n" << a << std::endl; 55 | 56 | auto d = dlib::pointwise_multiply(a, b); // element wise multiplication 57 | std::cout << "d = a * b \n" << d << std::endl; 58 | 59 | auto t = dlib::trans(a); // transpose matrix 60 | std::cout << "transposed matrix a \n" << t << std::endl; 61 | } 62 | // partial access 63 | { 64 | dlib::matrix m; 65 | m = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16; 66 | auto sm = 67 | dlib::subm(m, dlib::range(1, 2), 68 | dlib::range(1, 2)); // original matrix can't be updated 69 | std::cout << "Sub matrix \n" << sm << std::endl; 70 | 71 | dlib::set_subm(m, dlib::range(1, 2), dlib::range(1, 2)) = 100; 72 | std::cout << "Updated sub matrix \n" << m << std::endl; 73 | } 74 | // there are no implicit broadcasting in dlib 75 | { 76 | // we can simulate broadcasting with partial access 77 | dlib::matrix v; 78 | v = 10, 10; 79 | dlib::matrix m; 80 | m = 1, 2, 3, 4, 5, 6; 81 | for (int i = 0; i < m.nc(); ++i) { 82 | dlib::set_colm(m, i) += v; 83 | } 84 | std::cout << "Matrix with updated columns \n" << m << std::endl; 85 | } 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /Chapter11/pytorch/export.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from transformers import BertModel, BertTokenizer 4 | 5 | from pathlib import Path 6 | from tqdm import tqdm 7 | 8 | model_name = "bert-base-cased" 9 | 10 | 11 | def save_vocab(tokenizer): 12 | vocab_file = open("vocab.txt", "w") 13 | for i, j in tokenizer.get_vocab().items(): 14 | vocab_file.write(f"{i} {j}\n") 15 | vocab_file.close() 16 | 17 | 18 | def tokenize(tokenizer, text): 19 | max_length = 128 20 | tokenizer_out = tokenizer( 21 | text, 22 | padding="max_length", 23 | max_length=max_length, 24 | truncation=True, 25 | return_tensors="pt", 26 | ) 27 | attention_mask = tokenizer_out.attention_mask 28 | input_ids = tokenizer_out.input_ids 29 | 30 | return input_ids, attention_mask 31 | 32 | 33 | def export_model(model, tokenizer): 34 | sample_text = "Not even the Beatles could write songs everyone liked, and although Walter Hill is no mop-top he's second to none when it comes to thought provoking action movies. The nineties came and social platforms were changing in music and film, the emergence of the Rapper turned movie star was in full swing, the acting took a back seat to each man's overpowering regional accent and transparent acting. This was one of the many ice-t movies i saw as a kid and loved, only to watch them later and cringe. Bill Paxton and William Sadler are firemen with basic lives until a burning building tenant about to go up in flames hands over a map with gold implications. I hand it to Walter for quickly and neatly setting up the main characters and location. But i fault everyone involved for turning out Lame-o performances. Ice-t and cube must have been red hot at this time, and while I've enjoyed both their careers as rappers, in my opinion they fell flat in this movie. It's about ninety minutes of one guy ridiculously turning his back on the other guy to the point you find yourself locked in multiple states of disbelief. Now this is a movie, its not a documentary so i wont waste my time recounting all the stupid plot twists in this movie, but there were many, and they led nowhere. I got the feeling watching this that everyone on set was sord of confused and just playing things off the cuff. There are two things i still enjoy about it, one involves a scene with a needle and the other is Sadler's huge 45 pistol. Bottom line this movie is like domino's pizza. Yeah ill eat it if I'm hungry and i don't feel like cooking, But I'm well aware it tastes like crap. 3 stars, meh." 35 | 36 | tokenizer_out = tokenize(tokenizer, sample_text) 37 | input_ids = tokenizer_out[0] 38 | attention_mask = tokenizer_out[1] 39 | 40 | model.eval() 41 | traced_script_module = torch.jit.trace(model, [input_ids, attention_mask]) 42 | traced_script_module.save("bert_model.pt") 43 | 44 | 45 | tokenizer = BertTokenizer.from_pretrained(model_name, torchscript=True) 46 | bert = BertModel.from_pretrained(model_name, torchscript=True) 47 | 48 | export_model(bert, tokenizer) 49 | -------------------------------------------------------------------------------- /Chapter14/android_detection/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /Chapter12/flashlight/fl_save.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | fl::init(); 7 | 8 | int64_t n = 10000; 9 | 10 | auto x = fl::randn({n}); 11 | auto y = x * 0.3f + 0.4f; 12 | 13 | // Normalize data 14 | auto x_mean = fl::mean(x, /*reduction axis*/ {0}, /*keep_dims*/ true); 15 | auto x_std = fl::std(x, /*reduction axis*/ {0}, /*keep_dims*/ true); 16 | x = (x - x_mean) / x_std; 17 | 18 | // Define dataset 19 | std::vector fields{x, y}; 20 | auto dataset = std::make_shared(fields); 21 | fl::BatchDataset batch_dataset(dataset, /*batch_size=*/64); 22 | 23 | // Deifine model 24 | fl::Sequential model; 25 | model.add(fl::View({1, 1, 1, -1})); // to process a batch 26 | model.add(fl::Linear(1, 8)); 27 | model.add(fl::ReLU()); 28 | model.add(fl::Linear(8, 16)); 29 | model.add(fl::ReLU()); 30 | model.add(fl::Linear(16, 32)); 31 | model.add(fl::ReLU()); 32 | model.add(fl::Linear(32, 1)); 33 | 34 | // define MSE loss 35 | auto loss = fl::MeanSquaredError(); 36 | 37 | // Define optimizer 38 | float learning_rate = 0.01; 39 | float momentum = 0.5; 40 | auto sgd = fl::SGDOptimizer(model.params(), learning_rate, momentum); 41 | 42 | // Define epoch average MSE meter 43 | fl::AverageValueMeter meter; 44 | 45 | const int epochs = 5; 46 | for (int epoch_i = 0; epoch_i < epochs; ++epoch_i) { 47 | meter.reset(); 48 | for (auto& batch : batch_dataset) { 49 | sgd.zeroGrad(); 50 | 51 | // Forward propagation 52 | auto predicted = model(fl::input(batch[0])); 53 | 54 | // Calculate loss 55 | auto local_batch_size = batch[0].shape().dim(0); 56 | auto target = fl::reshape(batch[1], {1, 1, 1, local_batch_size}); 57 | auto loss_value = loss(predicted, fl::noGrad(target)); 58 | 59 | // Backward propagation 60 | loss_value.backward(); 61 | 62 | // Update parameters 63 | sgd.step(); 64 | 65 | meter.add(loss_value.scalar()); 66 | } 67 | std::cout << "Epoch: " << epoch_i << " MSE: " << meter.value()[0] 68 | << std::endl; 69 | } 70 | // fl::save("model.dat", model); 71 | fl::save("model_params.dat", model.params()); 72 | 73 | fl::Sequential model_loaded; 74 | // fl::load("model.dat", model_loaded); 75 | 76 | model_loaded.add(fl::View({1, 1, 1, -1})); // to process a batch 77 | model_loaded.add(fl::Linear(1, 8)); 78 | model_loaded.add(fl::ReLU()); 79 | model_loaded.add(fl::Linear(8, 16)); 80 | model_loaded.add(fl::ReLU()); 81 | model_loaded.add(fl::Linear(16, 32)); 82 | model_loaded.add(fl::ReLU()); 83 | model_loaded.add(fl::Linear(32, 1)); 84 | std::vector params; 85 | fl::load("model_params.dat", params); 86 | for (int i = 0; i < static_cast(params.size()); ++i) { 87 | model_loaded.setParams(params[i], i); 88 | } 89 | 90 | auto predicted = model_loaded(fl::noGrad(x)); 91 | auto target = fl::reshape(y, {1, 1, 1, x.shape().dim(0)}); 92 | auto mse = loss(predicted, fl::noGrad(target)); 93 | std::cout << "Final MSE: " << mse.scalar() << std::endl; 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /Chapter01/eigen_samples/linalg_eigen.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef Eigen::Matrix MyMatrix33f; 5 | typedef Eigen::Matrix MyVector3f; 6 | typedef Eigen::Matrix MyMatrix; 7 | 8 | int main() { 9 | { 10 | // declaration 11 | MyMatrix33f a; 12 | MyVector3f v; 13 | MyMatrix m(10, 15); 14 | 15 | // initialization 16 | a = MyMatrix33f::Zero(); 17 | std::cout << "Zero matrix:\n" << a << std::endl; 18 | 19 | a = MyMatrix33f::Identity(); 20 | std::cout << "Identity matrix:\n" << a << std::endl; 21 | 22 | v = MyVector3f::Random(); 23 | std::cout << "Random vector:\n" << v << std::endl; 24 | 25 | a << 1, 2, 3, 4, 5, 6, 7, 8, 9; 26 | std::cout << "Comma initilized matrix:\n" << a << std::endl; 27 | 28 | a(0, 0) = 3; 29 | std::cout << "Matrix with changed element[0][0]:\n" << a << std::endl; 30 | 31 | int data[] = {1, 2, 3, 4}; 32 | Eigen::Map v_map(data, 4); 33 | std::cout << "Row vector mapped to array:\n" << v_map << std::endl; 34 | 35 | std::vector vdata = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 36 | Eigen::Map a_map(vdata.data()); 37 | std::cout << "Matrix mapped to array:\n" << a_map << std::endl; 38 | } 39 | // arithmetic 40 | { 41 | Eigen::Matrix2d a; 42 | a << 1, 2, 3, 4; 43 | Eigen::Matrix2d b; 44 | b << 1, 2, 3, 4; 45 | 46 | // element wise operations 47 | Eigen::Matrix2d result = a.array() * b.array(); 48 | std::cout << "element wise a * b :\n" << result << std::endl; 49 | 50 | result = a.array() / b.array(); 51 | std::cout << "element wise a / b :\n" << result << std::endl; 52 | 53 | a = b.array() * 4; 54 | std::cout << "element wise a = b * 4 :\n" << a << std::endl; 55 | 56 | // matrix operations 57 | result = a + b; 58 | std::cout << "matrices a + b :\n" << result << std::endl; 59 | 60 | a += b; 61 | std::cout << "matrices a += b :\n" << result << std::endl; 62 | 63 | result = a * b; 64 | std::cout << "matrices a * b :\n" << result << std::endl; 65 | } 66 | 67 | // patial access 68 | { 69 | Eigen::MatrixXf m = Eigen::MatrixXf::Random(4, 4); 70 | std::cout << "Random 4x4 matrix :\n" << m << std::endl; 71 | 72 | Eigen::Matrix2f b = 73 | m.block(1, 1, 2, 2); // coping the middle part of matrix 74 | std::cout << "Middle of 4x4 matrix :\n" << b << std::endl; 75 | 76 | m.block(1, 1, 2, 2) *= 0; // change values in original matrix 77 | std::cout << "Modified middle of 4x4 matrix :\n" << m << std::endl; 78 | 79 | m.row(1).array() += 3; 80 | std::cout << "Modified row of 4x4 matrix :\n" << m << std::endl; 81 | 82 | m.col(2).array() /= 4; 83 | std::cout << "Modified col of 4x4 matrix :\n" << m << std::endl; 84 | } 85 | 86 | // broadcasting 87 | { 88 | Eigen::MatrixXf mat = Eigen::MatrixXf::Random(2, 4); 89 | std::cout << "Random 2x4 matrix :\n" << mat << std::endl; 90 | 91 | Eigen::VectorXf v(2); // column vector 92 | v << 100, 100; 93 | mat.colwise() += v; 94 | std::cout << "Sum broadcasted over columns :\n" << mat << std::endl; 95 | } 96 | return 0; 97 | }; 98 | -------------------------------------------------------------------------------- /Chapter12/dlib/dlib-save.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | using namespace dlib; 8 | 9 | using NetworkType = loss_mean_squared>>>; 10 | using SampleType = matrix; 11 | using KernelType = linear_kernel; 12 | 13 | float func(float x) { 14 | return 4.f + 0.3f * x; // line coeficients 15 | } 16 | 17 | void TrainAndSaveKRR(const std::vector>& x, 18 | const std::vector& y) { 19 | krr_trainer trainer; 20 | trainer.set_kernel(KernelType()); 21 | decision_function df = trainer.train(x, y); 22 | serialize("dlib-krr.dat") << df; 23 | } 24 | 25 | void LoadAndPredictKRR(const std::vector>& x) { 26 | decision_function df; 27 | 28 | deserialize("dlib-krr.dat") >> df; 29 | 30 | // Predict 31 | 32 | std::cout << "KRR predictions \n"; 33 | for (auto& v : x) { 34 | auto p = df(v); 35 | std::cout << static_cast(p) << std::endl; 36 | } 37 | } 38 | 39 | void TrainAndSaveNetwork(const std::vector>& x, 40 | const std::vector& y) { 41 | NetworkType network; 42 | sgd solver; 43 | dnn_trainer trainer(network, solver); 44 | trainer.set_learning_rate(0.0001); 45 | trainer.set_mini_batch_size(50); 46 | trainer.set_max_num_epochs(300); 47 | trainer.be_verbose(); 48 | trainer.train(x, y); 49 | network.clean(); 50 | 51 | serialize("dlib-net.dat") << network; 52 | net_to_xml(network, "net.xml"); 53 | } 54 | 55 | void LoadAndPredictNetwork(const std::vector>& x) { 56 | NetworkType network; 57 | 58 | deserialize("dlib-net.dat") >> network; 59 | 60 | // Predict 61 | auto predictions = network(x); 62 | 63 | std::cout << "Net predictions \n"; 64 | for (auto p : predictions) { 65 | std::cout << static_cast(p) << std::endl; 66 | } 67 | } 68 | 69 | int main() { 70 | size_t n = 1000; 71 | std::vector> x(n); 72 | std::vector y(n); 73 | 74 | std::random_device rd; 75 | std::mt19937 re(rd()); 76 | std::uniform_real_distribution dist(-1.5, 1.5); 77 | 78 | // generate data 79 | for (size_t i = 0; i < n; ++i) { 80 | x[i].set_size(1, 1); 81 | x[i](0, 0) = i; 82 | 83 | y[i] = func(i) + dist(re); 84 | } 85 | 86 | // normalize data 87 | vector_normalizer> normalizer_x; 88 | // let the normalizer learn the mean and standard deviation of the samples 89 | normalizer_x.train(x); 90 | // now normalize each sample 91 | for (size_t i = 0; i < x.size(); ++i) { 92 | x[i] = normalizer_x(x[i]); 93 | } 94 | 95 | TrainAndSaveNetwork(x, y); 96 | TrainAndSaveKRR(x, y); 97 | 98 | // Generate new data 99 | std::cout << "Target values \n"; 100 | std::vector> new_x(5); 101 | for (size_t i = 0; i < 5; ++i) { 102 | new_x[i].set_size(1, 1); 103 | new_x[i](0, 0) = i; 104 | new_x[i] = normalizer_x(new_x[i]); 105 | std::cout << func(i) << std::endl; 106 | } 107 | 108 | // Predict 109 | LoadAndPredictNetwork(new_x); 110 | LoadAndPredictKRR(new_x); 111 | 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /Chapter01/arrayfire_samples/linalg_af.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int main() { 11 | // declaration 12 | { 13 | af::array a(3, 3, af::dtype::f32); 14 | af::array v(3, af::dtype::f64); 15 | 16 | // initialization 17 | a = af::constant(0, 3, 3); 18 | std::cout << "Zero matrix:\n"; 19 | af_print(a); 20 | 21 | a = af::identity(3, 3); 22 | std::cout << "Identity matrix:\n"; 23 | af_print(a); 24 | 25 | v = af::randu(3); 26 | std::cout << "Random vector:\n"; 27 | af_print(v); 28 | 29 | // arrayfire uses the column major format 30 | a = af::array(af::dim4(3, 3), {1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f}); 31 | std::cout << "Initializer list matrix:\n"; 32 | af_print(a); 33 | 34 | a = af::constant(3, 3, 3); 35 | std::cout << "Uniformly initialized matrix:\n"; 36 | af_print(a); 37 | 38 | a(0, 0) = 7; 39 | std::cout << "Matrix with changed element[0][0]:\n"; 40 | af_print(a); 41 | 42 | // arrayfire will copy host allcated array into its own array storage 43 | // Only device allocated memory will be not copied, but AF will take ownership of a pointer 44 | std::vector mdata = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 45 | a = af::array(3, 3, mdata.data()); 46 | std::cout << "Matrix from array:\n"; 47 | af_print(a); 48 | } 49 | // arithmetic 50 | { 51 | auto a = af::array(af::dim4(2, 2), {1, 2, 3, 4}); 52 | a = a.as(af::dtype::f32); 53 | auto b = a.copy(); 54 | 55 | // element wise operations 56 | auto result = a * b; 57 | std::cout << "element wise a * b :\n"; 58 | af_print(result); 59 | 60 | a = b * 4; 61 | std::cout << "element wise a = b * 4 :\n"; 62 | af_print(a); 63 | 64 | // matrix operations 65 | result = a + b; 66 | std::cout << "matrices a + b :\n"; 67 | af_print(result); 68 | 69 | a += b; 70 | std::cout << "matrices a += b :\n"; 71 | af_print(a); 72 | 73 | result = af::matmul(a, b); 74 | std::cout << "matrices a * b :\n"; 75 | af_print(result); 76 | } 77 | 78 | // patial access 79 | { 80 | auto m = af::iota(af::dim4(4, 4)); 81 | std::cout << "4x4 matrix :\n"; 82 | af_print(m); 83 | 84 | auto b = m(af::seq(1, 2), af::seq(1, 2)); 85 | std::cout << "Middle of 4x4 matrix :\n"; 86 | af_print(b); 87 | 88 | b *= 0; 89 | std::cout << "Modified middle of 4x4 matrix :\n"; 90 | af_print(m); 91 | 92 | m.row(1) += 3; 93 | std::cout << "Modified row of 4x4 matrix :\n"; 94 | af_print(m); 95 | 96 | m.col(2) /= 4; 97 | std::cout << "Modified col of 4x4 matrix :\n"; 98 | af_print(m); 99 | } 100 | 101 | // broadcasting 102 | { 103 | auto mat = af::constant(2, 4, 4); 104 | std::cout << "Uniform 4x4 matrix :\n"; 105 | af_print(mat); 106 | 107 | auto vec = af::array(4, {1, 2, 3, 4}); 108 | 109 | mat = af::batchFunc(vec, mat, [](const auto& a, const auto& b) { return a + b; }); 110 | 111 | std::cout << "Sum broadcasted over rows :\n"; 112 | af_print(mat); 113 | } 114 | return 0; 115 | }; 116 | -------------------------------------------------------------------------------- /Chapter14/android_detection/app/src/main/java/com/example/objectdetection/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.example.objectdetection 2 | 3 | import android.app.NativeActivity 4 | import android.content.Context 5 | import android.content.pm.PackageManager 6 | import android.hardware.camera2.CameraCharacteristics 7 | import android.hardware.camera2.CameraManager 8 | import android.hardware.camera2.CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY 9 | import android.hardware.camera2.CameraMetadata.LENS_FACING_BACK 10 | import android.os.Bundle 11 | import android.view.WindowManager 12 | import android.widget.Toast 13 | import androidx.core.app.ActivityCompat 14 | 15 | class MainActivity : NativeActivity(), ActivityCompat.OnRequestPermissionsResultCallback { 16 | 17 | // private lateinit var camId: String 18 | // 19 | // override fun onCreate(savedInstanceState: Bundle?) { 20 | // super.onCreate(savedInstanceState) 21 | // 22 | // } 23 | 24 | override fun onResume() { 25 | super.onResume() 26 | 27 | // Ask for camera permission if necessary 28 | val cameraPermission = android.Manifest.permission.CAMERA 29 | if (checkSelfPermission(cameraPermission) != PackageManager.PERMISSION_GRANTED 30 | ) { 31 | requestPermissions(arrayOf(cameraPermission), CAM_PERMISSION_CODE) 32 | } else { 33 | val camId = getCameraBackCameraId() 34 | if (camId.isEmpty()) { 35 | Toast.makeText( 36 | this, "Camera probably won't work on this device!", 37 | Toast.LENGTH_LONG 38 | ).show() 39 | finish() 40 | } 41 | 42 | initObjectDetection(camId) 43 | } 44 | } 45 | 46 | override fun onPause() { 47 | super.onPause() 48 | stopObjectDetection() 49 | } 50 | 51 | override fun onRequestPermissionsResult( 52 | requestCode: Int, 53 | permissions: Array, 54 | grantResults: IntArray 55 | ) { 56 | super.onRequestPermissionsResult(requestCode, permissions, grantResults) 57 | if (requestCode == CAM_PERMISSION_CODE 58 | && grantResults[0] != PackageManager.PERMISSION_GRANTED 59 | ) { 60 | Toast.makeText(this, "This app requires camera permission", Toast.LENGTH_SHORT).show() 61 | finish() 62 | } 63 | } 64 | 65 | private fun getCameraBackCameraId(): String { 66 | val camManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager 67 | 68 | for (camId in camManager.cameraIdList) { 69 | val characteristics = camManager.getCameraCharacteristics(camId) 70 | val hwLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL) 71 | val facing = characteristics.get(CameraCharacteristics.LENS_FACING) 72 | if (hwLevel != INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY && facing == LENS_FACING_BACK) { 73 | return camId 74 | } 75 | } 76 | 77 | return "" 78 | } 79 | 80 | 81 | private external fun initObjectDetection(camId: String) 82 | private external fun stopObjectDetection() 83 | 84 | companion object { 85 | const val CAM_PERMISSION_CODE = 1 86 | 87 | init { 88 | System.loadLibrary("object-detection") 89 | } 90 | } 91 | } -------------------------------------------------------------------------------- /Chapter13/flashlight/mlflow.cc: -------------------------------------------------------------------------------- 1 | #include "mlflow.h" 2 | 3 | #include 4 | #include 5 | 6 | namespace { 7 | bool check_result(const httplib::Result& res, int code) { 8 | if (!res) { 9 | throw std::runtime_error("REST error: " + httplib::to_string(res.error())); 10 | } 11 | return res->status == code; 12 | }; 13 | 14 | void handle_result(const httplib::Result& res) { 15 | if (check_result(res, 200)) 16 | return; 17 | std::ostringstream oss; 18 | oss << "Request error status: " << res->status << " " << httplib::status_message(res->status); 19 | oss << ", message: " << std::endl 20 | << res->body; 21 | throw std::runtime_error(oss.str()); 22 | }; 23 | } // namespace 24 | 25 | MLFlow::MLFlow() 26 | : http_client_("http://127.0.0.1:5000") {} 27 | 28 | MLFlow::MLFlow(const std::string& host, size_t port) 29 | : http_client_(host, port) { 30 | } 31 | 32 | void MLFlow::set_experiment(const std::string& name) { 33 | auto res = http_client_.Get("/api/2.0/mlflow/experiments/get-by-name?experiment_name=" + name); 34 | if (check_result(res, 404)) { 35 | // create new one 36 | nlohmann::json request; 37 | request["name"] = name; 38 | res = http_client_.Post("/api/2.0/mlflow/experiments/create", request.dump(), "application/json"); 39 | handle_result(res); 40 | // remember id 41 | auto json = nlohmann::json::parse(res->body); 42 | experiment_id_ = json["experiment_id"].get(); 43 | } else if (check_result(res, 200)) { 44 | // remember id 45 | auto json = nlohmann::json::parse(res->body); 46 | experiment_id_ = json["experiment"]["experiment_id"].get(); 47 | } else { 48 | handle_result(res); 49 | } 50 | } 51 | 52 | void MLFlow::start_run() { 53 | nlohmann::json request; 54 | request["experiment_id"] = experiment_id_; 55 | request["start_time"] = duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); 56 | auto res = http_client_.Post("/api/2.0/mlflow/runs/create", request.dump(), "application/json"); 57 | handle_result(res); 58 | auto json = nlohmann::json::parse(res->body); 59 | run_id_ = json["run"]["info"]["run_id"]; 60 | } 61 | 62 | void MLFlow::end_run() { 63 | nlohmann::json request; 64 | request["run_id"] = run_id_; 65 | request["status"] = "FINISHED"; 66 | request["end_time"] = duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); 67 | 68 | auto res = http_client_.Post("/api/2.0/mlflow/runs/update", request.dump(), "application/json"); 69 | handle_result(res); 70 | } 71 | 72 | void MLFlow::log_metric(const std::string& name, float value, size_t epoch) { 73 | nlohmann::json request; 74 | request["run_id"] = run_id_; 75 | request["key"] = name; 76 | request["value"] = value; 77 | request["step"] = epoch; 78 | request["timestamp"] = duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); 79 | 80 | auto res = 81 | http_client_.Post("/api/2.0/mlflow/runs/log-metric", request.dump(), "application/json"); 82 | handle_result(res); 83 | } 84 | 85 | void MLFlow::log_param(const std::string& name, const std::string& value) { 86 | nlohmann::json request; 87 | request["run_id"] = run_id_; 88 | request["key"] = name; 89 | request["value"] = value; 90 | 91 | auto res = 92 | http_client_.Post("/api/2.0/mlflow/runs/log-parameter", request.dump(), "application/json"); 93 | handle_result(res); 94 | } 95 | -------------------------------------------------------------------------------- /Chapter02/csv/eigen/csv.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace fs = std::filesystem; 9 | 10 | template 11 | bool read_row_help(std::index_sequence, T& row, R& r) { 12 | return r.read_row(std::get(row)...); 13 | } 14 | 15 | template 16 | void fill_values(std::index_sequence, 17 | T& row, 18 | std::vector& data) { 19 | data.insert(data.end(), {std::get(row)...}); 20 | } 21 | 22 | int main(int argc, char** argv) { 23 | if (argc > 1) { 24 | auto file_path = fs::path(argv[1]); 25 | if (fs::exists(file_path)) { 26 | const uint32_t columns_num = 5; 27 | io::CSVReader csv_reader(file_path); 28 | 29 | std::vector categorical_column; 30 | std::vector values; 31 | using RowType = std::tuple; 32 | RowType row; 33 | 34 | uint32_t rows_num = 0; 35 | try { 36 | bool done = false; 37 | while (!done) { 38 | done = !read_row_help( 39 | std::make_index_sequence::value>{}, row, 40 | csv_reader); 41 | if (!done) { 42 | categorical_column.push_back(std::get<4>(row)); 43 | fill_values(std::make_index_sequence{}, row, 44 | values); 45 | ++rows_num; 46 | } 47 | } 48 | } catch (const io::error::no_digit& err) { 49 | // ignore bad formated samples 50 | std::cerr << err.what() << std::endl; 51 | } 52 | 53 | auto x_data = Eigen::Map>( 55 | values.data(), rows_num, columns_num - 1); 56 | 57 | std::cout << x_data << std::endl; 58 | 59 | // Feature-scaling(Normalization): 60 | // Standardization - zero mean + 1 std 61 | Eigen::Array std_dev = 62 | ((x_data.rowwise() - x_data.colwise().mean()) 63 | .array() 64 | .square() 65 | .colwise() 66 | .sum() / 67 | (x_data.rows() - 1)) 68 | .sqrt(); 69 | 70 | Eigen::Matrix x_data_std = 71 | (x_data.rowwise() - x_data.colwise().mean()).array().rowwise() / 72 | std_dev; 73 | 74 | std::cout << x_data_std << std::endl; 75 | 76 | // Min-Max normalization 77 | Eigen::Matrix x_data_min_max = 78 | (x_data.rowwise() - x_data.colwise().minCoeff()).array().rowwise() / 79 | (x_data.colwise().maxCoeff() - x_data.colwise().minCoeff()).array(); 80 | 81 | std::cout << x_data_min_max << std::endl; 82 | 83 | // Average normalization 84 | Eigen::Matrix x_data_avg = 85 | (x_data.rowwise() - x_data.colwise().mean()).array().rowwise() / 86 | (x_data.colwise().maxCoeff() - x_data.colwise().minCoeff()).array(); 87 | 88 | std::cout << x_data_avg << std::endl; 89 | 90 | } else { 91 | std::cout << "File path is incorrect " << file_path << "\n"; 92 | } 93 | } else { 94 | std::cout << "Please provide a path to a dataset file\n"; 95 | } 96 | 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /Chapter02/hdf5/hdf5.cc: -------------------------------------------------------------------------------- 1 | #include "../json/reviewsreader.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | const std::string file_name("reviews.h5"); 8 | 9 | // convert datset from json to hdf5 format and read from it 10 | 11 | int main(int argc, char** argv) { 12 | try { 13 | if (argc > 1) { 14 | auto papers = ReadPapersReviews(argv[1]); 15 | 16 | // write dataset 17 | { 18 | HighFive::File file(file_name, HighFive::File::ReadWrite | 19 | HighFive::File::Create | 20 | HighFive::File::Truncate); 21 | 22 | auto papers_group = file.createGroup("papers"); 23 | for (const auto& paper : papers) { 24 | auto paper_group = 25 | papers_group.createGroup("paper_" + std::to_string(paper.id)); 26 | std::vector id = {paper.id}; 27 | auto id_attr = paper_group.createAttribute( 28 | "id", HighFive::DataSpace::From(id)); 29 | 30 | id_attr.write(id); 31 | auto dec_attr = paper_group.createAttribute( 32 | "preliminary_decision", 33 | HighFive::DataSpace::From(paper.preliminary_decision)); 34 | dec_attr.write(paper.preliminary_decision); 35 | auto reviews_group = paper_group.createGroup("reviews"); 36 | 37 | std::vector dims = {3}; 38 | std::vector values(3); 39 | for (const auto& r : paper.reviews) { 40 | auto dataset = reviews_group.createDataSet( 41 | std::to_string(r.id), HighFive::DataSpace(dims)); 42 | values[0] = std::stoi(r.confidence); 43 | values[1] = std::stoi(r.evaluation); 44 | values[2] = std::stoi(r.orientation); 45 | dataset.write(values); 46 | } 47 | } 48 | } 49 | // read dataset 50 | { 51 | HighFive::File file(file_name, HighFive::File::ReadOnly); 52 | auto papers_group = file.getGroup("papers"); 53 | auto papers_names = papers_group.listObjectNames(); 54 | for (const auto& pname : papers_names) { 55 | auto paper_group = papers_group.getGroup(pname); 56 | std::vector id; 57 | paper_group.getAttribute("id").read(id); 58 | std::cout << id[0]; 59 | 60 | std::string decision; 61 | paper_group.getAttribute("preliminary_decision").read(decision); 62 | std::cout << " " << decision << std::endl; 63 | 64 | auto reviews_group = paper_group.getGroup("reviews"); 65 | auto reviews_names = reviews_group.listObjectNames(); 66 | std::vector values(2); 67 | for (const auto& rname : reviews_names) { 68 | std::cout << "\t review: " << rname << std::endl; 69 | auto dataset = reviews_group.getDataSet(rname); 70 | auto selection = dataset.select( 71 | {1}, {2}); // or use just dataset.read method to get whole data 72 | selection.read(values); 73 | std::cout << "\t\t evaluation: " << values[0] << std::endl; 74 | std::cout << "\t\t orientation: " << values[1] << std::endl; 75 | } 76 | } 77 | } 78 | 79 | } else { 80 | std::cerr << "Please provide path to the dataset \n"; 81 | } 82 | } catch (const std::exception& err) { 83 | std::cerr << err.what() << std::endl; 84 | } 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /Chapter01/xtensor_samples/linalg_xtensor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | int main() { 13 | { 14 | // declaration of dynamically sized array 15 | { 16 | std::vector shape = {3, 2, 4}; 17 | xt::xarray a(shape); 18 | } 19 | // declaration of dynamically sized tensor with fixed dimmentions number 20 | { 21 | std::array shape = {3, 2, 4}; 22 | xt::xtensor a(shape); 23 | } 24 | 25 | // declaration of tensor with shape fixed at compile time. 26 | { xt::xtensor_fixed> a; } 27 | 28 | // Initialization of xtensor arrays can be done with C++ initializer lists: 29 | { 30 | xt::xarray arr1{{1.0, 2.0, 3.0}, 31 | {2.0, 5.0, 7.0}, 32 | {2.0, 5.0, 7.0}}; // initialize a 3x3 array 33 | std::cout << "Tensor from initializer list :\n" << arr1 << std::endl; 34 | } 35 | // Special types of initializers 36 | { 37 | std::vector shape = {2, 2}; 38 | std::cout << "Ones matrix :\n" << xt::ones(shape) << std::endl; 39 | std::cout << "Zero matrix :\n" << xt::zeros(shape) << std::endl; 40 | std::cout << "Matrix with ones on the diagonal:\n" 41 | << xt::eye(shape) << std::endl; 42 | } 43 | // Mapping c++ array to tensors 44 | { 45 | std::vector data{1, 2, 3, 4}; 46 | std::vector shape{2, 2}; 47 | auto data_x = xt::adapt(data, shape); 48 | std::cout << "Matrix from vector :\n" << data_x << std::endl; 49 | } 50 | // Element access 51 | { 52 | std::vector shape = {3, 2, 4}; 53 | xt::xarray a = xt::ones(shape); 54 | a(2, 1, 3) = 3.14f; 55 | std::cout << "Updated element :\n" << a << std::endl; 56 | } 57 | // Arithmetic operations examples 58 | { 59 | xt::xarray a = xt::random::rand({2, 2}); 60 | xt::xarray b = xt::random::rand({2, 2}); 61 | 62 | std::cout << "A: \n" << a << std::endl; 63 | std::cout << "B: \n" << b << std::endl; 64 | 65 | xt::xarray c = a + b; 66 | std::cout << "c = a + b \n" << c << std::endl; 67 | a -= b; 68 | std::cout << "a -= b \n" << a << std::endl; 69 | c = xt::linalg::dot(a, b); 70 | std::cout << "a dot b \n" << c << std::endl; 71 | c = a + 5; 72 | std::cout << "c = a + 5 \n" << c << std::endl; 73 | c = a * b; 74 | std::cout << "c = a * b \n" << c << std::endl; 75 | } 76 | // Partial access to xtensor containers 77 | { 78 | xt::xarray a{ 79 | {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; 80 | auto b = xt::view(a, xt::range(1, 3), xt::range(1, 3)); 81 | std::cout << "Partial view on a \n" << b << std::endl; 82 | } 83 | // Broadcasting 84 | { 85 | auto a = xt::xarray({{1, 2}, {3, 4}}); 86 | auto b = xt::xarray({10, 20}); 87 | b.reshape({2, 1}); 88 | std::cout << "A: \n" << a << std::endl; 89 | std::cout << "B: \n" << b << std::endl; 90 | auto c = a + b; 91 | std::cout << "Columns broadcasting: \n" << c << std::endl; 92 | } 93 | } 94 | return 0; 95 | }; 96 | -------------------------------------------------------------------------------- /Chapter09/dlib/dlib_ensemble.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using DataType = double; 10 | using SampleType = dlib::matrix; 11 | using Samples = std::vector; 12 | using Labels = std::vector; 13 | using DataSamples = std::vector; 14 | 15 | void PlotResults(const DataSamples& test_features, 16 | const Labels& test_labels, 17 | const Labels& pred_labels, 18 | const std::string& title, 19 | const std::string& file_name) { 20 | plotcpp::Plot plt; 21 | plt.SetTerminal("png"); 22 | plt.SetOutput(file_name + ".png"); 23 | plt.SetTitle(title); 24 | plt.SetXLabel("x"); 25 | plt.SetYLabel("y"); 26 | plt.SetAutoscale(); 27 | plt.GnuplotCommand("set grid"); 28 | 29 | plt.Draw2D( 30 | plotcpp::Points(test_features.begin(), test_features.end(), test_labels.begin(), 31 | "orig", "lc rgb 'black' pt 7"), 32 | plotcpp::Lines(test_features.begin(), test_features.end(), pred_labels.begin(), 33 | "pred", "lc rgb 'red' lw 2")); 34 | plt.Flush(); 35 | } 36 | 37 | std::pair GenerateNoiseData(DataType start, 38 | DataType end, 39 | size_t n) { 40 | Samples x; 41 | x.resize(n); 42 | Labels y; 43 | y.resize(n); 44 | 45 | std::mt19937 re(3467); 46 | std::uniform_real_distribution dist(start, end); 47 | std::normal_distribution noise_dist; 48 | 49 | for (size_t i = 0; i < n; ++i) { 50 | auto x_val = dist(re); 51 | auto y_val = std::cos(M_PI * x_val) + (noise_dist(re) * 0.3); 52 | x[i] = SampleType({x_val}); 53 | y[i] = y_val; 54 | } 55 | 56 | return {x, y}; 57 | } 58 | 59 | std::pair GenerateData(DataType start, 60 | DataType end, 61 | size_t n) { 62 | Samples x; 63 | x.resize(n); 64 | Labels y; 65 | y.resize(n); 66 | 67 | auto step = (end - start) / (n - 1); 68 | auto x_val = start; 69 | size_t i = 0; 70 | for (auto& x_item : x) { 71 | x_item = SampleType({x_val}); 72 | auto y_val = std::cos(M_PI * x_val); 73 | y[i] = y_val; 74 | x_val += step; 75 | ++i; 76 | } 77 | 78 | return {x, y}; 79 | } 80 | 81 | int main() { 82 | using namespace dlib; 83 | 84 | constexpr DataType start = -10; 85 | constexpr DataType end = 10; 86 | constexpr size_t num_samples = 1000; 87 | constexpr size_t num_trees = 1000; 88 | 89 | random_forest_regression_trainer trainer; 90 | trainer.set_num_trees(num_trees); 91 | trainer.set_seed("random forest"); 92 | 93 | auto [train_samples, train_lables] = GenerateNoiseData(start, end, num_samples); 94 | auto random_forest = trainer.train(train_samples, train_lables); 95 | 96 | DataSamples data_samples(num_samples); 97 | Labels pred_lables(num_samples); 98 | size_t i = 0; 99 | auto [test_samples, test_lables] = GenerateData(start, end, num_samples); 100 | for (const auto& sample : test_samples) { 101 | auto prediction = random_forest(sample); 102 | data_samples[i] = sample(0); 103 | pred_lables[i] = prediction; 104 | ++i; 105 | } 106 | PlotResults(data_samples, test_lables, pred_lables, "DLib Random Forest", "dlib-rf"); 107 | 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /Chapter11/pytorch/main.cpp: -------------------------------------------------------------------------------- 1 | #include "imdbdataset.h" 2 | #include "model.h" 3 | 4 | #include 5 | #include 6 | 7 | namespace fs = std::filesystem; 8 | 9 | struct Stack : public torch::data::transforms::Collation { 10 | ImdbExample apply_batch(std::vector examples) override { 11 | std::vector input_ids; 12 | std::vector attention_masks; 13 | std::vector labels; 14 | input_ids.reserve(examples.size()); 15 | attention_masks.reserve(examples.size()); 16 | labels.reserve(examples.size()); 17 | for (auto& example : examples) { 18 | input_ids.push_back(std::move(example.data.first)); 19 | attention_masks.push_back(std::move(example.data.second)); 20 | labels.push_back(std::move(example.target)); 21 | } 22 | return {{torch::stack(input_ids), torch::stack(attention_masks)}, torch::stack(labels)}; 23 | } 24 | }; 25 | 26 | int main(int argc, char** argv) { 27 | if (argc == 4) { 28 | auto dataset_path = fs::path(argv[1]); 29 | if (!fs::exists(dataset_path)) { 30 | std::cerr << "Incorrect dataset path!\n"; 31 | } 32 | auto vocab_path = fs::path(argv[2]); 33 | if (!fs::exists(vocab_path)) { 34 | std::cerr << "Incorrect vocabulary path!\n"; 35 | } 36 | auto model_path = fs::path(argv[3]); 37 | if (!fs::exists(model_path)) { 38 | std::cerr << "Incorrect model path!\n"; 39 | } 40 | 41 | torch::DeviceType device = torch::cuda::is_available() 42 | ? torch::DeviceType::CUDA 43 | : torch::DeviceType::CPU; 44 | 45 | auto tokenizer = std::make_shared(vocab_path); 46 | ImdbDataset train_dataset(dataset_path / "train", tokenizer); 47 | 48 | int batch_size = 8; 49 | auto train_loader = torch::data::make_data_loader( 50 | train_dataset.map(Stack()), 51 | torch::data::DataLoaderOptions().batch_size(batch_size).workers(8)); // random sampler is default 52 | 53 | Model model(model_path); 54 | model->to(device); 55 | 56 | torch::optim::AdamW optimizer(model->parameters(), 57 | torch::optim::AdamWOptions(1e-5)); 58 | 59 | int epochs = 100; 60 | for (int epoch = 0; epoch < epochs; ++epoch) { 61 | model->train(); 62 | 63 | int batch_index = 0; 64 | for (auto& batch : (*train_loader)) { 65 | optimizer.zero_grad(); 66 | 67 | auto batch_label = batch.target.to(device); 68 | auto batch_input_ids = batch.data.first.squeeze(1).to(device); 69 | auto batch_attention_mask = batch.data.second.squeeze(1).to(device); 70 | 71 | auto output = model(batch_input_ids, batch_attention_mask); 72 | 73 | torch::Tensor loss = 74 | torch::cross_entropy_loss(output, batch_label); 75 | 76 | loss.backward(); 77 | torch::nn::utils::clip_grad_norm_(model->parameters(), 1.0); 78 | optimizer.step(); 79 | 80 | // Output the loss and accuracy every 10 batches. 81 | if (++batch_index % 10 == 0) { 82 | auto predictions = output.argmax(/*dim=*/1); 83 | auto acc = (predictions == batch_label).sum().item() / batch_size; 84 | 85 | std::cout << "Epoch: " << epoch << " | Batch: " << batch_index 86 | << " | Loss: " << loss.item() << " | Acc: " << acc << std::endl; 87 | } 88 | } 89 | } 90 | return 0; 91 | } 92 | 93 | std::cerr << "Please specify dataset folder, vocablary file path, and model file path\n"; 94 | return 1; 95 | } 96 | -------------------------------------------------------------------------------- /Chapter03/mlpack/grid-mlpack.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | std::pair GenerateData(size_t num_samples) { 7 | arma::mat samples = arma::randn(1, num_samples); 8 | arma::rowvec labels = samples + arma::randn(num_samples, arma::distr_param(1.0, 1.5)); 9 | return {samples, labels}; 10 | } 11 | 12 | int main(int /*argc*/, char** /*argv*/) { 13 | using namespace mlpack; 14 | 15 | size_t num_samples = 1000; 16 | auto [raw_samples, raw_labels] = GenerateData(num_samples); 17 | 18 | auto mm = std::minmax_element( 19 | raw_samples.begin(), raw_samples.end(), 20 | [](const auto& a, const auto& b) { return a < b; }); 21 | std::pair x_minmax{*mm.first, *mm.second}; 22 | 23 | // Normalize data 24 | data::StandardScaler sample_scaler; 25 | sample_scaler.Fit(raw_samples); 26 | arma::mat samples(1, num_samples); 27 | sample_scaler.Transform(raw_samples, samples); 28 | 29 | data::StandardScaler label_scaler; 30 | label_scaler.Fit(raw_labels); 31 | arma::rowvec labels(num_samples); 32 | label_scaler.Transform(raw_labels, labels); 33 | 34 | // Randomize data 35 | arma::mat rand_samples(1, num_samples); 36 | arma::rowvec rand_labels(num_samples); 37 | ShuffleData(samples, labels, rand_samples, rand_labels); 38 | 39 | // Grid search for the best regularization parameter lambda 40 | // Using 80% of data for training and remaining 20% for assessing MSE. 41 | double validation_size = 0.2; 42 | HyperParameterTuner, MSE, SimpleCV> parameters_tuner(validation_size, 43 | rand_samples, rand_labels); 44 | 45 | // Finding the best value for lambda from the values 0.0, 0.001, 0.01, 0.1, 46 | // and 1.0. 47 | arma::vec lambdas{0.0, 0.001, 0.01, 0.1, 1.0}; 48 | double best_lambda = 0; 49 | std::tie(best_lambda) = parameters_tuner.Optimize(lambdas); 50 | 51 | std::cout << "Best lambda: " << best_lambda << std::endl; 52 | 53 | // Train model 54 | // double lambda = 0.01; 55 | // LinearRegression<> linear_regression(rand_samples, rand_labels, lambda); 56 | 57 | // Use best model 58 | LinearRegression<>& linear_regression = parameters_tuner.BestModel(); 59 | 60 | // Make new perdictions 61 | size_t num_new_samples = 50; 62 | arma::dvec new_samples_values = arma::linspace(x_minmax.first, x_minmax.second, num_new_samples); 63 | arma::mat new_samples(1, num_new_samples); 64 | new_samples.row(0) = arma::trans(new_samples_values); 65 | arma::mat norm_new_samples(1, num_new_samples); 66 | sample_scaler.Transform(new_samples, norm_new_samples); 67 | arma::rowvec predictions(num_new_samples); 68 | linear_regression.Predict(norm_new_samples, predictions); 69 | 70 | // Plot perdictions 71 | plotcpp::Plot plt; 72 | plt.SetTerminal("png"); 73 | plt.SetOutput("plot.png"); 74 | plt.SetTitle("Linear regression"); 75 | plt.SetXLabel("x"); 76 | plt.SetYLabel("y"); 77 | plt.SetAutoscale(); 78 | plt.GnuplotCommand("set grid"); 79 | 80 | std::vector x_coords(samples.size()); 81 | std::copy(samples.begin(), samples.end(), x_coords.begin()); 82 | 83 | std::vector x_pred_coords(new_samples.size()); 84 | std::copy(new_samples.begin(), new_samples.end(), x_pred_coords.begin()); 85 | 86 | plt.Draw2D(plotcpp::Points(x_coords.begin(), x_coords.end(), 87 | labels.begin(), "orig", "lc rgb 'black' pt 7"), 88 | plotcpp::Lines(x_pred_coords.begin(), x_pred_coords.end(), 89 | predictions.begin(), "pred", "lc rgb 'red' lw 2")); 90 | plt.Flush(); 91 | 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /Chapter13/flashlight/fl_track.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "mlflow.h" 5 | 6 | #include 7 | 8 | std::shared_ptr make_dataset(int64_t n, int batch_size) { 9 | // generate some linear dependency 10 | auto x = fl::randn({n}); 11 | auto y = x * 0.3f + 0.4f; 12 | 13 | // and add some small noise 14 | auto noise = fl::randn({n}) * 0.1f; 15 | y += noise; 16 | 17 | std::vector fields{x, y}; 18 | auto dataset = std::make_shared(fields); 19 | return std::make_shared(dataset, batch_size); 20 | } 21 | 22 | int main() { 23 | fl::init(); 24 | 25 | MLFlow mlflow; 26 | mlflow.set_experiment("Linear regression"); 27 | 28 | // set run parameters 29 | int batch_size = 64; 30 | float learning_rate = 0.0001; 31 | float momentum = 0.01; 32 | const int epochs = 100; 33 | 34 | // start a run 35 | mlflow.start_run(); 36 | 37 | auto train_dataset = make_dataset(/*n=*/10000, batch_size); 38 | auto test_dataset = make_dataset(/*n=*/1000, batch_size); 39 | 40 | // Define a model 41 | fl::Sequential model; 42 | model.add(fl::View({1, 1, 1, -1})); // to process a batch 43 | model.add(fl::Linear(1, 1)); 44 | 45 | // define MSE loss 46 | auto loss = fl::MeanSquaredError(); 47 | 48 | // Define optimizer 49 | auto sgd = fl::SGDOptimizer(model.params(), learning_rate, momentum); 50 | 51 | // Define epoch average MSE meter 52 | fl::AverageValueMeter meter; 53 | 54 | // launch the training cycle 55 | for (int epoch_i = 0; epoch_i < epochs; ++epoch_i) { 56 | meter.reset(); 57 | model.train(); 58 | for (auto& batch : *train_dataset) { 59 | sgd.zeroGrad(); 60 | 61 | // Forward propagation 62 | auto predicted = model(fl::input(batch[0])); 63 | 64 | // Calculate loss 65 | auto local_batch_size = batch[0].shape().dim(0); 66 | auto target = fl::reshape(batch[1], {1, 1, 1, local_batch_size}); 67 | auto loss_value = loss(predicted, fl::noGrad(target)); 68 | 69 | // Backward propagation 70 | loss_value.backward(); 71 | 72 | // Update parameters 73 | sgd.step(); 74 | 75 | meter.add(loss_value.scalar()); 76 | } 77 | 78 | auto avr_loss_value = meter.value()[0]; 79 | mlflow.log_metric("train loss", avr_loss_value, epoch_i); 80 | std::cout << "Epoch: " << epoch_i << "\n\ttrain MSE: " << avr_loss_value << std::endl; 81 | 82 | // every 10th epoch calculate test metric 83 | if (epoch_i % 10 == 0) { 84 | fl::AverageValueMeter test_meter; 85 | model.eval(); 86 | for (auto& batch : *test_dataset) { 87 | auto predicted = model(fl::input(batch[0])); 88 | // Calculate loss 89 | auto local_batch_size = batch[0].shape().dim(0); 90 | auto target = fl::reshape(batch[1], {1, 1, 1, local_batch_size}); 91 | auto loss_value = loss(predicted, fl::noGrad(target)); 92 | test_meter.add(loss_value.scalar()); 93 | } 94 | auto avr_loss_value = test_meter.value()[0]; 95 | mlflow.log_metric("test loss", avr_loss_value, epoch_i); 96 | std::cout << "\t test MSE: " << avr_loss_value << std::endl; 97 | } 98 | } 99 | mlflow.end_run(); 100 | mlflow.log_param("epochs", epochs); 101 | mlflow.log_param("batch_size", batch_size); 102 | mlflow.log_param("learning_rate", learning_rate); 103 | mlflow.log_param("momentum", momentum); 104 | 105 | std::cout << "Learned model params:\n"; 106 | for (auto& param : model.params()) { 107 | std::cout << param.host()[0] << std::endl; 108 | } 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /Chapter08/mlpack/mlpack_recommender.cc: -------------------------------------------------------------------------------- 1 | #include "../eigen/data_loader.h" 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace fs = std::filesystem; 12 | using DataType = double; 13 | 14 | int main(int argc, char** argv) { 15 | if (argc > 1) { 16 | mlpack::Log::Info.ignoreInput = false; 17 | mlpack::Log::Warn.ignoreInput = false; 18 | // mlpack::Log::Debug.ignoreInput = false; 19 | mlpack::Log::Fatal.ignoreInput = false; 20 | 21 | auto root_path = fs::path(argv[1]); 22 | 23 | std::cout << "Data loading .." << std::endl; 24 | 25 | auto movies_file = root_path / "movies.csv"; 26 | auto movies = LoadMovies(movies_file); 27 | 28 | auto ratings_file = root_path / "ratings.csv"; 29 | auto ratings = LoadRatings(ratings_file); 30 | 31 | std::cout << "Data loaded" << std::endl; 32 | 33 | // The data which the CF constructor takes should be an Armadillo matrix 34 | // (arma::mat ) with three rows. The first row corresponds to users; the 35 | // second row corresponds to items; the third column corresponds to the 36 | // rating. This is a coordinate list format. Or a sparse matrix representing 37 | // (user, item) table 38 | 39 | arma::SpMat ratings_matrix(ratings.size(), movies.size()); 40 | std::vector movie_titles; 41 | { 42 | // merge movies and users 43 | std::cout << "Data merging..." << std::endl; 44 | // fill matrix 45 | movie_titles.resize(movies.size()); 46 | 47 | size_t user_idx = 0; 48 | for (auto& r : ratings) { 49 | for (auto& m : r.second) { 50 | auto mi = movies.find(m.first); 51 | auto movie_idx = std::distance(movies.begin(), mi); 52 | movie_titles[static_cast(movie_idx)] = mi->second; 53 | ratings_matrix(user_idx, movie_idx) = static_cast(m.second); 54 | } 55 | ++user_idx; 56 | } 57 | std::cout << "Data merged" << std::endl; 58 | } 59 | 60 | // factorization rank 61 | size_t n_factors = 100; 62 | 63 | // mlpack to avoid calculating the full rating matrix, do 64 | // nearest neighbor search only on the H matrix, using the observation that 65 | // if the rating matrix X = W*H, then d(X.col(i), X.col(j)) = d(W H.col(i), 66 | // W H.col(j)). This can be seen as nearest neighbor search on the H 67 | // matrix with the Mahalanobis distance. 68 | size_t neighborhood = 50; 69 | 70 | // Non negative matrix factorization with Alternating Least Squares 71 | // approach 72 | mlpack::NMFPolicy decomposition_policy; 73 | 74 | // mlpack::BatchSVDPolicy decomposition_policy; 75 | 76 | // stoping criterions 77 | size_t max_iterations = 20; 78 | double min_residue = 1e-3; 79 | 80 | std::cout << "Training..." << std::endl; 81 | mlpack::CFType cf(ratings_matrix, decomposition_policy, neighborhood, 82 | n_factors, max_iterations, min_residue); 83 | 84 | std::cout << "Training done" << std::endl; 85 | 86 | std::cout << "Predicting..." << std::endl; 87 | arma::Mat recommendations; 88 | // Get 5 recommendations for specified users. 89 | arma::Col users{1, 2, 3}; 90 | 91 | cf.GetRecommendations(5, recommendations, users); 92 | std::cout << "Predicting done" << std::endl; 93 | 94 | for (size_t u = 0; u < recommendations.n_cols; ++u) { 95 | std::cout << "User " << users(u) << " recomendations are: "; 96 | for (size_t i = 0; i < recommendations.n_rows; ++i) { 97 | std::cout << movie_titles[recommendations(i, u)] << ";"; 98 | } 99 | std::cout << std::endl; 100 | } 101 | } else { 102 | std::cerr << "Please provider path to the dataset folder\n"; 103 | } 104 | 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /Chapter10/pytorch/mnistdataset.cpp: -------------------------------------------------------------------------------- 1 | #include "mnistdataset.h" 2 | #include 3 | #include 4 | 5 | namespace { 6 | template 7 | bool read_header(T* out, std::istream& stream) { 8 | auto size = static_cast(sizeof(T)); 9 | T value; 10 | if (!stream.read(reinterpret_cast(&value), size)) { 11 | return false; 12 | } else { 13 | // flip endianness 14 | *out = (value << 24) | ((value << 8) & 0x00FF0000) | 15 | ((value >> 8) & 0X0000FF00) | (value >> 24); 16 | return true; 17 | } 18 | } 19 | 20 | torch::Tensor CvImageToTensor(const cv::Mat& image, torch::DeviceType device) { 21 | assert(image.channels() == 1); 22 | 23 | std::vector dims{static_cast(1), 24 | static_cast(image.rows), 25 | static_cast(image.cols)}; 26 | 27 | torch::Tensor tensor_image = 28 | torch::from_blob( 29 | image.data, torch::IntArrayRef(dims), 30 | torch::TensorOptions().dtype(torch::kFloat).requires_grad(false)) 31 | .clone(); // clone is required to copy data from temporary object 32 | return tensor_image.to(device); 33 | } 34 | 35 | } // namespace 36 | 37 | void MNISTDataset::ReadLabels(const std::string& labels_file_name) { 38 | std::ifstream labels_file(labels_file_name, 39 | std::ios::binary | std::ios::binary); 40 | labels_file.exceptions(std::ifstream::failbit | std::ifstream::badbit); 41 | if (labels_file) { 42 | uint32_t magic_num = 0; 43 | uint32_t num_items = 0; 44 | if (read_header(&magic_num, labels_file) && 45 | read_header(&num_items, labels_file)) { 46 | labels_.resize(static_cast(num_items)); 47 | labels_file.read(reinterpret_cast(labels_.data()), num_items); 48 | } 49 | } 50 | } 51 | 52 | void MNISTDataset::ReadImages(const std::string& images_file_name) { 53 | std::ifstream labels_file(images_file_name, 54 | std::ios::binary | std::ios::binary); 55 | labels_file.exceptions(std::ifstream::failbit | std::ifstream::badbit); 56 | if (labels_file) { 57 | uint32_t magic_num = 0; 58 | uint32_t num_items = 0; 59 | rows_ = 0; 60 | columns_ = 0; 61 | if (read_header(&magic_num, labels_file) && 62 | read_header(&num_items, labels_file) && 63 | read_header(&rows_, labels_file) && 64 | read_header(&columns_, labels_file)) { 65 | assert(num_items == labels_.size()); 66 | images_.resize(num_items); 67 | cv::Mat img(static_cast(rows_), static_cast(columns_), CV_8UC1); 68 | 69 | for (uint32_t i = 0; i < num_items; ++i) { 70 | labels_file.read(reinterpret_cast(img.data), 71 | static_cast(img.size().area())); 72 | img.convertTo(images_[i], CV_32F); 73 | images_[i] /= 255; // normalize 74 | cv::resize(images_[i], images_[i], 75 | cv::Size(32, 32)); // Resize to 32x32 size 76 | } 77 | } 78 | } 79 | } 80 | 81 | MNISTDataset::MNISTDataset(const std::string& images_file_name, 82 | const std::string& labels_file_name, 83 | torch::DeviceType device) 84 | : device_(device) { 85 | ReadLabels(labels_file_name); 86 | ReadImages(images_file_name); 87 | } 88 | 89 | void MNISTDataset::ShowItem(size_t index) const { 90 | cv::imshow(std::to_string(labels_[index]), images_[index]); 91 | cv::waitKey(0); 92 | cv::destroyAllWindows(); 93 | } 94 | 95 | torch::data::Example<> MNISTDataset::get(size_t index) { 96 | return {CvImageToTensor(images_[index], device_), 97 | torch::tensor(static_cast(labels_[index]), 98 | torch::TensorOptions() 99 | .dtype(torch::kLong) 100 | .device(device_))}; 101 | } 102 | 103 | torch::optional MNISTDataset::size() const { 104 | return labels_.size(); 105 | } 106 | -------------------------------------------------------------------------------- /Chapter02/img/opencv/ocv.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace fs = std::filesystem; 9 | 10 | int main(int argc, char** argv) { 11 | cv::Mat img; 12 | if (argc > 1) { 13 | auto file_path = fs::path(argv[1]); 14 | if (fs::exists(file_path)) { 15 | cv::Mat img = cv::imread(file_path.string()); 16 | } else { 17 | std::cerr << "File path " << file_path << " is invalid\n"; 18 | } 19 | } 20 | if (img.empty()) { 21 | const int width = 512; 22 | img = cv::Mat(width, width, CV_32FC3); 23 | img = cv::Scalar(255, 255, 255); 24 | cv::rectangle(img, {width / 4, width / 4}, {width / 2, width / 2}, 25 | cv::Scalar(0, 0, 0), -1); // negative thickness fill rect 26 | } 27 | cv::namedWindow("Image", 28 | cv::WINDOW_AUTOSIZE); // Create a window for display. 29 | cv::imshow("Image", img); // Show our image inside it. 30 | cv::waitKey(0); 31 | 32 | // scaling 33 | // use cv::INTER_AREA for shrinking and 34 | // cv::INTER_CUBIC (slow) or cv::INTER_LINEAR for zooming. By default, 35 | // cv::INTER_LINEAR is used for all resizing purposes. 36 | 37 | cv::resize(img, img, {img.cols / 2, img.rows / 2}, 0, 0, cv::INTER_AREA); 38 | cv::imshow("Image", img); 39 | cv::waitKey(0); 40 | 41 | cv::resize(img, img, {}, 1.5, 1.5, cv::INTER_CUBIC); 42 | cv::imshow("Image", img); 43 | cv::waitKey(0); 44 | 45 | // cropping 46 | img = img(cv::Rect(0, 0, img.cols / 2, img.rows / 2)); 47 | cv::imshow("Image", img); 48 | cv::waitKey(0); 49 | 50 | // translation 51 | cv::Mat trm = (cv::Mat_(2, 3) << 1, 0, -50, 0, 1, -50); 52 | cv::warpAffine(img, img, trm, {img.cols, img.rows}); 53 | cv::imshow("Image", img); 54 | cv::waitKey(0); 55 | 56 | // rotations 57 | auto rotm = cv::getRotationMatrix2D({img.cols / 2.f, img.rows / 2.f}, 45, 1); 58 | cv::warpAffine(img, img, rotm, {img.cols, img.rows}); 59 | cv::imshow("Image", img); 60 | cv::waitKey(0); 61 | 62 | // padding 63 | // types of orders: 64 | // BORDER_CONSTANT - single color 65 | // BORDER_REPLICATE - copy last values - aaaaaa|abcdefgh|hhhhhhh 66 | // BORDER_REFLECT - copy opossite image values - fedcba|abcdefgh|hgfedcb 67 | // BORDER_WRAP - simulate image duplication - cdefgh|abcdefgh|abcdefg 68 | // From doc: bWhen the source image is a part (ROI) of a bigger image, the 69 | // function will try to use the pixels outside of the ROI to form a border. To 70 | // disable this feature and always do extrapolation, as if src was not a ROI, 71 | // use borderType | BORDER_ISOLATED. 72 | int top = 50; // px 73 | int bottom = 20; // px 74 | int left = 150; // px 75 | int right = 5; // px 76 | cv::copyMakeBorder(img, img, top, bottom, left, right, 77 | cv::BORDER_CONSTANT | cv::BORDER_ISOLATED, 78 | cv::Scalar(255, 0, 0)); 79 | cv::imshow("Image", img); 80 | cv::waitKey(0); 81 | 82 | // Convert from default BGR to rgb 83 | cv::cvtColor(img, img, cv::COLOR_BGR2RGB); 84 | cv::imshow("Image", img); 85 | cv::waitKey(0); 86 | 87 | // Make grayscale 88 | cv::cvtColor(img, img, 89 | cv::COLOR_RGB2GRAY); // now pixels values are in range 0-1 90 | std::cout << "Grayscale image channels " << img.channels() << std::endl; 91 | cv::imshow("Image", img); 92 | cv::waitKey(0); 93 | 94 | // Change underlaying data type 95 | img.convertTo(img, CV_8UC1, 255); // float -> byte 96 | cv::imshow("Image", img); 97 | cv::waitKey(0); 98 | 99 | // Mix layers 100 | // layout of channels i in memory n OpenCV can be non continuous and 101 | // interleaved, so usually before passing OpenCV image to another library we 102 | // mix to restructure them 103 | img = cv::Mat(512, 512, CV_32FC3); 104 | img = cv::Scalar(255, 255, 255); 105 | cv::Mat bgr[3]; 106 | cv::split(img, bgr); 107 | cv::Mat ordered_channels; 108 | cv::vconcat(bgr[2], bgr[1], ordered_channels); 109 | cv::vconcat(ordered_channels, bgr[0], ordered_channels); 110 | 111 | std::cout << "Memory layout is continuous " << ordered_channels.isContinuous() 112 | << std::endl; 113 | 114 | return 0; 115 | } 116 | -------------------------------------------------------------------------------- /Chapter02/img/dlib/img_dlib.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | 8 | namespace fs = std::filesystem; 9 | 10 | int main(int argc, char** argv) { 11 | using namespace dlib; 12 | array2d img; 13 | try { 14 | if (argc > 1) { 15 | if (fs::exists(argv[1])) { 16 | load_image(img, argv[1]); 17 | } else { 18 | std::cerr << "Invalid file path " << argv[1] << std::endl; 19 | return 1; 20 | } 21 | } else { 22 | long width = 512; 23 | img.set_size(width, width); 24 | assign_all_pixels(img, rgb_pixel(255, 255, 255)); 25 | fill_rect( 26 | img, 27 | rectangle(img.nc() / 4, img.nr() / 4, img.nc() / 2, img.nr() / 2), 28 | rgb_pixel(0, 0, 0)); 29 | } 30 | unsigned long key; 31 | bool is_printable; 32 | // show original image 33 | image_window window(img, "Image"); 34 | window.get_next_keypress(key, is_printable); 35 | 36 | // scale 37 | array2d img2(img.nr() / 2, img.nc() / 2); 38 | // there are also other intepolation methods interpolate_quadratic() and 39 | // interpolate_bilinear 40 | resize_image(img, img2, interpolate_nearest_neighbor()); 41 | std::swap(img, img2); 42 | window.set_image(img); 43 | window.get_next_keypress(key, is_printable); 44 | 45 | resize_image(1.5, img); // default interpolate_bilinear 46 | window.set_image(img); 47 | window.get_next_keypress(key, is_printable); 48 | 49 | // crop 50 | extract_image_chip(img, rectangle(0, 0, img.nc() / 2, img.nr() / 2), img2); 51 | std::swap(img, img2); 52 | window.set_image(img); 53 | window.get_next_keypress(key, is_printable); 54 | 55 | // translation 56 | img2.set_size(img.nr(), img.nc()); 57 | transform_image(img, img2, interpolate_bilinear(), 58 | point_transform_affine(identity_matrix(2), 59 | dlib::vector(-50, -50))); 60 | std::swap(img, img2); 61 | window.set_image(img); 62 | window.get_next_keypress(key, is_printable); 63 | 64 | // rotations 65 | rotate_image(img, img2, -45, interpolate_bilinear()); 66 | std::swap(img, img2); 67 | window.set_image(img); 68 | window.get_next_keypress(key, is_printable); 69 | 70 | // padding 71 | // There is no copyMakeBorder analog in dlib 72 | // There is assign_border_pixels and zero_border_pixels for equal pixels 73 | 74 | int top = 50; // px 75 | int bottom = 20; // px 76 | int left = 150; // px 77 | int right = 5; // px 78 | img2.set_size(img.nr() + top + bottom, img.nc() + left + right); 79 | transform_image( 80 | img, img2, interpolate_bilinear(), 81 | point_transform_affine(identity_matrix(2), 82 | dlib::vector(-left / 2, -top / 2))); 83 | std::swap(img, img2); 84 | window.set_image(img); 85 | window.get_next_keypress(key, is_printable); 86 | 87 | // Convert from default BGR to rgb 88 | array2d img_bgr; 89 | assign_image(img_bgr, img); 90 | 91 | // Make grayscale 92 | array2d img_gray; 93 | assign_image(img_gray, img); 94 | 95 | // Change underlaying data type 96 | array2d img_float; 97 | assign_image(img_float, img); 98 | 99 | // Mix layers 100 | // There are no special functions in dlib to manipulate channels 101 | // but we can access raw data with image_data function and get row padding 102 | // with width_step. Pixels stored in row major order with interleaved 103 | // channels or do simple loop over all pixels 104 | array2d rgb_img(5, 5); 105 | assign_all_pixels(rgb_img, rgb_pixel(255, 128, 64)); 106 | auto channel_size = static_cast(rgb_img.nc() * rgb_img.nr()); 107 | std::vector ch1(channel_size); 108 | std::vector ch2(channel_size); 109 | std::vector ch3(channel_size); 110 | size_t i{0}; 111 | for (long r = 0; r < rgb_img.nr(); ++r) { 112 | for (long c = 0; c < rgb_img.nc(); ++c) { 113 | ch1[i] = rgb_img[r][c].red; 114 | ch2[i] = rgb_img[r][c].green; 115 | ch3[i] = rgb_img[r][c].blue; 116 | ++i; 117 | } 118 | } 119 | } catch (const std::exception& err) { 120 | std::cerr << err.what(); 121 | } 122 | 123 | return 0; 124 | } 125 | -------------------------------------------------------------------------------- /Chapter01/blaze_samples/linalg_blaze.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "blaze/math/TransposeFlag.h" 4 | #include "blaze/math/dense/UniformVector.h" 5 | #include "blaze/math/expressions/Forward.h" 6 | #include "blaze/math/sparse/IdentityMatrix.h" 7 | #include "blaze/util/Random.h" 8 | 9 | typedef blaze::StaticMatrix MyMatrix33f; 10 | typedef blaze::StaticVector MyVector3f; 11 | typedef blaze::DynamicMatrix MyMatrix; 12 | 13 | int main() { 14 | { 15 | // declaration 16 | MyMatrix33f a; 17 | MyVector3f v; 18 | MyMatrix m(10, 15); 19 | 20 | // initialization 21 | a = blaze::zero(3UL, 3UL); 22 | std::cout << "Zero matrix:\n" 23 | << a << std::endl; 24 | 25 | a = blaze::IdentityMatrix(3UL); 26 | std::cout << "Identity matrix:\n" 27 | << a << std::endl; 28 | 29 | blaze::Rand rnd; 30 | v = blaze::generate(3UL, [&](size_t) { return rnd.generate(); }); 31 | std::cout << "Random vector:\n" 32 | << v << std::endl; 33 | 34 | a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; 35 | std::cout << "Initializer list matrix:\n" 36 | << a << std::endl; 37 | 38 | a = blaze::uniform(3UL, 3UL, 3.f); 39 | std::cout << "Uniformly initialized matrix:\n" 40 | << a << std::endl; 41 | 42 | a(0, 0) = 7; 43 | std::cout << "Matrix with changed element[0][0]:\n" 44 | << a << std::endl; 45 | 46 | std::array data = {1, 2, 3, 4}; 47 | blaze::CustomVector v2(data.data(), data.size()); 48 | data[1] = 5; 49 | std::cout << "Vector mapped to array:\n" 50 | << v2 << std::endl; 51 | 52 | std::vector mdata = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 53 | blaze::CustomMatrix a2(mdata.data(), 3UL, 3UL); 54 | std::cout << "Matrix mapped to array:\n" 55 | << a2 << std::endl; 56 | } 57 | // arithmetic 58 | { 59 | blaze::StaticMatrix a = {{1, 2}, {3, 4}}; 60 | auto b = a; 61 | 62 | // element wise operations 63 | blaze::StaticMatrix result = a % b; 64 | std::cout << "element wise a * b :\n" 65 | << result << std::endl; 66 | 67 | a = b * 4; 68 | std::cout << "element wise a = b * 4 :\n" 69 | << a << std::endl; 70 | 71 | // matrix operations 72 | result = a + b; 73 | std::cout << "matrices a + b :\n" 74 | << result << std::endl; 75 | 76 | a += b; 77 | std::cout << "matrices a += b :\n" 78 | << a << std::endl; 79 | 80 | result = a * b; 81 | std::cout << "matrices a * b :\n" 82 | << result << std::endl; 83 | } 84 | 85 | // patial access 86 | { 87 | blaze::StaticMatrix m = {{1, 2, 3, 4}, 88 | {5, 6, 7, 8}, 89 | {9, 10, 11, 12}, 90 | {13, 14, 15, 16}}; 91 | std::cout << "4x4 matrix :\n" 92 | << m << std::endl; 93 | 94 | blaze::StaticMatrix b = 95 | blaze::submatrix<1UL, 1UL, 2UL, 2UL>(m); // coping the middle part of matrix 96 | std::cout << "Middle of 4x4 matrix :\n" 97 | << b << std::endl; 98 | 99 | blaze::submatrix<1UL, 1UL, 2UL, 2UL>(m) *= 0; // change values in original matrix 100 | std::cout << "Modified middle of 4x4 matrix :\n" 101 | << m << std::endl; 102 | 103 | blaze::row<1UL>(m) += 3; 104 | std::cout << "Modified row of 4x4 matrix :\n" 105 | << m << std::endl; 106 | 107 | blaze::column<2UL>(m) /= 4; 108 | std::cout << "Modified col of 4x4 matrix :\n" 109 | << m << std::endl; 110 | } 111 | 112 | // broadcasting (There is no implicit brodcasting in Blaze ) 113 | { 114 | blaze::DynamicMatrix mat = blaze::uniform(4UL, 4UL, 2); 115 | std::cout << "Uniform 4x4 matrix :\n" 116 | << mat << std::endl; 117 | 118 | blaze::DynamicVector vec = {1, 2, 3, 4}; 119 | auto ex_vec = blaze::expand(vec, 4UL); // no allocation, gives a proxy object 120 | std::cout << "expanded vector :\n" 121 | << ex_vec << std::endl; 122 | 123 | mat += ex_vec; 124 | std::cout << "Sum broadcasted over rows :\n" 125 | << mat << std::endl; 126 | } 127 | return 0; 128 | }; 129 | --------------------------------------------------------------------------------