在使用 MindSpore 的 ResNet 模型时,可能会遇到以下错误:

RuntimeError: mindspore\core\ops\conv2d.cc:185 Conv2dInferShape] x_shape[C_in] / group must equal to w_shape[C_in] = 3, but got 224
The function call stack (See file 'D:\pythonProject7\rank_0\om/analyze_fail.dat' for more details):
# 0 In file D:\pythonProject7\main.py(84)
        x = self.conv1(x)
            ^
# 1 In file D:\miniconda3\envs\MindSpore\lib\site-packages\mindspore\nn\layer\conv.py(267)
        if self.has_bias:
# 2 In file D:\miniconda3\envs\MindSpore\lib\site-packages\mindspore\nn\layer\conv.py(266)
        output = self.conv2d(x, self.weight)
                 ^

WARNING: Logging before InitGoogleLogging() is written to STDERR
[EXCEPTION] CORE(16432,1,?):2023-4-4 15:42:11 [mindspore\core\ops\conv2d.cc:185] Conv2dInferShape] x_shape[C_in] / group must equal to w_shape[C_in] = 3, but got 224

Process finished with exit code 1

该错误通常是因为模型定义中的输入通道数与实际输入数据的通道数不匹配导致的。例如,如果 ResNet 模型的输入通道数定义为 3,但实际输入数据的通道数为 1,就会出现上述错误。

解决方法:

可以在模型定义中将输入通道数改为需要的值。比如,如果需要将输入通道数改为 1,可以在 ResNet 的初始化函数中将输入通道数 num_channels 改为 1。同时,需要修改第一层卷积层的输入通道数,将其改为 1。具体实现可以参考以下代码:

class ResNet(nn.Cell):
    def __init__(self, block, layers, num_classes=1000, num_channels=3):
        super(ResNet, self).__init__()

        self.in_channels = num_channels
        self.conv1 = nn.Conv2d(self.in_channels, 64, 7, stride=2, padding=3, has_bias=False, weight_init=initializer)
        self.bn1 = nn.BatchNorm2d(64, momentum=BN_MOMENTUM)
        self.relu = nn.ReLU()
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode='same')
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.avgpool = nn.AvgPool2d(kernel_size=7, stride=1)
        self.fc = nn.Dense(512 * block.expansion, num_classes)

    def _make_layer(self, block, out_channels, blocks, stride=1):
        downsample = None
        if stride != 1 or self.in_channels != out_channels * block.expansion:
            downsample = nn.SequentialCell([
                nn.Conv2d(self.in_channels, out_channels * block.expansion, kernel_size=1, stride=stride, has_bias=False),
                nn.BatchNorm2d(out_channels * block.expansion, momentum=BN_MOMENTUM)
            ])

        layers = []
        layers.append(block(self.in_channels, out_channels, stride, downsample))
        self.in_channels = out_channels * block.expansion
        for _ in range(1, blocks):
            layers.append(block(self.in_channels, out_channels))

        return nn.SequentialCell(layers)

    def construct(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = mindspore.ops.Reshape()(x, (x.shape[0], -1))
        x = self.fc(x)

        return x

network = ResNet(BasicBlock, [2, 2, 2, 2], num_classes=100, num_channels=1)

在修改完代码后,重新训练模型即可。

注意:

  • 如果模型已经训练完成,则需要重新训练模型,以适应新的输入通道数。
  • 如果模型的输入通道数与实际输入数据的通道数不匹配,会导致模型预测结果不准确。
MindSpore 模型输入通道调整:解决 RuntimeError: x_shape[C_in] / group must equal to w_shape[C_in] = 3, but got 224

原文地址: https://www.cveoy.top/t/topic/mUUL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录