Coremltools无法成功将输出更改为图像 [英] Coremltools cannot successfully change the output to an image

查看:72
本文介绍了Coremltools无法成功将输出更改为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用coremltools 4.0将Keras模型转换为MLModel,但效果有限.它有效,但仅当我将MLMultiArray用于输出并隐式转换为图像时才有效.转换为图像所需的时间比推理要长.使其无法使用它适用于TensorFlow 1和coremltools 3.4,但现在不适用于TensorFlow 2和coremltools 4.0b1.添加新层以转换[0,1]-> gt的输出. [0,255]不能解决问题.我们还意识到,Cormel会自动添加一些额外的层,这可能是问题所在.在这里,您有图像

I have converted a Keras model to a MLModel using coremltools 4.0 with limited success. It works but only if I use an MLMultiArray for the output and covert to an image. Converting to an image takes magnitudes longer than inferencing; making it unusable It was working for TensorFlow 1 and coremltools 3.4 but now for TensorFlow 2 and coremltools 4.0b1 is not. Adding a new layer to convert the output from [0,1] -> [0, 255] does not do the trick. We have also realised that some extra layers where added automatically by cormel that might be the problem. Here you have the image

我试图使用np.transpose来转置输入,但是它不能解决问题,但是创建了一个新的输入.如果输入遵循格式(3,256,256),则会出现以下错误:

I tried to transpose the input using np.transpose but it didn't solve the problem but created a new one. If the input follows the format (3, 256, 256) I get the following error:

RuntimeError:{ NSLocalizedDescription =输入图像特征input_1与模型描述不匹配"; NSUnderlyingError ="Error Domain = com.apple.CoreML Code = 0"图像高度(256)不在允许范围(200..400)". UserInfo = {NSLocalizedDescription =图像高度(256)不在允许范围内(200..400)}"; }

RuntimeError: { NSLocalizedDescription = "Input image feature input_1 does not match model description"; NSUnderlyingError = "Error Domain=com.apple.CoreML Code=0 "Image height (256) is not in allowed range (200..400)" UserInfo={NSLocalizedDescription=Image height (256) is not in allowed range (200..400)}"; }

但是,如果大小为(256,256,3),则会出现以下错误:

But if the size is (256, 256, 3) I get the following error:

NSLocalizedDescription =无法将输出output_1转换为图像"; NSUnderlyingError ="Error Domain = com.apple.CoreML Code = 0"用于转换为灰度图像的无效数组形状(\ n 256,\ n 256,\ n 1 \ n)" UserInfo = {NSLocalizedDescription =用于转换为灰度图像的无效数组形状(\ n 256,\ n 256,\ n 1 \ n)};

NSLocalizedDescription = "Failed to convert output output_1 to image"; NSUnderlyingError = "Error Domain=com.apple.CoreML Code=0 "Invalid array shape (\n 256,\n 256,\n 1\n) for converting to gray image" UserInfo={NSLocalizedDescription=Invalid array shape (\n 256,\n 256,\n 1\n) for converting to gray image}";

你有什么主意吗?

推荐答案

我们发现了错误!问题是转置图层会自动添加错误的索引.为了解决这个问题,我们使用以下解决方法:

We found the error! The problem is that a transpose layer is automatically added with wrong index. In order to solve it, we use the following workaround:

transpose_layer = mlmodel_spec.neuralNetwork.layers[-4].transpose
del transpose_layer.axes[:]
transpose_layer.axes.extend([0, 1, 2, 3])

这篇关于Coremltools无法成功将输出更改为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆