Tensorflow Lite, Skript mit EdgeTPU ohne Beschleunigung
Verfasst: Samstag 9. Januar 2021, 18:01
Hallo an alle,
ich streife nun interessante Gebiete, in denen meine Kenntnisse leider rudimentär sind. Mein kleines Projekt ist Bilderkennung.
Ich habe nun mit Googel "Teachable Machines" eine Model erstellt
https://teachablemachine.withgoogle.com ... kNxYfxmZM/
um Singvögel von Katzen und Tauben zu unterscheiden und dann Störenfriede per Wasserstrahl vom dem Futterhaus zu vertreiben.
Dann habe ebenfalls mangels Programmierkenntnisse eine Auswertung gesucht und ein Python-Skript für Tensorflow Lite mit TPU-Unterstützung gefunden,
https://github.com/google-coral/tflite/ ... sification
welche mit dem von der Google-Seite generierten Model funktioniert und gut per Bash-Skript auswertbar ist:
Leider habe ich keine TFLite-Skript ohne diese Co-Prozessor-Abhängigkeit gefunden. Mit leider meine ich, dieser Coral-Google-Accelator beschleunigt zwar die Erkennung, aber er verdoppelt dabei ungefähr die Hardware-Kosten.
Nun meine Frage an die Python-Profis, welche sich mit dieser Materie auskennen: Wie kann dieses Skript (classify_image.py) modifiziert werden, dass es auch ohne dem Coral-Accelator funktioniert?
In diesem Skript kommt noch eine Abhängigkeit von einem anderen Skript (classify.py), aber ich glaube dort gibt ews keinen Bezug zum Co-Prozessor:
Vielen Dank für Eure Aufmerksamkeit ![Smile :-)](./images/smilies/icon_smile.gif)
Grúße Jörn
ich streife nun interessante Gebiete, in denen meine Kenntnisse leider rudimentär sind. Mein kleines Projekt ist Bilderkennung.
Ich habe nun mit Googel "Teachable Machines" eine Model erstellt
https://teachablemachine.withgoogle.com ... kNxYfxmZM/
um Singvögel von Katzen und Tauben zu unterscheiden und dann Störenfriede per Wasserstrahl vom dem Futterhaus zu vertreiben.
Dann habe ebenfalls mangels Programmierkenntnisse eine Auswertung gesucht und ein Python-Skript für Tensorflow Lite mit TPU-Unterstützung gefunden,
https://github.com/google-coral/tflite/ ... sification
welche mit dem von der Google-Seite generierten Model funktioniert und gut per Bash-Skript auswertbar ist:
Code: Alles auswählen
TIER=`python3 classify_image.py --model tflite.tpu_model/model_edgetpu.tflite --labels tflite.tpu_model/labels.txt --input html/usbcambild.jpg |tail -1|cut -d: -f1`
Nun meine Frage an die Python-Profis, welche sich mit dieser Materie auskennen: Wie kann dieses Skript (classify_image.py) modifiziert werden, dass es auch ohne dem Coral-Accelator funktioniert?
Code: Alles auswählen
# Lint as: python3
# Copyright 2019 Google LLC
#
# 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
#
# https://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.
r"""Example using TF Lite to classify a given image using an Edge TPU.
To run this code, you must attach an Edge TPU attached to the host and
install the Edge TPU runtime (`libedgetpu.so`) and `tflite_runtime`. For
device setup instructions, see g.co/coral/setup.
Example usage (use `install_requirements.sh` to get these files):
```
python3 classify_image.py \
--model models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \
--labels models/inat_bird_labels.txt \
--input images/parrot.jpg
```
"""
import argparse
import time
from PIL import Image
import classify
import tflite_runtime.interpreter as tflite
import platform
EDGETPU_SHARED_LIB = {
'Linux': 'libedgetpu.so.1',
'Darwin': 'libedgetpu.1.dylib',
'Windows': 'edgetpu.dll'
}[platform.system()]
def load_labels(path, encoding='utf-8'):
"""Loads labels from file (with or without index numbers).
Args:
path: path to label file.
encoding: label file encoding.
Returns:
Dictionary mapping indices to labels.
"""
with open(path, 'r', encoding=encoding) as f:
lines = f.readlines()
if not lines:
return {}
if lines[0].split(' ', maxsplit=1)[0].isdigit():
pairs = [line.split(' ', maxsplit=1) for line in lines]
return {int(index): label.strip() for index, label in pairs}
else:
return {index: line.strip() for index, line in enumerate(lines)}
def make_interpreter(model_file):
model_file, *device = model_file.split('@')
return tflite.Interpreter(
model_path=model_file,
experimental_delegates=[
tflite.load_delegate(EDGETPU_SHARED_LIB,
{'device': device[0]} if device else {})
])
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'-m', '--model', required=True, help='File path of .tflite file.')
parser.add_argument(
'-i', '--input', required=True, help='Image to be classified.')
parser.add_argument(
'-l', '--labels', help='File path of labels file.')
parser.add_argument(
'-k', '--top_k', type=int, default=1,
help='Max number of classification results')
parser.add_argument(
'-t', '--threshold', type=float, default=0.0,
help='Classification score threshold')
parser.add_argument(
'-c', '--count', type=int, default=5,
help='Number of times to run inference')
args = parser.parse_args()
labels = load_labels(args.labels) if args.labels else {}
interpreter = make_interpreter(args.model)
interpreter.allocate_tensors()
size = classify.input_size(interpreter)
image = Image.open(args.input).convert('RGB').resize(size, Image.ANTIALIAS)
classify.set_input(interpreter, image)
print('----INFERENCE TIME----')
print('Note: The first inference on Edge TPU is slow because it includes',
'loading the model into Edge TPU memory.')
for _ in range(args.count):
start = time.perf_counter()
interpreter.invoke()
inference_time = time.perf_counter() - start
classes = classify.get_output(interpreter, args.top_k, args.threshold)
print('%.1fms' % (inference_time * 1000))
print('-------RESULTS--------')
for klass in classes:
print('%s: %.5f' % (labels.get(klass.id, klass.id), klass.score))
if __name__ == '__main__':
main()
Code: Alles auswählen
# Lint as: python3
# Copyright 2019 Google LLC
#
# 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
#
# https://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.
"""Functions to work with classification models."""
import collections
import operator
import numpy as np
Class = collections.namedtuple('Class', ['id', 'score'])
def input_details(interpreter, key):
"""Returns input details by specified key."""
return interpreter.get_input_details()[0][key]
def input_size(interpreter):
"""Returns input image size as (width, height) tuple."""
_, height, width, _ = input_details(interpreter, 'shape')
return width, height
def input_tensor(interpreter):
"""Returns input tensor view as numpy array of shape (height, width, 3)."""
tensor_index = input_details(interpreter, 'index')
return interpreter.tensor(tensor_index)()[0]
def output_tensor(interpreter, dequantize=True):
"""Returns output tensor of classification model.
Integer output tensor is dequantized by default.
Args:
interpreter: tflite.Interpreter;
dequantize: bool; whether to dequantize integer output tensor.
Returns:
Output tensor as numpy array.
"""
output_details = interpreter.get_output_details()[0]
output_data = np.squeeze(interpreter.tensor(output_details['index'])())
if dequantize and np.issubdtype(output_details['dtype'], np.integer):
scale, zero_point = output_details['quantization']
return scale * (output_data - zero_point)
return output_data
def set_input(interpreter, data):
"""Copies data to input tensor."""
input_tensor(interpreter)[:, :] = data
def get_output(interpreter, top_k=1, score_threshold=0.0):
"""Returns no more than top_k classes with score >= score_threshold."""
scores = output_tensor(interpreter)
classes = [
Class(i, scores[i])
for i in np.argpartition(scores, -top_k)[-top_k:]
if scores[i] >= score_threshold
]
return sorted(classes, key=operator.itemgetter(1), reverse=True)
![Smile :-)](./images/smilies/icon_smile.gif)
Grúße Jörn