【问题标题】:Keras.Net - Model predict doesn't return when model is global variableKeras.Net - 当模型是全局变量时,模型预测不会返回
【发布时间】:2021-10-19 08:42:12
【问题描述】:

我正在尝试在相机源上运行模型。 因此,我想加载模型并将其存储为全局变量,并在框架进入时使用它。 当我为每个帧加载模型时(在过程函数中)它可以工作,但是当我将模型存储为全局变量时,预测行无限期地挂起,所以感觉资源在不应该被释放时被释放。

我怎样才能让它工作?

private BaseModel model;

public void Initialize()
{
    model = BaseModel.LoadModel(networkPath);
}

public dynamic Process(Mat frame)
{
    var mat = new Mat();
    Cv2.Resize(frame, mat, inputSize);
    var image_in = mat.Convert(np.uint8);

    image_in = np.asfarray(image_in, np.float32);
    image_in /= 256;

    image_in = np.expand_dims(image_in, 0);

    var output = model.Predict(image_in);

    output = output[0];

    return (Input: image_in, Output: output);
}

【问题讨论】:

    标签: c# machine-learning autoencoder ml.net


    【解决方案1】:

    我们的问题与在多线程中使用 PythonEngine 有关。 现在我们使用

    进行初始化
    if (!PythonEngine.IsInitialized) { PythonEngine.Initialize(); }
    model = BaseModel.LoadModel(networkPath);
    PythonEngine.BeginAllowThreads();
    

    在我们的工作线程中

    using (Py.GIL()) {}
    

    围绕我们的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 2021-03-03
      • 2021-12-08
      • 2020-09-12
      • 2019-05-28
      • 2018-10-08
      • 2018-09-06
      相关资源
      最近更新 更多