Skip to content

Commit

Permalink
Add pack description
Browse files Browse the repository at this point in the history
  • Loading branch information
l-bat committed Jan 9, 2020
1 parent 752653c commit 7eba3a7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 39 deletions.
2 changes: 1 addition & 1 deletion modules/dnn/include/opencv2/dnn/all_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
std::vector<size_t> pads_begin, pads_end;
CV_DEPRECATED_EXTERNAL Size kernel, stride, pad;
CV_DEPRECATED_EXTERNAL int pad_l, pad_t, pad_r, pad_b;
bool globalPooling;
CV_DEPRECATED_EXTERNAL bool globalPooling;
std::vector<bool> isGlobalPooling;
bool computeMaxIdx;
String padMode;
Expand Down
20 changes: 16 additions & 4 deletions modules/dnn/src/layers/layers_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,26 @@ void getStrideAndPadding(const LayerParams &params, std::vector<size_t>& pads_be
}
}

void getPoolingKernelParams(const LayerParams &params, std::vector<size_t>& kernel, bool &globalPooling,
void getPoolingKernelParams(const LayerParams &params, std::vector<size_t>& kernel, std::vector<bool>& globalPooling,
std::vector<size_t>& pads_begin, std::vector<size_t>& pads_end,
std::vector<size_t>& strides, cv::String &padMode)
{
globalPooling = params.has("global_pooling") &&
params.get<bool>("global_pooling");
bool is_global = params.get<bool>("global_pooling", false);
globalPooling = std::vector<bool>(3, is_global);
if (params.has("global_d"))
{
globalPooling[0] = params.get<bool>("global_d");
}
else if (params.has("global_h"))
{
globalPooling[1] = params.get<bool>("global_h");
}
else if (params.has("global_w"))
{
globalPooling[2] = params.get<bool>("global_w");
}

if (globalPooling)
if (is_global)
{
util::getStrideAndPadding(params, pads_begin, pads_end, strides, padMode);
if(params.has("kernel_h") || params.has("kernel_w") || params.has("kernel_size"))
Expand Down
2 changes: 1 addition & 1 deletion modules/dnn/src/layers/layers_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void getConvolutionKernelParams(const LayerParams &params, std::vector<size_t>&
std::vector<size_t>& pads_end, std::vector<size_t>& strides, std::vector<size_t>& dilations,
cv::String &padMode, std::vector<size_t>& adjust_pads);

void getPoolingKernelParams(const LayerParams &params, std::vector<size_t>& kernel, bool &globalPooling,
void getPoolingKernelParams(const LayerParams &params, std::vector<size_t>& kernel, std::vector<bool>& globalPooling,
std::vector<size_t>& pads_begin, std::vector<size_t>& pads_end, std::vector<size_t>& strides, cv::String &padMode);

void getConvPoolOutParams(const std::vector<int>& inp, const std::vector<size_t>& kernel,
Expand Down
42 changes: 16 additions & 26 deletions modules/dnn/src/layers/pooling_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class PoolingLayerImpl CV_FINAL : public PoolingLayer
{
computeMaxIdx = true;
globalPooling = false;
isGlobalPooling = std::vector<bool>(3, false);
stride = Size(1, 1);
pad_t = pad_l = pad_b = pad_r = 0;

Expand All @@ -95,7 +96,8 @@ class PoolingLayerImpl CV_FINAL : public PoolingLayer
else
CV_Error(Error::StsBadArg, "Unknown pooling type \"" + pool + "\"");

getPoolingKernelParams(params, kernel_size, globalPooling, pads_begin, pads_end, strides, padMode);
getPoolingKernelParams(params, kernel_size, isGlobalPooling, pads_begin, pads_end, strides, padMode);
globalPooling = std::accumulate(isGlobalPooling.begin(), isGlobalPooling.end(), 0) == 3;
if (kernel_size.size() == 2) {
kernel = Size(kernel_size[1], kernel_size[0]);
stride = Size(strides[1], strides[0]);
Expand Down Expand Up @@ -125,14 +127,7 @@ class PoolingLayerImpl CV_FINAL : public PoolingLayer

setParamsFrom(params);
ceilMode = params.get<bool>("ceil_mode", true);
if (params.has("is_global_pooling"))
{
const DictValue &global_axis = params.get("is_global_pooling");
int size = global_axis.size();
isGlobalPooling.resize(size);
for (int i = 0; i < size; i++)
isGlobalPooling[i] = global_axis.get<bool>(i);
}

spatialScale = params.get<float>("spatial_scale", 1);
avePoolPaddedArea = params.get<bool>("ave_pool_padded_area", true);
}
Expand All @@ -155,17 +150,14 @@ class PoolingLayerImpl CV_FINAL : public PoolingLayer
inp.push_back(inputs[0].size[i]);
out.push_back(outputs[0].size[i]);
}
if (globalPooling) {
kernel = Size(inp[1], inp[0]);
kernel_size = std::vector<size_t>(inp.begin(), inp.end());
} else if (!isGlobalPooling.empty()) {
for (int i = 0; i < isGlobalPooling.size(); i++)
{
if (isGlobalPooling[i])
kernel_size[i] = inp[i];
}
kernel = Size(kernel_size[1], kernel_size[0]);
kernel_size.resize(out.size());
int diff_size = isGlobalPooling.size() - kernel_size.size();
for (int i = 0; i < kernel_size.size(); i++)
{
if (isGlobalPooling[i + diff_size])
kernel_size[i] = inp[i];
}
kernel = Size(kernel_size[1], kernel_size[0]);

getConvPoolPaddings(inp, kernel_size, strides, padMode, pads_begin, pads_end);
if (pads_begin.size() == 2) {
Expand Down Expand Up @@ -1053,14 +1045,12 @@ virtual Ptr<BackendNode> initNgraph(const std::vector<Ptr<BackendWrapper> >& inp
outShape[0] = inputs[1][0]; // Number of proposals;
outShape[1] = psRoiOutChannels;
}
else if (!isGlobalPooling.empty())

int diff_size = isGlobalPooling.size() - (outShape.size() - 2);
for (int i = 2; i < outShape.size(); i++)
{
CV_Assert(isGlobalPooling.size() == inpShape.size());
for (int i = 0; i < isGlobalPooling.size(); i++)
{
if (isGlobalPooling[i])
outShape[2 + i] = 1;
}
if (isGlobalPooling[i - 2 + diff_size])
outShape[i] = 1;
}

int numOutputs = requiredOutputs ? requiredOutputs : (type == MAX ? 2 : 1);
Expand Down
16 changes: 9 additions & 7 deletions modules/dnn/src/tensorflow/tf_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1961,8 +1961,7 @@ void TFImporter::populateNet(Net dstNet)
CV_Assert(layer_id.find(avgName) == layer_id.end());
avgLp.set("pool", "ave");
// pooling kernel H x 1
bool isGlobalPooling[] = {true, false};
avgLp.set("is_global_pooling", DictValue::arrayInt(&isGlobalPooling[0], 2));
avgLp.set("global_h", true);
avgLp.set("kernel_size", 1);
int avgId = dstNet.addLayer(avgName, "Pooling", avgLp);
layer_id[avgName] = avgId;
Expand Down Expand Up @@ -2025,6 +2024,12 @@ void TFImporter::populateNet(Net dstNet)
}
else if (type == "Pack")
{
// op: tf.stack(list of tensors, axis=0)
// Join a list of inputs along a new axis.
// The "axis" specifies the index of the new axis in the dimensions of the output.
// Example: given a list with "N" tensors of shape (C, H, W):
// if axis == 0 then the output tensor will have the shape (N, C, H, W),
// if axis == 1 then the output tensor will have the shape (C, N, H, W).
CV_Assert(hasLayerAttr(layer, "axis"));
int dim = (int)getLayerAttr(layer, "axis").i();
if (dim != 0)
Expand Down Expand Up @@ -2054,11 +2059,8 @@ void TFImporter::populateNet(Net dstNet)
int id = dstNet.addLayer(name, "Concat", layerParams);
layer_id[name] = id;

for (int li = 0; li < num; li++) {
Pin inp = parsePin(reshape_names[li]);
connect(layer_id, dstNet, inp, id, li);
}

for (int li = 0; li < num; li++)
connect(layer_id, dstNet, Pin(reshape_names[li]), id, li);
}
else if (type == "ClipByValue")
{
Expand Down

0 comments on commit 7eba3a7

Please sign in to comment.