国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

RuntimeError: Given groups=1, weight of size [14, 464, 1, 1], expected input問題解決

這篇具有很好參考價(jià)值的文章主要介紹了RuntimeError: Given groups=1, weight of size [14, 464, 1, 1], expected input問題解決。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

BUG解決:RuntimeError: Given groups=1, weight of size [14, 464, 1, 1], expected input[16, 116, 56, 1] to have 464 channels, but got 116 channels instead
首選說一下這個(gè)問題,這個(gè)問題提示想要得到的是464個(gè)通道數(shù)但是實(shí)際上得到的是116個(gè)通道。
例如我給某個(gè)深度學(xué)習(xí)網(wǎng)絡(luò)中加CBAM注意力集中機(jī)制,具體可參照此文章鏈接: link.(以下為實(shí)現(xiàn)代碼):

# 通道注意力機(jī)制
class ChannelAttention(nn.Module):
    def __init__(self, in_planes, ratio=16):
        super(ChannelAttention, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.max_pool = nn.AdaptiveMaxPool2d(1)

        self.fc1 = nn.Conv2d(in_planes, in_planes //ratio, 1, bias=False)
        self.relu1 = nn.ReLU()
        self.fc2 = nn.Conv2d(in_planes //ratio, in_planes, 1, bias=False)

        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x))))
        max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x))))
        out = avg_out + max_out
        return self.sigmoid(out)

# 空間注意力機(jī)制
class SpatialAttention(nn.Module):
    def __init__(self, kernel_size=7):
        super(SpatialAttention, self).__init__()

        assert kernel_size in (3, 7), 'kernel size must be 3 or 7'
        padding = 3 if kernel_size == 7 else 1

        self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        avg_out = torch.mean(x, dim=1, keepdim=True)
        max_out, _ = torch.max(x, dim=1, keepdim=True)
        x = torch.cat([avg_out, max_out], dim=1)
        x = self.conv1(x)
        return self.sigmoid(x)

問題出現(xiàn)的可能原因1:

 # 在網(wǎng)絡(luò)的某層加入CBAM注意力機(jī)制
 self.ca = ChannelAttention(self.inplanes)
 self.sa = SpatialAttention()

self.inplanes修改為你上一層輸出的通道數(shù);
出現(xiàn)原因2:是我在假的過程中出現(xiàn)的錯(cuò)誤,是一個(gè)非常小的錯(cuò)誤,就是在初始層和末尾分別加入CBAM的時(shí)候,沒有區(qū)分不同位置加入后的函數(shù)名,因此出現(xiàn)錯(cuò)誤,例如如下:

 # 在網(wǎng)絡(luò)的第一層加入CBAM注意力機(jī)制
 self.ca = ChannelAttention(self.inplanes)
 self.sa = SpatialAttention()
 # 在網(wǎng)絡(luò)的最后層加入CBAM注意力機(jī)制
 self.ca1 = ChannelAttention(self.inplanes)
 self.sa1 = SpatialAttention()

哎?。。。⌒枰獏^(qū)分函數(shù)名;
出現(xiàn)原因3:如果不是在開始或者最后層加入的注意力機(jī)制,而是在網(wǎng)絡(luò)結(jié)構(gòu)中加入,例如可以在resnet中的殘差結(jié)構(gòu)中,加入后可print(model)看一看是不是和自己想的一樣,我出現(xiàn)的問題是我想在每個(gè)block中加入注意力集中機(jī)制,因此把加入的部分寫在的模型結(jié)構(gòu)的block中,結(jié)果也出現(xiàn)了2所出現(xiàn)的問題,原因還是和2一樣。

總結(jié):其實(shí)加入注意力集中機(jī)制還是比較容易的,仔細(xì)再仔細(xì),一定沒問題,共勉。

附集中注意力集中機(jī)制實(shí)現(xiàn)代碼(PYTORCH):文章來源地址http://www.zghlxwxcb.cn/news/detail-402922.html

#SE
class SELayer(nn.Module):
    def __init__(self, c1, r=16):
        super(SELayer, self).__init__()
        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.l1 = nn.Linear(c1, c1 // r, bias=False)
        self.relu = nn.ReLU(inplace=True)
        self.l2 = nn.Linear(c1 // r, c1, bias=False)
        self.sig = nn.Sigmoid()
    def forward(self, x):
        b, c, _, _ = x.size()
        y = self.avgpool(x).view(b, c)
        y = self.l1(y)
        y = self.relu(y)
        y = self.l2(y)
        y = self.sig(y)
        y = y.view(b, c, 1, 1)
        return x * y.expand_as(x)
# ECA注意力機(jī)制
class eca_layer(nn.Module):
    """Constructs a ECA module.
    Args:
        channel: Number of channels of the input feature map
        k_size: Adaptive selection of kernel size
    """
    def __init__(self, channel, k_size=3):
        super(eca_layer, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1) // 2, bias=False)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        # feature descriptor on the global spatial information
        y = self.avg_pool(x)

        # Two different branches of ECA module
        y = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)

        # Multi-scale information fusion
        y = self.sigmoid(y)
        x=x*y.expand_as(x)

        return x * y.expand_as(x)
#CoorAttention
class h_sigmoid(nn.Module):
    def __init__(self, inplace=True):
        super(h_sigmoid, self).__init__()
        self.relu = nn.ReLU6(inplace=inplace)

    def forward(self, x):
        return self.relu(x + 3) / 6


class h_swish(nn.Module):
    def __init__(self, inplace=True):
        super(h_swish, self).__init__()
        self.sigmoid = h_sigmoid(inplace=inplace)

    def forward(self, x):
        return x * self.sigmoid(x)


class CoordAtt(nn.Module):
    def __init__(self, inp, oup, reduction=32):
        super(CoordAtt, self).__init__()
        self.pool_h = nn.AdaptiveAvgPool2d((None, 1))
        self.pool_w = nn.AdaptiveAvgPool2d((1, None))

        mip = max(8, inp // reduction)

        self.conv1 = nn.Conv2d(inp, mip, kernel_size=1, stride=1, padding=0)
        self.bn1 = nn.BatchNorm2d(mip)
        self.act = h_swish()

        self.conv_h = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)
        self.conv_w = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)

    def forward(self, x):
        identity = x

        n, c, h, w = x.size()
        x_h = self.pool_h(x)
        x_w = self.pool_w(x).permute(0, 1, 3, 2)

        y = torch.cat([x_h, x_w], dim=2)
        y = self.conv1(y)
        y = self.bn1(y)
        y = self.act(y)

        x_h, x_w = torch.split(y, [h, w], dim=2)
        x_w = x_w.permute(0, 1, 3, 2)

        a_h = self.conv_h(x_h).sigmoid()
        a_w = self.conv_w(x_w).sigmoid()

        out = identity * a_w * a_h

        return out

到了這里,關(guān)于RuntimeError: Given groups=1, weight of size [14, 464, 1, 1], expected input問題解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • RuntimeError: stack expects each tensor to be equal size ??

    RuntimeError: stack expects each tensor to be equal size, but got [1200, 1200, 3] at entry 0 and [1200, 1344, 3] at entry 1 pytorch 數(shù)據(jù)處理錯(cuò)誤, 網(wǎng)上的各種方法都試過了 1: 檢查過數(shù)據(jù)的輸入通道是3, 標(biāo)簽是1,但是輸入的大小尺寸不同 2: 進(jìn)行如下方法也不行??! :3: bs=1,不報(bào)錯(cuò),bs1 報(bào)錯(cuò) 4: bug 未解

    2024年02月01日
    瀏覽(30)
  • 【報(bào)錯(cuò)處理】RuntimeError: input.size(-1) must be equal to input_size. Expected 5, got 21

    1、 原因 : 使用view時(shí)維度指定錯(cuò)誤,LSTM(input,(h0,c0)) 指定batch_first=True?后,input就是(batch_size,seq_len,input_size)否則為input(seq_len, batch, input_size) 2、原因:并不是rnn的錯(cuò)誤,而是因?yàn)橄乱缓瘮?shù)的輸入和這一層輸出維度不一樣,對(duì)照維度信息和尺寸信息修改即可。 推薦報(bào)錯(cuò)解決方

    2024年02月16日
    瀏覽(19)
  • RuntimeError: stack expects each tensor to be equal size, but got at entry

    RuntimeError: stack expects each tensor to be equal size, but got at entry

    參考鏈接:??????解決Pytorch dataloader時(shí)報(bào)錯(cuò)每個(gè)tensor維度不一樣的問題_python_腳本之家 記錄一下自己遇到的bug: 問題描述:? 問題分析: torch.stack(batch, 0, out=out)出錯(cuò),原因可能是: 同一個(gè)batch的數(shù)據(jù)圖片的維度(H, W, C)要相同(可以見官方文檔:其shape必須一致) 問

    2024年02月15日
    瀏覽(24)
  • DataLoader問題解決:RuntimeError: stack expects each tensor to be equal size, but got [3, 200, 200]entry1

    DataLoader問題解決:RuntimeError: stack expects each tensor to be equal size, but got [3, 200, 200]entry1

    ? ? ? ? 最近,在數(shù)據(jù)集處理并載入DataLoader進(jìn)行訓(xùn)練的時(shí)候出現(xiàn)了問題: ? ? ? ? 我看了一下,大意就是維度也就是通道數(shù)不匹配,所以我覺得應(yīng)該是數(shù)據(jù)集圖片出現(xiàn)了問題。以下是我的普通數(shù)據(jù)集處理代碼: ? ? ? ? ? 我一張一張圖片放入DataLoader,然后按順序一張一張的

    2023年04月25日
    瀏覽(19)
  • Pytorch中報(bào)錯(cuò)RuntimeError: The size of tensor a (60) must match the size of tensor b (56)

    Pytorch中報(bào)錯(cuò)RuntimeError: The size of tensor a (60) must match the size of tensor b (56)

    最近在學(xué)習(xí)YOLOV5的時(shí)候,剛開始遇到了如下的問題: 這可能是因?yàn)?.0的工程下載了個(gè)6.1的模型,所以不匹配 yolov5s.pt [https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt] 下載下來替換掉工程文件夾里的yolov5s.pt文件 發(fā)現(xiàn)下面這個(gè)問題直接消失了! 順利完成 如果對(duì)你有用麻

    2024年02月01日
    瀏覽(19)
  • RuntimeError: shape ‘[-1, 784]‘ is invalid for input of size 68076

    在應(yīng)用torch進(jìn)行測試時(shí),有可能出現(xiàn)這種錯(cuò)誤: RuntimeError: shape \\\'[-1, 784]\\\' is invalid for input of size 68076 這個(gè)錯(cuò)誤通常是由于輸入數(shù)據(jù)的大小與模型期望的輸入大小不匹配導(dǎo)致的。具體地說,在這個(gè)錯(cuò)誤信息中, [-1, 784] 表示輸入張量的形狀是一個(gè)二維張量,第一個(gè)維度大小是 -1,

    2024年02月12日
    瀏覽(16)
  • 【解決問題】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton

    【解決問題】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton

    你可以去github上,這兒我用的是YOLOv5.5的版本,就去Tags6里面的model/common.py里面去找到這個(gè)SPPF的類,把它拷過來到你這個(gè)Tags5的model/common.py里面,這樣你的代碼就也有這個(gè)類了,還要引入一個(gè)warnings包就行了 點(diǎn)開common.py文件 將這個(gè)復(fù)制到對(duì)應(yīng)的類就行了。 剛解決了上一個(gè)問題,結(jié)

    2024年02月16日
    瀏覽(19)
  • 生成器報(bào)錯(cuò),RuntimeError: Sizes of tensors must match except in dimension

    RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 2 but got size 3 for tensor number 1 in the list. 常見的模型報(bào)錯(cuò),比方說pix2pix模型 In[18], line 84, in Generator.forward(self, x) ????????82 bottleneck = self.bottleneck(d7) ????????83 up1 = self.up1(bottleneck) --- 84 up2 = self.up2(torch.cat([up1, d

    2024年02月09日
    瀏覽(24)
  • 【Python】解決CNN中訓(xùn)練權(quán)重參數(shù)不匹配size mismatch for fc.weight,size mismatch for fc.bias

    【Python】解決CNN中訓(xùn)練權(quán)重參數(shù)不匹配size mismatch for fc.weight,size mismatch for fc.bias

    目錄 1.問題描述 2.問題原因 3.問題解決 3.1思路1——忽視最后一層權(quán)重 額外說明:假如載入權(quán)重不寫strict=False, 直接是model.load_state_dict(pre_weights, strict=False),會(huì)報(bào)錯(cuò)找不到key? 解決辦法是:加上strict=False,這個(gè)語句就是指忽略掉模型和參數(shù)文件中不匹配的參數(shù) 3.2思路2——更

    2023年04月14日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包