mirror of
https://github.com/opencv/opencv.git
synced 2025-08-06 14:36:36 +08:00
Added reduce sum by channel support
This commit is contained in:
parent
aab62aa6dd
commit
cc6d48959e
@ -2360,12 +2360,9 @@ void TFImporter::parseNode(const tensorflow::NodeDef& layer_)
|
|||||||
// To keep correct order after squeeze dims we first need to change layout from NCHW to NHWC
|
// To keep correct order after squeeze dims we first need to change layout from NCHW to NHWC
|
||||||
LayerParams permLP;
|
LayerParams permLP;
|
||||||
int order[] = {0, 2, 3, 1}; // From OpenCV's NCHW to NHWC.
|
int order[] = {0, 2, 3, 1}; // From OpenCV's NCHW to NHWC.
|
||||||
permLP.set("order", DictValue::arrayInt<int*>(order, 4));
|
|
||||||
std::string permName = name + "/nchw";
|
std::string permName = name + "/nchw";
|
||||||
CV_Assert(layer_id.find(permName) == layer_id.end());
|
Pin inpId = Pin(name);
|
||||||
int permId = dstNet.addLayer(permName, "Permute", permLP);
|
addPermuteLayer(order, permName, inpId);
|
||||||
layer_id[permName] = permId;
|
|
||||||
connect(layer_id, dstNet, Pin(name), permId, 0);
|
|
||||||
|
|
||||||
LayerParams squeezeLp;
|
LayerParams squeezeLp;
|
||||||
std::string squeezeName = name + "/squeeze";
|
std::string squeezeName = name + "/squeeze";
|
||||||
@ -2377,6 +2374,38 @@ void TFImporter::parseNode(const tensorflow::NodeDef& layer_)
|
|||||||
connect(layer_id, dstNet, Pin(permName), squeezeId, 0);
|
connect(layer_id, dstNet, Pin(permName), squeezeId, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (axis == 1)
|
||||||
|
{
|
||||||
|
int order[] = {0, 2, 3, 1}; // From OpenCV's NCHW to NHWC.
|
||||||
|
Pin inpId = parsePin(layer.input(0));
|
||||||
|
addPermuteLayer(order, name + "/nhwc", inpId);
|
||||||
|
|
||||||
|
layerParams.set("pool", type == "Mean" ? "ave" : "sum");
|
||||||
|
layerParams.set("kernel_h", 1);
|
||||||
|
layerParams.set("global_pooling_w", true);
|
||||||
|
int id = dstNet.addLayer(name, "Pooling", layerParams);
|
||||||
|
layer_id[name] = id;
|
||||||
|
connect(layer_id, dstNet, inpId, id, 0);
|
||||||
|
|
||||||
|
if (!keepDims)
|
||||||
|
{
|
||||||
|
LayerParams squeezeLp;
|
||||||
|
std::string squeezeName = name + "/squeeze";
|
||||||
|
CV_Assert(layer_id.find(squeezeName) == layer_id.end());
|
||||||
|
int channel_id = 3; // TF NHWC layout
|
||||||
|
squeezeLp.set("axis", channel_id - 1);
|
||||||
|
squeezeLp.set("end_axis", channel_id);
|
||||||
|
int squeezeId = dstNet.addLayer(squeezeName, "Flatten", squeezeLp);
|
||||||
|
layer_id[squeezeName] = squeezeId;
|
||||||
|
connect(layer_id, dstNet, Pin(name), squeezeId, 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int order[] = {0, 3, 1, 2}; // From NHWC to OpenCV's NCHW.
|
||||||
|
Pin inpId = parsePin(name);
|
||||||
|
addPermuteLayer(order, name + "/nchw", inpId);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (indices.total() != 2 || indices.at<int>(0) != 1 || indices.at<int>(1) != 2)
|
if (indices.total() != 2 || indices.at<int>(0) != 1 || indices.at<int>(1) != 2)
|
||||||
CV_Error(Error::StsNotImplemented, "Unsupported mode of reduce_mean or reduce_sum operation.");
|
CV_Error(Error::StsNotImplemented, "Unsupported mode of reduce_mean or reduce_sum operation.");
|
||||||
|
@ -135,6 +135,16 @@ TEST_P(Test_TensorFlow_layers, reduce_sum)
|
|||||||
runTensorFlowNet("sum_pool_by_axis");
|
runTensorFlowNet("sum_pool_by_axis");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_P(Test_TensorFlow_layers, reduce_sum_channel)
|
||||||
|
{
|
||||||
|
runTensorFlowNet("reduce_sum_channel");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_P(Test_TensorFlow_layers, reduce_sum_channel_keep_dims)
|
||||||
|
{
|
||||||
|
runTensorFlowNet("reduce_sum_channel", false, 0.0, 0.0, false, "_keep_dims");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_P(Test_TensorFlow_layers, conv_single_conv)
|
TEST_P(Test_TensorFlow_layers, conv_single_conv)
|
||||||
{
|
{
|
||||||
runTensorFlowNet("single_conv");
|
runTensorFlowNet("single_conv");
|
||||||
|
Loading…
Reference in New Issue
Block a user