错误:原因: java.lang.IllegalArgumentException:无法在类型为FLOAT32的TensorFlowLite张量和类型为java.lang.String (与TensorFlowLite类型字符串兼容)的FLOAT32对象之间进行转换。
我已经从我的数据集构建了一个神经网络,并有两层,然后我将模型保存为h5,然后使用tf.keras模型和转换将其转换为tflite,但当我将其部署到应用程序中时,它给出了上面的错误
我尝试过输入很多类型的数组和数组列表
错误:原因: java.lang.IllegalArgumentException:无法在类型为FLOAT32的TensorFlowLite张量和类型为java.lang.String (与TensorFlowLite类型字符串兼容)的FLOAT32对象之间进行转换。
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);
}发布于 2019-05-29 13:29:32
找到错误了。在队伍中,
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]https://stackoverflow.com/questions/56323246
复制相似问题