From e15e5f3aba955c84eb6908875ae944d7cd84970c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= Date: Tue, 4 Feb 2020 12:53:41 +0900 Subject: [PATCH] Remove code, which has been submitted as an example --- resnet/resnet.h | 98 --------------- resnet/resnet50.cpp | 113 ------------------ ... resnet50_1000_imagenet_classifier.dnn.bz2 | Bin 3 files changed, 211 deletions(-) delete mode 100644 resnet/resnet.h delete mode 100644 resnet/resnet50.cpp rename resnet/resnet50_1000_imagenet_classifier.dnn.bz2 => resnet50_1000_imagenet_classifier.dnn.bz2 (100%) diff --git a/resnet/resnet.h b/resnet/resnet.h deleted file mode 100644 index 0e0ef64..0000000 --- a/resnet/resnet.h +++ /dev/null @@ -1,98 +0,0 @@ -#pragma once - -#include - -// BATCHNORM must be bn_con or affine layer -template class BATCHNORM> -struct resnet -{ - // the resnet basic block, where REG is bn_con or affine - template class BN, int stride, typename SUBNET> - using basicblock = BN>>>>; - - // the resnet bottleneck block - template class BN, int stride, typename SUBNET> - using bottleneck = BN>>>>>>>; - - // the resnet residual - template< - template class, int, typename> class BLOCK, // basicblock or bottleneck - long num_filters, - template class BN, // bn_con or affine - typename SUBNET - > // adds the block to the result of tag1 (the subnet) - using residual = dlib::add_prev1>>; - - // a resnet residual that does subsampling on both paths - template< - template class, int, typename> class BLOCK, // basicblock or bottleneck - long num_filters, - template class BN, // bn_con or affine - typename SUBNET - > - using residual_down = dlib::add_prev2>>>>>; - - // residual block with optional downsampling and custom regularization (bn_con or affine) - template< - template class, int, typename> class, long, templateclass, typename> class RESIDUAL, - template class, int, typename> class BLOCK, - long num_filters, - template class BN, // bn_con or affine - typename SUBNET - > - using residual_block = dlib::relu>; - - template - using resbasicblock_down = residual_block; - template - using resbottleneck_down = residual_block; - - // some definitions to allow the use of the repeat layer - template using resbasicblock_512 = residual_block; - template using resbasicblock_256 = residual_block; - template using resbasicblock_128 = residual_block; - template using resbasicblock_64 = residual_block; - template using resbottleneck_512 = residual_block; - template using resbottleneck_256 = residual_block; - template using resbottleneck_128 = residual_block; - template using resbottleneck_64 = residual_block; - - // common processing for standard resnet inputs - template class BN, typename INPUT> - using input_processing = dlib::max_pool<3, 3, 2, 2, dlib::relu>>>; - - // the resnet backbone with basicblocks - template - using backbone_basicblock = - dlib::repeat>>>>>>>; - - // the resnet backbone with bottlenecks - template - using backbone_bottleneck = - dlib::repeat>>>>>>>; - - // the backbones for the classic architectures - template using backbone_18 = backbone_basicblock<1, 1, 1, 2, INPUT>; - template using backbone_34 = backbone_basicblock<2, 5, 3, 3, INPUT>; - template using backbone_50 = backbone_bottleneck<2, 5, 3, 3, INPUT>; - template using backbone_101 = backbone_bottleneck<2, 22, 3, 3, INPUT>; - template using backbone_152 = backbone_bottleneck<2, 35, 7, 3, INPUT>; - - // the typical classifier models - using l18 = dlib::loss_multiclass_log>>>; - using l34 = dlib::loss_multiclass_log>>>; - using l50 = dlib::loss_multiclass_log>>>; - using l101 = dlib::loss_multiclass_log>>>; - using l152 = dlib::loss_multiclass_log>>>; -}; diff --git a/resnet/resnet50.cpp b/resnet/resnet50.cpp deleted file mode 100644 index 4adb305..0000000 --- a/resnet/resnet50.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include "resnet.h" - -// In this simple example we will show how to load a pretrained network -// and use it for a different task. In particular, we will load a ResNet50 -// trained on ImageNet and use it as a pretrained backbone for some metric -// learning task - -using namespace std; -using namespace dlib; - -// here's an example of how one could define a metric learning network -// using the ResNet50's backbone from resnet.h -namespace model -{ - template class BN> - using net_type = loss_metric< - fc_no_bias<128, - avg_pool_everything< - typename resnet::template backbone_50< - input_rgb_image - >>>>; - - using train = net_type; - using infer = net_type; -} - -class visitor_lr_multiplier -{ -public: - - visitor_lr_multiplier(double new_lr_multiplier_) : new_lr_multiplier(new_lr_multiplier_) {} - - template - void set_learning_rate_multipler(T&) const - { - // ignore other layer detail types - } - - template - void set_learning_rate_multipler(bn_& l) const - { - l.set_learning_rate_multiplier(new_lr_multiplier); - l.set_bias_learning_rate_multiplier(new_lr_multiplier); - } - - template - void set_learning_rate_multipler(con_& l) const - { - l.set_learning_rate_multiplier(new_lr_multiplier); - l.set_bias_learning_rate_multiplier(new_lr_multiplier); - } - - template - void operator()(size_t , input_layer_type& ) const - { - // ignore other layers - } - - template - void operator()(size_t , add_layer& l) const - { - set_learning_rate_multipler(l.layer_details()); - } - -private: - - double new_lr_multiplier; -}; - - -int main() try -{ - - // ResNet50 classifier trained on ImageNet - resnet::l50 resnet50; - std::vector labels; - deserialize("resnet/resnet50_1000_imagenet_classifier.dnn") >> resnet50 >> labels; - - // The ResNet50 backbone - auto backbone = resnet50.subnet().subnet(); - - // We can now assign ResNet50's backbone to our network skipping the different layers, - // in our case, the loss layer and the fc layer: - model::train net; - net.subnet().subnet() = backbone; - - // An alternative way to use the pretrained network on a different network is - // to extract the relevant part of the network (we remove loss and fc layers), - // stack the new layers on top of it and assign the network - using net_type = loss_metric>; - net_type net2; - - // copy the backbone to the newly defined network - net2.subnet().subnet() = backbone; - - // now we are going to adjust the learning rates of different layers - visit_layers_range< 2, 37, net_type, visitor_lr_multiplier>(net2, visitor_lr_multiplier(0.1)); - visit_layers_range< 38, 106, net_type, visitor_lr_multiplier>(net2, visitor_lr_multiplier(0.01)); - visit_layers_range<107, 153, net_type, visitor_lr_multiplier>(net2, visitor_lr_multiplier(0.001)); - visit_layers_range<154, 192, net_type, visitor_lr_multiplier>(net2, visitor_lr_multiplier(0.0001)); - - // check the results - cout << net2 << endl; - - // From this point on, we can finetune the new network using this pretrained backbone. - - return EXIT_SUCCESS; -} -catch (const std::exception& e) -{ - std::cout << e.what() << std::endl; - return EXIT_FAILURE; -} diff --git a/resnet/resnet50_1000_imagenet_classifier.dnn.bz2 b/resnet50_1000_imagenet_classifier.dnn.bz2 similarity index 100% rename from resnet/resnet50_1000_imagenet_classifier.dnn.bz2 rename to resnet50_1000_imagenet_classifier.dnn.bz2