起因:將CCNet的十字交叉注意力模塊移植到Y(jié)OLOv5中。
經(jīng)過:在注意力模塊中,會(huì)有較多的矩陣運(yùn)算,在訓(xùn)練時(shí)出現(xiàn)了cuda和cup類型的沖突(另一篇我寫的文章);而在驗(yàn)證時(shí)出現(xiàn)了上述錯(cuò)誤。
出錯(cuò)的代碼如下:
# [b1*w1, c1, h1] -> [b1, w1, c1, h1] -> [b1, c1, h1, w1]
out_H = torch.bmm(value_H, att_H.permute(0, 2, 1)).view(b1, w1, -1, h1).permute(0, 2, 3, 1)
# [b1 * h1, c1, w1] -> [b1, h1, c1, w1] -> [b1, c1, h1, w1]
out_W = torch.bmm(value_W, att_W.permute(0, 2, 1)).view(b1, h1, -1, w1).permute(0, 2, 1, 3)
出錯(cuò)的位置在torch.bmm()處,在這里進(jìn)行了一次矩陣乘法運(yùn)算。由于兩個(gè)數(shù)據(jù)的類型不同,因此發(fā)生沖突。文章來源:http://www.zghlxwxcb.cn/news/detail-507128.html
解決方案:仍然是用to()方法,修改數(shù)據(jù)類型為另一個(gè)數(shù)據(jù)的類型。文章來源地址http://www.zghlxwxcb.cn/news/detail-507128.html
# [b1*w1, c1, h1] -> [b1, w1, c1, h1] -> [b1, c1, h1, w1]
out_H = torch.bmm(value_H, att_H.permute(0, 2, 1).to(value_H.dtype)).view(b1, w1, -1, h1).permute(0, 2, 3, 1)
# [b1 * h1, c1, w1] -> [b1, h1, c1, w1] -> [b1, c1, h1, w1]
out_W = torch.bmm(value_W, att_W.permute(0, 2, 1).to(value_W.dtype)).view(b1, h1, -1, w1).permute(0, 2, 1, 3)
到了這里,關(guān)于RuntimeError: expected scalar type Half but found Float的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!