Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[JS API] Fix inference for outputs with missing names (#28665)

Kirill Suvorovffffa3c45f2a
diff --git a/src/bindings/js/node/CMakeLists.txt b/src/bindings/js/node/CMakeLists.txt
index 95c6682bc40..4217b8f64e6 100644
--- a/src/bindings/js/node/CMakeLists.txt
+++ b/src/bindings/js/node/CMakeLists.txt
@@ -78,7 +78,7 @@ target_include_directories(${PROJECT_NAME} PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/.."
)
-target_link_libraries(${PROJECT_NAME} PRIVATE openvino::runtime openvino::util ${DELAYIMP_LIB} ${CMAKE_JS_LIB})
+target_link_libraries(${PROJECT_NAME} PRIVATE openvino::core::dev openvino::runtime openvino::util ${DELAYIMP_LIB} ${CMAKE_JS_LIB})
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
# Generate node.lib
diff --git a/src/bindings/js/node/src/async_reader.cpp b/src/bindings/js/node/src/async_reader.cpp
index 7499ba45632..877f0e8094e 100644
--- a/src/bindings/js/node/src/async_reader.cpp
+++ b/src/bindings/js/node/src/async_reader.cpp
@@ -4,12 +4,14 @@
#include "node/include/async_reader.hpp"
#include "node/include/model_wrap.hpp"
+#include "openvino/core/model_util.hpp"
void ReaderWorker::Execute() {
if (_args->model_str.empty())
_model = _core.read_model(_args->model_path, _args->bin_path);
else
_model = _core.read_model(_args->model_str, _args->weight_tensor);
+ ov::util::set_tensors_names(ov::AUTO, *_model);
}
void ReaderWorker::OnOK() {
diff --git a/src/bindings/js/node/src/core_wrap.cpp b/src/bindings/js/node/src/core_wrap.cpp
index 176fa940b5e..fbf69fe2c24 100644
--- a/src/bindings/js/node/src/core_wrap.cpp
+++ b/src/bindings/js/node/src/core_wrap.cpp
@@ -11,6 +11,7 @@
#include "node/include/model_wrap.hpp"
#include "node/include/read_model_args.hpp"
#include "node/include/type_validation.hpp"
+#include "openvino/core/model_util.hpp"
#include "openvino/util/common_util.hpp"
void validate_set_property_args(const Napi::CallbackInfo& info) {
@@ -92,6 +93,7 @@ Napi::Value CoreWrap::read_model_sync(const Napi::CallbackInfo& info) {
} else {
OPENVINO_THROW("'readModelSync'", ov::js::get_parameters_error_msg(info, allowed_signatures));
}
+ ov::util::set_tensors_names(ov::AUTO, *model);
return cpp_to_js(info.Env(), model);
} catch (std::runtime_error& err) {
diff --git a/src/bindings/js/node/tests/setup.js b/src/bindings/js/node/tests/setup.js
index b4885d24157..34c110c04e9 100644
--- a/src/bindings/js/node/tests/setup.js
+++ b/src/bindings/js/node/tests/setup.js
@@ -6,4 +6,5 @@ if (require.main === module) {
async function main() {
await downloadTestModel(testModels.testModelFP32);
+ await downloadTestModel(testModels.modelV3Small);
}
diff --git a/src/bindings/js/node/tests/unit/infer_request.test.js b/src/bindings/js/node/tests/unit/infer_request.test.js
index b324630d08d..b0593dba81e 100644
--- a/src/bindings/js/node/tests/unit/infer_request.test.js
+++ b/src/bindings/js/node/tests/unit/infer_request.test.js
@@ -2,6 +2,7 @@
// Copyright (C) 2018-2025 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
+const fs = require('node:fs/promises');
const { addon: ov } = require('../..');
const assert = require('assert');
const { describe, it, before, beforeEach } = require('node:test');
@@ -324,3 +325,45 @@ describe('ov.InferRequest tests', () => {
});
});
});
+
+describe('ov.InferRequest tests with missing outputs names', () => {
+ const { modelV3Small } = testModels;
+ let compiledModel = null;
+ let tensorData = null;
+ let tensor = null;
+ let inferRequest = null;
+
+ before(async () => {
+ await isModelAvailable(modelV3Small);
+
+ const core = new ov.Core();
+
+ let modelData = await fs.readFile(modelV3Small.xml, 'utf8');
+ const weights = await fs.readFile(modelV3Small.bin);
+ modelData = modelData.replace(
+ 'names="MobilenetV3/Predictions/Softmax:0"',
+ ''
+ );
+ const model = await core.readModel(Buffer.from(modelData, 'utf8'), weights);
+
+ compiledModel = await core.compileModel(model, 'CPU');
+ inferRequest = compiledModel.createInferRequest();
+
+ tensorData = Float32Array.from(
+ { length: lengthFromShape(modelV3Small.inputShape) },
+ () => Math.random() + epsilon,
+ );
+ tensor = new ov.Tensor(ov.element.f32, modelV3Small.inputShape, tensorData);
+ });
+
+ it('Test infer(inputData: Tensor[])', () => {
+ const result = inferRequest.infer([tensor]);
+ assert.deepStrictEqual(Object.keys(result).length, 1);
+ });
+
+ it('Test inferAsync(inputData: Tensor[])', () => {
+ inferRequest.inferAsync([tensor]).then((result) => {
+ assert.deepStrictEqual(Object.keys(result).length, 1);
+ });
+ });
+});
diff --git a/src/bindings/js/node/tests/utils.js b/src/bindings/js/node/tests/utils.js
index 88f48066d1a..243a39cef74 100644
--- a/src/bindings/js/node/tests/utils.js
+++ b/src/bindings/js/node/tests/utils.js
@@ -26,6 +26,16 @@ const testModels = {
binURL:
'https://media.githubusercontent.com/media/openvinotoolkit/testdata/master/models/test_model/test_model_fp32.bin',
},
+ modelV3Small: {
+ xml: getModelPath('v3-small_224_1.0_float.xml'),
+ bin: getModelPath('v3-small_224_1.0_float.bin'),
+ inputShape: [1, 224, 224, 3],
+ outputShape: [1, 1001],
+ xmlURL:
+ 'https://storage.openvinotoolkit.org/repositories/openvino_notebooks/models/mobelinet-v3-tf/FP32/v3-small_224_1.0_float.xml',
+ binURL:
+ 'https://storage.openvinotoolkit.org/repositories/openvino_notebooks/models/mobelinet-v3-tf/FP32/v3-small_224_1.0_float.bin',
+ },
};
module.exports = {