pytorch异常——RuntimeError:Given groups=1, weight of size..., expected of...

文章目录

省流

  • nn.Conv2d 需要的输入张量格式为 (batch_size, channels, height, width),但您的示例输入张量 x 是 (batch_size, height, width, channels)。因此,需要对输入张量进行转置。

  • 注意,TensorFlow使用"NHWC"(批次、高度、宽度、通道)格式,而PyTorch使用"NCHW"(批次、通道、高度、宽度)格式

异常报错

bash 复制代码
RuntimeError: Given groups=1, weight of size [16, 3, 2, 3], 
expected input[8, 65, 66, 3] to have 3 channels, 
but got 65 channels instead

异常截图

异常代码

python 复制代码
def down_shifted_conv2d(x , num_filters , filters_size = [2,3],stride = 1, **kwargs):
    batch_size,H,W,channels = x.shape

    padding = (0,0,
        int(((filters_size[1]) - 1) / 2 ) , int((int(filters_size[1]) - 1) / 2),
        int(filters_size[0]) - 1 , 0,
        0,0)
    x_paded = nn.functional.pad(x, padding)
    print(x_paded.shape)
    conv_layer = nn.Conv2d(in_channels=channels, out_channels=num_filters, 
                           kernel_size=filters_size,
                           stride=stride, **kwargs)
    
    return conv_layer(x_paded)
# Example usage
x = torch.randn(8, 64, 64, 3)  # Example input with batch size 8, height and width 64, and 3 channels
num_filters = 16
output = down_shifted_conv2d(x, num_filters)
print(output.shape)

原因解释

  • 在pytorch中,"nn.Conv2d"需要输入的张量格式为(batch_size,channels,height,width),原图输入的x的格式是(batch_size,height ,weight,channel)所以需要对tensor进行转置。

  • 矩阵交换维度的函数permute,按照编号,将新的顺序填好即可

python 复制代码
def down_shifted_conv2d(x , num_filters , filters_size = [2,3], stride = 1, **kwargs):
    batch_size, H, W, channels = x.shape
    
    # Transpose the input tensor to (batch_size, channels, height, width)
    x = x.permute(0, 3, 1, 2)
    
    # Padding
    padding = (int((filters_size[1] - 1) / 2), int((filters_size[1] - 1) / 2),
               filters_size[0] - 1, 0)
    
    x_paded = F.pad(x, padding)

修正代码

python 复制代码
def down_shifted_conv2d(x , num_filters , filters_size = [2,3],stride = 1, **kwargs):
    batch_size,H,W,channels = x.shape
    # 按照顺序对4个维度分别进行填充
    padding = (0,0,
        int(((filters_size[1]) - 1) / 2 ) , int((int(filters_size[1]) - 1) / 2),
        int(filters_size[0]) - 1 , 0,
        0,0)
    x_paded = nn.functional.pad(x, padding)
    x_paded = x_paded.permute(0,3,1,2)
    # 进行卷积
    conv_layer = nn.Conv2d(in_channels=channels, out_channels=num_filters, 
                           kernel_size=filters_size,
                           stride=stride, **kwargs)
    
    return conv_layer(x_paded)
# Example usage
x = torch.randn(8, 64, 64, 3)  
num_filters = 16
output = down_shifted_conv2d(x, num_filters)
print(output.shape)

执行结果

相关推荐
仗剑_走天涯2 小时前
基于pytorch.nn模块实现线性模型
人工智能·pytorch·python·深度学习
2401_878624799 小时前
pytorch 自动微分
人工智能·pytorch·python·机器学习
水龙吟啸10 小时前
从零开始搭建深度学习大厦系列-2.卷积神经网络基础(5-9)
人工智能·pytorch·深度学习·cnn·mxnet
慕婉030713 小时前
深度学习中的常见损失函数详解及PyTorch实现
人工智能·pytorch·深度学习
聚客AI15 小时前
搜索引擎vs向量数据库:LangChain混合检索架构实战解析
人工智能·pytorch·语言模型·自然语言处理·数据分析·gpt-3·文心一言
咸鱼鲸17 小时前
【PyTorch】PyTorch中数据准备工作(AI生成)
人工智能·pytorch·python
羊八井17 小时前
使用 Earth2Studio 和 AI 模型进行全球天气预测:太阳辐照
pytorch·python·nvidia
向左转, 向右走ˉ17 小时前
PyTorch随机擦除:提升模型抗遮挡能力
人工智能·pytorch·python·深度学习
HuashuiMu花水木1 天前
PyTorch笔记3----------统计学相关函数
人工智能·pytorch·笔记
AndrewHZ1 天前
【图像处理基石】如何检测到画面中的ppt并对其进行增强?
图像处理·人工智能·pytorch·opencv·目标检测·计算机视觉·图像增强