标签编号 3 与轴 1 上的形状不匹配
Posted
技术标签:
【中文标题】标签编号 3 与轴 1 上的形状不匹配【英文标题】:Label number 3 mismatch the shape on axis 1 【发布时间】:2021-12-09 18:34:46 【问题描述】:我目前正在尝试使用 TensorFlow Lite 模型创建图像分类 android 应用。当我打开 Android 应用程序并尝试执行分类时,我不断收到此错误消息
java.lang.IllegalArgumentException:标签号 3 与轴 1 上的形状不匹配
这是我的标签文件中的内容
0 A
1 B
2 C
这是我的 Classifier 类的代码:
package com.ukzn.signchat;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.Image;
import android.util.Log;
import androidx.camera.core.ImageProxy;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.Interpreter;
import org.tensorflow.lite.support.common.FileUtil;
import org.tensorflow.lite.support.common.TensorProcessor;
import org.tensorflow.lite.support.common.ops.NormalizeOp;
import org.tensorflow.lite.support.image.ImageProcessor;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.support.image.ops.ResizeOp;
import org.tensorflow.lite.support.image.ops.ResizeWithCropOrPadOp;
import org.tensorflow.lite.support.image.ops.Rot90Op;
import org.tensorflow.lite.support.label.TensorLabel;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.util.List;
import java.util.Map;
public class Classifier
private Context context;
Interpreter tflite;
final String ASSOCIATED_AXIS_LABELS = "labels.txt";
List<String> associatedAxisLabels = null;
public Classifier(Context context)
this.context = context;
// load labels to a List<String>
try
associatedAxisLabels = FileUtil.loadLabels(context, ASSOCIATED_AXIS_LABELS);
catch (IOException e)
Log.e("tfliteSupport", "Error reading label file", e);
// load model to interpreter
try
MappedByteBuffer tfliteModel = FileUtil.loadMappedFile(context, "model.tflite");
tflite = new Interpreter(tfliteModel);
catch (IOException e)
Log.e("tfliteSupport", "Error reading model", e);
public String classify(ImageProxy image)
@SuppressLint("UnsafeExperimentalUsageError")
Image img = image.getImage();
Bitmap bitmap = Utils.toBitmap(img);
int rotation = Utils.getImageRotation(image);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int size = height > width ? width : height;
ImageProcessor imageProcessor = new ImageProcessor.Builder()
.add(new ResizeWithCropOrPadOp(size, size))
.add(new ResizeOp(224, 224, ResizeOp.ResizeMethod.BILINEAR)) // changed from 128x128
.add(new Rot90Op(rotation))
.build();
TensorImage tensorImage = new TensorImage(DataType.UINT8);
tensorImage.load(bitmap);
tensorImage = imageProcessor.process(tensorImage);
TensorBuffer probabilityBuffer = TensorBuffer.createFixedSize(new int[]1, 224, 224, 3, DataType.UINT8);
if (null != tflite)
tflite.run(tensorImage.getBuffer(), probabilityBuffer.getBuffer());
TensorProcessor probabilityProcessor = new TensorProcessor.Builder().add(new NormalizeOp(0, 255)).build();
String result = "";
if (null != associatedAxisLabels)
// Map of labels and their corresponding probability
TensorLabel labels = new TensorLabel(associatedAxisLabels, probabilityProcessor.process(probabilityBuffer));
// Create a map to access the result based on label
Map<String, Float> floatMap = labels.getMapWithFloatValue();
result = Utils.writeResults(floatMap);
return result;
【问题讨论】:
【参考方案1】:分类器可能基于 MobileNet 标签格式,它要求标签从 1 开始。由于您有 0、1、2 并且它忽略了 0,所以它找不到 3。
【讨论】:
以上是关于标签编号 3 与轴 1 上的形状不匹配的主要内容,如果未能解决你的问题,请参考以下文章
Altium designer 同一个工程中怎么把两个原理图的相同网络编号关联起来?