我正在使用ImageNet验证数据集来测试那些经过量化的预训练的量化移动网络模型是否实际上可以具有tensorflow自述文件(https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/models.md#image-classification-quantized-models)中指示的确切精度。但是,在对imagenet验证数据集进行测试之后,我感觉我的val精度比上面的tensorflow自述文件中显示的精度低得多。我认为我的代码是否做错了什么?另外,自述文件还说他们使用单幅图像裁剪来测试这些val精度,因此使用了opencv并裁剪了图像的对象,并将图像的大小调整为(224,224,3)。但是,我的top-5精度仅为70%,top-1精度大约为50%,但自述文件说,val top-5的实际精度可以为86%,top-1的实际精度为66%。 (我测试了模型Mobilenet_V1_0.75_224_quant和Mobilenet_V1_0.50_224_quant)。 这是我的代码:
if __name__ == '__main__':
# Load TFLite model and allocate tensors.
interpreter = tf.contrib.lite.Interpreter(model_path=
"/mnt/ficusspain/cqli/tensorflow_models/Quantized_Models/mobilenet_v1_0.5_224_quant/mobilenet_v1_0.5_224_quant.tflite")
interpreter.allocate_tensors()
print("can we get here?")
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print("can we get here")
# Test model on random input data.
input_shape = input_details[0]['shape']
input_index = input_details[0]['index']
output_index = output_details[0]['index']
print(input_shape)
print(input_index)
print(output_index)
with open("/mnt/ficusspain/cqli/tensorflow_models/Quantized_Models/image_to_labels_224.list", 'r') as f:
content = f.readlines()
contents = [x.strip() for x in content]
total_count = 0
top_1_acc_count = 0
top_5_acc_count = 0
for i in range(len(contents)):
total_count += 1
array = str(contents[i]).split()
image_path = array[0]
image_label = array[1]
color_img = cv2.imread(image_path)
color_img = np.expand_dims(color_img, axis=0)
color_img.astype(np.uint8)
interpreter.set_tensor(input_index, color_img)
interpreter.invoke()
prediction = interpreter.get_tensor(output_index)
top_5 = np.argsort(prediction[0])[::-1][0:5]
for j in range(len(top_5)):
if int(top_5[j]) == int(image_label):
top_5_acc_count += 1
break
if int(top_5[0]) == int(image_label):
top_1_acc_count += 1
if total_count % 100 == 0:
print(total_count)
print("the final top-1 accuracy is : " + str(top_1_acc_count/total_count))
print("the final top-5 accuracy is : " + str(top_5_acc_count/total_count))
其中从tensorflow下载mobilenet_v1_0.5_224_quant.tflite,并且它是经过预训练和转换的tflite模型。另外,image_to_labels_224.list包含imagenet的验证图像的路径及其标签。我只是按照本教程(https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/tutorials/post_training_quant.ipynb)来编写和测试这些模型,但事实证明,我无法达到与tensorflow文档所示相同的准确性。再次,通过使用图像的opencv裁剪对象并将这些裁剪后的图像调整为(224,224,3)大小来完成预处理后的图像。我不确定该怎么做才能达到与tensorflow自述文件所示相同的精度。有人可以帮我吗?非常感谢你!