日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

list python 转tensor_Tensorflow模型量化4 --pb转tflite(uint8量化)小结

發布時間:2023/12/15 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 list python 转tensor_Tensorflow模型量化4 --pb转tflite(uint8量化)小结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Tensorflow模型量化4 --pb轉tflite小結(uint8量化)

  • 實驗環境:tensorflow-gpu1.15+cuda10.0
  • 模型的fp16量化和int8量化我之前有寫,參考:

    龜龜:Tensorflow模型量化實踐2--量化自己訓練的模型?zhuanlan.zhihu.com

    這次發現uint8量化時有參數設置,所以準備是從頭再梳理一遍

    2.參與量化的模型:

    訓練tensorflow-object-detection API 得到的ssdlite_mobilenet _v2模型,導出為frozen_inference_graph.pb

    3.獲取輸入輸出節點

    進行frozen_inference_graph.pb模型解析,得到輸入輸出節點信息

    代碼入下:

    """ code by zzg """ import tensorflow as tf import os os.environ["CUDA_VISIBLE_DEVICES"] = "0" config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session() as sess:with open('frozen_inference_graph_resnet.pb','rb') as f:graph_def = tf.GraphDef()graph_def.ParseFromString(f.read())tf.import_graph_def(graph_def, name='')tensor_name_list = [tensor.name for tensor in tf.get_default_graph().as_graph_def().node]for tensor_name in tensor_name_list:print(tensor_name,'n')

    之后找到輸入節點在預處理之后入下所示:

    找到輸出節點在后處理之前,如下圖所示:

    4.量化(pb->tflite)

    4.1方法一:利用TFLiteConverter

    ''' code by zzg 2020-04-27 ''' import tensorflow as tf import os os.environ["CUDA_VISIBLE_DEVICES"] = "0" config = tf.ConfigProto() config.gpu_options.allow_growth = True graph_def_file = "frozen_inference_graph.pb"input_names = ["FeatureExtractor/MobilenetV2/MobilenetV2/input"] output_names = ["concat", "concat_1"] input_tensor = {input_names[0]:[1,300,300,3]}#uint8 quant converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, input_names, output_names, input_tensor) converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,tf.lite.OpsSet.SELECT_TF_OPS] converter.allow_custom_ops=Trueconverter.inference_type = tf.uint8 #tf.lite.constants.QUANTIZED_UINT8 input_arrays = converter.get_input_arrays() converter.quantized_input_stats = {input_arrays[0]: (127.5, 127.5)} # mean, std_dev converter.default_ranges_stats = (0, 255)tflite_uint8_model = converter.convert() open("uint8.tflite", "wb").write(tflite_uint8_model)

    4.2方法二:利用TOCO

    toco --graph_def_file ./frozen_inference_graph.pb --output_file test.tflite --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --inference_type=QUANTIZED_UINT8 --input_shape='1,300,300,3' --input_array='FeatureExtractor/MobilenetV2/MobilenetV2/input' --output_array='concat,concat_1' --std_dev_value 127.5 --mean_value 127.5 --default_ranges_min 0 --default_ranges_max 255

    補充重點:uint8量化時的參數設置

    01.由于是進行uint8量化,所以輸出范圍為[0,255]

    即default_ranges_min =0,default_ranges_max=255

    02.std_dev_value和mean_value參數

    參考:https://www.cnblogs.com/sdu20112013/p/11960552.html

    結論:
    訓練時模型的輸入tensor的值在不同范圍時,對應的mean_values,std_dev_values分別如下:

    • range (0,255) then mean = 0, std_dev = 1
    • range (-1,1) then mean = 127.5, std_dev = 127.5
    • range (0,1) then mean = 0, std_dev = 255

    我查看了我的輸入tensor范圍是[-1,1], 所以我設置參數為 mean = 127.5, std_dev = 127.5

    5.tflite測試

    在轉換完成后,進行tflie解析測試,驗證最后轉換成功。

    代碼入下:

    ''' code by zzg 2020-04-30 ''' import tensorflow as tf import numpy as np InputSize = 300def test_tflite(input_test_tflite_file):interpreter = tf.lite.Interpreter(model_path = input_test_tflite_file)tensor_details = interpreter.get_tensor_details()for i in range(0,len(tensor_details)):# print("tensor:", i, tensor_details[i])interpreter.allocate_tensors()input_details = interpreter.get_input_details()print("=======================================")print("input :", str(input_details))output_details = interpreter.get_output_details()print("ouput :", str(output_details))print("=======================================")new_img = np.random.uniform(0,1,(1,InputSize,InputSize,3))# image_np_expanded = np.expand_dims(new_img, axis=0)new_img = new_img.astype('uint8')# 類型也要滿足要求interpreter.set_tensor(input_details[0]['index'],new_img)# 注意注意,我要調用模型了interpreter.invoke()output_data = interpreter.get_tensor(output_details[0]['index'])print("test_tflite finish!")intput_tflite_file = "uint8.tflite" test_tflite(intput_tflite_file)

    最后顯示如下:

    補充:獲取輸入輸出節點的話采用神經網絡模型可視化工具Netron更加方便直觀

    參考:

    模型結構可視化神器--Netron(支持tf, caffe, keras,mxnet等多種框架)?blog.csdn.net輕量好用的神經網絡模型可視化工具netron_網絡_Mingyong_Zhuang的技術博客-CSDN博客?blog.csdn.net

    安裝比較簡單:windows直接安裝.exe文件,linux直接 pip install netron即可

    總結

    以上是生活随笔為你收集整理的list python 转tensor_Tensorflow模型量化4 --pb转tflite(uint8量化)小结的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。