Closed staebchen0 closed 2 years ago
can you please share your CPU tflite model? edgetpu api
is deprecated, please try to rewrite the code using pycoral api
i know the edgetpu api is out of date, but with the pycoral api i get the error list index out of range
with this Test Script:
# 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.
"""A demo that runs object detection on camera frames using OpenCV.
TEST_DATA=../all_models
Run face detection model:
python3 detect.py \
--model ${TEST_DATA}/mobilenet_ssd_v2_face_quant_postprocess_edgetpu.tflite
Run coco model:
python3 detect.py \
--model ${TEST_DATA}/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite \
--labels ${TEST_DATA}/coco_labels.txt
"""
import argparse
import cv2
import os
from pycoral.adapters.common import input_size
from pycoral.adapters.detect import get_objects
from pycoral.utils.dataset import read_label_file
from pycoral.utils.edgetpu import make_interpreter
from pycoral.utils.edgetpu import run_inference
def main():
default_model_dir = './model/'
default_model = 'example_edetpu13.tflite'
default_labels = 'labels.txt'
parser = argparse.ArgumentParser()
parser.add_argument('--model', help='.tflite model path',
default=os.path.join(default_model_dir,default_model))
parser.add_argument('--labels', help='label file path',
default=os.path.join(default_model_dir, default_labels))
parser.add_argument('--top_k', type=int, default=1,
help='number of categories with highest score to display')
parser.add_argument('--camera_idx', type=int, help='Index of which video source to use. ', default = 0)
parser.add_argument('--threshold', type=float, default=0.1,
help='classifier score threshold')
args = parser.parse_args()
print('Loading {} with {} labels.'.format(args.model, args.labels))
interpreter = make_interpreter(args.model)
interpreter.allocate_tensors()
labels = read_label_file(args.labels)
inference_size = input_size(interpreter)
cap = cv2.VideoCapture(args.camera_idx)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
cv2_im = frame
cv2_im_rgb = cv2.cvtColor(cv2_im, cv2.COLOR_BGR2RGB)
cv2_im_rgb = cv2.resize(cv2_im_rgb, inference_size)
run_inference(interpreter, cv2_im_rgb.tobytes())
#objs = get_objects(interpreter, args.threshold)[:args.top_k]
objs = model.ClassifyWithImage(frame, top_k=1)
cv2_im = append_objs_to_img(cv2_im, inference_size, objs, labels)
cv2.imshow('frame', cv2_im)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
def append_objs_to_img(cv2_im, inference_size, objs, labels):
height, width, channels = cv2_im.shape
scale_x, scale_y = width / inference_size[0], height / inference_size[1]
for obj in objs:
bbox = obj.bbox.scale(scale_x, scale_y)
x0, y0 = int(bbox.xmin), int(bbox.ymin)
x1, y1 = int(bbox.xmax), int(bbox.ymax)
percent = int(100 * obj.score)
label = '{}% {}'.format(percent, labels.get(obj.id, obj.id))
cv2_im = cv2.rectangle(cv2_im, (x0, y0), (x1, y1), (0, 255, 0), 2)
cv2_im = cv2.putText(cv2_im, label, (x0, y0+30),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), 2)
return cv2_im
if __name__ == '__main__':
main()
Code line: objs = get_objects (interpreter, args.threshold) [: args.top_k]
That's why I tried the edgetpu api
attached my model: example_edetpu13.zip
Hi get_objects
is for object detection models. we can't use it for classification models.
could you please first quantize the model and try classify_image.py
okay i'm one step further now. I have my model as mymodel_edgetpu.tflite created.
and then compile it with:
edgetpu_compiler -s -m 13 $ TFLITE_FILE
classify_image.py is running now :-)
Which example can I now use to test the classification with a camera?
Please try with classes = classify.get_classes(interpreter, args.top_k, args.threshold)
to get the outputs and add labels to the image..
This calssify_capture.py might be helpful.
Thanks a Million! It finally works under Win10 :-)
I hope the bug in the newest one
edgetpu_runtime_20210726.zip
is found quickly so that I can also use it with Windows 10.
that would have made a lot easier
Have a few minutes? We'd love your feedback about the Coral developer experience! Take our 5-minute survey.
while testing, I just found out that the classes are no longer recognized correctly!
Could that have happened when compiling on runtime version 13?
Please try the with some images detections and compare the results CPU tflite model vs edgetpu tflite model.
i already have that, the cpu tflite model gives 95% the right result
with the same pictures, the edgetpu model always brings the wrong class
aside from that, I only get two classes even though I set top_k to 4
parser.add_argument (
'-k', '--top_k', type = int, default = 4
Okay, please create new performance issue with the details below:
Thanks
Description
Since I use Windows 10, I unfortunately cannot use the current runtime, but have to use the version edgetpu_runtime_20210119.zip to use.
In order for this to work, I have to compile my model to 13 with the Edge TPU compiler
edgetpu_compiler -s -m 13 model_unquant_8020.tflite
new Mode: example_edetpu13.tfliteMy model is still not running
Test Code:
Failure:
what is the problem now?
Click to expand!
### Issue Type Build/Install ### Operating System Windows 10 ### Coral Device USB Accelerator ### Other Devices _No response_ ### Programming Language Python 3.7 ### Relevant Log Output _No response_