OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Fix Interpolate reference kernel to support NHWC axes (#38844)
n1harika277117efb28e
diff --git a/src/frontends/tensorflow_common/src/op/interpolate.cpp b/src/frontends/tensorflow_common/src/op/interpolate.cppindex ec4a7cdbd58..286122f6007 100644--- a/src/frontends/tensorflow_common/src/op/interpolate.cpp+++ b/src/frontends/tensorflow_common/src/op/interpolate.cpp@@ -10,6 +10,7 @@#include "openvino/op/divide.hpp"#include "openvino/op/shape_of.hpp"#include "openvino/op/slice.hpp"+#include "utils.hpp"using namespace std;using namespace ov;@@ -67,10 +68,6 @@ OutputVector translate_interpolate_op(const NodeContext& node) {interpolate_attrs.coordinate_transformation_mode = v11::Interpolate::CoordinateTransformMode::ASYMMETRIC;}- // since Interpolate is layout agnostic- // we can avoid Transpose operation by specifying axes = {1, 2} for original NHWC layout- auto axes = make_shared<v0::Constant>(element::i32, Shape{2}, std::vector<int>({1, 2}));-// according to the specification of ResizeBilinear,// it always returns FP32 output type so we immediately align input type for itif (op_type == "ResizeBilinear" || op_type == "RESIZE_BILINEAR") {@@ -81,8 +78,24 @@ OutputVector translate_interpolate_op(const NodeContext& node) {images = make_shared<v0::Convert>(images, element::i32);}+ // Interpolate is layout agnostic, so we normally avoid a Transpose by specifying+ // axes = {1, 2} for the original NHWC layout. The LINEAR_ONNX kernel (used above for+ // 4D ResizeBilinear) only supports axes being either all axes or the trailing rank-2+ // ones, so for that one case we transpose to NCHW, use axes = {2, 3}, and transpose back.+ bool needs_nchw_layout = interpolate_attrs.mode == v11::Interpolate::InterpolateMode::LINEAR_ONNX;+ std::vector<int> axes_values{1, 2};+ if (needs_nchw_layout) {+ axes_values = {2, 3};+ convert_nhwc_to_nchw(true, images, Rank(4));+ }+ auto axes = make_shared<v0::Constant>(element::i32, Shape{2}, axes_values);+Output<Node> interpolate = make_shared<v11::Interpolate>(images, size, axes, interpolate_attrs);+ if (needs_nchw_layout) {+ convert_nchw_to_nhwc(true, interpolate, Rank(4));+ }+// it needs to return original i16 and u16 input_type due to temporal adjustment before// this is not required for ResizeBilinear case since it always returns f32 by specificationif (op_type != "ResizeBilinear" && op_type != "RESIZE_BILINEAR" &&diff --git a/tests/layer_tests/tensorflow_lite_tests/test_tfl_ResizeBilinear4DAxes.py b/tests/layer_tests/tensorflow_lite_tests/test_tfl_ResizeBilinear4DAxes.pynew file mode 100644index 00000000000..54afeb3801e--- /dev/null+++ b/tests/layer_tests/tensorflow_lite_tests/test_tfl_ResizeBilinear4DAxes.py@@ -0,0 +1,74 @@+# Copyright (C) 2018-2026 Intel Corporation+# SPDX-License-Identifier: Apache-2.0++import os++import numpy as np+import tensorflow as tf+from openvino import Core, convert_model+++def make_4d_resize_bilinear_tflite(save_path):+ tf.compat.v1.reset_default_graph()+ with tf.compat.v1.Session() as sess:+ placeholder = tf.compat.v1.placeholder(tf.float32, [1, 3, 4, 3], name="Input")+ tf.compat.v1.image.resize_bilinear(placeholder, size=[8, 8], name="Output")+ graph_def = sess.graph_def++ with tf.Graph().as_default() as g:+ tf.graph_util.import_graph_def(graph_def, name="")+ input_tensors = [g.get_tensor_by_name("Input:0")]+ output_tensors = [g.get_tensor_by_name("Output:0")]++ tflite_model = tf.compat.v1.lite.TFLiteConverter(+ graph_def, input_tensors=input_tensors, output_tensors=output_tensors+ ).convert()++ tflite_path = os.path.join(save_path, "resize_bilinear_4d.tflite")+ with open(tflite_path, "wb") as f:+ f.write(tflite_model)+ return tflite_path+++def test_resize_bilinear_4d_uses_supported_interpolate_axes(tmp_path, ie_device, precision):+ tflite_path = make_4d_resize_bilinear_tflite(str(tmp_path))+ model = convert_model(tflite_path)++ interpolate_ops = [op for op in model.get_ordered_ops() if op.get_type_name() == "Interpolate"]+ assert len(interpolate_ops) == 1, f"expected exactly 1 Interpolate op, got {len(interpolate_ops)}"+ interpolate = interpolate_ops[0]++ axes_input = interpolate.input_value(2).get_node()+ assert axes_input.get_type_name() == "Constant", "Interpolate axes input should be a Constant"+ axes_values = list(axes_input.get_vector())+ assert axes_values == [2, 3], (+ f"Interpolate axes should be the natively-supported {{2, 3}} (reached via an "+ f"explicit Transpose to NCHW), got {axes_values}"+ )++ transpose_ops = [op for op in model.get_ordered_ops() if op.get_type_name() == "Transpose"]+ assert len(transpose_ops) == 2, f"expected 2 Transpose ops (NHWC<->NCHW around Interpolate), got {len(transpose_ops)}"+++def test_resize_bilinear_4d_constant_folds_correctly(ie_device, precision):+ # Converts the raw TF graph directly, skipping TFLiteConverter (which would pre-fold+ # a constant resize on its own) so that OpenVINO's own ConstantFolding evaluates the+ # Interpolate node during conversion -- the exact path that used to throw+ # "Axes are not correct!" before the fix.+ np.random.seed(0)+ image_data = np.random.rand(1, 3, 4, 3).astype(np.float32)+ placeholder_data = np.array([0.0], dtype=np.float32)++ tf.compat.v1.reset_default_graph()+ with tf.compat.v1.Session() as sess:+ placeholder = tf.compat.v1.placeholder(tf.float32, [1], name="Input")+ resized = tf.compat.v1.image.resize_bilinear(tf.constant(image_data), size=[8, 8])+ output = tf.add(resized, placeholder, name="Output")+ expected = sess.run(output, feed_dict={placeholder: placeholder_data})+ graph_def = sess.graph_def++ model = convert_model(graph_def)+ compiled = Core().compile_model(model, "CPU")+ result = compiled([placeholder_data])[compiled.output(0)]++ np.testing.assert_allclose(result, expected, rtol=1e-4, atol=1e-5)