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

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

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

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

91閲覧

C++でOpenVINOを実装する時にライブラリのエラーが解決できない

kkjiji

総合スコア40

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2024/10/23 12:17

編集2024/10/23 12:22

実現したいこと

C++でOpenVINOをincludeで使っているのですが、2024.4.0のバージョンの仕様変更でエラーが発生してしまい解決できず困っています。
structの宣言の仕方が間違っていると思うのですが、見当違いの可能性もあるためまずは実行できるところまで修正したいです。
ソースコードはエラーが発生する箇所だけ抽出して、同様のエラーが発生するようにした簡易のソースコードです。

実行環境

言語:C++
開発環境:Visual Studio2017
外部ライブラリ:OpenCV、OpenVINO2024.4.0

#ダウンロード先
https://www.intel.com/content/www/us/en/developer/tools/openvino-toolkit/download.html?PACKAGE=OPENVINO_BASE&VERSION=v_2024_4_0&OP_SYSTEM=WINDOWS&DISTRIBUTION=ARCHIVE

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

今までOpenVINO2023を使用していたのですが、OpenVINO2024のアップデートでOepnVINOのライブラリ内にあるallocator.hppの仕様が変更になったようです。
そのため、ビルドできず解決策も分からず困っているためご教授頂きたいです。

エラーメッセージ

error

1'is_equal':'std::shared_ptr<main::SharedMatAllocator>'のメンバーではありません。 2 3上記のようなエラーが出ています。 4

該当のソースコード

C++

1//===簡易ソースコードのmain.cpp============== 2#include <string> 3#include <opencv2/opencv.hpp> 4#include <openvino/openvino.hpp> 5 6int main() { 7 8 9struct SharedMatAllocator { 10 const cv::Mat mat; 11 void* allocate(size_t bytes, size_t) {return bytes <= mat.rows * mat.step[0] ? mat.data : nullptr;} 12 void deallocate(void*, size_t, size_t) {} 13 bool is_equal(const SharedMatAllocator& other) const noexcept {return this == &other;} 14}; 15 16cv::Mat img = cv::imread("画像のパス"); 17cv::Mat& mat = img; 18 19bool isMatFloat = mat.type() == CV_32F; 20auto precision = isMatFloat ? ov::element::f32 : ov::element::u8; 21auto allocator = std::make_shared<SharedMatAllocator>(mat); 22 23return ov::Tensor(precision, ov::Shape{1,226,356,3}, ov::Allocator(allocator)); 24 25} 26 27 28 29//===参考・OpenVINOのエラーが発生しているallocator.hppファイル==== 30//==文字数の関係でコメントは削除しております。======= 31#pragma once 32 33#include <cstddef> 34#include <memory> 35 36#include "openvino/core/any.hpp" 37#include "openvino/core/core_visibility.hpp" 38 39namespace ov { 40 41class Tensor; 42 43class OPENVINO_API Allocator { 44 Allocator(const Allocator& other, const std::shared_ptr<void>& so); 45 46 friend class ov::Tensor; 47 48 struct Base : public std::enable_shared_from_this<Base> { 49 virtual void* addressof() = 0; 50 const void* addressof() const { 51 return const_cast<Base*>(this)->addressof(); 52 } 53 virtual const std::type_info& type_info() const = 0; 54 virtual void* allocate(const size_t bytes, const size_t alignment = alignof(max_align_t)) = 0; 55 virtual void deallocate(void* handle, const size_t bytes, size_t alignment = alignof(max_align_t)) = 0; 56 virtual bool is_equal(const Base& other) const = 0; 57 58 protected: 59 virtual ~Base() = default; 60 }; 61 62 template <typename A> 63 struct Impl : public Base { 64 template <typename... Args> 65 explicit Impl(Args&&... args) : a(std::forward<Args>(args)...) {} 66 void* addressof() override { 67 return &a; 68 } 69 const std::type_info& type_info() const override { 70 return typeid(a); 71 } 72 void* allocate(const size_t bytes, const size_t alignment = alignof(max_align_t)) override { 73 return a.allocate(bytes, alignment); 74 } 75 void deallocate(void* handle, const size_t bytes, size_t alignment = alignof(max_align_t)) override { 76 a.deallocate(handle, bytes, alignment); 77 } 78 bool is_equal(const Base& other) const override { 79 if (util::equal(type_info(), other.type_info())) { 80 return a.is_equal(*static_cast<const A*>(other.addressof())); 81 } 82 return false; 83 } 84 A a; 85 }; 86 87 std::shared_ptr<Base> _impl; 88 std::shared_ptr<void> _so; 89 90public: 91 ~Allocator(); 92 93 Allocator(); 94 95 Allocator(const Allocator& other) = default; 96 97 Allocator& operator=(const Allocator& other) = default; 98 99 Allocator(Allocator&& other) = default; 100 101 Allocator& operator=(Allocator&& other) = default; 102 103 template < 104 typename A, 105 typename std::enable_if<!std::is_same<typename std::decay<A>::type, Allocator>::value && 106 !std::is_abstract<typename std::decay<A>::type>::value && 107 !std::is_convertible<typename std::decay<A>::type, std::shared_ptr<Base>>::value, 108 bool>::type = true> 109 Allocator(A&& a) : _impl{std::make_shared<Impl<typename std::decay<A>::type>>(std::forward<A>(a))} {} 110 111 void* allocate(const size_t bytes, const size_t alignment = alignof(max_align_t)); 112 113 void deallocate(void* ptr, const size_t bytes = 0, const size_t alignment = alignof(max_align_t)); 114 115 bool operator==(const Allocator& other) const; 116 117 bool operator!() const noexcept; 118 119 explicit operator bool() const noexcept; 120}; 121 122} // namespace ov 123

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

元々実行できていたときは、main文の中になる

C++

1struct SharedMatAllocator { 2 const cv::Mat mat; 3 void* allocate(size_t bytes, size_t) {return bytes <= mat.rows * mat.step[0] ? mat.data : nullptr;} 4 void deallocate(void*, size_t, size_t) {} 5 bool is_equal(const SharedMatAllocator& other) const noexcept {return this == &other;} 6}; 7 8上記のSharedMatAllocator が以下のようになっていました。 9 10class SharedTensorAllocator final : public ov::AllocatorImpl { 11public: 12 SharedTensorAllocator(const cv::Mat& img) : img(img) {} 13 14 ~SharedTensorAllocator() = default; 15 16 void* allocate(const size_t bytes, const size_t) override { 17 return bytes <= img.rows * img.step[0] ? img.data : nullptr; 18 } 19 20 void deallocate(void* handle, const size_t bytes, const size_t) override {} 21 22 bool is_equal(const AllocatorImpl& other) const override { 23 auto other_tensor_allocator = dynamic_cast<const SharedTensorAllocator*>(&other); 24 return other_tensor_allocator != nullptr && other_tensor_allocator == this; 25 } 26 27private: 28 const cv::Mat img; 29};

しかし、OpenVINO2024のアップデートでAllocatorImplが削除されたことで使うことができなくなりました。
OpenVINO2023から削除された部分を追記してみたのですが、
エラーコード:C4996→要はAllocatorImplは2024で使えないからダメだよ的なエラーが多数発生して実行することができませんでした。

参考までに追記した内容です。
下記の内容をallocator.hppの

namespace ov {
→ここに追記
class Tensor;

#======下記追記したコード=========

C++

1/** 2 * @interface AllocatorImpl 3 * @deprecated This class will be removed in 2024.0 release 4 * @brief Tries to act like [std::pmr::memory_resource](https://en.cppreference.com/w/cpp/memory/memory_resource) 5 */ 6OPENVINO_SUPPRESS_DEPRECATED_START 7struct OPENVINO_DEPRECATED("Do not inherit from AllocatorImpl. This class will be removed in 2024.0 release. Pass " 8 "std::pmr::memory_resource like object directly to ov::Allocator") AllocatorImpl 9 : public std::enable_shared_from_this<AllocatorImpl> { 10 /** 11 * @brief A smart pointer containing AllocatorImpl object 12 */ 13 using Ptr = std::shared_ptr<AllocatorImpl>; 14 15 /** 16 * @brief Allocates memory 17 * 18 * @param bytes The size in bytes at least to allocate 19 * @param alignment The alignment of storage 20 * @return Handle to the allocated resource 21 * @throw Exception if specified size and alignment is not supported 22 */ 23 virtual void* allocate(const size_t bytes, const size_t alignment = alignof(max_align_t)) = 0; 24 25 /** 26 * @brief Releases the handle and all associated memory resources which invalidates the handle. 27 * @param handle The handle to free 28 * @param bytes The size in bytes that was passed into allocate() method 29 * @param alignment The alignment of storage that was passed into allocate() method 30 */ 31 virtual void deallocate(void* handle, const size_t bytes, size_t alignment = alignof(max_align_t)) = 0; 32 33 /** 34 * @brief Compares with other AllocatorImpl 35 * @param other Other instance of allocator 36 * @return `true` if and only if memory allocated from one AllocatorImpl can be deallocated from the other and vice 37 * versa 38 */ 39 40 virtual bool is_equal(const AllocatorImpl& other) const = 0; 41 42protected: 43 virtual ~AllocatorImpl() = default; 44}; 45OPENVINO_SUPPRESS_DEPRECATED_END

補足

特になし

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

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

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

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

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

guest

回答1

0

ベストアンサー

質問文にある情報からの雑な推測ですが、ov::Allocator() に渡すのは std::shared_ptr ではなく、ov::Allocator(SharedMatAllocator{mat}) のように SharedMatAllocator のインスタンスを渡してムーブさせる仕様みたいに見えます。

投稿2024/10/23 14:16

int32_t

総合スコア21568

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.38%

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

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

質問する

関連した質問