为什么yolov8在C++中开始训练、验证和测试时自动启动并行进程

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

我已经为我的应用程序实现了 YOLOv8,它在 python 中运行得非常好。这个Python文件包含一个名为“ProductDetection”的类,因此使用这个调用我可以对自定义数据集进行训练和测试。每当在 python 文件中调用此类时,它都会正常工作。

现在我想通过在 C++ 代码中调用此类“ProductDetection”来执行相同的操作。为此,我制作了wrapper.pyd并将其加载到c++代码中。我已经完成了c++实现的所有python设置。在 C++ 代码中,它一直工作到训练步骤。在训练开始时,它会持续进行多个并行进程统计,直到使用完整的 CPU 内存。

这是我的Python训练函数:


def Train(self, freeze=0, recipe_name="dummy"):
    with self.lock:
        recipe_set_path = os.path.join(self.recipe_path, str(recipe_name))
        self.model.train(data=self.data_path,
                         epochs=self.epochs,
                         time=0.5,
                         patience=10,
                         batch=self.batch_size,
                         imgsz=self.image_size,
                         cache=False,
                         save=True,  
                         resume=False,
                         name=recipe_set_path,
                         exist_ok=True,
                         device=self.device,
                         workers=0,
                         seed=42,
                         single_cls=True,
                         close_mosaic=0,
                         profile=True,
                         freeze=0,
                         plots=False)
        return

c++ 中的训练函数:



bool TrainDET(std::string recipe_name, float freez_layer, bool cache, bool profile) {
#ifndef _DEBUG
    PyGILState_STATE gstate;
    gstate = PyGILState_Ensure();
    PyObject* pcache = cache ? Py_True : Py_False;
    PyObject* pprofile = profile ? Py_True : Py_False;

    PyObject* result = PyObject_CallMethod((PyObject*)pInstance, "Train", "(sfOO)", recipe_name.c_str(), freez_layer, pcache, pprofile);
    if (!result) {
        PyErr_Print();
        Py_DECREF(result);
        PyGILState_Release(gstate);
        return false;
    }
    Py_DECREF(result);
    PyGILState_Release(gstate);
#endif
    return true;
}

c++主要函数:

int main() {

    Py_Initialize();

    PyEval_InitThreads(); // Initialize Python's Global Interpreter Lock (GIL) for single-threaded execution

    PyInitialize(true);

    GetPyInstance(4, 2, 640, 640, "mywrapper");

    std::string recipe_model_path = "C:\\Users\\Karan\\Documents\\Visual Studio 2015\\Projects\\testwrapper\\testwrapper\\DATA\\Models\\Blisbeat_BlisterTopA\\";

    SetDETPath(recipe_model_path, "C:\\Users\\Karan\\Documents\\Visual Studio 2015\\Projects\\testwrapper\\testwrapper\\DATA\\BASE_DATASET\\data.yaml", "DATA\\DL\\det_model.pt");

    LoadDLModel("dummy");

    TrainDET("Unknown", 0, false, true);
return 0;

首先,我在 cpp 中初始化 python,即 python 类的 get 实例。然后使用不同的函数在给定路径的数据集中进行训练和测试。 问题从“TrainDET("Unknown", 0, false, true);”开始 在此步骤中,任务管理器中启动多个并行进程,如上图所示。

我尝试使用

 putenv("OMP_NUM_THREADS=1");     putenv("OPENBLAS_NUM_THREADS=1");     putenv("MKL_NUM_THREADS=1");
在单线程中运行 C++ 代码,尝试使用
gstate = PyGILState_Ensure();
锁定线程,并通过
import threading
在 python 代码中尝试相同的操作。这样多进程就可以停止。仍然没有任何改善。

multithreading multiprocessing yolov8 ultralytics
1个回答
0
投票

我也面临着类似的问题。但就我而言,我只使用 Python 进行推理(没有使用 C++)。具体来说,是 detector() 函数。

根据我的研究,如果我们尝试在 CPU 上进行推理,Pytorch:YOLO 的底层框架,使用多线程并行(并发)执行所有计算以获得更好的性能。为了验证我的研究是否正确,我只使用了 CPU 版本的 Pytorch 并运行了 YOLO 相同的 detector() 函数。我的所有 12 个核心都在一秒钟内飙升至 100%,即使对于单个图像也是如此。但是当我使用 GPU 版本的 Pytorch 时,它改为使用 GPU 的 RAM,并且 CPU 运行正常。

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