Skip to content

tensor flow #439

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified lite/examples/audio_classification/raspberry_pi/setup.sh
100644 → 100755
Empty file.
150 changes: 150 additions & 0 deletions lite/examples/image_classification/raspberry_pi/Gary_classify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Main script to run image classification."""

import argparse
import sys
import time

from tflite_support.task import core
from tflite_support.task import processor
from tflite_support.task import vision
from PIL import Image

# Visualization parameters
_ROW_SIZE = 20 # pixels
_LEFT_MARGIN = 24 # pixels
_TEXT_COLOR = (0, 0, 255) # red
_FONT_SIZE = 1
_FONT_THICKNESS = 1
_FPS_AVERAGE_FRAME_COUNT = 10


def run(model: str, max_results: int, score_threshold: float, num_threads: int,
enable_edgetpu: bool, width: int, height: int) -> None:
"""Continuously run inference on images acquired from the camera.

Args:
model: Name of the TFLite image classification model.
max_results: Max of classification results.
score_threshold: The score threshold of classification results.
num_threads: Number of CPU threads to run the model.
enable_edgetpu: Whether to run the model on EdgeTPU.
camera_id: The camera id to be passed to OpenCV.
width: The width of the frame captured from the camera.
height: The height of the frame captured from the camera.
"""

# Initialize the image classification model
base_options = core.BaseOptions(
file_name=model, use_coral=enable_edgetpu, num_threads=num_threads)

# Enable Coral by this setting
classification_options = processor.ClassificationOptions(
max_results=max_results, score_threshold=score_threshold)
options = vision.ImageClassifierOptions(
base_options=base_options, classification_options=classification_options)

classifier = vision.ImageClassifier.create_from_options(options)

# Variables to calculate FPS
counter, fps = 0, 0
start_time = time.time()


# Create TensorImage from the RGB image
# rgb_image = Image.open("test_data/fox.jpeg").convert('RGB').resize((width, height))
# tensor_image = vision.TensorImage.create_from_array(rgb_image)

# test_file = "test_data/fox.jpeg"
test_file = "test_data/squirrel.jpeg"

tensor_image = vision.TensorImage.create_from_file(test_file)

# List classification results
categories = classifier.classify(tensor_image)


# Show classification results on the image
print(test_file+" : RESULTS")
for idx, category in enumerate(categories.classifications[0].categories):
category_name = category.category_name
score = round(category.score, 2)
result_text = category_name + ' (' + str(score) + ')'
print(" : "+result_text )

# Calculate the FPS
if counter % _FPS_AVERAGE_FRAME_COUNT == 0:
end_time = time.time()
fps = _FPS_AVERAGE_FRAME_COUNT / (end_time - start_time)
start_time = time.time()

# Show the FPS
fps_text = 'FPS = ' + str(int(fps))
text_location = (_LEFT_MARGIN, _ROW_SIZE)




def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--model',
help='Name of image classification model.',
required=False,
default='efficientnet_lite0.tflite')
parser.add_argument(
'--maxResults',
help='Max of classification results.',
required=False,
default=3)
parser.add_argument(
'--scoreThreshold',
help='The score threshold of classification results.',
required=False,
type=float,
default=0.0)
parser.add_argument(
'--numThreads',
help='Number of CPU threads to run the model.',
required=False,
default=4)
parser.add_argument(
'--enableEdgeTPU',
help='Whether to run the model on EdgeTPU.',
action='store_true',
required=False,
default=False)
# parser.add_argument(
# '--cameraId', help='Id of camera.', required=False, default=0)
parser.add_argument(
'--frameWidth',
help='Width of frame to capture from camera.',
required=False,
default=640)
parser.add_argument(
'--frameHeight',
help='Height of frame to capture from camera.',
required=False,
default=480)
args = parser.parse_args()

run(args.model, int(args.maxResults),
args.scoreThreshold, int(args.numThreads), bool(args.enableEdgeTPU),
args.frameWidth, args.frameHeight)


if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Example of how to classify a single file from
# https://blog.paperspace.com/tensorflow-lite-raspberry-pi/

from tflite_runtime.interpreter import Interpreter
from PIL import Image
import numpy as np
import time

def load_labels(path): # Read the labels from the text file as a Python list.
with open(path, 'r') as f:
return [line.strip() for i, line in enumerate(f.readlines())]

def set_input_tensor(interpreter, image):
tensor_index = interpreter.get_input_details()[0]['index']
input_tensor = interpreter.tensor(tensor_index)()[0]
input_tensor[:, :] = image

def classify_image(interpreter, image, top_k=1):
set_input_tensor(interpreter, image)

interpreter.invoke()
output_details = interpreter.get_output_details()[0]
output = np.squeeze(interpreter.get_tensor(output_details['index']))

scale, zero_point = output_details['quantization']
output = scale * (output - zero_point)

ordered = np.argpartition(-output, 1)
return [(i, output[i]) for i in ordered[:top_k]][0]

data_folder = "./"

model_path = data_folder + "efficientnet_lite0.tflite"
#label_path = data_folder + "labels_mobilenet_quant_v1_224.txt"

interpreter = Interpreter(model_path)
print("Model Loaded Successfully.")

interpreter.allocate_tensors()
_, height, width, _ = interpreter.get_input_details()[0]['shape']
print("Image Shape (", width, ",", height, ")")

# Load an image to be classified.
image = Image.open(data_folder + "test_data/fox.jpeg").convert('RGB').resize((width, height))

# Classify the image.
time1 = time.time()
label_id, prob = classify_image(interpreter, image)
time2 = time.time()
classification_time = np.round(time2-time1, 3)
print("Classificaiton Time =", classification_time, "seconds.")


# Read class labels.
#labels = load_labels(label_path)

# Return the classification label of the image.
#classification_label = labels[label_id]
#print("Image Label is :", classification_label, ", with Accuracy :", np.round(prob*100, 2), "%.")
3 changes: 3 additions & 0 deletions lite/examples/image_classification/raspberry_pi/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def run(model: str, max_results: int, score_threshold: float, num_threads: int,
'ERROR: Unable to read from webcam. Please verify your webcam settings.'
)




counter += 1
image = cv2.flip(image, 1)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"folders": [
{
"path": "."
}
]
}
Empty file modified lite/examples/image_classification/raspberry_pi/setup.sh
100644 → 100755
Empty file.
58 changes: 58 additions & 0 deletions lite/examples/image_classification/raspberry_pi/single_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Example of how to classify a single file from
# https://blog.paperspace.com/tensorflow-lite-raspberry-pi/

from tflite_runtime.interpreter import Interpreter
from PIL import Image
import numpy as np
import time

def load_labels(path): # Read the labels from the text file as a Python list.
with open(path, 'r') as f:
return [line.strip() for i, line in enumerate(f.readlines())]

def set_input_tensor(interpreter, image):
tensor_index = interpreter.get_input_details()[0]['index']
input_tensor = interpreter.tensor(tensor_index)()[0]
input_tensor[:, :] = image

def classify_image(interpreter, image, top_k=1):
set_input_tensor(interpreter, image)

interpreter.invoke()
output_details = interpreter.get_output_details()[0]
output = np.squeeze(interpreter.get_tensor(output_details['index']))

scale, zero_point = output_details['quantization']
output = scale * (output - zero_point)

ordered = np.argpartition(-output, 1)
return [(i, output[i]) for i in ordered[:top_k]][0]

data_folder = "/home/pi/TFLite_MobileNet/"

model_path = data_folder + "mobilenet_v1_1.0_224_quant.tflite"
label_path = data_folder + "labels_mobilenet_quant_v1_224.txt"

interpreter = Interpreter(model_path)
print("Model Loaded Successfully.")

interpreter.allocate_tensors()
_, height, width, _ = interpreter.get_input_details()[0]['shape']
print("Image Shape (", width, ",", height, ")")

# Load an image to be classified.
image = Image.open(data_folder + "test.jpg").convert('RGB').resize((width, height))

# Classify the image.
time1 = time.time()
label_id, prob = classify_image(interpreter, image)
time2 = time.time()
classification_time = np.round(time2-time1, 3)
print("Classificaiton Time =", classification_time, "seconds.")

# Read class labels.
labels = load_labels(label_path)

# Return the classification label of the image.
classification_label = labels[label_id]
print("Image Label is :", classification_label, ", with Accuracy :", np.round(prob*100, 2), "%.")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.