我正在尝试在Flutter上使用MobileNetV2模型实现对象检测。由于Flutter应用程序在线上可用的大多数示例或实现都没有使用MobileNetV2,因此我走了很长一段路才能到达那个阶段。
我实现这一目标的方法如下:
1)创建了一个python脚本,在这里我使用Keras(后端Tensorflow)的MobileNetV2模型(在ImageNet上进行了1000个类的预训练),并对其进行了图像测试,以查看在正确检测对象之后它是否返回正确的标签。 [下面提供的Python脚本供参考]
2)将相同的MobileNetV2 keras模型(MobileNetV2.h5)转换为Tensorflow Lite模型(MobileNetV2.tflite)
3)遵循了创建Flutter应用程序以使用Tensorflow Lite(https://itnext.io/working-with-tensorflow-lite-in-flutter-f00d733a09c3)的现有示例。将示例中显示的TFLite模型替换为MobileNetV2.tflite模型,并使用https://gist.github.com/aaronpolhamus/964a4411c0906315deb9f4a3723aac57中的ImageNet类/标签作为labels.txt。 [此处提供了Flutter示例的GitHub项目:https://github.com/umair13adil/tensorflow_lite_flutter]
当我现在运行Flutter应用程序时,它正在运行,没有任何错误,但是在分类/预测标签期间,输出不正确。例如:将橙色(对象ID:n07747607)分类为雨披(对象ID:n03980874),将石榴(对象ID:n07768694)分类为banded_gecko(对象ID:n01675722)。
但是,如果我使用相同的图片并使用python脚本对其进行测试,则它将返回正确的标签。因此,我想知道问题是否出在Flutter应用程序中使用的label.txt(包含标签)上,其中标签的顺序与模型的推论不匹配。
谁能提及我如何解决该问题以对正确的对象进行分类?如何获取MobileNetV2(keras)使用的ImageNet标签,以便可以在Flutter应用程序中使用它?
我的Flutter应用程序可以使用MobileNetv2检测物体,可以从此处下载:https://github.com/somdipdey/Tensorflow_Lite_Object_Detection_Flutter_App
我的python脚本将MobileNetV2模型(keras)转换为TFLite,同时在图像上对其进行测试以进行分类,如下所示:
import tensorflow as tf
from tensorflow import keras
from keras.preprocessing import image
from keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
import numpy as np
import PIL
from PIL import Image
import requests
from io import BytesIO
# load the model
model = tf.keras.applications.MobileNetV2(weights='imagenet', include_top=True)
#model = tf.keras.models.load_model('MobileNetV2.h5')
# To save model
model.save('MobileNetV2.h5')
# chose the URL image that you want
URL = "https://images.unsplash.com/photo-1557800636-894a64c1696f?ixlib=rb-1.2.1&w=1000&q=80"
# get the image
response = requests.get(URL)
img = Image.open(BytesIO(response.content))
# resize the image according to each model (see documentation of each model)
img = img.resize((224, 224))
##############################################
# if you want to read the image from your PC
#############################################
# img_path = 'myimage.jpg'
# img = image.load_img(img_path, target_size=(299, 299))
#############################################
# convert to numpy array
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
# return the top 10 detected objects
num_top = 10
labels = decode_predictions(features, top=num_top)
print(labels)
#load keras model
new_model= tf.keras.models.load_model(filepath="MobileNetV2.h5")
# Create a converter # I could also directly use keras model instead of loading it again
converter = tf.lite.TFLiteConverter.from_keras_model(new_model)
# Convert the model
tflite_model = converter.convert()
# Create the tflite model file
tflite_model_name = "MobileNetV2.tflite"
open(tflite_model_name, "wb").write(tflite_model)
答案 0 :(得分:0)
让我从共享JSON和txt两种格式的ImageNet标签开始。鉴于MobileNetV2是在ImageNet上进行训练的事实,它应该基于这些标签返回结果。
我最初的想法是管道的第二步一定有错误。我假设您正在尝试将经过训练的基于Keras的权重转换为Tensorflow Lite权重(与纯Tensorflow的格式相同吗?)一个不错的选择是尝试以Tensorflow Lite的格式查找已经保存的权重(但是我想它们可能不可用,这就是为什么要进行转换)。我在将TF权重转换为Keras时也遇到了类似的问题,因此您必须确定转换是否已成功完成,甚至在进行第3步(创建Flutter应用以使用Tensorflow Lite之前)之前。实现此目的的一种好方法是打印分类器的所有可用类,并将它们与上面给出的原始ImageNet标签进行比较。