【问题标题】:CoreML: Unable to perform matrix multiplicationCoreML:无法执行矩阵乘法
【发布时间】:2019-12-20 01:51:36
【问题描述】:

我正在尝试使用 NetworkBuilder 在我的网络中实现矩阵乘法运算。 我希望将两个大小为 (20x50) 和 (50x100) 的张量相乘,得到一个大小为 (20x100) 的张量。

我该怎么做?我尝试使用 add_batched_mat_mul 但在 coremltools==3.0b3 和 coremltools==3.0b4 上出现以下错误

如何使用上述张量维度执行 matmul 操作?

coremltools==3.0b3 上的错误

RuntimeWarning: You will not be able to run predict() on this Core ML model. Underlying exception message was: Error compiling model: "Error reading protobuf spec. validator error: Unsupported layer type (CoreML.Specification.NeuralNetworkLayer) for layer 'matmul'.".
  RuntimeWarning)

coremltools==3.0b4 上的错误

  File "test2.py", line 28, in <module>
    out = model.predict({"matrix_left": np.zeros((20, 50, 1))})
  File "python2.7/site-packages/coremltools/models/model.py", line 345, in predict
    raise Exception('Unable to load CoreML.framework. Cannot make predictions.')
Exception: Unable to load CoreML.framework. Cannot make predictions.
exception loading model proxy: dlopen(python2.7/site-packages/coremltools/libcoremlpython.so, 2): Symbol not found: _objc_opt_class
  Referenced from: python2.7/site-packages/coremltools/libcoremlpython.so (which was built for Mac OS X 10.15)
  Expected in: /usr/lib/libobjc.A.dylib
 in python2.7/site-packages/coremltools/libcoremlpython.so

使用的脚本:

import coremltools.models.datatypes as datatypes
from coremltools.models.neural_network import NeuralNetworkBuilder
from coremltools.models import MLModel

import numpy as np

model_input_features = [
    ("matrix_left", datatypes.Array(20, 50, 1)),
]
model_output_features = [
    ("y", datatypes.Array(20, 100, 1)),
]

builder = NeuralNetworkBuilder(input_features=model_input_features, output_features=model_output_features)

np.random.seed(42)

matrix_right = np.random.rand(50, 100, 1)
builder.add_load_constant(name="matrix_right", output_name="y",
                          constant_value=matrix_right, shape=(50, 100, 1))


builder.add_batched_mat_mul(name="matmul", input_names=["matrix_left", "matrix_right"],
                            output_name="y")

model = MLModel(builder.spec)
out = model.predict({"matrix_left": np.zeros((20, 50, 1))})
y = out["y"]
print(y)
print(y.shape)

我也尝试使用 add_elementwise 使用点积,但得到以下错误:

RuntimeWarning: You will not be able to run predict() on this Core ML model. 
Underlying exception message was: Error compiling model: "compiler error:  Dot product layer: 'matmul': 
height dimension of the input blob must be 1.".

脚本:

matrix_right = np.random.rand(50, 100, 1)
builder.add_load_constant(name="matrix_right", output_name="matrix_right", constant_value=matrix_right, shape=(50, 100, 1))
builder.add_elementwise("matmul", input_names=["matrix_left", "matrix_right"], output_name="y", mode="DOT")

【问题讨论】:

    标签: coreml coremltools


    【解决方案1】:

    试试这个:

    builder.add_load_constant(name="matrix_right", output_name="matrix_right",
                          constant_value=matrix_right, shape=(50, 100, 1))
    

    请注意,输出名称现在是 "matrix_right" 而不是 "y"

    该模型仍然不适用于 3.0b3 或 3.0b4,但至少它现在是一个有效的模型。 :-)

    【讨论】:

    • 感谢您修复模型,但我无法在 CoreML 中执行 matmul 操作吗?为什么证明如此困难?
    • 我已经尝试使用 eadd_elementwise 使用点积,但它也中断了。
    • 这很困难,因为它是一个测试版。您最好的选择是向 Apple 提交错误报告,或者等到下一个测试版并希望它得到修复。另外,您是否在 Xcode 中尝试过?它在 coremltools 中不起作用的事实通常是因为它链接到的版本与 Xcode 提供的版本不同,但是从 macOS/iOS 项目中运行模型不应该有这个问题。
    猜你喜欢
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 2018-07-05
    • 2013-09-14
    相关资源
    最近更新 更多