Возникла проблема при загрузке преобразованной модели TFLite в приложение для Android.
Модель: Deeplabv3 Mobilenetv2 (Обучен на Pascal VOC) Версия TFLite: 1.10
Использовал tflite_convert для преобразования pb-файла в tflite.(Версия Tensorflow: 1.11.0)
Код:
private MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename)
throws IOException {
AssetFileDescriptor fileDescriptor = assets.openFd(modelFilename);
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);
}
/**
* Initializes a native TensorFlow session for classifying images.
*
* @param assetManager The asset manager to be used to load assets.
* @param modelFilename The filepath of the model GraphDef protocol buffer.
*/
public static Segmentor create(
final AssetManager assetManager,
final String modelFilename,
final String labelFilename,
final int inputWidth,
final int inputHeight,
final int numClass, final int numOutput) throws IOException {
final TFLiteObjectSegmentationAPIModel d = new TFLiteObjectSegmentationAPIModel();
d.inputWidth = inputWidth;
d.inputHeight = inputHeight;
d.numClass = numClass;
try {
d.tfLite = new Interpreter(d.loadModelFile(assetManager, modelFilename));
d.tfLite.setNumThreads(4);
} catch (Exception e) {
throw new RuntimeException(e);
}
Ошибка:
E/tensorflow: CameraActivity: Exception!
java.lang.RuntimeException: java.lang.IllegalArgumentException: ByteBuffer is not a valid flatbuffer model
at org.tensorflow.demo.segmentation.TFLiteObjectSegmentationAPIModel.create(TFLiteObjectSegmentationAPIModel.java:93)
at org.tensorflow.demo.SegmentorActivity.onPreviewSizeChosen(SegmentorActivity.java:112)
at org.tensorflow.demo.CameraActivity.onPreviewFrame(CameraActivity.java:122)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1266)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Изменение версии tenorflow-lite на 'org.tensorflow :ensorflow-lite: 0.0.0-nightly 'в build.gradle, получается следующая ошибка
E/AndroidRuntime: FATAL EXCEPTION: front
Process: me.tantara.real_time_segmentation, PID: 17205
java.lang.IllegalArgumentException: Internal error: Failed to run on the given Interpreter: tensorflow/lite/kernels/depthwise_conv.cc:99 params->depth_multiplier * SizeOfDimension(input, 3) != SizeOfDimension(filter, 3) (0 != 32)Node number 30 (DEPTHWISE_CONV_2D) failed to prepare.
at org.tensorflow.lite.NativeInterpreterWrapper.run(Native Method)
at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:145)
at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:229)
at org.tensorflow.lite.Interpreter.run(Interpreter.java:208)
at org.tensorflow.demo.segmentation.TFLiteObjectSegmentationAPIModel.segmentImage(TFLiteObjectSegmentationAPIModel.java:146)
at org.tensorflow.demo.SegmentorActivity$3.run(SegmentorActivity.java:221)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.os.HandlerThread.run(HandlerThread.java:61)