Search code examples
c#machine-learningautoencoderml.net

Keras.Net - Model predict doesn't return when model is global variable


I am trying to run a model on a camera feed. Therefore I want to load in the model and store it as a global variable and use it while the frames come in. It works when I load in the model for each frame (in the process function) but when I store the model as a global variable, the predict line hangs indefinitely so it feels like resources are being released when they shouldn't be.

How I can get this to work?

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);
}

Solution

  • The issue for us had to do with using the PythonEngine in mutliple threads. Now we initialise using

    if (!PythonEngine.IsInitialized) { PythonEngine.Initialize(); }
    model = BaseModel.LoadModel(networkPath);
    PythonEngine.BeginAllowThreads();
    

    In our worker thread we have

    using (Py.GIL()) {}
    

    wrapped around our code.