├── .clang-format ├── .editorconfig ├── .gitignore ├── CMakeLists.txt ├── LICENSE.txt ├── README.md ├── build.sh └── src ├── benchmark.h ├── benchmark_classification.cpp ├── classification ├── alexnet.h ├── darknet.h ├── densenet.h ├── googlenet.h ├── repvgg.h ├── resnet.h ├── squeezenet.h ├── vggnet.h └── vovnet.h ├── detection ├── yolov5.h ├── yolov5p6.h └── yolov7.h └── lm └── slm_dels.h /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | AccessModifierOffset: '0' 3 | AlignAfterOpenBracket: AlwaysBreak 4 | AlignConsecutiveMacros: 'false' 5 | AlignConsecutiveAssignments: 'false' 6 | AlignConsecutiveDeclarations: 'false' 7 | AlignEscapedNewlines: Left 8 | AlignOperands: 'true' 9 | AlignTrailingComments: 'false' 10 | AllowAllArgumentsOnNextLine: 'false' 11 | AllowAllConstructorInitializersOnNextLine: 'false' 12 | AllowAllParametersOfDeclarationOnNextLine: 'false' 13 | AllowShortBlocksOnASingleLine: 'false' 14 | AllowShortCaseLabelsOnASingleLine: 'false' 15 | AllowShortFunctionsOnASingleLine: InlineOnly 16 | AllowShortIfStatementsOnASingleLine: Never 17 | AllowShortLambdasOnASingleLine: Inline 18 | AllowShortLoopsOnASingleLine: 'false' 19 | AlwaysBreakAfterReturnType: None 20 | AlwaysBreakBeforeMultilineStrings: 'false' 21 | AlwaysBreakTemplateDeclarations: 'No' 22 | BinPackArguments: 'false' 23 | BinPackParameters: 'false' 24 | BreakBeforeBinaryOperators: None 25 | BreakBeforeBraces: Allman 26 | ColumnLimit: '99' 27 | CompactNamespaces: 'false' 28 | ConstructorInitializerAllOnOneLineOrOnePerLine: 'true' 29 | DerivePointerAlignment: 'true' 30 | DisableFormat: 'false' 31 | FixNamespaceComments: 'true' 32 | IncludeBlocks: Regroup 33 | IndentCaseLabels: 'false' 34 | IndentPPDirectives: None 35 | IndentWidth: '4' 36 | IndentWrappedFunctionNames: 'true' 37 | Language: Cpp 38 | MaxEmptyLinesToKeep: '1' 39 | NamespaceIndentation: All 40 | PointerAlignment: Left 41 | ReflowComments: 'true' 42 | SortIncludes: 'true' 43 | SortUsingDeclarations: 'true' 44 | SpaceAfterCStyleCast: 'false' 45 | SpaceAfterLogicalNot: 'false' 46 | SpaceAfterTemplateKeyword: 'true' 47 | SpaceBeforeAssignmentOperators: 'true' 48 | SpaceBeforeCpp11BracedList: 'false' 49 | SpaceBeforeCtorInitializerColon: 'true' 50 | SpaceBeforeInheritanceColon: 'true' 51 | SpaceBeforeParens: ControlStatements 52 | SpaceBeforeRangeBasedForLoopColon: 'true' 53 | SpaceInEmptyParentheses: 'false' 54 | SpacesBeforeTrailingComments: '2' 55 | SpacesInAngles: 'false' 56 | SpacesInCStyleCastParentheses: 'false' 57 | SpacesInParentheses: 'false' 58 | SpacesInSquareBrackets: 'false' 59 | Standard: Cpp11 60 | TabWidth: '4' 61 | UseTab: Never 62 | 63 | ... 64 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*.{cpp, h}] 4 | indent_style = space 5 | indent_size = 4 6 | tab_width = 4 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = false 11 | max_line_length = off 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /* 2 | !/build.sh 3 | !/.clang-format 4 | !/CMakeLists.txt 5 | !/.editorconfig 6 | !/.gitignore 7 | !/LICENSE 8 | !/README.md 9 | !/src/ 10 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | project("dlib template project" LANGUAGES CXX) 3 | 4 | set(CPACK_PACKAGE_NAME "dlib-template-project") 5 | set(CPACK_PACKAGE_VERSION_MAJOR "0") 6 | set(CPACK_PACKAGE_VERSION_MINOR "0") 7 | set(CPACK_PACKAGE_VERSION_PATCH "0") 8 | set(VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}) 9 | 10 | # Use C++ 17 11 | set(CMAKE_CXX_STANDARD 17) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | set(CMAKE_CXX_EXTENSIONS ON) 14 | 15 | # Colored warnings 16 | option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored output (GNU/Clang only)." ON) 17 | if(${FORCE_COLORED_OUTPUT}) 18 | if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") 19 | add_compile_options (-fdiagnostics-color=always) 20 | elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 21 | add_compile_options (-fcolor-diagnostics) 22 | endif() 23 | endif() 24 | 25 | # DenseNets have a LOT of layers... 26 | add_compile_options(-ftemplate-depth=2000) 27 | 28 | # Enable ccache if it exists 29 | find_program(CCACHE_FOUND ccache) 30 | if(CCACHE_FOUND) 31 | set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) 32 | set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) 33 | endif (CCACHE_FOUND) 34 | 35 | # Optimization flags 36 | include(CheckCXXCompilerFlag) 37 | if (CMAKE_BUILD_TYPE STREQUAL "Release") 38 | CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE) 39 | if(COMPILER_SUPPORTS_MARCH_NATIVE) 40 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") 41 | endif() 42 | endif() 43 | 44 | # Dependency management 45 | include(FetchContent) 46 | macro(fetch_content name tag repository) 47 | FetchContent_Declare( 48 | ${name} 49 | GIT_REPOSITORY ${repository} 50 | GIT_TAG ${tag} 51 | GIT_PROGRESS TRUE 52 | USES_TERMINAL_DOWNLOAD TRUE 53 | SOURCE_DIR ${PROJECT_SOURCE_DIR}/external/${name} 54 | ) 55 | message("-- Fetching ${name} ${tag}") 56 | FetchContent_MakeAvailable(${name}) 57 | endmacro() 58 | 59 | macro(add_dlib_executable name) 60 | add_executable(${name} src/${name}.cpp) 61 | target_link_libraries(${name} PRIVATE dlib::dlib) 62 | target_include_directories(${name} PRIVATE src) 63 | target_compile_options(${name} PRIVATE -Wall -Wextra -pedantic -Wno-deprecated-copy) 64 | install(TARGETS ${name} DESTINATION bin) 65 | endmacro() 66 | 67 | fetch_content(dlib master https://github.com/davisking/dlib.git) 68 | 69 | add_dlib_executable(benchmark_classification) 70 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | 3 | Permission is hereby granted, free of charge, to any person or organization 4 | obtaining a copy of the software and accompanying documentation covered by 5 | this license (the "Software") to use, reproduce, display, distribute, 6 | execute, and transmit the Software, and to prepare derivative works of the 7 | Software, and to permit third-parties to whom the Software is furnished to 8 | do so, all subject to the following: 9 | 10 | The copyright notices in the Software and this entire statement, including 11 | the above license grant, this restriction and the following disclaimer, 12 | must be included in all copies of the Software, in whole or in part, and 13 | all derivative works of the Software, unless such copies or derivative 14 | works are solely in the form of machine-executable object code generated by 15 | a source language processor. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dnn 2 | Deep Neural Network Architectures 3 | 4 | This repository contains the definitions for the following architectures, organized by task. 5 | 6 | ## Contents 7 | - [Classification](#classification) 8 | - [AlexNet](#alexnet) 9 | - [SqueezeNet](#squeezenet) 10 | - [VGGNet](#vggnet) 11 | - [GoogLeNet](#googlenet) 12 | - [ResNet](#resnet) 13 | - [DenseNet](#densenet) 14 | - [DarkNet](#darknet) 15 | - [VoVNet](#vovnet) 16 | - [RepVGG](#repvgg) 17 | - [Detection](#detection) 18 | - [YOLOv5](#yolov5) 19 | 20 | ## [Classification](./src/classification) 21 | 22 | ### [AlexNet](./src/classification/alexnet.h) 23 | 24 | It contains the definition for the model that started it all. 25 | 26 | Papers: 27 | - [ImageNet Classification with Deep Convolutional Neural Networks](https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks) 28 | 29 | ### [SqueezeNet](./src/classification/squeezenet.h) 30 | 31 | In particular, it contains SqueezeNet-{v1.0,v1.1}. 32 | 33 | Papers: 34 | - [SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size](https://arxiv.org/abs/1602.07360) 35 | 36 | ### [VGGNet](./src/classification/vggnet.h) 37 | 38 | In particular, it contains VGGNet-{11,13,16,19} variants with batch normalization. 39 | 40 | Papers: 41 | - [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556) 42 | 43 | ### [GoogLeNet](./src/classification/googlenet.h) 44 | 45 | It contains the definition of the GoogLeNet, also known as InceptionV1. 46 | 47 | Papers: 48 | - [Going Deeper with Convolutions](https://arxiv.org/abs/1409.4842) 49 | 50 | ### [ResNet](./src/classification/resnet.h) 51 | 52 | In particular, it contains ResNet-{18,34,50,101,152}-B definitions, in contrast to dlib, which contains the A variants. 53 | 54 | Papers: 55 | - [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385) 56 | 57 | ### [DenseNet](./src/classification/densenet.h) 58 | 59 | In particular, it contains DenseNet-{121,169,201,264,161} definitions. 60 | 61 | Papers: 62 | - [Densely Connected Convolutional Networks](https://arxiv.org/abs/1608.06993) 63 | 64 | ### [DarkNet](./src/classification/darknet.h) 65 | 66 | In particular, it contains the backbones for DarkNet-19 (introduced in YOLOv1), DarkNet-53 (YOLOv3) and CSPDarknet-53 (YOLOv4). 67 | 68 | Papers: 69 | - [You Only Look Once: Unified, Real-Time Object Detection](https://arxiv.org/abs/1506.02640) 70 | - [YOLOv3: An Incremental Improvement](https://arxiv.org/abs/1804.02767) 71 | - [CSPNet: A New Backbone that can Enhance Learning Capability of CNN](https://arxiv.org/abs/1911.11929) 72 | - [YOLOv4: Optimal Speed and Accuracy of Object Detection](https://arxiv.org/abs/2004.10934) 73 | 74 | ### [VoVNet](./src/classification/vovnet.h) 75 | 76 | In particular, it contains implementations for VoVNetv2-{19slim,19,27slim,27,39,57,99}, which are very similar to VoVNetv1 (V2 have identiy mapping and effective Squeeze and Excitation on top of V1). 77 | 78 | Papers: 79 | - [An Energy and GPU-Computation Efficient Backbone Network for Real-Time Object Detection](https://arxiv.org/abs/1904.09730) 80 | - [CenterMask: Real-Time Anchor-Free Instance Segmentation](https://arxiv.org/abs/1911.06667) 81 | 82 | ### [RepVGG](./src/classification/repvgg.h) 83 | 84 | In particular, it contains implementations for RepVGG-{A0,A1,A2,B0,B1,B2,B3}. 85 | 86 | Note that, at the moment, there is no way to convert from a trained RepVGG model into its inference counterpart. 87 | I will investigate how to do that soon. 88 | 89 | Papers: 90 | - [RepVGG: Making VGG-style ConvNets Great Again](https://arxiv.org/abs/2101.03697) 91 | 92 | ## [Detection](./src/detection) 93 | 94 | ### [YOLOv5](./src/detection/yolov5.h) 95 | 96 | In particular, it contains implementations for YOLOv5{n,s,m,l,x}, which match the ones in [ultralytics/yolov5](https://github.com/ultralytics/yolov5). 97 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | BUILD_PREFIX=build 4 | 5 | ARG1=`echo $1 | awk '{print tolower($0)}'` 6 | 7 | BUILD_TYPE=Release 8 | 9 | case ${ARG1} in 10 | release|release/) 11 | BUILD_TYPE=Release 12 | shift 1 13 | ;; 14 | debug|debug/) 15 | BUILD_TYPE=Debug 16 | shift 1 17 | ;; 18 | *) 19 | BUILD_TYPE=Release 20 | ;; 21 | esac 22 | 23 | cmake -B ${BUILD_PREFIX}/${BUILD_TYPE} -GNinja \ 24 | -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ 25 | -DUSE_AVX_INSTRUCTIONS=ON \ 26 | -DUSE_SSE2_INSTRUCTIONS=ON \ 27 | -DUSE_SSE4_INSTRUCTIONS=ON \ 28 | ${@} 29 | 30 | cmake --build ${BUILD_PREFIX}/${BUILD_TYPE} --config ${BUILD_TYPE} 31 | -------------------------------------------------------------------------------- /src/benchmark.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class visitor_con_disable_bias 4 | { 5 | public: 6 | visitor_con_disable_bias() = default; 7 | // ignore other layers 8 | template void operator()(size_t, T&) {} 9 | template 10 | void operator()(size_t, dlib::add_layer, SUBNET>& l) 11 | { 12 | l.layer_details().disable_bias(); 13 | } 14 | }; 15 | 16 | class visitor_count_convolutions 17 | { 18 | public: 19 | visitor_count_convolutions(size_t& num_convolutions) : num_convolutions(num_convolutions) {} 20 | // ignore other layers 21 | template void operator()(size_t, T&) {} 22 | template 23 | void operator()(size_t, dlib::add_layer, SUBNET>&) 24 | { 25 | ++num_convolutions; 26 | } 27 | 28 | private: 29 | size_t& num_convolutions; 30 | }; 31 | 32 | template auto benchmark( 33 | const std::string& name, 34 | net_type& net, 35 | const size_t batch_size = 1, 36 | const size_t image_size = 224, 37 | const int iterations = 100) 38 | { 39 | using fms = std::chrono::duration; 40 | dlib::resizable_tensor x; 41 | dlib::matrix image(image_size, image_size); 42 | assign_all_pixels(image, dlib::rgb_pixel(0, 0, 0)); 43 | std::vector> batch(batch_size, image); 44 | dlib::running_stats rs; 45 | net.to_tensor(batch.begin(), batch.end(), x); 46 | // warmup for 10 iterations 47 | for (int i = 0; i < 10; ++i) 48 | { 49 | net.forward(x); 50 | } 51 | // std::cout << net << '\n'; 52 | for (int i = 0; i < iterations; ++i) 53 | { 54 | const auto t0 = std::chrono::steady_clock::now(); 55 | net.forward(x); 56 | const auto& t = net.subnet().get_output(); 57 | t.host(); 58 | const auto t1 = std::chrono::steady_clock::now(); 59 | rs.add(std::chrono::duration_cast(t1 - t0).count()); 60 | } 61 | std::cout << name << " inference: " << rs.mean() << " ms"; 62 | std::cout << " (" << 1.0 / rs.mean() * 1000.0 * batch_size << " fps)"; 63 | std::cout << " #params: " << count_parameters(net); 64 | std::ostringstream sout; 65 | serialize(net, sout); 66 | std::cout << " (memory usage: " << sout.str().size() / 1024.0 / 1024.0 << " MiB)"; 67 | size_t num_convolutions = 0; 68 | dlib::visit_layers(net, visitor_count_convolutions(num_convolutions)); 69 | std::cout << " #num convolutions: " << num_convolutions << ' '; 70 | std::cout << " #num layers: " << net_type::num_computational_layers << '\n'; 71 | std::cin.get(); 72 | } 73 | -------------------------------------------------------------------------------- /src/benchmark_classification.cpp: -------------------------------------------------------------------------------- 1 | #include "benchmark.h" 2 | #include "classification/alexnet.h" 3 | #include "classification/darknet.h" 4 | #include "classification/densenet.h" 5 | #include "classification/googlenet.h" 6 | #include "classification/resnet.h" 7 | #include "classification/squeezenet.h" 8 | #include "classification/vggnet.h" 9 | #include "classification/vovnet.h" 10 | #include "classification/repvgg.h" 11 | 12 | #include 13 | 14 | #define DNN_BENCH_ALEXNET 1 15 | #define DNN_BENCH_VGGNET 1 16 | #define DNN_BENCH_GOOGLENET 1 17 | #define DNN_BENCH_RESNET 1 18 | #define DNN_BENCH_DARKNET 1 19 | #define DNN_BENCH_DENSENET 1 20 | #define DNN_BENCH_VOVNET 1 21 | #define DNN_BENCH_SQUEEZENET 1 22 | #define DNN_BENCH_REPVGG 1 23 | 24 | int main(const int argc, const char** argv) 25 | try 26 | { 27 | 28 | dlib::command_line_parser parser; 29 | parser.add_option("batch-size", "set the batch size (default: 1)", 1); 30 | parser.add_option("image-size", "set the image size (default: 224)", 1); 31 | parser.add_option("num-outputs", "set the number of fc outputs (default: 1000)", 1); 32 | parser.add_option("num-iters", "set the number of iterations (default: 100)", 1); 33 | parser.add_option("cuda-blocking", "disable cuda synchronization"); 34 | parser.set_group_name("Help Options"); 35 | parser.add_option("h", "alias for --help"); 36 | parser.add_option("help", "display this message and exit"); 37 | parser.parse(argc, argv); 38 | 39 | if (parser.option("h") or parser.option("help")) 40 | { 41 | parser.print_options(); 42 | return EXIT_SUCCESS; 43 | } 44 | 45 | const std::string cuda_blocking = parser.option("cuda-blocking") ? "1" : "0"; 46 | const size_t batch_size = dlib::get_option(parser, "batch-size", 1); 47 | const size_t image_size = dlib::get_option(parser, "image-size", 224); 48 | const size_t num_outputs = dlib::get_option(parser, "num-outputs", 1000); 49 | const int num_iters = dlib::get_option(parser, "num-iters", 100); 50 | setenv("CUDA_LAUNCH_BLOCKING", cuda_blocking.c_str(), 1); 51 | std::cout << std::fixed << std::setprecision(3); 52 | 53 | #if DNN_BENCH_ALEXNET 54 | { 55 | alexnet::train tnet; 56 | dlib::disable_duplicative_biases(tnet); 57 | alexnet::infer net(tnet); 58 | net.subnet().layer_details().set_num_outputs(num_outputs); 59 | benchmark("alexnet ", net, batch_size, image_size, num_iters); 60 | } 61 | #endif 62 | 63 | #if DNN_BENCH_SQUEEZENET 64 | { 65 | squeezenet::train_v1_0 tnet; 66 | dlib::disable_duplicative_biases(tnet); 67 | squeezenet::infer_v1_0 net(tnet); 68 | net.subnet().subnet().subnet().layer_details().set_num_filters(num_outputs); 69 | benchmark("sqznet1.0", net, batch_size, image_size, num_iters); 70 | } 71 | { 72 | squeezenet::train_v1_1 tnet; 73 | dlib::disable_duplicative_biases(tnet); 74 | squeezenet::infer_v1_1 net(tnet); 75 | net.subnet().subnet().subnet().layer_details().set_num_filters(num_outputs); 76 | benchmark("sqznet1.1", net, batch_size, image_size, num_iters); 77 | } 78 | #endif 79 | 80 | #if DNN_BENCH_VGGNET 81 | { 82 | vggnet::train_11 tnet; 83 | dlib::disable_duplicative_biases(tnet); 84 | vggnet::infer_11 net(tnet); 85 | net.subnet().layer_details().set_num_outputs(num_outputs); 86 | benchmark("vggnet11 ", net, batch_size, image_size, num_iters); 87 | } 88 | { 89 | vggnet::train_13 tnet; 90 | dlib::disable_duplicative_biases(tnet); 91 | vggnet::infer_13 net(tnet); 92 | net.subnet().layer_details().set_num_outputs(num_outputs); 93 | benchmark("vggnet13 ", net, batch_size, image_size, num_iters); 94 | } 95 | { 96 | vggnet::train_16 tnet; 97 | dlib::disable_duplicative_biases(tnet); 98 | vggnet::infer_16 net(tnet); 99 | net.subnet().layer_details().set_num_outputs(num_outputs); 100 | benchmark("vggnet16 ", net, batch_size, image_size, num_iters); 101 | } 102 | { 103 | vggnet::train_19 tnet; 104 | dlib::disable_duplicative_biases(tnet); 105 | vggnet::infer_19 net(tnet); 106 | net.subnet().layer_details().set_num_outputs(num_outputs); 107 | benchmark("vggnet19 ", net, batch_size, image_size, num_iters); 108 | } 109 | #endif 110 | 111 | #if DNN_BENCH_GOOGLENET 112 | { 113 | googlenet::train tnet; 114 | dlib::disable_duplicative_biases(tnet); 115 | googlenet::infer net(tnet); 116 | net.subnet().layer_details().set_num_outputs(num_outputs); 117 | benchmark("googlenet", net, batch_size, image_size, num_iters); 118 | } 119 | #endif 120 | 121 | #if DNN_BENCH_RESNET 122 | { 123 | resnet::train_18 tnet; 124 | dlib::disable_duplicative_biases(tnet); 125 | resnet::infer_18 net(tnet); 126 | net.subnet().layer_details().set_num_outputs(num_outputs); 127 | benchmark("resnet18 ", net, batch_size, image_size, num_iters); 128 | } 129 | { 130 | resnet::train_34 tnet; 131 | dlib::disable_duplicative_biases(tnet); 132 | resnet::infer_34 net(tnet); 133 | net.subnet().layer_details().set_num_outputs(num_outputs); 134 | benchmark("resnet34 ", net, batch_size, image_size, num_iters); 135 | } 136 | { 137 | resnet::train_50 tnet; 138 | dlib::disable_duplicative_biases(tnet); 139 | resnet::infer_50 net(tnet); 140 | net.subnet().layer_details().set_num_outputs(num_outputs); 141 | benchmark("resnet50 ", net, batch_size, image_size, num_iters); 142 | } 143 | { 144 | resnet::train_101 tnet; 145 | dlib::disable_duplicative_biases(tnet); 146 | resnet::infer_101 net(tnet); 147 | net.subnet().layer_details().set_num_outputs(num_outputs); 148 | benchmark("resnet101", net, batch_size, image_size, num_iters); 149 | } 150 | { 151 | resnet::train_152 tnet; 152 | dlib::disable_duplicative_biases(tnet); 153 | resnet::infer_152 net(tnet); 154 | net.subnet().layer_details().set_num_outputs(num_outputs); 155 | benchmark("resnet152", net, batch_size, image_size, num_iters); 156 | } 157 | #endif 158 | 159 | #if DNN_BENCH_DARKNET 160 | { 161 | darknet::train_19 tnet; 162 | dlib::disable_duplicative_biases(tnet); 163 | darknet::infer_19 net(tnet); 164 | net.subnet().layer_details().set_num_outputs(num_outputs); 165 | benchmark("darknet19", net, batch_size, image_size, num_iters); 166 | } 167 | { 168 | darknet::train_53 tnet; 169 | dlib::disable_duplicative_biases(tnet); 170 | darknet::infer_53 net(tnet); 171 | net.subnet().layer_details().set_num_outputs(num_outputs); 172 | benchmark("darknet53", net, batch_size, image_size, num_iters); 173 | } 174 | { 175 | darknet::train_53csp tnet; 176 | dlib::disable_duplicative_biases(tnet); 177 | darknet::infer_53csp net(tnet); 178 | net.subnet().layer_details().set_num_outputs(num_outputs); 179 | benchmark("darknet53csp", net, batch_size, image_size, num_iters); 180 | } 181 | #endif 182 | 183 | #if DNN_BENCH_DENSENET 184 | { 185 | densenet::train_121 tnet; 186 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 187 | densenet::infer_121 net(tnet); 188 | net.subnet().layer_details().set_num_outputs(num_outputs); 189 | benchmark("densenet121", net, batch_size, image_size, num_iters); 190 | } 191 | { 192 | densenet::train_169 tnet; 193 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 194 | densenet::infer_169 net(tnet); 195 | net.subnet().layer_details().set_num_outputs(num_outputs); 196 | benchmark("densenet169", net, batch_size, image_size, num_iters); 197 | } 198 | { 199 | densenet::train_201 tnet; 200 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 201 | densenet::infer_201 net(tnet); 202 | net.subnet().layer_details().set_num_outputs(num_outputs); 203 | benchmark("densenet201", net, batch_size, image_size, num_iters); 204 | } 205 | { 206 | densenet::train_265 tnet; 207 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 208 | densenet::infer_265 net(tnet); 209 | net.subnet().layer_details().set_num_outputs(num_outputs); 210 | benchmark("densenet265", net, batch_size, image_size, num_iters); 211 | } 212 | { 213 | densenet::train_161 tnet; 214 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 215 | densenet::infer_161 net(tnet); 216 | net.subnet().layer_details().set_num_outputs(num_outputs); 217 | benchmark("densenet161", net, batch_size, image_size, num_iters); 218 | } 219 | #endif 220 | 221 | #if DNN_BENCH_VOVNET 222 | { 223 | vovnet::train_19_slim tnet; 224 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 225 | vovnet::infer_19_slim net(tnet); 226 | net.subnet().layer_details().set_num_outputs(num_outputs); 227 | benchmark("vovnet19s", net, batch_size, image_size, num_iters); 228 | } 229 | { 230 | vovnet::train_19 tnet; 231 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 232 | vovnet::infer_19 net(tnet); 233 | net.subnet().layer_details().set_num_outputs(num_outputs); 234 | benchmark("vovnet19 ", net, batch_size, image_size, num_iters); 235 | } 236 | { 237 | vovnet::train_27_slim tnet; 238 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 239 | vovnet::infer_27_slim net(tnet); 240 | net.subnet().layer_details().set_num_outputs(num_outputs); 241 | benchmark("vovnet27s", net, batch_size, image_size, num_iters); 242 | } 243 | { 244 | vovnet::train_27 tnet; 245 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 246 | vovnet::infer_27 net(tnet); 247 | net.subnet().layer_details().set_num_outputs(num_outputs); 248 | benchmark("vovnet27 ", net, batch_size, image_size, num_iters); 249 | } 250 | { 251 | vovnet::train_39 tnet; 252 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 253 | vovnet::infer_39 net(tnet); 254 | net.subnet().layer_details().set_num_outputs(num_outputs); 255 | benchmark("vovnet39 ", net, batch_size, image_size, num_iters); 256 | } 257 | { 258 | vovnet::train_57 tnet; 259 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 260 | vovnet::infer_57 net(tnet); 261 | net.subnet().layer_details().set_num_outputs(num_outputs); 262 | benchmark("vovnet57 ", net, batch_size, image_size, num_iters); 263 | } 264 | { 265 | vovnet::train_99 tnet; 266 | dlib::visit_layers(tnet, visitor_con_disable_bias()); 267 | vovnet::infer_99 net(tnet); 268 | net.subnet().layer_details().set_num_outputs(num_outputs); 269 | benchmark("vovnet99 ", net, batch_size, image_size, num_iters); 270 | } 271 | #endif 272 | 273 | #if DNN_BENCH_REPVGG 274 | { 275 | repvgg::infer_a0 net; 276 | net.subnet().layer_details().set_num_outputs(num_outputs); 277 | benchmark("repvgg_a0 ", net, batch_size, image_size, num_iters); 278 | } 279 | { 280 | repvgg::infer_a1 net; 281 | net.subnet().layer_details().set_num_outputs(num_outputs); 282 | benchmark("repvgg_a1 ", net, batch_size, image_size, num_iters); 283 | } 284 | { 285 | repvgg::infer_a2 net; 286 | net.subnet().layer_details().set_num_outputs(num_outputs); 287 | benchmark("repvgg_a2 ", net, batch_size, image_size, num_iters); 288 | } 289 | { 290 | repvgg::infer_b0 net; 291 | net.subnet().layer_details().set_num_outputs(num_outputs); 292 | benchmark("repvgg_b0 ", net, batch_size, image_size, num_iters); 293 | } 294 | { 295 | repvgg::infer_b1 net; 296 | net.subnet().layer_details().set_num_outputs(num_outputs); 297 | benchmark("repvgg_b1 ", net, batch_size, image_size, num_iters); 298 | } 299 | { 300 | repvgg::infer_b2 net; 301 | net.subnet().layer_details().set_num_outputs(num_outputs); 302 | benchmark("repvgg_b2 ", net, batch_size, image_size, num_iters); 303 | } 304 | { 305 | repvgg::infer_b3 net; 306 | net.subnet().layer_details().set_num_outputs(num_outputs); 307 | benchmark("repvgg_b3 ", net, batch_size, image_size, num_iters); 308 | } 309 | #endif 310 | 311 | return EXIT_SUCCESS; 312 | } 313 | catch (const std::exception& e) 314 | { 315 | std::cout << e.what() << '\n'; 316 | return EXIT_FAILURE; 317 | } 318 | -------------------------------------------------------------------------------- /src/classification/alexnet.h: -------------------------------------------------------------------------------- 1 | #ifndef AlexNet_H 2 | #define AlexNet_H 3 | 4 | #include 5 | 6 | namespace alexnet 7 | { 8 | // clang-format off 9 | using namespace dlib; 10 | template