Skip to content

Commit

Permalink
[CPU] Fix RegionYolo (v3) node and Ngraph reference implementations
Browse files Browse the repository at this point in the history
Destination size was calculated incorrectly for v3 versions
  • Loading branch information
EgorDuplensky committed May 25, 2021
1 parent cc81029 commit cf2cd66
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,31 +369,38 @@ inline void MKLDNNRegionYoloNode::calculate_logistic(size_t start_index, int cou
void MKLDNNRegionYoloNode::execute(mkldnn::stream strm) {
auto inputDesc = getParentEdgeAt(0)->getDesc();
auto outputDesc = getChildEdgeAt(0)->getDesc();
size_t mask_size = mask.size();

size_t IW = (inputDesc.getDims().size() > 3) ? inputDesc.getDims()[3] : 1;
size_t IH = (inputDesc.getDims().size() > 2) ? inputDesc.getDims()[2] : 1;
size_t IC = (inputDesc.getDims().size() > 1) ? inputDesc.getDims()[1] : 1;
size_t B = (inputDesc.getDims().size() > 0) ? inputDesc.getDims()[0] : 1;
size_t IC = (inputDesc.getDims().size() > 1) ? inputDesc.getDims()[1] : 1;
size_t IH = (inputDesc.getDims().size() > 2) ? inputDesc.getDims()[2] : 1;
size_t IW = (inputDesc.getDims().size() > 3) ? inputDesc.getDims()[3] : 1;

size_t mask_size = mask.size();
int end_index = 0;
int num_ = 0;
int output_size = 0;
if (do_softmax) {
// Region layer (Yolo v2)
end_index = IW * IH;
num_ = num;
output_size = B * IH * IW * IC; // different shape combinations with the same overall size;
} else {
// Yolo layer (Yolo v3)
end_index = IW * IH * (classes + 1);
num_ = mask_size;
output_size = B * IH * IW * mask_size * (classes + coords + 1);
}

if (output_size != getChildEdgeAt(0)->getMemoryPtr()->GetElementsCount())
IE_THROW() << "Incorrect layer configuration or output dimensions. " << output_size << " != " << getChildEdgeAt(0)->getMemoryPtr()->GetElementsCount();

size_t inputs_size = IH * IW * num_ * (classes + coords + 1);
size_t total_size = 2 * IH * IW;

const auto *src_data = reinterpret_cast<const uint8_t *>(getParentEdgeAt(0)->getMemoryPtr()->GetPtr());
auto *dst_data = reinterpret_cast<uint8_t *>(getChildEdgeAt(0)->getMemoryPtr()->GetPtr());

cpu_convert(src_data, dst_data, inputDesc.getPrecision(), outputDesc.getPrecision(), B * IC * IH * IW);
cpu_convert(src_data, dst_data, inputDesc.getPrecision(), outputDesc.getPrecision(), output_size);

for (int b = 0; b < B; b++) {
for (int n = 0; n < num_; n++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ class RegionYoloCPULayerTest : public testing::WithParamInterface<regionYoloPara

selectedType = getPrimitiveType() + "_" + inPrc.name();

auto ngPrc = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(inPrc);
auto param = std::make_shared<ngraph::op::Parameter>(ngPrc, inputShape);
auto region_yolo = std::make_shared<ngraph::op::v0::RegionYolo>(param, attributes.coordinates, attributes.classes, attributes.num_regions,
attributes.do_softmax, mask, attributes.start_axis, attributes.end_axis);
function = std::make_shared<ngraph::Function>(std::make_shared<ngraph::opset1::Result>(region_yolo), ngraph::ParameterVector{param}, "RegionYolo");
const auto ngPrc = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(inPrc);
auto paramRegionYolo = ngraph::builder::makeParams(ngPrc, {inputShape});

const auto region_yolo = std::make_shared<ngraph::op::v0::RegionYolo>(paramRegionYolo[0],
attributes.coordinates, attributes.classes, attributes.num_regions,
attributes.do_softmax, mask, attributes.start_axis, attributes.end_axis);

function = makeNgraphFunction(ngPrc, paramRegionYolo, region_yolo, "RegionYolo");
}
};

Expand All @@ -99,7 +102,8 @@ const std::vector<ngraph::Shape> inShapes_mxnet = {
{1, 75, 26, 26},
{1, 75, 16, 16},
{1, 75, 13, 13},
{1, 75, 8, 8}
{1, 75, 8, 8},
{1, 303, 28, 28},
};

const std::vector<ngraph::Shape> inShapes_v3 = {
Expand Down Expand Up @@ -158,4 +162,4 @@ INSTANTIATE_TEST_CASE_P(smoke_TestsRegionYolov3CPU, RegionYoloCPULayerTest, test
INSTANTIATE_TEST_CASE_P(smoke_TestsRegionYoloMxnetCPU, RegionYoloCPULayerTest, testCase_yolov3_mxnet, RegionYoloCPULayerTest::getTestCaseName);
INSTANTIATE_TEST_CASE_P(smoke_TestsRegionYoloCaffeCPU, RegionYoloCPULayerTest, testCase_yolov2_caffe, RegionYoloCPULayerTest::getTestCaseName);
} // namespace
} // namespace CPULayerTestsDefinitions
} // namespace CPULayerTestsDefinitions
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,27 @@ namespace ngraph

const auto mask_size = mask.size();

std::copy(input, input + shape_size(input_shape), output);

int num_regions = 0;
int end_index = 0;
int output_size = 0;

if (do_softmax)
{
// Region layer (Yolo v2)
num_regions = regions;
end_index = width * height;
output_size = shape_size(input_shape);
}
else
{
// Yolo layer (Yolo v3)
num_regions = mask_size;
end_index = width * height * (classes + 1);
output_size = width * height * num_regions * (classes + coords + 1);
}

std::copy(input, input + output_size, output);

const int inputs_size = width * height * num_regions * (classes + coords + 1);

for (int batch_idx = 0; batch_idx < batches; batch_idx++)
Expand Down

0 comments on commit cf2cd66

Please sign in to comment.