Giter Club home page Giter Club logo

Comments (5)

brmarkus avatar brmarkus commented on June 26, 2024 1

Are you sure you processed all previous cells and they completed successfully?
Like this, cell 6:

from PIL import Image
from ultralytics import YOLO

DET_MODEL_NAME = "yolov8n"

det_model = YOLO(models_dir / f"{DET_MODEL_NAME}.pt")
label_map = det_model.model.names

res = det_model(IMAGE_PATH)
Image.fromarray(res[0].plot()[:, :, ::-1])

With

det_model.predictor.inference = infer
AttributeError: 'NoneType' object has no attribute 'inference'

It sounds like det_model.predictor is not yet/not anymore initialized because of NoneType.

from openvino_notebooks.

yangtianyu92 avatar yangtianyu92 commented on June 26, 2024 1
import cv2
import torch
import numpy as np
import openvino as ov
from PIL import Image
from ultralytics.models.yolo.detect import DetectionPredictor
from ultralytics.utils import ASSETS


class RunIntelModel:
    def __init__(self) -> None:
        # openvino official tutorial and the official issue of yolov8 provided guidelines and the original code base can not correspond to, here according to the library source code logic to make changes
        # Set up the cfg file first (call it according to the comment part of /ultralytics/models/yolo/detect/predict.py).
        # Use the setup_model method in ultralytics/engine/predictor.py to initialise the model, otherwise the model property in __init__ is None
        # None value can not change model.pt property, not change the property will lead to letterbox output and openvino fixed input size discrepancy, resulting in openvino can not reason about the issue

        self.args = dict(model='hand.pt', source=ASSETS, imgsz=(640,640), save=True)
        self.predictor = DetectionPredictor(overrides=self.args)
        self.predictor.setup_model("hand.pt")
        self.predictor.model.pt = False
        self.det_model_path = "hand_openvino_model/hand.xml"
        self.openvino_model = self.setup_model()
        self.predictor.inference = self.infer

    def setup_model(self):
        core = ov.Core() 
        det_ov_model = core.read_model(self.det_model_path)
        ov_config = {}
        det_ov_model.reshape({0: [1, 3, 640, 640]})
        ov_config = {"GPU_DISABLE_WINOGRAD_CONVOLUTION": "YES"}
        det_compiled_model = core.compile_model(det_ov_model, "GPU.1", ov_config) 
        return det_compiled_model

    def infer(self, *args):
        print(args[0].shape)
        result = self.openvino_model(args)

        return torch.from_numpy(result[0])

    def predict(self, raw_img):
        res = self.predictor(raw_img)
        if res[0].boxes.shape[0] > 0:
            return res[0].boxes.conf[0], res[0].boxes.xywh
        else:
            return 0, [0, 0, 0, 0]

    def __call__(self, img):
        return self.predict(raw_img=img)

from openvino_notebooks.

yangtianyu92 avatar yangtianyu92 commented on June 26, 2024 1

Above is my solution

from openvino_notebooks.

yangtianyu92 avatar yangtianyu92 commented on June 26, 2024

Are you sure you processed all previous cells and they completed successfully? Like this, cell 6:

from PIL import Image
from ultralytics import YOLO

DET_MODEL_NAME = "yolov8n"

det_model = YOLO(models_dir / f"{DET_MODEL_NAME}.pt")
label_map = det_model.model.names

res = det_model(IMAGE_PATH)
Image.fromarray(res[0].plot()[:, :, ::-1])

With

det_model.predictor.inference = infer
AttributeError: 'NoneType' object has no attribute 'inference'

It sounds like det_model.predictor is not yet/not anymore initialized because of NoneType.
you are right

from openvino_notebooks.

zoldaten avatar zoldaten commented on June 26, 2024

@yangtianyu92 hi
trying to use your example:

class RunIntelModel:
    def __init__(self) -> None:
        # openvino official tutorial and the official issue of yolov8 provided guidelines and the original code base can not correspond to, here according to the library source code logic to make changes
        # Set up the cfg file first (call it according to the comment part of /ultralytics/models/yolo/detect/predict.py).
        # Use the setup_model method in ultralytics/engine/predictor.py to initialise the model, otherwise the model property in __init__ is None
        # None value can not change model.pt property, not change the property will lead to letterbox output and openvino fixed input size discrepancy, resulting in openvino can not reason about the issue

        self.args = dict(model="models/yolov8n_openvino_model/yolov8n.bin", source=ASSETS, imgsz=(640,640), save=True)
        self.predictor = DetectionPredictor(overrides=self.args)
        self.predictor.setup_model("models/yolov8n_openvino_model/yolov8n.bin")
        self.predictor.model.pt = False
        self.det_model_path = "models/yolov8n_openvino_model/yolov8n.xml"
        self.openvino_model = self.setup_model()
        self.predictor.inference = self.infer

    def setup_model(self):
        core = ov.Core() 
        det_ov_model = core.read_model(self.det_model_path)
        ov_config = {}
        det_ov_model.reshape({0: [1, 3, 640, 640]})
        ov_config = {"GPU_DISABLE_WINOGRAD_CONVOLUTION": "YES"}
        det_compiled_model = core.compile_model(det_ov_model, "GPU", ov_config) 
        return det_compiled_model

    def infer(self, *args):
        print(args[0].shape)
        result = self.openvino_model(args)

        return torch.from_numpy(result[0])

    def predict(self, raw_img):
        res = self.predictor(raw_img)
        if res[0].boxes.shape[0] > 0:
            return res[0].boxes.conf[0], res[0].boxes.xywh
        else:
            return 0, [0, 0, 0, 0]

    def __call__(self, img):
        return self.predict(raw_img=img)

r=RunIntelModel()
r.setup_model()
IMAGE_PATH=cv2.imread('coco_bike.jpg')

r.predict(IMAGE_PATH)

but got this:

fp16 &= pt or jit or onnx or xml or engine or nn_module or triton  # FP16
TypeError: unsupported operand type(s) for &=: 'bool' and 'str'

may be .bin type is not accepted ?

from openvino_notebooks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.