【问题标题】:post training quantization for mobilenet V1 not workingmobilenet V1的训练后量化不起作用
【发布时间】:2019-09-10 10:45:35
【问题描述】:

我正在尝试将 mobilenet V1 .pb 文件转换为量化的 tflite 文件。我使用以下命令进行量化:

  tflite_convert \
  --output_file=/home/wc/users/Mostafiz/TPU/models/mobilnet/test2_4thSep/mobilenetv1_test5.tflite \
  --graph_def_file=/home/wc/users/Mostafiz/TPU/models/mobilnet/mobileNet_frozen_graph.pb \
  --output_format=TFLITE \
  --inference_type=QUANTIZED_UINT8 \
  --inference_input_type=QUANTIZED_UINT8 \
  --input_shape=1,224,224,3 \
  --input_array=input \
  --output_array=MobilenetV1/Predictions/Reshape_1 \
  --inference_output_type=QUANTIZED_UINT8 \
  --default_ranges_min=0 \
  --default_ranges_max=6 \
  --std_dev_values=127 \
  --mean_value=128

.tflile 文件创建时没有任何错误。但是当我尝试使用 .tflile 进行推理时,输出类就搞砸了。没有一个测试图像给出正确的结果。

不知道我哪里做错了,有人可以帮助我吗?

为了推断,我使用的是 tensorflow 提供的“label_image.py”。这是代码:

"""label_image for tflite"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import numpy as np

from PIL import Image

from tensorflow.lite.python import interpreter as interpreter_wrapper

def load_labels(filename):
  my_labels = []
  input_file = open(filename, 'r')
  for l in input_file:
    my_labels.append(l.strip())
  return my_labels

if __name__ == "__main__":
  floating_model = False

  parser = argparse.ArgumentParser()
  parser.add_argument("-i", "--image", default="/tmp/grace_hopper.bmp", \
    help="image to be classified")
  parser.add_argument("-m", "--model_file", \
    default="/tmp/mobilenet_v1_1.0_224_quant.tflite", \
    help=".tflite model to be executed")
  parser.add_argument("-l", "--label_file", default="/tmp/labels.txt", \
    help="name of file containing labels")
  parser.add_argument("--input_mean", default=127.5, help="input_mean")
  parser.add_argument("--input_std", default=127.5, \
    help="input standard deviation")
  args = parser.parse_args()

  interpreter = interpreter_wrapper.Interpreter(model_path=args.model_file)
  interpreter.allocate_tensors()

  input_details = interpreter.get_input_details()
  output_details = interpreter.get_output_details()

  # check the type of the input tensor
  if input_details[0]['dtype'] == np.float32:
    floating_model = True

  # NxHxWxC, H:1, W:2
  height = input_details[0]['shape'][1]
  width = input_details[0]['shape'][2]
  img = Image.open(args.image)
  img = img.resize((width, height))

  # add N dim
  input_data = np.expand_dims(img, axis=0)

  if floating_model:
    input_data = (np.float32(input_data) - args.input_mean) / args.input_std

  interpreter.set_tensor(input_details[0]['index'], input_data)

  interpreter.invoke()

  output_data = interpreter.get_tensor(output_details[0]['index'])
  results = np.squeeze(output_data)

  top_k = results.argsort()[-5:][::-1]
  labels = load_labels(args.label_file)
  for i in top_k:
    if floating_model:
      print('{0:08.6f}'.format(float(results[i]))+":", labels[i])
    else:
      print('{0:08.6f}'.format(float(results[i]/255.0))+":", labels[i])

谢谢。

【问题讨论】:

  • 模型未量化时结果是否令人满意?
  • @Shubham 是的,.pb 文件给出了完美的结果,事实上,如果我将其转换为 tflite 而不进行量化,它就可以工作。
  • 量化有助于减小模型的大小,但其准确性会受到影响。
  • 是的,我同意,但这并不意味着准确率是 0%,我的测试图像都没有给出正确的答案,所以出了点问题。不知道是什么
  • 您是否在此处尝试过训练后量化:tensorflow.org/lite/performance/post_training_integer_quant

标签: tensorflow quantization uint8t


【解决方案1】:

虚拟量化可能无法正常工作,因为我们需要猜测激活函数的 default_max 和 defual_min 值。

正如 Sudarsh 在评论中提到的,我们应该做一篇训练全整数量化以将 .pb 转换为 INT8 tflite 文件的帖子。

您可以点击此链接开始 - here

希望有所帮助。

问候。

【讨论】:

    猜你喜欢
    • 2020-03-26
    • 2019-08-05
    • 1970-01-01
    • 2019-01-26
    • 2021-11-05
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 2019-06-20
    相关资源
    最近更新 更多