public async Task <CustomNetworkOutput> EvaluateAsync(CustomNetworkInput input) { // Ensure the input and output fields are bound to the correct // layer names in the onnx model binding.Bind("input_2", input.features); var result = await session.EvaluateAsync(binding, "0"); var output = new CustomNetworkOutput(); output.prediction = result.Outputs["Identity"] as TensorFloat; return(output); }
public async Task <NetworkResult> EvaluateVideoFrameAsync( VideoFrame inputFrame) { // Sometimes on HL RS4 the D3D surface returned is null, so simply skip those frames if (_customNetworkModel == null || inputFrame == null || (inputFrame.Direct3DSurface == null && inputFrame.SoftwareBitmap == null)) { UnityEngine.Debug.Log("EvaluateVideoFrameAsync: No detection, null frame or model not initialized."); return(new NetworkResult("None", 0f, 0));; } // Cache the input video frame to network input _customNetworkInput.features = ImageFeatureValue.CreateFromVideoFrame(inputFrame); // Perform network model inference using the input data tensor, cache output and time operation var stopwatch = Stopwatch.StartNew(); _customNetworkOutput = await _customNetworkModel.EvaluateAsync(_customNetworkInput); stopwatch.Stop(); // Convert prediction to datatype var outVec = _customNetworkOutput.prediction.GetAsVectorView().ToList(); // LINQ query to check for highest probability digit if (outVec.Max() > DetectionThreshold) { // Get the index of max probability value var maxProb = outVec.Max(); var maxIndex = outVec.IndexOf(maxProb); UnityEngine.Debug.Log($"EvaluateVideoFrameAsync: Prediction [{_labels[maxIndex]}] time: [{stopwatch.ElapsedMilliseconds} ms]"); // Return the detections return(new NetworkResult(_labels[maxIndex], maxProb, stopwatch.ElapsedMilliseconds)); } else { return(new NetworkResult("No prediction exceeded probability threshold.", 0f, stopwatch.ElapsedMilliseconds));; } }