Unable to load DLL 'tensorflow' or one of its dependencies (ML.NET)
Asked Answered
M

10

10

I have a .NET Core 3 app for image classification, using Microsoft's ML.NET framework.

On my development machine, I can run the code and it all works fine.

However, when I deploy it to my staging server, I get this error at runtime:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
     System.DllNotFoundException: Unable to load DLL 'tensorflow' or one of its dependencies: The specified module could not be found. (0x8007007E)
     at Tensorflow.c_api.TF_NewGraph()
     at Tensorflow.Graph..ctor()
     at Microsoft.ML.Transforms.Dnn.DnnUtils.LoadTFSession(IExceptionContext ectx, Byte[] modelBytes, String modelFile)
...

I've tried copying tensorflow.dll into the bin folder during deployment; I've tried building as x64, x86, or AnyCPU. I've verified that the SciSharp.TensorFlow.Redist and Microsoft.ML.Tensorflow.Redist NuGet packages are included. Nothing so far has worked.

Any idea why it can't find the DLL file, or how I can get it to work?

Maimaia answered 24/10, 2019 at 21:51 Comment(1)
Just to add for anyone else having the same problem: Refer here - developers.de/2019/10/25/hosting-ml-net-in-appservice , it gave me pointer on where to look.Unbutton
C
19

Installed "SciSharp.TensorFlow.Redist" version 1.14.0 and this issue is gone now.

My package references are exactly as follows:

<PackageReference Include="Microsoft.ML" Version="1.4.0" />
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="1.4.0" />
<PackageReference Include="Microsoft.ML.Vision" Version="1.4.0" />
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="1.14.0" />
Che answered 7/1, 2020 at 9:43 Comment(1)
My problem was resolved as soon as I included SciSharp.TensorFlow.Redist. In my case v2.1 worked as well.Raffle
S
8

I've figured out the root cause.

Reason 1: Microsoft.ML works on x64 only

Reason 2: Latest stable version (1.4.0) of below packages is throwing error. Below v1.3.1 packages are working fine.

  • Microsoft.ML v1.3.1
  • Microsoft.ML.ImageAnalytics v1.3.1
  • Microsoft.ML.TensorFlow v1.3.1

Reason 3: ML.NET and Tensor flow works fine only in .NET CORE not in .NET framework.

Hope this works for you too ! Happy Coding !

Spoken answered 10/11, 2019 at 22:4 Comment(0)
K
7

I have experienced a number of problems with the ML libraries:

The particular one mentioned in the OQ. The error: Unable to load DLL 'tensorflow' or one of its dependencies: The specified module could not be found. (0x8007007E), was indeed resolved by updating to the latest VC++ x64 Redistributable. For me that link was here

When getting the error: Unable to find an entry point named 'TF_StringEncodedSize' in DLL 'tensorflow' I needed to downgrade the SciSharp.TensorFlow.Redist library from 2.4.x to 2.3.x.

When getting the Check whether your GraphDef-interpreting binary is up to date with your GraphDef-generating binary error, I noticed that installing the Microsoft.ML.TensorFlow.Redist was a mistake and needed to be removed.

Kayleen answered 16/2, 2021 at 17:59 Comment(0)
W
6

Faced the same problem with a tensorflow neural network in a C# desktop application running ok in developement environment but failing in other machines. It was solved installing Microsoft Visual C++ redistributable in client machines

Weakness answered 3/5, 2020 at 20:30 Comment(1)
That did the trick for me. My setup is Microsoft.ML = 1.5.2, Microsoft.ML.ImageAnalytics= 1.5.2, Microsoft.ML.TensorFlow= 1.5.2, SciSharp.TensorFlow.Redist=2.3.1. Loading a model created by tensorflow 2.1.x and running on a Windows 8 x64Ephor
C
4

Install the LATEST VC_redist then it works!

Carolacarolan answered 31/7, 2020 at 9:4 Comment(0)
K
2

Installing vc_redist.x64.exe helped me

Kilocycle answered 8/2, 2021 at 5:3 Comment(0)
R
1

If you add a reference to Microsoft.ML.TensorFlow.Redist this will probably help. It solved it for me.

Reformatory answered 21/6, 2021 at 12:39 Comment(0)
A
1
  1. First of all, make sure that your hardware is capable of handling the latest software technologies such as CPU architecture or motherboard compability. I had this problem because I was using an old system more than 10 years old.
  2. Install the latest compatable vc_redist_64x distributions.
  3. Install Windows updates that are installing the framework compatibility packages for x64 system.

It worked for me when I did all the following.

Adela answered 22/2, 2022 at 7:51 Comment(0)
I
1

Just in case anybody stumbles upon this issue. For me the stumbling block was one of the older servers which did not support the AVX/AVX2 CPU features required for Tensorflow library to work.

Here is a similar issue: https://mcmap.net/q/1160368/-import-tensorflow-error-in-windows-server-2016-dll-load-failed-importing-_pywrap_tensorflow_internal

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheckForAVXSupport
{
    internal class Program
    {
        public static bool HasAvxSupport()
        {
            try
            {
                return (GetEnabledXStateFeatures() & 4) != 0;
            }
            catch
            {
                return false;
            }
        }

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern long GetEnabledXStateFeatures();

        static void Main(string[] args)
        {
            Console.WriteLine("AVX/AVX2 support: " + HasAvxSupport().ToString());
            Console.WriteLine("\nPress any key...");
            Console.ReadKey();
        }
    }
}

Source: https://mcmap.net/q/1160369/-check-avx-instruction-set-support

Illona answered 30/12, 2022 at 20:48 Comment(0)
S
1

Changing azure windows function runtime to x64 was the solution in my case. None of the other suggestions worked.

Skink answered 30/10, 2023 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.