google-coral / tflite

Examples using TensorFlow Lite API to run inference on Coral devices
https://coral.withgoogle.com
Apache License 2.0
181 stars 67 forks source link

Running UNet with tflite_runtime #48

Closed ItsMeTheBee closed 3 years ago

ItsMeTheBee commented 3 years ago

Hey there!

I've trained a UNet model following these instructions but with two classes instead of three. I've also converted the network to tflite and compiled it with the coral edge compiler, but when I try to invoke the interpreter I get this error: Didn't find op for builtin opcode 'TRANSPOSE_CONV' version '3'

Do you happen to have an example code of running UNet with tflite_runtime, similar to the code in this repo? I know pycoral exists but I need to run everything on an aarch64 system which is why I'm very hesitant to change to another library.

Best regards, Sally

hjonnala commented 3 years ago

@ItsMeTheBee please share the invoking the interpreter script to reproduce the issue.

ItsMeTheBee commented 3 years ago

@hjonnala here you go =)

import numpy as np
import tflite_runtime.interpreter as tflite
import sys
from time import time

import random

EDGETPU_SHARED_LIB = "libedgetpu.so.1"
DEBUG = True

def make_interpreter(model_path, edge_tpu=True):
    # Load the TF-Lite model and delegate to Edge TPU
    if edge_tpu:
        interpreter = tflite.Interpreter(model_path=model_path,
                experimental_delegates=[
                    tflite.load_delegate(EDGETPU_SHARED_LIB)
                    ])
    else:
        interpreter = tflite.Interpreter(model_path=model_path)

    return interpreter

def main(args=None):
    make_interpreter("/mnt/Storage/training/unet_multi_class/Multiclass-Segmentation-in-Unet/conversion/model_256x256_edgetpu.tflite", False)

if __name__ == '__main__':
    main()

I'm currently working with Python 3.6 and tflite_runtime 2.1.0. I'm guessing I either need to upgrade/ downgrade something or to specify the transpose_conv operator version.

hjonnala commented 3 years ago

The script is working fine for me with edgetpu output form shared colab notebook. Is the error coming with model_256x256_edgetpu.tflite? If yes, please share model_256x256_edgetpu.tflite file.

ItsMeTheBee commented 3 years ago

Huh interesting, could it be an issue because I am training and converting with TensorFlow 2.3.0 instead of nightly? The model file can be downloaded from my private Google Drive: here.

hjonnala commented 3 years ago

Hello, I am using python 3.8 and corresponding tflite_runtime whl package from this source: https://github.com/google-coral/pycoral/releases

In my case correct whl would be: https://github.com/google-coral/pycoral/releases/download/v1.0.1/tflite_runtime-2.5.0-cp38-cp38-linux_x86_64.whl

The script is working fine with both True and False flags.

can you please share detail error message. Screenshot from 2021-07-13 06-41-45

ItsMeTheBee commented 3 years ago

Ah sweet, it seems like it was an issue with my conda enviroment! I made a new one with Python 3.8 and it worked fine, made another one with Python 3.6 and that one worked as well :D

Thanks a lot!

Do you happen to have some code sample on how to use UNet with tflite_runtime? I'm guessing the image needs to be converted into a numpy array just like during the training (so with normal tensorflow and keras) or are there any differences?

hjonnala commented 3 years ago

I don't have anything specific to Unet. May be this is https://github.com/google-coral/edgetpu/issues/417#issue-940647083 helpful.