我用tflite库为flutter做了一个分割,它工作得很好,我加载模型,输入RGB [3,224,224],并通过tflite_flutter_helper库的解释器运行它。
但是如何将我的模型[1,1,224,224]的输出转换回TensorImage或一般的Image?当我运行
TensorImage resultImage = TensorImage.fromTensorBuffer(tensorBuffer);
或
TensorImage resultImage = TensorImage(tensorBuffer.getDataType());
resultImage.loadTensorBuffer(tensorBuffer);
我得到错误消息:
The shape of a RGB image should be (h, w, c) or (1, h, w, c), and channels representing R, G, B in order. The provided image shape is [1, 224, 224, 1]
我试图通过将输出重新排列为(1,h,w,c)的形状来解决它,就像错误[1,224,224,1]中所示的那样,但我得到了相同的结果。以下是我的完整代码:
ImageProcessor imageProcessor = ImageProcessorBuilder()
.add(ResizeOp(224, 224, ResizeMethod.NEAREST_NEIGHBOUR))
.add(NormalizeOp(127.5, 127.5))
.build();
SequentialProcessor<TensorBuffer> probabilityProcessor = TensorProcessorBuilder().add(DequantizeOp(0, 1 / 255)).build();
TensorImage tensorImage = TensorImage(TfLiteType.float32);
tensorImage.loadImage(img.Image.fromBytes(224, 224, image.readAsBytesSync()));
tensorImage = imageProcessor.process(tensorImage);
TensorBuffer tensorBuffer;
try{
Interpreter interpreter = await Interpreter.fromAsset('models/enet.tflite');
tensorBuffer = TensorBuffer.createFixedSize(interpreter.getOutputTensor(0).shape, interpreter.getOutputTensor(0).type);
interpreter.run(tensorImage.buffer, tensorBuffer.getBuffer());
tensorBuffer = probabilityProcessor.process(tensorBuffer);
// ignore: invalid_use_of_protected_member
tensorBuffer.resize(List<int>.of([1, 224, 224, 1]));
TensorImage resultImage = TensorImage(tensorBuffer.getDataType());
resultImage.loadTensorBuffer(tensorBuffer);
}catch(e){
print('Error loading model: ' + e.toString());
}
我还尝试通过以下方式从tensorBuffer直接阅读缓冲区中的flutter图像
Image result = Image.memory(tensorBuffer.getBuffer().asUint8List());
其结果是无效的IMAE数据异常。
EDIT我还尝试了tflite_flutter_helper中的ImageConversions类
img.Image resultImage = ImageConversions.convertGrayscaleTensorBufferToImage(tensorBuffer);
但还是没有成功
1条答案
按热度按时间jobtbby31#
您可以将缓冲区转换为列表,然后尝试填充图像像素