Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensor Flow Lite Model with Standard Tensorflow

I am working on a Tensorflow simple app (would like to detect if people are in a captured image).

I'm familiar with the python interface to Tensorflow. I see Tensorflow Lite has a different reduced format.

I'm interested in using the model linked (for not wanting to spend time to create my own) in the Tensorflow Lite Examples in a traditional tensorflow python program with a PC Based GPU.

https://www.tensorflow.org/lite/models/image_classification/overview

Is this possible?

When I run the following code I receive

import tensorflow as tf
def load_pb(path_to_pb):
    with tf.gfile.GFile(path_to_pb, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
     with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name='')
        return graph

 load_pb('detect.tflite')

main.py:5: RuntimeWarning: Unexpected end-group tag: Not all data was converted graph_def.ParseFromString(f.read())

like image 306
ffejrekaburb Avatar asked Feb 27 '26 11:02

ffejrekaburb


1 Answers

You can follow the example provided by the Tensorflow documentation. The tflite model and labels were taken from here. The code runs on a regular desktop PC.

import tensorflow as tf
import requests
import io
from PIL import Image
import numpy as np

# load model
interpreter = tf.contrib.lite.Interpreter(model_path="mobilenet_v1_1.0_224_quant.tflite")
interpreter.allocate_tensors()

# get details of model
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# load an image
r = requests.get('https://www.tensorflow.org/lite/models/image_classification/images/dog.png')

# convert the image RGB, see input_details[0].shape
img = Image.open(io.BytesIO(r.content)).convert('RGB')

# resize the image and convert it to a Numpy array
img_data = np.array(img.resize(input_details[0]['shape'][1:3]))

# run the model on the image
interpreter.set_tensor(input_details[0]['index'], [img_data])
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])

# get the labels
with open('labels_mobilenet_quant_v1_224.txt') as f:
    labels = f.readlines()

print(labels[np.argmax(output_data[0])])

West Highland white terrier

enter image description here

like image 73
Maximilian Peters Avatar answered Mar 02 '26 02:03

Maximilian Peters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!