Skip to content
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

TFLiteInvoke threw a exception on Raspbian but not on Windows #55

Open
RickyYCheng opened this issue Jan 22, 2021 · 4 comments
Open

TFLiteInvoke threw a exception on Raspbian but not on Windows #55

RickyYCheng opened this issue Jan 22, 2021 · 4 comments

Comments

@RickyYCheng
Copy link

RickyYCheng commented Jan 22, 2021

I found my model and script works good on windows, but not on raspbian, so i wrote a new short program but still unknow what's wrong. These are the steps of my test script:

  1. nuget. Including Emgu.TF.Lite, Emgu.TF.Lite.runtime.raspbian, Emgu.TF.runtime.windows.
  2. code. like this:
class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Interpreter interpreter = new Interpreter();
                interpreter.AllocateTensors();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
            Console.WriteLine("Hello interpreter!");
            Console.ReadKey();
        }
    }
  1. test. I used Visual Studio and debug on windows. It shows "Hello interpreter!", which means it works fine.
  2. test on raspbian. This slice of code is only to have a test, so i just "Release" on Visual Studio and set config. (linux arm, netcoreapp3.1, independent)
    I'm sure my computer and raspberry pi installed tensorflow==2.4.0 already, but on rasbian it just shows
    "Emgu.TF.Lite.TFLiteInvoke threw a exception"

I'm wondering what's wrong with the test code. BTW, my workable image classification code cannot work correctly as well.
Thanks for your answer! :)

@RickyYCheng
Copy link
Author

RickyYCheng commented Jan 22, 2021

I'm glad to show the image classification code i used to test Emgu.TF.Lite.
The problem is also similiar to the topic——it works good on windows, but not on raspbian.

using System;
using Emgu.TF.Lite;
using System.Linq;
using Emgu.Models;

namespace tester
{
    class Program
    {
        static void Main(string[] args)
        {
            String imageFile = "1.jpg";
            String modelDir = "trash_model_lite.tflite";
            int width = 224;
            int height = 224;
            float mean = 0.0f;
            float scale = 1.0f;
            bool flipUpsideDown = false;
            bool swapBR = true;

            Interpreter interpreter = new Interpreter(new FlatBufferModel(modelDir));
            interpreter.AllocateTensors();

            int[] input = interpreter.InputIndices;
            var inputTensor = interpreter.Inputs[0];

            int[] output = interpreter.InputIndices;
            var outputTensor = interpreter.Outputs[0];

            NativeImageIO.ReadImageFileToTensor<float>(imageFile, inputTensor.DataPointer, height, width, mean, scale, flipUpsideDown, swapBR);

            interpreter.Invoke();

            float[] outputData = outputTensor.Data as float[];

            string[] classNames = { "Hazardous Waste", "Kitchen Waste", "Other Waste", "Recyclable Waste" };

            int index;
            float a = Softmax(outputData, out index);
            Console.WriteLine($"The image has been predicted as {classNames[index]} with probability of {a:N2}%.");
            Console.ReadKey();
        }
        static float Softmax(float[] data, out int index)
        {
            var l = data.Length;
            float[] expData = new float[l];
            float sum = 0;
            for (int i = 0; i < l; i++) expData[i] = (float)Math.Exp(data[i]);
            foreach (var item in expData) sum += item;
            for (int i = 0; i < l; i++) expData[i] /= sum;
            index = expData.ToList<float>().IndexOf(expData.Max());
            return expData.Max() * 100;
        }
        static float Softmax(float[] data)
        {
            var l = data.Length;
            float[] expData = new float[l];
            float sum = 0;
            for (int i = 0; i < l; i++) expData[i] = (float)Math.Exp(data[i]);
            foreach (var item in expData) sum += item;
            for (int i = 0; i < l; i++) expData[i] /= sum;
            return expData.Max() * 100;
        }
    }
}

nuget packages:
Emgu.TF
Emgu.TF.Lite
Emgu.TF.Lite.Models
Emgu.TF.Lite.runtime.rasbian
Emgu.TF.Lite.runtime.windows

@RickyYCheng
Copy link
Author

RickyYCheng commented Jan 23, 2021

I've checked the tfliteextern.dll of x64 folder, which shows in KernelBase.dll, many dlls error opening file, 'cause the system cannot find. But the program works well on windows, still threw exception on rasbian.

@emgucv
Copy link
Owner

emgucv commented Jan 25, 2021

You cannot use tfliteextern.dll for raspbian. It is a windows binary.

You will need the libtfliteextern.so file for raspbian. Officially Raspberry Pi OS only support 32bit, there is an experienmental 64bit raspberry pi OS that we do not support. Are you using the 32bit version or the 64bit version of raspberry pi OS? If you are using the 64bit version of raspberry pi OS you will need to build the binary yourself.

@RickyYCheng
Copy link
Author

RickyYCheng commented Jan 26, 2021

You cannot use tfliteextern.dll for raspbian. It is a windows binary.

You will need the libtfliteextern.so file for raspbian. Officially Raspberry Pi OS only support 32bit, there is an experienmental 64bit raspberry pi OS that we do not support. Are you using the 32bit version or the 64bit version of raspberry pi OS? If you are using the 64bit version of raspberry pi OS you will need to build the binary yourself.

yes,i knew that. i just checked dll but i still used .so lib on linux. Os is Raspbian official so it must be linux arm7vl.
Now i'm using process to run python script and get returned value 'cause emgu still threw an exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants