質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.32%

Q&A

解決済

2回答

509閲覧

Visual Studio環境でC++にOpenVINOを実装する方法

kkjiji

総合スコア42

0グッド

0クリップ

投稿2024/10/18 14:24

実現したいこと

Visual Studio2017の環境でOpenVINOの開発環境の作り方が分からず困っています。
コマンド実行するのではなく、Visual Studio上で実行できるようにしたいです。

発生している問題・分からないこと

#環境

  • 開発言語:C++
  • 開発環境:Visual Studio2017
  • OpenVINOのバージョン:2024.4.0
  • その他外部ライブラリ:OpenCV

該当のソースコード

C++

1#コード 2コードは実物はお見せできないので、ChatGPTで簡易的に作成したものです。 3```c++ 4#include <openvino/openvino.hpp> 5#include <iostream> 6 7// モデルを読み込む関数 8ov::CompiledModel load_model(const std::string& model_path, ov::Core& core) { 9 try { 10 // .xmlファイルからIRモデルを読み込む 11 ov::Model model = core.read_model(model_path); 12 // コンパイル済みモデルを取得 13 ov::CompiledModel compiled_model = core.compile_model(model, "CPU"); 14 std::cout << "Model loaded successfully." << std::endl; 15 return compiled_model; 16 } 17 catch (const std::exception& e) { 18 std::cerr << "Error loading model: " << e.what() << std::endl; 19 throw; 20 } 21} 22 23// モデルを評価する関数 24void evaluate_model(ov::CompiledModel& compiled_model, const std::vector<float>& input_data) { 25 try { 26 // 入力データの形状を取得 27 ov::InferRequest infer_request = compiled_model.create_infer_request(); 28 auto input_tensor = infer_request.get_input_tensor(); 29 30 // データを入力テンソルにコピー 31 std::memcpy(input_tensor.data<float>(), input_data.data(), input_data.size() * sizeof(float)); 32 33 // 推論を実行 34 infer_request.infer(); 35 36 // 出力テンソルを取得 37 auto output_tensor = infer_request.get_output_tensor(); 38 auto output_data = output_tensor.data<float>(); 39 40 // 結果を表示 41 std::cout << "Model output: " << output_data[0] << std::endl; 42 } 43 catch (const std::exception& e) { 44 std::cerr << "Error during inference: " << e.what() << std::endl; 45 throw; 46 } 47} 48 49// メモリを解放する関数 50void release_resources(ov::Core& core) { 51 // OpenVINOはスマートポインタでメモリを管理しているため、明示的な解放は不要ですが、 52 // 念のために関連リソースを破棄して終了をサポートします。 53 try { 54 core = ov::Core(); // リソース解放を意図して新しいCoreを生成 55 std::cout << "Resources released successfully." << std::endl; 56 } 57 catch (const std::exception& e) { 58 std::cerr << "Error releasing resources: " << e.what() << std::endl; 59 throw; 60 } 61} 62 63int main() { 64 try { 65 // OpenVINO Coreオブジェクトを作成(ここでエラーが出ている) 66 ov::Core core; 67 68 // モデルのパス 69 std::string model_path = "path_to_model.xml"; 70 71 // モデルを読み込む 72 ov::CompiledModel compiled_model = load_model(model_path, core); 73 74 // ダミーの入力データ 75 std::vector<float> input_data(100, 1.0f); // 必要に応じてサイズや値を調整 76 77 // モデルを評価 78 evaluate_model(compiled_model, input_data); 79 80 // リソースを解放 81 release_resources(core); 82 } 83 catch (const std::exception& e) { 84 std::cerr << "An error occurred: " << e.what() << std::endl; 85 return -1; 86 } 87 88 return 0; 89}
### 試したこと・調べたこと - [x] teratailやGoogle等で検索した - [x] ソースコードを自分なりに変更した - [ ] 知人に聞いた - [ ] その他 ##### 上記の詳細・結果 #やったこと - システム環境変数の設定:Path→~\runtime\bin\intel64\Release - Visual Studioの構成プロパティの設定: VC++ ディレクトリ内のインクルードディレクトリ→~\runtime\include VC++ ディレクトリ内のライブラリディレクトリ→~\runtime\lib\intel64\Release リンカー内の入力→openvino.lib, oepnvino_c.lib, openvino_onnx_frontend.lib #実行状態 openvino.hppの読み込みはできています。 問題は実行中のov::Core* core = new ov::Core(); でCoreインスタンスを作ろうとすると「bad allocation」とエラーが出ます。 #考察 Coreインスタンスを作成するときのエラーはメモリの不足ではないと思っています。 #include <openvino/openvino.hpp>でエラーは出ていないですが、正しく読み込めれていないことが原因だと思い、Visual Studioの設定などの問題ではないかと思っています。 また、ハードウェアはOpenVINO2024に該当のものを使用しています。 ### 補足 特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

VIsual Studioの構成プロパティ内のライブラリディレクトリの設定パスを正しく設定する。
システム環境変数の設定ももれなく設定する。

・構成「Debug」
[リンカー]-[全般]-[追加のライブラリディレクトリ]に、C:\Program Files (x86)\Intel\openvino_2024\runtime\lib\intel64\Debug
[リンカー]-[入力]-[追加の依存ファイル]に、openvinod.lib

・構成「Release」
[リンカー]-[全般]-[追加のライブラリディレクトリ]に、C:\Program Files (x86)\Intel\openvino_2024\runtime\lib\intel64\Release
[リンカー]-[入力]-[追加の依存ファイル]に、openvino.lib

投稿2024/10/20 06:35

kkjiji

総合スコア42

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

Windows 11、Visual Studio 2022で確認しました。

以下の2点を前提とします。

#include <openvino/openvino.hpp> #include <iostream> int main() { try { // OpenVINO Coreオブジェクトを作成(ここでエラーが出ている) ov::Core core; } catch (const std::exception& e) { std::cerr << "An error occurred: " << e.what() << std::endl; return -1; } return 0; }

プロジェクトのプロパティに以下を設定してください。既存の設定値がある場合は、セミコロンを付けて追加します。

  • 構成「すべての構成」
    • [C/C++]-[全般]-[追加のインクルードディレクトリ]に、C:\Program Files (x86)\Intel\openvino_2024\runtime\include
  • 構成「Debug」
    • [リンカー]-[全般]-[追加のライブラリディレクトリ]に、C:\Program Files (x86)\Intel\openvino_2024\runtime\lib\intel64\Debug
    • [リンカー]-[入力]-[追加の依存ファイル]に、openvinod.lib
  • 構成「Release」
    • [リンカー]-[全般]-[追加のライブラリディレクトリ]に、C:\Program Files (x86)\Intel\openvino_2024\runtime\lib\intel64\Release
    • [リンカー]-[入力]-[追加の依存ファイル]に、openvino.lib

ソースを書いていくうちにライブラリが足りなくなってきたら、同じフォルダーにあるlibファイルを追記していってください。

不明な点がありましたら、コメントしてください。

投稿2024/10/18 23:11

編集2024/10/18 23:26
hiroki-o

総合スコア1332

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kkjiji

2024/10/18 23:49

ありがとうございます。 私の設定が間違えており、ov::Core core;のところは実行できました。 ただ、別のエラーが出ており ---------------------------------- ・説明 メンバー "ov::AttributeVisitore::invalid_node_id"は初期化できません ・ファイル attribute_visitor.hpp ・説明 class "ov::Output<const ov::Node>"の明示的特殊化はそれを初めて使用する前に指定する必要があります(行108、ファイル名"~/runtime/include/openvino/core/node_output.hpp") ・ファイル node_output.hpp ・説明 コンストラクター名として使用されている型が型 "ov::Output<const ov::Node>"と一致しません ・ファイル node_output.hpp ---------------------------------- 実行は完了するのですが、上記のようなエラーが出ています。 他にも同じようなものが出ており、合計12エラーがあります。 これも設定の問題なのでしょうか? 関連する質問かわからないのですが、続けての質問ですみません。
hiroki-o

2024/10/19 00:26

そのエラーは提示しているソースのものですか? 実物のものですか? 私はChatGPTが出したソースを、他人のためにデバッグする時間は無いです。 実物を見せられないなら、そのエラーを再現するサンプルを出してください。
kkjiji

2024/10/19 01:26

記載に不備があり、すみません。 エラーが出ているのは、実物のものでインストールしたOpenVINO内の~/runtime/include/openvino/core/node_output.hppのファイルなどです。 エラーが出ているのは全て、OpneVINO内のファイルの話になります。 以下に、詳細記載しますがおっしゃるとおりお忙しいと思いますので、面倒であればおっしゃってください。 一旦本来の問題は解決したので、この質問は閉じようと思います。 ご迷惑をおかけしました。 ↓↓詳細(冗長ですみません) ファイルパス:~/runtime/include/openvino/core/node_output.hpp エラー内容: 1. class "ov::Output<const ov::Node>"の明示的特殊化はそれを初めて使用する前に指定する必要があります(行108、ファイル名"~/runtime/include/openvino/core/node_output.hpp") 2. コンストラクター名として使用されている型が型 "ov::Output<const ov::Node>"と一致しません 以下、ソースコード(中身はnode_output.hpp) // Copyright (C) 2018-2024 Intel Corporation // SPDX-License-Identifier: Apache-2.0 // #pragma once #include <cstring> #include <map> #include <unordered_set> #include "openvino/core/core_visibility.hpp" #include "openvino/core/descriptor/tensor.hpp" #include "openvino/core/partial_shape.hpp" #include "openvino/core/runtime_attribute.hpp" #include "openvino/core/shape.hpp" #include "openvino/core/type/element_type.hpp" namespace ov { class Node; template <typename NodeType> class Input; template <typename NodeType> class Output {}; /// \brief A handle for one of a node's outputs. /// \ingroup ov_model_cpp_api template <> class OPENVINO_API Output<Node> { public: /// \brief Constructs a Output. /// \param node A pointer to the node for the output handle. /// \param index The index of the output. Output(Node* node, size_t index); /// \brief Constructs a Output. /// \param node A `shared_ptr` to the node for the output handle. /// \param index The index of the output. /// Output(const std::shared_ptr<Node>& node, size_t index); /// \brief Constructs a Output, referencing the zeroth output of the node. /// \param node A `shared_ptr` to the node for the output handle. template <typename T> Output(const std::shared_ptr<T>& node) : Output(node ? node->get_default_output() : Output<Node>()) {} /// A null output Output() = default; void reset(); /// This output position for a different node Output<Node> for_node(const std::shared_ptr<Node>& node); /// \return A pointer to the node referred to by this output handle. Node* get_node() const; /// \return A `shared_ptr` to the node referred to by this output handle. /// std::shared_ptr<Node> get_node_shared_ptr() const; /// \return The index of the output referred to by this output handle. size_t get_index() const; /// \return A reference to the tensor descriptor for this output. OV_NO_DANGLING descriptor::Tensor& get_tensor() const; /// \return A shared point to the tensor ptr for this output. std::shared_ptr<descriptor::Tensor> get_tensor_ptr() const; /// \return Set new tensor desc shared pointer to this output void set_tensor_ptr(std::shared_ptr<descriptor::Tensor> tensor_ptr); /// \return The element type of the output referred to by this output handle. OV_NO_DANGLING const element::Type& get_element_type() const; /// \return The shape of the output referred to by this output handle. OV_NO_DANGLING const Shape& get_shape() const; /// \return The partial shape of the output referred to by this output handle. OV_NO_DANGLING const PartialShape& get_partial_shape() const; /// \return The reference to runtime info map RTMap& get_rt_info(); /// \return The constant reference to runtime info map OV_NO_DANGLING const RTMap& get_rt_info() const; /// \return The tensor names associated with this output OV_NO_DANGLING const std::unordered_set<std::string>& get_names() const; /// \return Any tensor names associated with this output std::string get_any_name() const; /// \return Set tensor names associated with this output void set_names(const std::unordered_set<std::string>& names); /// \return Add tensor names associated with this output void add_names(const std::unordered_set<std::string>& names); /// \return A set containing handles for all inputs targeted by the output referenced by /// this output handle. std::set<Input<Node>> get_target_inputs() const; /// \brief Removes a target input from the output referenced by this output handle. /// \param target_input The target input to remove. /// void remove_target_input(const Input<Node>& target_input) const; /// \brief Replace all users of this value with replacement void replace(const Output<Node>& replacement); bool operator==(const Output& other) const; bool operator!=(const Output& other) const; bool operator<(const Output& other) const; bool operator>(const Output& other) const; bool operator<=(const Output& other) const; bool operator>=(const Output& other) const; operator Output<const Node>() const; private: std::shared_ptr<Node> m_node; size_t m_index{0}; }; /// \brief A handle for one of a node's outputs. /// \ingroup ov_model_cpp_api template <> class OPENVINO_API Output<const Node> { public: /// \brief Constructs a Output. /// \param node A pointer to the node for the output handle. /// \param index The index of the output. Output(const Node* node, size_t index); /// \brief Constructs a Output. /// \param node A `shared_ptr` to the node for the output handle. /// \param index The index of the output. /// Output(const std::shared_ptr<const Node>& node, size_t index); /// \brief Constructs a Output, referencing the zeroth output of the node. /// \param node A `shared_ptr` to the node for the output handle. template <typename T> Output(const std::shared_ptr<const T>& node) : Output(node ? node->get_default_output() : Output<const Node>()) {} /// A null output Output() = default; void reset(); /// This output position for a different node Output<const Node> for_node(const std::shared_ptr<const Node>& node); /// \return A pointer to the node referred to by this output handle. const Node* get_node() const; /// \return A `shared_ptr` to the node referred to by this output handle. /// std::shared_ptr<const Node> get_node_shared_ptr() const; /// \return The index of the output referred to by this output handle. size_t get_index() const; /// \return A reference to the tensor descriptor for this output. OV_NO_DANGLING descriptor::Tensor& get_tensor() const; /// \return A shared point to the tensor ptr for this output. std::shared_ptr<descriptor::Tensor> get_tensor_ptr() const; /// \return The element type of the output referred to by this output handle. OV_NO_DANGLING const element::Type& get_element_type() const; /// \return The shape of the output referred to by this output handle. OV_NO_DANGLING const Shape& get_shape() const; /// \return The partial shape of the output referred to by this output handle. OV_NO_DANGLING const PartialShape& get_partial_shape() const; /// \return The constant reference to runtime info map OV_NO_DANGLING const RTMap& get_rt_info() const; /// \return The tensor names associated with this output OV_NO_DANGLING const std::unordered_set<std::string>& get_names() const; /// \return Any tensor name associated with this output std::string get_any_name() const; /// \return A set containing handles for all inputs targeted by the output referenced by /// this output handle. std::set<Input<Node>> get_target_inputs() const; bool operator==(const Output& other) const; bool operator!=(const Output& other) const; bool operator<(const Output& other) const; bool operator>(const Output& other) const; bool operator<=(const Output& other) const; bool operator>=(const Output& other) const; private: std::shared_ptr<const Node> m_node; size_t m_index{0}; }; OPENVINO_API std::ostream& operator<<(std::ostream& out, const Output<Node>& output); OPENVINO_API std::ostream& operator<<(std::ostream& out, const Output<const Node>& output); } // namespace ov
hiroki-o

2024/10/19 04:30

ライブラリ内でのエラーなら、2022では再現しないので、私の考えは以下の通り。 OpenVINOは、Visual Studio 2019以降対応です。 https://docs.openvino.ai/2024/about-openvino/release-notes-openvino/system-requirements.html その理由は標準C++のサポートによるものと推測しましたが、明確な証拠を見つけられませんでした。 https://learn.microsoft.com/ja-jp/cpp/overview/visual-cpp-language-conformance?view=msvc-170 Visual Studio 2017でも、細かいバージョンによって標準C++14の対応が異なるようですので、その辺を確認してください。 (こちらに2017/2019の環境が無いため再現は無理)
kkjiji

2024/10/20 06:34

ご回答ありがとうございます。 参考にさせていただきます。 本来の質問解決しましたので、この質問閉じます。 色々とありがとうございました。
hiroki-o

2024/10/20 06:47

タグ別のスコアが正しく付かないので、タグを元に戻してください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.32%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問