mirror of
https://github.com/opencv/opencv.git
synced 2026-12-02 10:11:04 -06:00
Layers for fast-neural-style models: https://github.com/jcjohnson/fast-neural-style
This commit is contained in:
@@ -10,6 +10,7 @@ Implementation of padding layer, which adds paddings to input blob.
|
||||
*/
|
||||
|
||||
#include "../precomp.hpp"
|
||||
#include "layers_common.hpp"
|
||||
#include "op_halide.hpp"
|
||||
#include <vector>
|
||||
|
||||
@@ -26,6 +27,7 @@ public:
|
||||
setParamsFrom(params);
|
||||
paddingValue = params.get<float>("value", 0);
|
||||
inputDims = params.get<int>("input_dims", -1);
|
||||
paddingType = params.get<String>("type", "constant");
|
||||
|
||||
CV_Assert(params.has("paddings"));
|
||||
const DictValue& paddingsParam = params.get("paddings");
|
||||
@@ -94,8 +96,45 @@ public:
|
||||
CV_TRACE_FUNCTION();
|
||||
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
|
||||
|
||||
outputs[0].setTo(paddingValue);
|
||||
inputs[0]->copyTo(outputs[0](dstRanges));
|
||||
if (paddingType == "constant")
|
||||
{
|
||||
outputs[0].setTo(paddingValue);
|
||||
inputs[0]->copyTo(outputs[0](dstRanges));
|
||||
}
|
||||
else if (paddingType == "reflect")
|
||||
{
|
||||
CV_Assert(inputs.size() == 1);
|
||||
CV_Assert(outputs.size() == 1);
|
||||
CV_Assert(inputs[0]->dims == 4);
|
||||
CV_Assert(outputs[0].dims == 4);
|
||||
|
||||
if (inputs[0]->size[0] != outputs[0].size[0] || inputs[0]->size[1] != outputs[0].size[1])
|
||||
CV_Error(Error::StsNotImplemented, "Only spatial reflection padding is supported.");
|
||||
|
||||
const int inpHeight = inputs[0]->size[2];
|
||||
const int inpWidth = inputs[0]->size[3];
|
||||
const int outHeight = outputs[0].size[2];
|
||||
const int outWidth = outputs[0].size[3];
|
||||
const int padTop = dstRanges[2].start;
|
||||
const int padBottom = outHeight - dstRanges[2].end;
|
||||
const int padLeft = dstRanges[3].start;
|
||||
const int padRight = outWidth - dstRanges[3].end;
|
||||
CV_Assert(padTop < inpHeight, padBottom < inpHeight,
|
||||
padLeft < inpWidth, padRight < inpWidth);
|
||||
|
||||
for (size_t n = 0; n < inputs[0]->size[0]; ++n)
|
||||
{
|
||||
for (size_t ch = 0; ch < inputs[0]->size[1]; ++ch)
|
||||
{
|
||||
copyMakeBorder(getPlane(*inputs[0], n, ch),
|
||||
getPlane(outputs[0], n, ch),
|
||||
padTop, padBottom, padLeft, padRight,
|
||||
BORDER_REFLECT_101);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
CV_Error(Error::StsNotImplemented, "Unknown padding type: " + paddingType);
|
||||
}
|
||||
|
||||
virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs)
|
||||
@@ -124,6 +163,7 @@ private:
|
||||
std::vector<Range> dstRanges;
|
||||
int inputDims;
|
||||
float paddingValue;
|
||||
std::string paddingType;
|
||||
};
|
||||
|
||||
Ptr<PaddingLayer> PaddingLayer::create(const LayerParams ¶ms)
|
||||
|
||||
Reference in New Issue
Block a user