繁体   English   中英

tensorflow float32的转换与java对象不兼容

[英]Conversion of tensorflow float32 is incompatible with a java object

错误:引起:java.lang.IllegalArgumentException:无法在类型为FLOAT32的TensorFlowLite张量和类型为java.lang.String的Java对象(与TensorFlowLite类型STRING兼容)之间进行转换。

我已经从我的数据集构建了一个神经网络,并且有2层,然后我将模型保存为h5,然后使用tf.keras模型和转换将其转换为tflite但是当我在应用程序中部署它时它会给我上面的错误

我尝试输入很多类型的数组和数组列表

错误:引起:java.lang.IllegalArgumentException:无法在类型为FLOAT32的TensorFlowLite张量和类型为java.lang.String的Java对象(与TensorFlowLite类型STRING兼容)之间进行转换。

model.add(layers.Dense(500, input_dim=3, activation='relu'))
model.add(layers.Dense(1, activation= "relu"))
model.summary() #Print model Summary
model.compile(loss='mean_squared_error',optimizer='adam')
model.fit(X_train,Y_train,epochs=1000,validation_split=0.3)

我如何转换: -

from tensorflow.contrib import lite
converter = lite.TFLiteConverter.from_keras_model_file( 'Model.h5')
tfmodel = converter.convert()
open ("model.tflite" , "wb") .write(tfmodel)

实现到android

ArrayList<String> list = new ArrayList<>();
list.add("-0.5698444");
list.add("-0.57369368");
list.add("-1.31490297");






try (Interpreter interpreter = new Interpreter(mappedByteBuffer)) {
    interpreter.run(list, "output");
}


private MappedByteBuffer loadModelFile() throws IOException {
    AssetFileDescriptor fileDescriptor = getAssets().openFd("model.tflite");
    FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
    FileChannel fileChannel = inputStream.getChannel();
    long startOffset = fileDescriptor.getStartOffset();
    long declaredLength = fileDescriptor.getDeclaredLength();
    return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}

发现了错误。 在线,

try (Interpreter interpreter = new Interpreter(mappedByteBuffer)) {
    interpreter.run(list, "output");
 }

interpreter.run()的第二个参数必须是float[]而不是"output" 当模型运行时, float[]将填充类概率。

interpreter.run()方法提供输入和输出的正确方法:

Interpreter interpreter = new Interpreter(mappedByteBuffer)

float[][] inputs = new float[1][num_features]
// populate the inputs float array above
float[][] outputs = new float[1][num_classes]

interpreter.run( inputs , outputs )

float[] classProb = outputs[0]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM