tensorflow - 已解决 - Tensorflow Lite 模型:输入和输出数组的形状不兼容
问题描述
我目前正在开发一个可以识别 UNO 卡的 Tensorflow Lite 图像分类器应用程序。但是当我在ImageClassifier类中运行浮动模型时,出现了问题。
错误是下一个:
java.lang.IllegalArgumentException: Cannot copy from a TensorFlowLite tensor (Identity) with shape [1, 10647, 4] to a Java object with shape [1, 15].
这是引发该错误的代码:
tflite.run(imgData, labelProbArray);
这就是我创建 imgData 和 labelProbArray 的方式:
private static final int DIM_BATCH_SIZE = 1;
private static final int DIM_PIXEL_SIZE = 3; //r+g+b = 1+1+1
static final int DIM_IMG_SIZE_X = 416;
static final int DIM_IMG_SIZE_Y = 416;
imgData = ByteBuffer.allocateDirect(DIM_BATCH_SIZE * DIM_IMG_SIZE_X * DIM_IMG_SIZE_Y * DIM_PIXEL_SIZE * 4); //The last value because size of float is 4
labelProbArray = new float[1][labelList.size()]; // {1, 15}
我知道您应该为输出值创建一个缓冲区,但我尝试导入它但没有奏效:
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;
有任何想法吗?非常感谢您阅读我^^
编辑 v2:
感谢yyoon,我意识到我没有用metadata填充我的模型,所以我在我的 cmd 中运行了这一行:
python ./metadata_writer_for_image_classifier_uno.py \ --model_file=./model_without_metadata/custom.tflite \ --label_file=./model_without_metadata/labels.txt \ --export_directory=model_with_metadata
在此之前,我用我的数据修改了这个文件:
_MODEL_INFO = {
"custom.tflite":
ModelSpecificInfo(
name="UNO image classifier",
version="v1",
image_width=416,
image_height=416,
image_min=0,
image_max=255,
mean=[127.5],
std=[127.5],
num_classes=15)
}
又出现了一个错误:
ValueError: The number of output tensors (2) should match the number of output tensor metadata (1)
Idk 为什么我的模型有 2 个张量输出...
解决方案
那是因为模型不对。我只是尝试使用另一个模型,它可以工作。
解决方案
推荐阅读
- r - 保存 XGBoost 模型的工作流程以备后用
- python - 为tensorflow keras准备数据?
- time - 在 Julia 中测量时间和函数调用的行为
- php - 如何在 Laravel 中制作一个单词的通用变量
- mysql - 使用未定义的常量 DOMAIN - 在 Laravel 中假定为“DOMAIN”
- python - 是否有更好的矢量化解决方案来使用在第二个数据帧中定义为变量的索引和列写入数据帧?
- r - 用 N 倍分隔符分隔字符串
- mysql - 如何忽略具有空值的列并仅获取不为空的列
- python - 使用 BERT 通过词嵌入生成相似词或同义词
- ansible - Ansible 错误“无法创建临时目录”