Skip to content

Commit

Permalink
Fixes for Interpolate-4. (#2281)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgavrilo authored Sep 16, 2020
1 parent 105cd18 commit 2023a7c
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 202 deletions.
13 changes: 9 additions & 4 deletions docs/ops/image/Interpolate_4.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ class InterpolateCalculation:
else:
self.scales = scales

if self.mode == 'nearest':
self.all_scales = np.ones(rank).astype(np.float)
for i, axis in enumerate(self.axes):
self.all_scales[axis] = self.scales[i]

self.input_shape = padded_data.shape
return self.func(padded_data)

Expand Down Expand Up @@ -446,9 +451,9 @@ class InterpolateCalculation:
num_of_axes = len(self.axes)
for coordinates in np.ndindex(tuple(self.output_shape)):
input_coords = np.array(coordinates, dtype=np.int64)
for i, axis in enumerate(self.axes):
in_coord = self.get_original_coordinate(coordinates[axis], self.scales[i], self.output_shape[axis], self.input_shape[axis])
nearest_pixel = self.get_nearest_pixel(in_coord, self.scales[i] < 1)
for axis, scale in enumerate(self.all_scales):
in_coord = self.get_original_coordinate(coordinates[axis], scale, self.output_shape[axis], self.input_shape[axis])
nearest_pixel = self.get_nearest_pixel(in_coord, scale < 1)
input_coords[axis] = max(0, min(nearest_pixel, self.input_shape[axis] - 1))
result[coordinates] = input_data[tuple(input_coords)]

Expand Down Expand Up @@ -487,4 +492,4 @@ class InterpolateCalculation:
</port>
</output>
</layer>
```
```
241 changes: 100 additions & 141 deletions inference-engine/src/mkldnn_plugin/nodes/mkldnn_interpolate_node.cpp

Large diffs are not rendered by default.

21 changes: 7 additions & 14 deletions inference-engine/src/mkldnn_plugin/nodes/mkldnn_interpolate_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,22 @@ class MKLDNNInterpolateNode : public MKLDNNNode {

private:
// nearest neighbor
void NNPlanar(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int ID, int IH, int IW,
float fx, float fy, float fz, int OD, int OH, int OW);
void NNCGathered(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int ID, int IH, int IW,
float fx, float fy, float fz, int OD, int OH, int OW);
void NNRef(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int ID, int IH, int IW,
float fx, float fy, float fz, int OD, int OH, int OW);
void NNPlanar(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int ID, int IH, int IW, int OD, int OH, int OW);
void NNCGathered(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int ID, int IH, int IW, int OD, int OH, int OW);
void NNRef(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int ID, int IH, int IW, int OD, int OH, int OW);

// onnx linear
void linearOnnxPlanar(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int IH, int IW,
float fx, float fy, int OH, int OW);
void linearOnnxCGathered(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int IH, int IW,
float fx, float fy, int OH, int OW);
void linearOnnxRef(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int IH, int IW,
float fx, float fy, int OH, int OW);
void linearOnnxPlanar(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int IH, int IW, int OH, int OW);
void linearOnnxCGathered(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int IH, int IW, int OH, int OW);
void linearOnnxRef(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int IH, int IW, int OH, int OW);

// linear
void linearInterpolation(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int ID, int IH, int IW,
float fx, float fy, float fz, int OD, int OH, int OW, int kernel_width, bool antialias);

// cubic
std::vector<float> getCubicCoeffs(float mantissa, float a);
void cubic(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int IH, int IW,
float fx, float fy, int OH, int OW, float a);
void cubic(const uint8_t *in_ptr_, uint8_t *out_ptr_, int B, int C, int IH, int IW, int OH, int OW, float a);

void buildTblNN(SizeVector& srcDimPad5d, SizeVector& dstDim5d, std::vector<float>& dataScales, InterpolateLayoutType layout);
void buildTblLinearOnnx(SizeVector& srcDimPad5d, SizeVector& dstDim5d, std::vector<float>& dataScales, InterpolateLayoutType layout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const std::vector<InferenceEngine::Precision> prc = {
};

const std::vector<std::vector<size_t>> inShapes = {
{1, 4, 30, 30},
{1, 1, 30, 30},
};

const std::vector<std::vector<size_t>> targetShapes = {
{1, 4, 40, 40},
{1, 1, 40, 40},
};

const std::vector<ngraph::op::v4::Interpolate::InterpolateMode> modesWithoutNearest = {
Expand Down Expand Up @@ -55,7 +55,7 @@ const std::vector<ngraph::op::v4::Interpolate::NearestMode> defaultNearestMode =
};

const std::vector<std::vector<size_t>> pads = {
{0, 0, 1, 1},
// {0, 0, 1, 1},
{0, 0, 0, 0},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ TEST_P(InterpolateLayerCPUTest, CompareWithRefs) {
SKIP_IF_CURRENT_TEST_IS_DISABLED()

Run();
CheckCPUImpl(executableNetwork, "interpolate", inFmts, outFmts, selectedType);
CheckCPUImpl(executableNetwork, "Interpolate", inFmts, outFmts, selectedType);
}

namespace {
Expand All @@ -97,14 +97,19 @@ namespace {
std::vector<CPUSpecificParams> filterCPUInfoForDevice() {
std::vector<CPUSpecificParams> resCPUParams;
if (with_cpu_x86_avx512f()) {
resCPUParams.push_back(CPUSpecificParams{{nChw16c}, {nChw16c}, {}, "unknown"});
resCPUParams.push_back(CPUSpecificParams{{nChw16c, x, x}, {nChw16c}, {"jit_avx512"}, "jit_avx512_FP32"});
resCPUParams.push_back(CPUSpecificParams{{nhwc, x, x}, {nhwc}, {"jit_avx512"}, "jit_avx512_FP32"});
resCPUParams.push_back(CPUSpecificParams{{nchw, x, x}, {nchw}, {"jit_avx2"}, "jit_avx2_FP32"});
} else if (with_cpu_x86_avx2()) {
resCPUParams.push_back(CPUSpecificParams{{nChw8c, x, x}, {nChw8c}, {"jit_avx2"}, "jit_avx2_FP32"});
resCPUParams.push_back(CPUSpecificParams{{nhwc, x, x}, {nhwc}, {"jit_avx2"}, "jit_avx2_FP32"});
resCPUParams.push_back(CPUSpecificParams{{nchw, x, x}, {nchw}, {"jit_avx2"}, "jit_avx2_FP32"});
} else if (with_cpu_x86_sse42()) {
resCPUParams.push_back(CPUSpecificParams{{nChw8c}, {nChw8c}, {}, "unknown"});
resCPUParams.push_back(CPUSpecificParams{{nChw8c, x, x}, {nChw8c}, {"jit_sse42"}, "jit_sse42_FP32"});
resCPUParams.push_back(CPUSpecificParams{{nhwc, x, x}, {nhwc}, {"jit_sse42"}, "jit_sse42_FP32"});
} else {
resCPUParams.push_back(CPUSpecificParams{{nchw, x, x}, {nchw}, {"ref"}, "ref_FP32"});
}
if (with_cpu_x86_sse42()) {
resCPUParams.push_back(CPUSpecificParams{{nhwc}, {nhwc}, {}, "unknown"});
}
resCPUParams.push_back(CPUSpecificParams{{nchw}, {nchw}, {}, "unknown"});
return resCPUParams;
}
/* ========== */
Expand Down Expand Up @@ -168,8 +173,8 @@ INSTANTIATE_TEST_CASE_P(InterpolateNN_Layout_Test, InterpolateLayerCPUTest,
::testing::Combine(
interpolateCasesNN,
::testing::ValuesIn(netPrecisions),
::testing::Values(std::vector<size_t>({1, 20, 40, 40})),
::testing::Values(std::vector<size_t>({1, 20, 50, 60})),
::testing::Values(std::vector<size_t>({1, 1, 40, 40})),
::testing::Values(std::vector<size_t>({1, 1, 50, 60})),
::testing::Values(CommonTestUtils::DEVICE_CPU)),
::testing::ValuesIn(filterCPUInfoForDevice())),
InterpolateLayerCPUTest::getTestCaseName);
Expand All @@ -179,8 +184,8 @@ INSTANTIATE_TEST_CASE_P(InterpolateLinearOnnx_Layout_Test, InterpolateLayerCPUTe
::testing::Combine(
interpolateCasesLinearOnnx,
::testing::ValuesIn(netPrecisions),
::testing::Values(std::vector<size_t>({1, 20, 40, 40})),
::testing::Values(std::vector<size_t>({1, 20, 50, 60})),
::testing::Values(std::vector<size_t>({1, 1, 40, 40})),
::testing::Values(std::vector<size_t>({1, 1, 50, 60})),
::testing::Values(CommonTestUtils::DEVICE_CPU)),
::testing::ValuesIn(filterCPUInfoForDevice())),
InterpolateLayerCPUTest::getTestCaseName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const char *CPUTestsBase::cpu_fmt2str(cpu_memory_format_t v) {
if (v == nCdhw8c) return "nCdhw8c";
if (v == nCdhw16c) return "nCdhw16c";
if (v == ndhwc) return "ndhwc";
if (v == x) return "x";
assert(!"unknown fmt");
return "undef";
}
Expand All @@ -33,6 +34,7 @@ cpu_memory_format_t CPUTestsBase::cpu_str2fmt(const char *str) {
CASE(nCdhw8c);
CASE(nCdhw16c);
CASE(ndhwc);
CASE(x);
#undef CASE
assert(!"unknown memory format");
return undef;
Expand Down Expand Up @@ -85,14 +87,12 @@ void CPUTestsBase::CheckCPUImpl(InferenceEngine::ExecutableNetwork &execNet, std
ASSERT_LE(inputMemoryFormats.size(), node->get_input_size());
ASSERT_LE(outputMemoryFormats.size(), node->get_output_size());
for (int i = 0; i < inputMemoryFormats.size(); i++) {
for (const auto & parentPort : node->input_values()) {
for (const auto & port : node->inputs()) {
if (port.get_tensor_ptr() == parentPort.get_tensor_ptr()) {
auto parentNode = parentPort.get_node_shared_ptr();
auto actualInputMemoryFormat = getExecValueOutputsLayout(parentNode);
ASSERT_EQ(inputMemoryFormats[i], cpu_str2fmt(actualInputMemoryFormat.c_str()));
}
}
const auto parentPort = node->input_values()[i];
const auto port = node->inputs()[i];
if ((parentPort.get_tensor_ptr() == port.get_tensor_ptr())) {
auto parentNode = parentPort.get_node_shared_ptr();
auto actualInputMemoryFormat = getExecValueOutputsLayout(parentNode);
ASSERT_EQ(inputMemoryFormats[i], cpu_str2fmt(actualInputMemoryFormat.c_str()));
}
}
for (int i = 0; i < outputMemoryFormats.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace CPUTestUtils {
nCdhw8c,
nCdhw16c,
ndhwc,
x,
undef
} cpu_memory_format_t;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ namespace ngraph
float length_resized,
float length_original) const
{
if ((x_scale == 1.0f) || (length_resized == length_original))
{
return x_resized;
}
return m_func(x_resized, x_scale, length_resized, length_original);
}

Expand Down Expand Up @@ -226,6 +222,14 @@ namespace ngraph
, m_out_shape{out_shape}
, m_scales{scales}
{
size_t input_rank = input_data_shape.size();
m_all_scales = std::vector<float>(input_rank, 1.0f);
size_t num_of_axes = axes.size();

for (size_t i = 0; i < num_of_axes; ++i)
{
m_all_scales[axes[i]] = scales[i];
}
}

~InterpolateEvalHelper() = default;
Expand Down Expand Up @@ -292,12 +296,6 @@ namespace ngraph
const InfoForLinearMode& info,
const Coordinate& index);

int64_t clip_coord(int64_t coord, float length)
{
return std::max(static_cast<int64_t>(0),
std::min(coord, static_cast<int64_t>(length) - 1));
}

private:
GetNearestPixel m_get_nearest_pixel;
GetOriginalCoordinate m_get_original_coord;
Expand All @@ -310,6 +308,7 @@ namespace ngraph
Shape m_out_shape;

std::vector<float> m_scales;
std::vector<float> m_all_scales;
};

/// \brief Class to perform interpolation calculation.
Expand Down Expand Up @@ -346,6 +345,8 @@ namespace ngraph
T* out,
const Shape& out_shape)
{
assert(axes.size() == scales.size());

m_input_data_shape = input_data_shape;
m_axes = axes;
m_out_shape = out_shape;
Expand Down Expand Up @@ -589,4 +590,4 @@ namespace ngraph
}
}
}
}
}
21 changes: 11 additions & 10 deletions ngraph/core/reference/src/runtime/reference/interpolate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ std::array<float, 4> InterpolateEvalHelper::get_cubic_coeff(float s, float a)

Coordinate InterpolateEvalHelper::get_input_coords_for_nearest_mode(const Coordinate& output_coord)
{
std::size_t num_of_axes = m_axes.size();
std::size_t input_rank = m_input_data_shape.size();
auto input_coord = output_coord;
for (std::size_t i = 0; i < num_of_axes; ++i)
for (std::size_t i = 0; i < input_rank; ++i)
{
int64_t axis = m_axes[i];
float length_original = static_cast<float>(m_input_data_shape[axis]);
float in_coord = m_get_original_coord(static_cast<float>(output_coord[axis]),
m_scales[i],
static_cast<float>(m_out_shape[axis]),
float length_original = static_cast<float>(m_input_data_shape[i]);
float in_coord = m_get_original_coord(static_cast<float>(output_coord[i]),
m_all_scales[i],
static_cast<float>(m_out_shape[i]),
length_original);
int64_t nearest_pixel = m_get_nearest_pixel(in_coord, m_scales[i] < 1.0);
input_coord[axis] = clip_coord(nearest_pixel, length_original);
int64_t nearest_pixel = m_get_nearest_pixel(in_coord, m_all_scales[i] < 1.0);
input_coord[i] =
std::max(static_cast<int64_t>(0),
std::min(nearest_pixel, static_cast<int64_t>(length_original) - 1));
}

return input_coord;
Expand Down Expand Up @@ -306,4 +307,4 @@ InterpolateEvalHelper::LinearModeInnerIterationResult
result.inner_coord = inner_coord;

return result;
}
}

0 comments on commit 2023a7c

Please sign in to comment.