首页 > 解决方案 > 尝试将简单卷积模型转换为 CoreML 时出错

问题描述

我正在尝试转换一个简单的 GAN 生成器(来自 ClusterGAN):

self.name = 'generator'
self.latent_dim = latent_dim
self.n_c = n_c
self.x_shape = x_shape
self.ishape = (128, 7, 7)
self.iels = int(np.prod(self.ishape))
self.verbose = verbose

self.model = nn.Sequential(
    # Fully connected layers
    torch.nn.Linear(self.latent_dim + self.n_c, 1024),
    nn.BatchNorm1d(1024),
    nn.LeakyReLU(0.2, inplace=True),
    torch.nn.Linear(1024, self.iels),
    nn.BatchNorm1d(self.iels),
    nn.LeakyReLU(0.2, inplace=True),

    # Reshape to 128 x (7x7)
    Reshape(self.ishape),

    # Upconvolution layers
    nn.ConvTranspose2d(128, 64, 4, stride=2, padding=1, bias=True),
    nn.BatchNorm2d(64),
    nn.LeakyReLU(0.2, inplace=True),

    nn.ConvTranspose2d(64, 1, 4, stride=2, padding=1, bias=True),
    nn.Sigmoid()
)

但是 onnx-coreml 失败了Error while converting op of type: BatchNormalization. Error message: provided number axes 2 not supported

我以为是 BatchNorm2d,所以我尝试重塑和应用 BatchNorm1d,但我得到了同样的错误。有什么想法吗?我很惊讶我在转换如此简单的模型时遇到问题,所以我假设我一定遗漏了一些明显的东西。

我的目标是 iOS 13 并使用 Opset v10 进行 onnx 转换。

标签: pytorchcoremlgenerative-adversarial-network

解决方案


Core ML 没有一维批量规范。张量必须至少具有 3 阶。

如果要转换此模型,则应将批范数权重折叠到前一层的权重中,并删除批范数层。(我认为 PyTorch 没有办法自动为您执行此操作。)


推荐阅读