-
Notifications
You must be signed in to change notification settings - Fork 535
NullReferenceException thrown in Tensorflow.Binding.dll when trying to run EfficientDetD0 #1077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Could you please provide the stack trace of the exception? |
The stack trace goes here: |
Please check if the |
The tensor isn't an EagerTensor indeed |
Could you please provide some steps to reproduce it? I don't know what |
Here is the DetectionEngine class:
Here is where I run inference:
|
I am using [EfficientDet D0 512x512] (http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d0_coco17_tpu-32.tar.gz) |
Ok, I'll reproduce it and give you a feedback later. Which version are you using? |
I am using the latest one, just got it yesterday
…On Thu, 18 May, 2023 at 11:27 AM, Rinne ***@***.***> wrote:
Ok, I'll reproduce it and give you a feedback later. Which version are you
using?
—
Reply to this email directly, view it on GitHub
<#1077 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APIIF6IKXZ5VL3YMJMTLQNLXGWXN5ANCNFSM6AAAAAAYF43PJE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
The |
On Thu, 18 May, 2023 at 12:43 PM, Rinne ***@***.***> wrote:
The UnmanagedMemoryBlock and Dispatcher are not defined in the code. Is
there any extra information?
—
Reply to this email directly, view it on GitHub
<#1077 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APIIF6OQQAHYXM3EQJHDOE3XGXAKTANCNFSM6AAAAAAYF43PJE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
I am using Avalonia UI with it, that’s where the dispatcher comes from, and
speaking of UnmanagedMemoryBlock, it comes from NumSharp.
|
The error is caused by some wrong usages. Note that tf.net has no longer depended on NumSharp, please use using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using Tensorflow;
using Tensorflow.NumPy;
using static Tensorflow.Binding;
namespace TensorFlowNET.UnitTest.Training
{
public record class Detection(float[] Box, float Score, float Class);
public abstract class DetectionEngine
{
public DetectionEngine(string modelPath, string labelPath)
{
ModelPath = modelPath;
LabelPath = labelPath;
}
public string ModelPath { get; set; }
public string LabelPath { get; set; }
public abstract IEnumerable<Detection> Detect(NDArray frame);
}
public class tfDetEngine : DetectionEngine
{
private readonly Session _session;
public tfDetEngine(string modelPath, string labelPath) : base(modelPath, labelPath)
{
_session = Session.LoadFromSavedModel(modelPath);
}
public override IEnumerable<Detection> Detect(NDArray image)
{
var graph = _session.graph;
graph.as_default();
try
{
var assigned = tf.convert_to_tensor(image, name: "input_image");
bool a = tf.Context.executing_eagerly();
var tensors = _session?.run(assigned);
if (tensors != null)
{
var boxesP = tensors["detection_boxes"].ToArray<float>();
var boxes = new float[boxesP.Length / 4][];
for (int i = 0; i < boxesP.Length; i += 4)
{
boxes[i / 4] = new float[] { boxesP[i], boxesP[i + 1], boxesP[i + 2], boxesP[i + 3] };
}
var scores = tensors["detection_scores"].ToArray<float>();
var classes = tensors["detection_classes"].ToArray<float>();
var detectedObjects = boxes.Select((box, i) => new Detection
(
Box: box,
Score: scores[i],
Class: classes[i]
));
return detectedObjects;
}
return new List<Detection>();
}
catch (NullReferenceException e)
{
return new List<Detection>();
}
finally
{
graph.Exit();
}
}
}
[TestClass]
public class ObjectDetection
{
tfDetEngine engine;
[TestMethod]
public void DetectionWithEfficientDet()
{
StartVideo();
}
public void StartVideo()
{
var devices = new List<int>();
while (devices.Count == 0)
{
devices = GetDevices();
}
Environment.SetEnvironmentVariable("TF_CPP_MIN_LOG_LEVEL", "2"); // Disable TensorFlow logs
Environment.SetEnvironmentVariable("OMP_NUM_THREADS", "4"); // Set the number of threads
engine = new tfDetEngine(@"C:\Users\liu_y\Downloads\efficientdet_d0_coco17_tpu-32\saved_model", "");
foreach (int device in devices)
{
StartVideoDaemon(device);
}
}
void StartVideoDaemon(int id)
{
VideoCapture capture = new(id);
Mat frame = new();
System.Drawing.Bitmap img;
while (true)
{
capture.Read(frame);
if (frame.Empty())
{
break;
}
Mat frameN = new Mat();
Cv2.Resize(frame, frameN, new Size(512, 512));
var detections = engine?.Detect(ToTensor(frameN));
}
}
public unsafe NDArray ToTensor(Mat src)
{
Shape shape = (src.Height, src.Width, src.Type().Channels);
SafeTensorHandle handle;
var tensor = new Tensor(handle = c_api.TF_AllocateTensor(TF_DataType.TF_UINT8, shape.dims.Select(d => (long)d).ToArray(), shape.ndim, (ulong)shape.size));
new Span<byte>((void*)src.DataPointer, (int)shape.size)
.CopyTo(new Span<byte>((byte*)c_api.TF_TensorData(handle), (int)shape.size));
return tensor.numpy();
}
List<int> GetDevices()
{
var devices = new List<int>();
int i = 0;
while (true)
{
var cap = new VideoCapture(i);
if (cap.IsOpened())
{
devices.Add(i);
i++;
continue;
}
break;
}
return devices;
}
}
} However it will still fail when executing |
_session.run still leaves me with a NullReferenceException, here is the stack trace
|
Are you using exactly the code I listed before? |
yes, still doesn't work |
Could it be that I have an installation issue? I might have a broken installation of Tensorflow. How can I test that TensorFlow.net will run using a simpler model? |
Please refer to https://github.com/SciSharp/SciSharp-Stack-Examples/tree/master/src/TensorFlowNET.Examples and choose one or two simple models of it |
I can't reproduce it at this time. Is there any further information? |
I think I have issues with my tensorflow installation or something. Everything should work fine. I am using SciSharp.Tensorflow.Redist as my laptop doesn't have a GPU but my PC does. What steps should I follow to make sure that my initial setups are correct? I suspect my setup is faulty, as you have a perfectly running Tensorflow.Net implementation on your end. |
Sorry, my bad. My local environment is actually the master branch. Could you please try it again with master branch? Please clone it and add a project reference but keep |
Pardon, which repo do I have to clone now? |
Please clone this repo and reference the |
But, there is no tensorflow.net.csproj but rather Tensorflow.Binding.csproj
…On Thu, 18 May, 2023 at 10:57 PM, Rinne ***@***.***> wrote:
Pardon, which repo do I have to clone now?
Please clone this repo and reference the
src/TensorFlowNET.Core/tensorflow.net.csproj
—
Reply to this email directly, view it on GitHub
<#1077 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APIIF6PBM42CU3TYWCZOXCDXGZIJNANCNFSM6AAAAAAYF43PJE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Yup, that's it. I got it wrong. |
I tried it with the source code, but it gave me the same error, but a little detailed. When debugging, I notcied that _session.status is null. Here is the stack trace:
|
Just curious, what should I put in the place of feed_dict params? |
Sorry for the confusion, I've fixed the error of null reference of status, could you please pull the newest master branch and try again? |
|
Now I get "Invalid slice notation: 'detection_boxes" |
Yes, that's the same with what acts like in my local environment. Since I don't know much about object detection, could you please further explain what you want by |
So, I couldn't convert boxes to Float array directly, because it is nullable. I couldn't just write it as ToArray<float[]>(), it should actually be a two dimensional array, and the extra for loop is there trying to convert the float[] to float[][] |
What are |
|
But graph has no nodes, everything shows 0. Maybe the model isn't loaded correctly? |
@Oceania2018 Could you please look at this issue? |
Description
I keep getting NullReferenceException thrown in Tensorflow.Binding.dll whenever I try to evaluate the model with a (512, 512, 3) sized image tensor
The text was updated successfully, but these errors were encountered: