Downloaded packages working in VS but not in Unity?

Hey all,

I’m using Tensorflow.NET to do machine learning in Unity. The output of this model will be displayed in Unity by capturing a webcam screenshot as the input and returning a heatmap as it’s output, which is computed when the webcam is paused.

The model/code appears to work inside of VS alone, but when running the game in Unity it returns the following error:
Assets\Scripts\TestGUI.cs(9,7): error CS0246: The type or namespace name ‘Tensorflow’ could not be found (are you missing a using directive or an assembly reference?)

Here are my packages in the project:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Google.Protobuf" version="3.11.2" targetFramework="net471" />
  <package id="NumSharp" version="0.20.5" targetFramework="net471" />
  <package id="SciSharp.TensorFlow.Redist" version="1.15.0" targetFramework="net471" />
  <package id="System.Buffers" version="4.4.0" targetFramework="net471" />
  <package id="System.Memory" version="4.5.3" targetFramework="net471" />
  <package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net471" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net471" />
  <package id="TensorFlow.NET" version="0.14.0" targetFramework="net471" />
</packages>

Here is what I’m importing:

using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;
using System.Collections;
using NumSharp;
using Tensorflow;
using static Tensorflow.Binding;

It appears fine in VS, so I’m assuming this has something to do in Unity.

You have not placed tensor flow stuff in a place where unity can see it. Check script reference regarding using c# dlls in unity.

1 Like

That makes a lot of sense actually, thanks!

Here’s the article:
https://docs.unity3d.com/Manual/UsingDLL.html

External dependencies in source form can be dropped into asset folder, an they’ll be compiled - IF unity can compile them. To prevent frequent and slow recompilation some people drop them into “Plugins” folder. At least that used to be a practice some time ago.

A dll needs to be dragged into project. See the link for more info.

1 Like

So I added the dll files from “C:\Users\Me\source\repos\ArRobotVis\bin\Debug\netcoreapp3.1” into a “Plugins” folder in Unity, but I get the following errors:

Assembly 'Library/ScriptAssemblies/Assembly-CSharp.dll' will not be loaded due to errors:
Reference has errors 'TensorFlow.NET'.
Assembly 'Assets/Plugins/NumSharp.Core.dll' will not be loaded due to errors:
Unable to resolve reference 'System.Runtime.CompilerServices.Unsafe'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'System.Memory'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Assembly 'Assets/Plugins/Google.Protobuf.dll' will not be loaded due to errors:
Unable to resolve reference 'System.Memory'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Assembly 'Assets/Plugins/Sylvester.tf.Api.dll' will not be loaded due to errors:
Unable to resolve reference 'System.Memory'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'System.Runtime.CompilerServices.Unsafe'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Assembly 'Assets/Plugins/Protobuf.Text.dll' will not be loaded due to errors:
Reference has errors 'Google.Protobuf'.
Assembly 'Assets/Plugins/TensorFlow.NET.dll' will not be loaded due to errors:
Unable to resolve reference 'System.Memory'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'System.Runtime.CompilerServices.Unsafe'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.

Both ‘System.Memory’ and ‘System.Runtime.CompilerServices.Unsafe’ are packages in VS, I’m assuming they don’t exist in Unity. I cannot seem to find the dll’s for these, however. Do you know where they would be stored?

No, I don’t. However I would be able to find out by looking for “NuGet dll location”, “VS nuget dll location” and so on.
Judging by the errors you might need to enable unsafe code. Additionally, check this document:
https://docs.unity3d.com/2019.1/Documentation/Manual/dotnetProfileSupport.html

And this stackoverflow thread:
https://stackoverflow.com/questions/56885284/implement-spant-in-unity

Also be aware that you should be probably asking this in scripting forum.

1 Like