首页 > 解决方案 > 无论提供任何图像,TFLite 都会得到相同的结果

问题描述

我尝试在 Android TFLite 中部署它,但无论我提供任何图像,解释器总是向我发送相同的结果

我使用 tf.keras 构建了一个 FaceNet 模型并尝试将其部署在 Android TFLite 中,转换(从 tf.keras 到 tflite)的处理变得平滑。

我尝试使用 tflite python api 进行推理,它有效。但是当我尝试在 Android 中部署它时,无论我提供任何图像,解释器总是给我发送相同的结果。

Android中有推理代码。

// Init
Activity activity = (Activity) othersMap.get("activity");
modelFile = loadModelFile(activity);
tfLite = new Interpreter(modelFile, tfliteOptions);
useCPU();
imgData = ByteBuffer.allocateDirect(
                    DIM_BATCH_SIZE * DIM_IMG_SIZE_X * DIM_IMG_SIZE_Y * DIM_PIXEL_SIZE * 4);
imgData.order(ByteOrder.nativeOrder());
embeddingArray = new float[1][128];
// DetectImage
Bitmap bmp = Bitmap.createBitmap(src.width(), src.height(),
                    Bitmap.Config.ARGB_8888);
Utils.matToBitmap(src, bmp);
convertBitmapToByteBuffer(bmp);

tfLite.resizeInput(0, new int[]{1, 96, 96, 3});
tfLite.run(imgData, embeddingArray);
// Inference
float[][] embededFace = detector.detectImage(faceMat);
float[][] embededZN = detector.detectImage(znMat);
Log.e(TAG, "--------------");
Log.e(TAG, Arrays.deepToString(embededFace));
Log.e(TAG, Arrays.deepToString(embededZN));
Log.e(TAG, "--------------");

并且python中有推理代码,它可以工作。

img = cv.imread('zn.jpg')
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img = cv.resize(img, (96, 96), interpolation=cv.INTER_CUBIC)
img = np.around(img/255.0, decimals=12)
x_train = np.array([img]).astype(np.float32)

input_shape = input_details[0]['shape']
input_data = x_train
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)

使用tflite android inference api有结果(使用zn.jpg和jzx.jpg)

2019-03-26 15:01:46.346 11635-11823/org.blackwalnutlabs.angel.facedetection E/MainActivity: --------------
2019-03-26 15:01:46.347 11635-11823/org.blackwalnutlabs.angel.facedetection E/MainActivity: [[0.13254969, 0.06506284, -0.12454985, -0.033946857, 0.13087335, 0.24849732, 0.13556245, -0.04702322, -0.1379856, -0.007572789, 0.019669764, 0.012697782, 0.13016781, -0.08282088, 0.07000842, -0.13634653, -0.033497285, 0.06713182, -0.056609895, 0.13066444, 0.031595007, 0.010904907, 0.06502004, 0.055475105, -0.042641174, -0.11620673, -0.13694304, -0.1973542, 0.023469506, 0.105563894, -0.018650757, 0.03307536, -0.098828435, 0.08058539, 0.08887855, 0.0022337863, 0.0055133225, 0.05411017, -0.044450507, -0.082797125, 0.07151169, -0.068259895, -0.09238541, 0.051023263, -0.26672673, 0.062153224, 0.089065045, 0.1272812, -0.15051614, 0.093154915, -0.04973236, -0.055004556, 0.028005369, 9.352369E-4, 0.021919679, -0.019822104, -0.02774606, 0.12849025, -0.051899977, -0.08593918, -0.08694194, 0.10073202, 0.20436272, -0.19222663, 0.084071614, -0.011063285, 0.037152126, 0.03323839, -0.13506632, -0.026216896, 0.012508951, 0.06862544, -0.023205, 0.013072543, -0.031873662, 0.025892422, -0.061958347, -0.06703131, 0.10233366, 0.06788932, -0.02056665, 0.0018195248, -0.07498662, -0.05542643, -0.01115404, 0.009607625, 0.029644933, -0.020729594, 0.0023015668, 0.09378674, 0.18983097, -0.11676965, -0.01661709, -0.019705301, -0.11590064, -0.13303259, 0.0070525194, 0.056587018, 0.02837071, -0.045306873, 0.105889864, 0.0897621, 0.0022198055, 0.043863844, -0.16235375, 0.06855683, -0.04233673, 0.10779853, -0.019658986, 0.096174344, 0.14861028, 0.09041121, -0.03691038, -0.040842745, -0.044187892, 0.06624289, -0.13923372, -0.038001977, -0.018037193, 0.114912674, -7.4458803E-4, -0.122649185, -0.010618781, 0.11189667, -0.03145326, 0.036915697, -0.0388509, -0.096018195]]
2019-03-26 15:01:46.347 11635-11823/org.blackwalnutlabs.angel.facedetection E/MainActivity: [[0.13254969, 0.06506284, -0.12454985, -0.033946857, 0.13087335, 0.24849732, 0.13556245, -0.04702322, -0.1379856, -0.007572789, 0.019669764, 0.012697782, 0.13016781, -0.08282088, 0.07000842, -0.13634653, -0.033497285, 0.06713182, -0.056609895, 0.13066444, 0.031595007, 0.010904907, 0.06502004, 0.055475105, -0.042641174, -0.11620673, -0.13694304, -0.1973542, 0.023469506, 0.105563894, -0.018650757, 0.03307536, -0.098828435, 0.08058539, 0.08887855, 0.0022337863, 0.0055133225, 0.05411017, -0.044450507, -0.082797125, 0.07151169, -0.068259895, -0.09238541, 0.051023263, -0.26672673, 0.062153224, 0.089065045, 0.1272812, -0.15051614, 0.093154915, -0.04973236, -0.055004556, 0.028005369, 9.352369E-4, 0.021919679, -0.019822104, -0.02774606, 0.12849025, -0.051899977, -0.08593918, -0.08694194, 0.10073202, 0.20436272, -0.19222663, 0.084071614, -0.011063285, 0.037152126, 0.03323839, -0.13506632, -0.026216896, 0.012508951, 0.06862544, -0.023205, 0.013072543, -0.031873662, 0.025892422, -0.061958347, -0.06703131, 0.10233366, 0.06788932, -0.02056665, 0.0018195248, -0.07498662, -0.05542643, -0.01115404, 0.009607625, 0.029644933, -0.020729594, 0.0023015668, 0.09378674, 0.18983097, -0.11676965, -0.01661709, -0.019705301, -0.11590064, -0.13303259, 0.0070525194, 0.056587018, 0.02837071, -0.045306873, 0.105889864, 0.0897621, 0.0022198055, 0.043863844, -0.16235375, 0.06855683, -0.04233673, 0.10779853, -0.019658986, 0.096174344, 0.14861028, 0.09041121, -0.03691038, -0.040842745, -0.044187892, 0.06624289, -0.13923372, -0.038001977, -0.018037193, 0.114912674, -7.4458803E-4, -0.122649185, -0.010618781, 0.11189667, -0.03145326, 0.036915697, -0.0388509, -0.096018195]]
2019-03-26 15:01:46.347 11635-11823/org.blackwalnutlabs.angel.facedetection E/MainActivity: --------------

使用tflite python inference api有结果(使用zn.jpg和jzx.jpg)

[[ 0.11388897  0.0725093  -0.02605356 -0.01048064  0.13486011  0.2478467
   0.14440472 -0.08799922 -0.09042776 -0.01699087 -0.01304216 -0.00571998
   0.08452216  0.0200549   0.15689915 -0.19767451  0.01055939  0.04823723
  -0.0236162   0.03551559 -0.03713445  0.07919028  0.00384108  0.07863859
   0.01265915 -0.16044462 -0.1557556  -0.08875822 -0.00050363  0.09660906
   0.04116863  0.02890096 -0.13043067  0.05401503  0.11558161  0.04363808
   0.01493979 -0.02636928 -0.0312182  -0.06017392  0.11421517 -0.02729253
  -0.0982411  -0.03221652 -0.22682378  0.09350454  0.00194081  0.1348505
  -0.08131604  0.01773626 -0.02530179 -0.00231936  0.09337027 -0.03695107
   0.00894977  0.01499904 -0.08354656  0.15669909 -0.05902651 -0.01955242
  -0.09552951  0.14642344  0.10130911 -0.20985658  0.07511081  0.05583977
   0.08974755  0.03895326 -0.17041707  0.01303602  0.07159603  0.08509548
  -0.01078814 -0.01519361 -0.02482661  0.07998506 -0.06343063 -0.12341296
   0.10723062  0.0521314   0.00146189  0.02375979 -0.02791258 -0.04265117
   0.02753739 -0.0534174   0.04096378 -0.04856863 -0.07195448  0.1120934
   0.1552053  -0.09029299  0.07750953  0.00552343 -0.13981637 -0.13607475
   0.0221287  -0.03371112  0.04638224 -0.01829939  0.11707122  0.12880346
  -0.00621982 -0.00639387 -0.17476223  0.07756975 -0.02970012  0.00726423
  -0.0368601   0.13965365  0.17757982  0.02349343 -0.04115034 -0.03476189
  -0.06320279  0.04965549 -0.14846669  0.01701067 -0.05107493  0.10043616
   0.02471918 -0.14686176 -0.05839831  0.07405312  0.02949061  0.07611959
  -0.0424039  -0.09250365]]
[[ 0.07983646  0.18764469 -0.1073711  -0.05049998  0.00499822  0.12150028
   0.08042646 -0.01210295 -0.05991281 -0.04436503  0.05246051 -0.01235358
   0.08685836 -0.13629174 -0.02383062 -0.06484984 -0.04617283 -0.04979375
  -0.10104349  0.14407381  0.03841608 -0.02601014 -0.02829801  0.02454884
  -0.04299033 -0.00861871 -0.03450659 -0.12293942  0.09096454  0.08878913
  -0.03709067  0.05936524 -0.01129172  0.09354664  0.02958448 -0.03306518
   0.10459264  0.15117857 -0.02248681 -0.08084158  0.06213554 -0.16485777
   0.03646875  0.11903016 -0.11887202  0.1105961   0.177684    0.04797998
  -0.24575731  0.05616427 -0.06487465  0.02870647 -0.03513253  0.04599205
   0.09469782 -0.02022276 -0.11111834  0.03512118 -0.04118481 -0.17735724
  -0.08443499 -0.04586984  0.1915695  -0.09305634  0.01631605 -0.00732839
   0.05056106 -0.02879676 -0.07254668 -0.05057898 -0.05678427 -0.06486657
  -0.05519347  0.04775465  0.09962408 -0.04996048 -0.02798802  0.07482754
   0.05294487  0.09077998 -0.02322999  0.03212555 -0.07331605 -0.06148532
   0.02072088  0.03205773 -0.01099079 -0.01667416  0.06782145  0.12291647
   0.13593872 -0.18584044 -0.08280354 -0.04093247 -0.08853628 -0.08608698
   0.05808447  0.05453911  0.05205403 -0.11206362 -0.01309075  0.06881329
   0.01091391  0.12481726 -0.19396386  0.06527282 -0.06483501  0.23652825
   0.00085458 -0.05038064  0.10557921  0.1129869  -0.16408154 -0.00994272
  -0.00418024 -0.03208672 -0.13337857 -0.15857975  0.06981471  0.08215429
   0.03227568  0.02853267  0.06400527  0.15060182  0.0306827  -0.04676049
   0.0670058  -0.10612144]]

标签: tensorflowtensorflow-lite

解决方案


推荐阅读