Flutter TFLite模型一直输出相同的结果。

huangapple go评论61阅读模式
英文:

Flutter TFLite model keeps outputting the same result

问题

在Flutter中,你的输出一直为0.0的问题可能是因为数据类型不匹配。在Python中,你已经注意到需要将输入数据转换为float32类型,以便正确运行模型并获得0到1范围内的输出值。在Flutter中,你尝试使用Float32List来转换数据,但结果仍然为0.0。

要解决这个问题,确保在Flutter中的数据类型与模型期望的数据类型匹配。你可以尝试在Flutter中将输入数据转换为float32类型,类似于你在Python中的操作。另外,确保输入数据的范围和标准化与在Python中训练模型时使用的相同,这也可能会影响输出结果。

此外,还要确保Flutter中的输入数据格式与模型的输入层匹配,包括形状和顺序。

如果问题仍然存在,可能需要进一步检查Flutter中的推理过程和数据预处理,以确保每一步都正确无误。

英文:

I am building a CNN classification model using tensorflow and python. The model has an input shape of [1, 50, 7] consisting the first column of timestamp, and sensor values for the rest of the columns. The output value is either 0 or 1 to specify motion of left or right. Then, I export the model as TFLite model and used it in Flutter using the tflite_flutter package (https://pub.dev/packages/tflite_flutter).

When I run using interpreter run, the output of the data is always 0.0. However, when I run using python, I noticed that after reading a csv data, I needed to add

    input_data = input_data.astype('float32')

to properly run the model and it output a value in range of 0 to 1, which is what I wanted, or else it will output that it cannot get tensor due to getting FLOAT64 instead of FLOAT32.
So, I tried to convert my data into float32 using the Float32List in Flutter, but the result is still 0.0.

    List<Float32List> group32Float = [];
    for (var i = 0; i < 50; i++) {
       group32Float.add(Float32List.fromList(group[i]));
    }
    interpreter!.run([group32Float], [output]);

My model is as such:

    input_shape = (50, 7)

    model = Sequential()
    model.add(Conv1D(filters=32, kernel_size=3, activation='relu', padding='same', input_shape=input_shape))
    model.add(BatchNormalization())
    model.add(MaxPooling1D(pool_size=2))
    model.add(Dropout(0.25))
    model.add(Conv1D(filters=64, kernel_size=3, activation='relu', padding='same'))
    model.add(BatchNormalization())
    model.add(MaxPooling1D(pool_size=2))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(units=64, activation='relu', kernel_regularizer=regularizers.l2(0.001)))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))
    model.add(Dense(units=32, activation='relu', kernel_regularizer=regularizers.l2(0.001)))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))
    model.add(Dense(1, activation='sigmoid'))

    optimizer = Adam(learning_rate=0.001)
    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])

    early_stop = EarlyStopping(monitor='val_loss', patience=100)

    model.fit(X_train, y_train, epochs=1000, validation_data=(X_val, y_val), callbacks=[early_stop])

Then saved as TFLite:

    model.save('model', save_format='tf')

    converter = tf.lite.TFLiteConverter.from_saved_model('model')
    tflite_model = converter.convert()

    with open('model.tflite', 'wb') as f:
        f.write(tflite_model)

My question is: Why is my output in Flutter always 0.0?

答案1

得分: 0

我找到了解决方案。我意识到我最初没有将我的输入数据设置为正确的类型,对我来说应该是:

List<List<double>> input

尝试检查你的输入和输出变量类型,确保其是正确的类型以发送到模型中。

英文:

For anyone having trouble with the same problem, I've found a solution. I realized that I haven't initially set my input data into the correct type which in my case is:

List&lt;List&lt;double&gt;&gt; input

Try checking your variable type of input and output and make sure it's the correct type to send into the model.

huangapple
  • 本文由 发表于 2023年5月13日 12:25:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定