Skip to content

Commit d8107a5

Browse files
committed
Merge pull request opencv#18996 from LupusSanctus:am/dnn_bilinear_resize
2 parents e8348e5 + a82c50e commit d8107a5

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

modules/dnn/src/layers/resize_layer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ResizeLayerImpl : public ResizeLayer
4242
CV_Check(interpolation, interpolation == "nearest" || interpolation == "opencv_linear" || interpolation == "bilinear", "");
4343

4444
alignCorners = params.get<bool>("align_corners", false);
45+
halfPixelCenters = params.get<bool>("half_pixel_centers", false);
4546
}
4647

4748
bool getMemoryShapes(const std::vector<MatShape> &inputs,
@@ -114,7 +115,7 @@ class ResizeLayerImpl : public ResizeLayer
114115

115116
Mat& inp = inputs[0];
116117
Mat& out = outputs[0];
117-
if (interpolation == "nearest" || interpolation == "opencv_linear")
118+
if (interpolation == "nearest" || interpolation == "opencv_linear" || (interpolation == "bilinear" && halfPixelCenters))
118119
{
119120
InterpolationFlags mode = interpolation == "nearest" ? INTER_NEAREST : INTER_LINEAR;
120121
for (size_t n = 0; n < inputs[0].size[0]; ++n)
@@ -236,6 +237,7 @@ class ResizeLayerImpl : public ResizeLayer
236237
String interpolation;
237238
float scaleWidth, scaleHeight;
238239
bool alignCorners;
240+
bool halfPixelCenters;
239241
};
240242

241243

modules/dnn/src/tensorflow/tf_importer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,9 @@ void TFImporter::populateNet(Net dstNet)
19621962
if (hasLayerAttr(layer, "align_corners"))
19631963
layerParams.set("align_corners", getLayerAttr(layer, "align_corners").b());
19641964

1965+
if (hasLayerAttr(layer, "half_pixel_centers"))
1966+
layerParams.set("half_pixel_centers", getLayerAttr(layer, "half_pixel_centers").b());
1967+
19651968
int id = dstNet.addLayer(name, "Resize", layerParams);
19661969
layer_id[name] = id;
19671970

modules/dnn/test/test_tf_importer.cpp

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ class Test_TensorFlow_layers : public DNNTestLayer
8181
{
8282
public:
8383
void runTensorFlowNet(const std::string& prefix, bool hasText = false,
84-
double l1 = 0.0, double lInf = 0.0, bool memoryLoad = false)
84+
double l1 = 0.0, double lInf = 0.0, bool memoryLoad = false, const std::string& groupPrefix = "")
8585
{
86-
std::string netPath = path(prefix + "_net.pb");
87-
std::string netConfig = (hasText ? path(prefix + "_net.pbtxt") : "");
86+
std::string netPath = path(prefix + groupPrefix + "_net.pb");
87+
std::string netConfig = (hasText ? path(prefix + groupPrefix + "_net.pbtxt") : "");
8888
std::string inpPath = path(prefix + "_in.npy");
89-
std::string outPath = path(prefix + "_out.npy");
89+
std::string outPath = path(prefix + groupPrefix + "_out.npy");
9090

9191
cv::Mat input = blobFromNPY(inpPath);
9292
cv::Mat ref = blobFromNPY(outPath);
@@ -975,10 +975,53 @@ TEST_P(Test_TensorFlow_layers, keras_mobilenet_head)
975975
runTensorFlowNet("keras_learning_phase");
976976
}
977977

978+
// TF case: align_corners=False, half_pixel_centers=False
978979
TEST_P(Test_TensorFlow_layers, resize_bilinear)
979980
{
980981
runTensorFlowNet("resize_bilinear");
982+
}
983+
984+
// TF case: align_corners=True, half_pixel_centers=False
985+
TEST_P(Test_TensorFlow_layers, resize_bilinear_align_corners)
986+
{
987+
runTensorFlowNet("resize_bilinear",
988+
false, 0.0, 0.0, false, // default parameters
989+
"_align_corners");
990+
}
991+
992+
// TF case: align_corners=False, half_pixel_centers=True
993+
TEST_P(Test_TensorFlow_layers, resize_bilinear_half_pixel)
994+
{
995+
if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
996+
applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
997+
998+
runTensorFlowNet("resize_bilinear", false, 0.0, 0.0, false, "_half_pixel");
999+
}
1000+
1001+
// TF case: align_corners=False, half_pixel_centers=False
1002+
TEST_P(Test_TensorFlow_layers, resize_bilinear_factor)
1003+
{
9811004
runTensorFlowNet("resize_bilinear_factor");
1005+
}
1006+
1007+
// TF case: align_corners=False, half_pixel_centers=True
1008+
TEST_P(Test_TensorFlow_layers, resize_bilinear_factor_half_pixel)
1009+
{
1010+
if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
1011+
applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
1012+
1013+
runTensorFlowNet("resize_bilinear_factor", false, 0.0, 0.0, false, "_half_pixel");
1014+
}
1015+
1016+
// TF case: align_corners=True, half_pixel_centers=False
1017+
TEST_P(Test_TensorFlow_layers, resize_bilinear_factor_align_corners)
1018+
{
1019+
runTensorFlowNet("resize_bilinear_factor", false, 0.0, 0.0, false, "_align_corners");
1020+
}
1021+
1022+
// TF case: align_corners=False, half_pixel_centers=False
1023+
TEST_P(Test_TensorFlow_layers, resize_bilinear_down)
1024+
{
9821025
runTensorFlowNet("resize_bilinear_down");
9831026
}
9841027

0 commit comments

Comments
 (0)