pytorch使用DCN
生活随笔
收集整理的這篇文章主要介紹了
pytorch使用DCN
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
pytorch使用DCN
- 前言
- 正文
前言
關于DCN可形變卷積神經網絡相信大家也都不陌生,使用額外的feature map區學習offset,以此達到可形變的效果。感覺和attention比較相似?
但是網絡實現的代碼版本各不相同,編譯環境存在很多難以協調等等的問題。而MMopenlab是一個非常不錯的工具,其有著實現可形變卷積的方法,因此本文只是做一個引入,如何像正常使卷積一樣的使用DCN
正文
from mmcv.ops import DeformConv2dPack import torch from torch import nn from thop import profile import timeclass Conv(nn.Module):def __init__(self,indim,outdim,kerner=3,stride=1):super(Conv, self).__init__()self.conv1 = nn.Conv2d(indim,outdim,kernel_size=kerner,stride=stride,padding=kerner//2)self.act = nn.LeakyReLU(0.1)self.bn = nn.BatchNorm2d(outdim)def forward(self,x):return self.bn(self.act(self.conv1(x)))class Dconv(nn.Module):def __init__(self, indim, outdim, kerner=3, stride=1):super(Dconv, self).__init__()self.conv1 = DeformConv2dPack(indim, outdim, kernel_size=kerner, stride=stride, padding=kerner // 2,deform_groups=2)self.act = nn.LeakyReLU(0.1)self.bn = nn.BatchNorm2d(outdim)def forward(self, x):return self.bn(self.act(self.conv1(x)))接下來是對兩個定義的結構進行測試
conv1 = Conv(128,256).cuda() dconv1 = Dconv(128,256).cuda() input = torch.randn(4,128,640,640).cuda()t1 = time.time() out1 = conv1(input) t2 = time.time() print('conv:',t2-t1) total = sum([param.nelement() for param in conv1.parameters()]) print("Number of parameter: %.2fM" % (total / 1e6)) out2 = dconv1(input) t3 =time.time() print('dconv:',t3-t2) total = sum([param.nelement() for param in dconv1.parameters()]) print("Number of parameter: %.2fM" % (total / 1e6))print(out1.shape) print(out2.shape)相比較正常的CONV而言,DCN的參數量更大一些。
總結
以上是生活随笔為你收集整理的pytorch使用DCN的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【嵌入式09】STM32串口通信,发送H
- 下一篇: 修改 QQ 任务栏托盘区小图标和等级图标