簡體   English   中英

將 tf model 轉換為 TFlite model 時出錯

[英]Error when converting a tf model to TFlite model

我目前正在構建一個 model 以將其用於我的 nano 33 BLE 傳感板上,通過測量濕度、壓力、溫度來預測天氣,我有 5 個類。 我使用了一個 kaggle 數據集來訓練它。

    df_labels = to_categorical(df.pop('Summary'))
    df_features = np.array(df)
    
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(df_features, df_labels, test_size=0.15)
    
    normalize = preprocessing.Normalization()
    normalize.adapt(X_train)
    
    
    activ_func = 'gelu'
    model = tf.keras.Sequential([
                 normalize,
                 tf.keras.layers.Dense(units=6, input_shape=(3,)),
                 tf.keras.layers.Dense(units=100,activation=activ_func),
                 tf.keras.layers.Dense(units=100,activation=activ_func),
                 tf.keras.layers.Dense(units=100,activation=activ_func),
                 tf.keras.layers.Dense(units=100,activation=activ_func),
                 tf.keras.layers.Dense(units=5, activation='softmax')
    ])
    
    model.compile(optimizer='adam',#tf.keras.optimizers.Adagrad(lr=0.001),
                 loss='categorical_crossentropy',metrics=['acc'])
    model.summary()
    model.fit(x=X_train,y=y_train,verbose=1,epochs=15,batch_size=32, use_multiprocessing=True)

然后訓練 model,我想將其轉換為 tflite model,當我運行命令轉換時,我收到以下消息:

    # Convert the model to the TensorFlow Lite format without quantization
    converter = tf.lite.TFLiteConverter.from_keras_model(model)
    tflite_model = converter.convert()
    
    # Save the model to disk
    open("gesture_model.tflite", "wb").write(tflite_model)
      
    import os
    basic_model_size = os.path.getsize("gesture_model.tflite")
    print("Model is %d bytes" % basic_model_size)




    <unknown>:0: error: failed while converting: 'main': Ops that can be supported by the flex runtime (enabled via setting the -emit-select-tf-ops flag):
        tf.Erf {device = ""}

為了您的信息,我使用 google colab 來設計 model。

如果有人對此問題有任何想法或解決方案,我將很高興聽到!

當您沒有設置轉換器支持的操作時,通常會發生這種情況。

這是一個例子:

import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)

converter.target_spec.supported_ops = [
  tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
  tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]

tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

此支持的操作列表不斷變化,因此如果仍然出現錯誤,您還可以嘗試設置實驗轉換器功能,如下所示:

converter.experimental_new_converter = True

我解決了這個問題。 TFlite 尚不支持激活 function 'gelu'。 我將其更改為“relu”,不再有問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM