| 9 | // BN is bn_con or affine layer |
| 10 | template<template<typename> class BN> |
| 11 | struct def |
| 12 | { |
| 13 | // the resnet basic block, where BN is bn_con or affine |
| 14 | template<long num_filters, int stride, typename SUBNET> |
| 15 | using basicblock = BN<con<num_filters, 3, 3, 1, 1, |
| 16 | relu<BN<con<num_filters, 3, 3, stride, stride, SUBNET>>>>>; |
| 17 | |
| 18 | // the resnet bottleneck block |
| 19 | template<long num_filters, int stride, typename SUBNET> |
| 20 | using bottleneck = BN<con<4 * num_filters, 1, 1, 1, 1, |
| 21 | relu<BN<con<num_filters, 3, 3, stride, stride, |
| 22 | relu<BN<con<num_filters, 1, 1, 1, 1, SUBNET>>>>>>>>; |
| 23 | |
| 24 | // the resnet residual, where BLOCK is either basicblock or bottleneck |
| 25 | template<template<long, int, typename> class BLOCK, long num_filters, typename SUBNET> |
| 26 | using residual = add_prev1<BLOCK<num_filters, 1, tag1<SUBNET>>>; |
| 27 | |
| 28 | // a resnet residual that does subsampling on both paths |
| 29 | template<template<long, int, typename> class BLOCK, long num_filters, typename SUBNET> |
| 30 | using residual_down = add_prev2<avg_pool<2, 2, 2, 2, |
| 31 | skip1<tag2<BLOCK<num_filters, 2, |
| 32 | tag1<SUBNET>>>>>>; |
| 33 | |
| 34 | // residual block with optional downsampling |
| 35 | template< |
| 36 | template<template<long, int, typename> class, long, typename> class RESIDUAL, |
| 37 | template<long, int, typename> class BLOCK, |
| 38 | long num_filters, |
| 39 | typename SUBNET |
| 40 | > |
| 41 | using residual_block = relu<RESIDUAL<BLOCK, num_filters, SUBNET>>; |
| 42 | |
| 43 | template<long num_filters, typename SUBNET> |
| 44 | using resbasicblock_down = residual_block<residual_down, basicblock, num_filters, SUBNET>; |
| 45 | template<long num_filters, typename SUBNET> |
| 46 | using resbottleneck_down = residual_block<residual_down, bottleneck, num_filters, SUBNET>; |
| 47 | |
| 48 | // some definitions to allow the use of the repeat layer |
| 49 | template<typename SUBNET> using resbasicblock_512 = residual_block<residual, basicblock, 512, SUBNET>; |
| 50 | template<typename SUBNET> using resbasicblock_256 = residual_block<residual, basicblock, 256, SUBNET>; |
| 51 | template<typename SUBNET> using resbasicblock_128 = residual_block<residual, basicblock, 128, SUBNET>; |
| 52 | template<typename SUBNET> using resbasicblock_64 = residual_block<residual, basicblock, 64, SUBNET>; |
| 53 | template<typename SUBNET> using resbottleneck_512 = residual_block<residual, bottleneck, 512, SUBNET>; |
| 54 | template<typename SUBNET> using resbottleneck_256 = residual_block<residual, bottleneck, 256, SUBNET>; |
| 55 | template<typename SUBNET> using resbottleneck_128 = residual_block<residual, bottleneck, 128, SUBNET>; |
| 56 | template<typename SUBNET> using resbottleneck_64 = residual_block<residual, bottleneck, 64, SUBNET>; |
| 57 | |
| 58 | // common processing for standard resnet inputs |
| 59 | template<typename INPUT> |
| 60 | using input_processing = max_pool<3, 3, 2, 2, relu<BN<con<64, 7, 7, 2, 2, INPUT>>>>; |
| 61 | |
| 62 | // the resnet backbone with basicblocks |
| 63 | template<long nb_512, long nb_256, long nb_128, long nb_64, typename INPUT> |
| 64 | using backbone_basicblock = |
| 65 | repeat<nb_512, resbasicblock_512, resbasicblock_down<512, |
| 66 | repeat<nb_256, resbasicblock_256, resbasicblock_down<256, |
| 67 | repeat<nb_128, resbasicblock_128, resbasicblock_down<128, |
| 68 | repeat<nb_64, resbasicblock_64, input_processing<INPUT>>>>>>>>; |