无法在 celery Worker 中使用 YOLO 加载和预测图像

问题描述 投票:0回答:1

这是我的代码

import sys
from question_detection.inference_object_detection.check_model import verify_model
from logger import logger
from ultralytics import YOLO


class ModelNotFoundError(Exception):
    pass


def load_model():
    try:
        success, model_path = verify_model()
        logger.info("model loading started")
        if not success:
            raise ModelNotFoundError("Model not found!")
        logger.info("123")
        logger.info(model_path)
        _model = YOLO(model_path)
        logger.info("456")
        if _model is None:
            raise ModelNotFoundError("Model not loaded!")
        logger.info("789")
        return _model, _model.names
    except ModelNotFoundError as e:
        logger.error(e)
        sys.exit(1)  # Exit the system with a non-zero exit code to indicate an error
    except Exception as e:
        logger.error(e)
        return None, None



def inference(images):
    """
    Inference on image
    :param images: It can be a single image, list of images or a directory
    :return: List of results
    """
    logger.info("started inference of images!")
    model, categories = load_model()
    if model is None:
        raise ModelNotFoundError("Model is not loaded!")

    res = model(images), categories
    logger.info("Completed inference of images!")
    return res

如果我正常调用推理,它就像黄油一样工作 但是,当同一个函数被另一个 celery 工作函数调用时,这不起作用。 它甚至不会抛出任何错误。

调试时,在

model_path
之后没有记录任何内容,基本上没有到达
456
线。

任何人都可以帮助调试为什么我无法加载模型。 它使用 YOLO,正如您在代码中看到的那样

python machine-learning celery yolov8
1个回答
0
投票

在处理 Celery Worker 的困难时,特别是在加载 YOLO 等模型进行图像处理时,有几个典型的位置需要研究。您的代码在 Celery 工作线程外部工作,但在内部工作却不起作用,这一事实表明问题可能出在 Celery 工作线程的环境或其处理模型加载的方式上。以下是调试问题时的一些步骤和注意事项:

确保 Celery Worker 与主应用程序在相同的环境中运行。这包括相同的 Python 版本、包和依赖项。环境的差异可能会导致程序表现不同。

改进日志记录以获得更精确的数据。目前尚不清楚代码是否在 YOLO 模型初始化之前或期间失败。考虑在重要代码行之前和之后添加更多日志语句,以查看问题出在哪里。

Celery 工作线程可能具有与主应用程序不同的资源限制。检查是否存在任何可能阻止 YOLO 模型加载到 Celery Worker 中的内存或 CPU 限制。

确保 model_path 可访问并针对 Celery 工作环境进行正确配置。在许多环境中运行程序时,路径困难是很常见的。

如果您的 Celery Worker 并发操作(例如,通过 Gevent 或 Eventlet),YOLO 模型可能难以执行并发操作。尝试在单独池(非并发模式)中运行 Celery 工作线程,看看是否可以解决问题。

特定设置有时可能会导致任务的正常操作出现问题。请特别注意作业序列化、时间限制和工作线程并发性的设置。

在 Celery Worker 设置中调试可能会更困难。如果可能,请使用远程调试工具或提供广泛的日志记录,以更好地了解工作人员内部发生的情况。

确定YOLO模型加载方法是否有任何可能与Celery工作环境不兼容的独特之处。这可能包括文件 I/O 活动、全局变量的使用或对其他系统资源的依赖。

确保任务的结构和名称合适。有时重命名或重新组织任务可以解决意想不到的问题。

作为故障排除步骤,尝试将更简单或不同的模型加载到 Celery Worker 中,以检查问题是否与 YOLO 有关。

© www.soinside.com 2019 - 2024. All rights reserved.