Caffe2 - C++ API
A deep learning, cross platform ML framework
conv_op.cc
1 
17 #include "caffe2/operators/conv_op.h"
18 #include "caffe2/operators/conv_op_impl.h"
19 #include "caffe2/operators/conv_pool_op_base.h"
20 
21 namespace caffe2 {
22 
23 const char* kConvDoc = R"DOC(
24 Note that other parameters, such as the stride and
25 kernel size, or the pads' sizes in each direction are not necessary for input
26 because they are provided by the ConvPoolOpBase operator. Various dimension
27 checks are done implicitly, and the sizes are specified in the Input docs for
28 this operator. As is expected, the filter is convolved with a subset of the
29 image and the bias is added; this is done throughout the image data and the
30 output is computed. As a side note on the implementation layout:
31 conv_op_impl.h is the templated implementation of the conv_op.h file, which is
32 why they are separate files.
33 )DOC";
34 
35 std::function<void(OpSchema&)> ConvDocGenerator(const char* dim) {
36  return [=](OpSchema& schema) {
37  string doc = R"DOC(
38 The convolution operator consumes an input vector, a {dim}filter blob
39 and a bias blob and computes the output. {conv_doc})DOC";
40  ReplaceAll(doc, "{dim}", dim);
41  ReplaceAll(doc, "{conv_doc}", kConvDoc);
42  schema.SetDoc(doc);
43  schema.Input(
44  0,
45  "X",
46  "Input data blob from previous layer; has size (N x C x H x W), "
47  "where N is the batch size, C is the number of channels, "
48  "and H and W are the height and width. Note that this is for the NCHW "
49  "usage. On the other hand, the NHWC Op has a different set of "
50  "dimension constraints. ");
51  schema.Input(
52  1,
53  "filter",
54  "The filter blob that will be used in the "
55  "convolutions; has size (M x C x kH x kW), where C is the number of "
56  "channels, and kH and kW are the height and width of the kernel.");
57  schema.Input(
58  2,
59  "bias",
60  "The 1D bias blob that is added through the "
61  "convolution; has size (M).");
62  schema.Output(
63  0,
64  "Y",
65  "Output data blob that contains the result of the "
66  "convolution. The output dimensions are functions of the kernel size, "
67  "stride size, and pad lengths."
68  "");
69  };
70 }
71 REGISTER_CPU_OPERATOR(Conv, ConvOp<float, CPUContext>);
72 
73 OPERATOR_SCHEMA(Conv)
74  .NumInputs(2, 3)
75  .NumOutputs(1)
76  .TensorInferenceFunction(ConvPoolOpBase<CPUContext>::TensorInferenceForConv)
77  .CostInferenceFunction(OpSchema::CostInferenceFunctionType(
78  ConvPoolOpBase<CPUContext>::CostInferenceForConv))
79  .FillUsing(ConvDocGenerator(""));
80 
81 REGISTER_CPU_OPERATOR(Conv1D, ConvOp<float, CPUContext>);
82 
83 OPERATOR_SCHEMA(Conv1D)
84  .NumInputs(2, 3)
85  .NumOutputs(1)
86  .TensorInferenceFunction(ConvPoolOpBase<CPUContext>::TensorInferenceForConv)
87  .FillUsing(ConvDocGenerator("1D "));
88 
89 REGISTER_CPU_OPERATOR(Conv2D, ConvOp<float, CPUContext>);
90 
91 OPERATOR_SCHEMA(Conv2D)
92  .NumInputs(2, 3)
93  .NumOutputs(1)
94  .CostInferenceFunction(OpSchema::CostInferenceFunctionType(
95  ConvPoolOpBase<CPUContext>::CostInferenceForConv))
96  .TensorInferenceFunction(ConvPoolOpBase<CPUContext>::TensorInferenceForConv)
97  .FillUsing(ConvDocGenerator("2D "));
98 
99 REGISTER_CPU_OPERATOR(Conv3D, ConvOp<float, CPUContext>);
100 
101 OPERATOR_SCHEMA(Conv3D)
102  .NumInputs(2, 3)
103  .NumOutputs(1)
104  .CostInferenceFunction(OpSchema::CostInferenceFunctionType(
105  ConvPoolOpBase<CPUContext>::CostInferenceForConv))
106  .TensorInferenceFunction(ConvPoolOpBase<CPUContext>::TensorInferenceForConv)
107  .FillUsing(ConvDocGenerator("3D "));
108 
109 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.
std::function< struct Cost(const OperatorDef &, const vector< TensorShape > &)> CostInferenceFunctionType
Registers a function that takes in an OperatorDef and a series of input shapes and returns the total ...