vgg.cpp 3.78 KB
Newer Older
Shahriar's avatar
Shahriar committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "vgg.h"

#include <unordered_map>
#include "modelsimpl.h"

namespace vision {
namespace models {
torch::nn::Sequential makeLayers(
    const std::vector<int>& cfg,
    bool batch_norm = false) {
  torch::nn::Sequential seq;
  auto channels = 3;

  for (const auto& V : cfg) {
    if (V <= -1)
      seq->push_back(torch::nn::Functional(modelsimpl::max_pool2d, 2, 2));
    else {
      seq->push_back(torch::nn::Conv2d(
          torch::nn::Conv2dOptions(channels, V, 3).padding(1)));

      if (batch_norm)
22
        seq->push_back(torch::nn::BatchNorm2d(V));
Shahriar's avatar
Shahriar committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
      seq->push_back(torch::nn::Functional(modelsimpl::relu_));

      channels = V;
    }
  }

  return seq;
}

void VGGImpl::_initialize_weights() {
  for (auto& module : modules(/*include_self=*/false)) {
    if (auto M = dynamic_cast<torch::nn::Conv2dImpl*>(module.get())) {
      torch::nn::init::kaiming_normal_(
          M->weight,
          /*a=*/0,
38
39
          torch::kFanOut,
          torch::kReLU);
Shahriar's avatar
Shahriar committed
40
      torch::nn::init::constant_(M->bias, 0);
Francisco Massa's avatar
Francisco Massa committed
41
42
    } else if (
        auto M = dynamic_cast<torch::nn::BatchNorm2dImpl*>(module.get())) {
Shahriar's avatar
Shahriar committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
      torch::nn::init::constant_(M->weight, 1);
      torch::nn::init::constant_(M->bias, 0);
    } else if (auto M = dynamic_cast<torch::nn::LinearImpl*>(module.get())) {
      torch::nn::init::normal_(M->weight, 0, 0.01);
      torch::nn::init::constant_(M->bias, 0);
    }
  }
}

VGGImpl::VGGImpl(
    torch::nn::Sequential features,
    int64_t num_classes,
    bool initialize_weights) {
  classifier = torch::nn::Sequential(
      torch::nn::Linear(512 * 7 * 7, 4096),
      torch::nn::Functional(modelsimpl::relu_),
      torch::nn::Dropout(),
      torch::nn::Linear(4096, 4096),
      torch::nn::Functional(modelsimpl::relu_),
      torch::nn::Dropout(),
      torch::nn::Linear(4096, num_classes));

  this->features = features;

  register_module("features", this->features);
  register_module("classifier", classifier);

  if (initialize_weights)
    _initialize_weights();
}

torch::Tensor VGGImpl::forward(torch::Tensor x) {
  x = features->forward(x);
  x = torch::adaptive_avg_pool2d(x, {7, 7});
  x = x.view({x.size(0), -1});
  x = classifier->forward(x);
  return x;
}

// clang-format off
83
static std::unordered_map<char, std::vector<int>> cfgs = {
Shahriar's avatar
Shahriar committed
84
85
86
87
88
89
90
  {'A', {64, -1, 128, -1, 256, 256, -1, 512, 512, -1, 512, 512, -1}},
  {'B', {64, 64, -1, 128, 128, -1, 256, 256, -1, 512, 512, -1, 512, 512, -1}},
  {'D', {64, 64, -1, 128, 128, -1, 256, 256, 256, -1, 512, 512, 512, -1, 512, 512, 512, -1}},
  {'E', {64,  64,  -1,  128, 128, -1,  256, 256, 256, 256, -1, 512, 512, 512, 512, -1,  512, 512, 512, 512, -1}}};
// clang-format on

VGG11Impl::VGG11Impl(int64_t num_classes, bool initialize_weights)
91
    : VGGImpl(makeLayers(cfgs['A']), num_classes, initialize_weights) {}
Shahriar's avatar
Shahriar committed
92
93

VGG13Impl::VGG13Impl(int64_t num_classes, bool initialize_weights)
94
    : VGGImpl(makeLayers(cfgs['B']), num_classes, initialize_weights) {}
Shahriar's avatar
Shahriar committed
95
96

VGG16Impl::VGG16Impl(int64_t num_classes, bool initialize_weights)
97
    : VGGImpl(makeLayers(cfgs['D']), num_classes, initialize_weights) {}
Shahriar's avatar
Shahriar committed
98
99

VGG19Impl::VGG19Impl(int64_t num_classes, bool initialize_weights)
100
    : VGGImpl(makeLayers(cfgs['E']), num_classes, initialize_weights) {}
Shahriar's avatar
Shahriar committed
101
102

VGG11BNImpl::VGG11BNImpl(int64_t num_classes, bool initialize_weights)
103
    : VGGImpl(makeLayers(cfgs['A'], true), num_classes, initialize_weights) {}
Shahriar's avatar
Shahriar committed
104
105

VGG13BNImpl::VGG13BNImpl(int64_t num_classes, bool initialize_weights)
106
    : VGGImpl(makeLayers(cfgs['B'], true), num_classes, initialize_weights) {}
Shahriar's avatar
Shahriar committed
107
108

VGG16BNImpl::VGG16BNImpl(int64_t num_classes, bool initialize_weights)
109
    : VGGImpl(makeLayers(cfgs['D'], true), num_classes, initialize_weights) {}
Shahriar's avatar
Shahriar committed
110
111

VGG19BNImpl::VGG19BNImpl(int64_t num_classes, bool initialize_weights)
112
    : VGGImpl(makeLayers(cfgs['E'], true), num_classes, initialize_weights) {}
Shahriar's avatar
Shahriar committed
113
114
115

} // namespace models
} // namespace vision