Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Downloaded packages working in VS but not in Unity?

Discussion in 'General Discussion' started by bw92, Jan 23, 2020.

  1. bw92

    bw92

    Joined:
    Apr 17, 2019
    Posts:
    20
    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:
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <packages>
    3.   <package id="Google.Protobuf" version="3.11.2" targetFramework="net471" />
    4.   <package id="NumSharp" version="0.20.5" targetFramework="net471" />
    5.   <package id="SciSharp.TensorFlow.Redist" version="1.15.0" targetFramework="net471" />
    6.   <package id="System.Buffers" version="4.4.0" targetFramework="net471" />
    7.   <package id="System.Memory" version="4.5.3" targetFramework="net471" />
    8.   <package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net471" />
    9.   <package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net471" />
    10.   <package id="TensorFlow.NET" version="0.14.0" targetFramework="net471" />
    11. </packages>
    Here is what I'm importing:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System;
    4. using System.IO;
    5. using System.Collections;
    6. using NumSharp;
    7. using Tensorflow;
    8. using static Tensorflow.Binding;
    It appears fine in VS, so I'm assuming this has something to do in Unity.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    You have not placed tensor flow stuff in a place where unity can see it. Check script reference regarding using c# dlls in unity.
     
    bw92 likes this.
  3. bw92

    bw92

    Joined:
    Apr 17, 2019
    Posts:
    20
    That makes a lot of sense actually, thanks!
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    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.
     
    bw92 likes this.
  5. bw92

    bw92

    Joined:
    Apr 17, 2019
    Posts:
    20
    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:

    Code (CSharp):
    1. Assembly 'Library/ScriptAssemblies/Assembly-CSharp.dll' will not be loaded due to errors:
    2. Reference has errors 'TensorFlow.NET'.
    Code (CSharp):
    1. Assembly 'Assets/Plugins/NumSharp.Core.dll' will not be loaded due to errors:
    2. Unable to resolve reference 'System.Runtime.CompilerServices.Unsafe'. Is the assembly missing or incompatible with the current platform?
    3. Reference validation can be disabled in the Plugin Inspector.
    4. Unable to resolve reference 'System.Memory'. Is the assembly missing or incompatible with the current platform?
    5. Reference validation can be disabled in the Plugin Inspector.
    Code (CSharp):
    1. Assembly 'Assets/Plugins/Google.Protobuf.dll' will not be loaded due to errors:
    2. Unable to resolve reference 'System.Memory'. Is the assembly missing or incompatible with the current platform?
    3. Reference validation can be disabled in the Plugin Inspector.
    Code (CSharp):
    1. Assembly 'Assets/Plugins/Sylvester.tf.Api.dll' will not be loaded due to errors:
    2. Unable to resolve reference 'System.Memory'. Is the assembly missing or incompatible with the current platform?
    3. Reference validation can be disabled in the Plugin Inspector.
    4. Unable to resolve reference 'System.Runtime.CompilerServices.Unsafe'. Is the assembly missing or incompatible with the current platform?
    5. Reference validation can be disabled in the Plugin Inspector.
    Code (CSharp):
    1. Assembly 'Assets/Plugins/Protobuf.Text.dll' will not be loaded due to errors:
    2. Reference has errors 'Google.Protobuf'.
    Code (CSharp):
    1. Assembly 'Assets/Plugins/TensorFlow.NET.dll' will not be loaded due to errors:
    2. Unable to resolve reference 'System.Memory'. Is the assembly missing or incompatible with the current platform?
    3. Reference validation can be disabled in the Plugin Inspector.
    4. Unable to resolve reference 'System.Runtime.CompilerServices.Unsafe'. Is the assembly missing or incompatible with the current platform?
    5. 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?
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    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.
     
    bw92 likes this.