pytorch - 尝试将简单卷积模型转换为 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 转换。
解决方案
Core ML 没有一维批量规范。张量必须至少具有 3 阶。
如果要转换此模型,则应将批范数权重折叠到前一层的权重中,并删除批范数层。(我认为 PyTorch 没有办法自动为您执行此操作。)
推荐阅读
- java - 如何转到 Thymleaf 中的 th:text 中的下一行?
- hadoop - 我们如何才能从 hdfs 联邦的一个命名空间中排除一些数据节点?
- java - 有没有办法使用java检查和释放数据库锁?
- c# - 统一:全方位的重力
- c++ - 如何将 stderr 与输出流相关联
- r - R Markdown:knitr选项结果='隐藏'不起作用
- c# - 如何使用向导在 Visual Studio 2017 中添加项目模板
- .net - MVC .NET CORE - DataAnnotation 的Required(ErrorMessage="Err") 不会出现在服务器端验证中
- postgresql - 如何将我的整个 postgres 数据库复制到另一个 postgres 数据库?
- android - 具有可检查状态的多组项目相互取消选中