看板 DataScience 關於我們 聯絡資訊
各位大大 試著把h5的檔案轉為 int8 的 tflite 我想釐清轉換後權重與scale與zero point的關係,但遇到了一些問題,我的模型很簡單 ,如下 tf.keras.layers.Conv2D(16, (5,5), input_shape=(width,height, 1), activati on='relu', padding='same') tf.keras.layers.GlobalAveragePooling2D() tf.keras.layers.Dense(3, activation='softmax') 我用以下代碼試著inference一張圖片 interpreter = tf.lite.Interpreter(model_path="test.tflite") interpreter.allocate_tensors() input_index = interpreter.get_input_details()[0]["index"] output_index = interpreter.get_output_details()[0]["index"] input_scale, input_zero_point = interpreter.get_input_details()[0]['quantizati on'] image_ = image_input / input_scale + input_zero_point interpreter.set_tensor(input_index, image_.astype(np.int8)) interpreter.invoke() interpreter.tensor(output_index)() 得到結果array([[-123, 77, -82]], dtype=int8),這應該是符合預期的分類結果 我想試著看 tflite 每一層中的關係,想先從GlobalAveragePooling2D著手 經過conv2後得到一個80*80*16的矩陣以及經過GAV計算後得到一個1*16的矩陣,如下 https://i.imgur.com/Zq9OZST.jpg
依我對權重與scale與zero point的理解,我的計算如下 https://i.imgur.com/JyBw6nv.jpg
但結果似乎跟tflite真正計算的結果有很大的差異 這是我自己計算的結果 array([[ 63, 12, 8, 31, 67, 32, 16, 16, 34, 2, 51, -15, 66, 43, 64, 5]], dtype=int8) 這是tflite計算的結果 array([[-123, 77, -82, 32, 67, 32, 17, 16, 35, 2, 52, -15, 67, 44, 65, 5]], dtype=int8) 不知道是不是我哪裡忽略算錯了呢 感謝!! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.161.29.180 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/DataScience/M.1649204986.A.2AF.html
chang1248w: 為啥不全丟截圖... 04/09 19:08
jack155861: 抱歉 因為最重點的就是 這兩張截圖的計算啦 04/09 21:08
chang1248w: 前三個數字發生什麼事我沒有頭緒,後面幾個的差異是 04/11 20:08
chang1248w: 因為你直接把tensor 壓進int裡 04/11 20:08
chang1248w: 印象中不管是tf還是np都是做無條件捨去 04/11 20:08
chang1248w: 中間補個round吧 04/11 20:09