File: depthwise_conv_2d_layer.hpp

package info (click to toggle)
frugally-deep 0.18.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,036 kB
  • sloc: cpp: 6,680; python: 1,262; makefile: 4; sh: 1
file content (66 lines) | stat: -rw-r--r-- 2,095 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2016, Tobias Hermann.
// https://github.com/Dobiasd/frugally-deep
// Distributed under the MIT License.
// (See accompanying LICENSE file or at
//  https://opensource.org/licenses/MIT)

#pragma once

#include "fdeep/depthwise_convolution.hpp"
#include "fdeep/filter.hpp"
#include "fdeep/layers/layer.hpp"
#include "fdeep/shape2.hpp"
#include "fdeep/tensor_shape.hpp"

#include <fplus/fplus.hpp>

#include <algorithm>
#include <cstddef>
#include <string>
#include <vector>

namespace fdeep {
namespace internal {

    // Convolve depth slices separately.
    class depthwise_conv_2d_layer : public layer {
    public:
        explicit depthwise_conv_2d_layer(
            const std::string& name, std::size_t input_depth,
            const tensor_shape& filter_shape,
            const shape2& strides, padding p,
            const shape2& dilation_rate,
            const float_vec& depthwise_weights,
            const float_vec& bias)
            : layer(name)
            , filters_(generate_im2col_filter_matrix(
                  generate_filters(dilation_rate, filter_shape,
                      input_depth, depthwise_weights, bias, false)))
            , strides_(strides)
            , padding_(p)
        {
            assertion(filter_shape.volume() > 0, "filter must have volume");
            assertion(strides.area() > 0, "invalid strides");
            assertion(filters_.filter_count_ == input_depth,
                "invalid number of filters");
            assertion(filters_.filter_shape_.depth_ == 1,
                "invalid filter shape");
        }

    protected:
        tensors apply_impl(const tensors& inputs) const override
        {
            const auto& input = single_tensor_from_tensors(inputs);
            const auto result = depthwise_convolve(strides_, padding_, filters_, input);
            assertion(result.shape().depth_ == input.shape().depth_,
                "Invalid output shape");
            return { result };
        }

        convolution_filter_matrices filters_;
        shape2 strides_;
        padding padding_;
    };

}
}