VoVNet pytorch实现
时间: 2023-12-19 08:29:56 浏览: 37
VoVNet是一种用于图像分类的深度卷积神经网络,其特点是具有高效的计算和较少的参数。以下是VoVNet骨干网络的pytorch实现:
```python
import torch.nn as nn
class Conv2dBNReLU(nn.Sequential):
def __init__(self, in_planes, out_planes, kernel_size=3, stride=1, groups=1):
padding = (kernel_size - 1) // 2
super(Conv2dBNReLU, self).__init__(
nn.Conv2d(in_planes, out_planes, kernel_size, stride, padding, groups=groups, bias=False),
nn.BatchNorm2d(out_planes),
nn.ReLU(inplace=True)
)
class Stem(nn.Module):
def __init__(self):
super(Stem, self).__init__()
self.conv1 = Conv2dBNReLU(3, 64, kernel_size=3, stride=2)
self.conv2 = Conv2dBNReLU(64, 64, kernel_size=3, stride=1)
self.conv3 = Conv2dBNReLU(64, 128, kernel_size=3, stride=1)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.maxpool(x)
return x
class VoVNet(nn.Module):
def __init__(self, cfg):
super(VoVNet, self).__init__()
self.cfg = cfg
self.stem = Stem()
self.stage1 = self._make_stage(self.cfg[0])
self.stage2 = self._make_stage(self.cfg[1])
self.stage3 = self._make_stage(self.cfg[2])
self.stage4 = self._make_stage(self.cfg[3])
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(self.cfg[-1][-1][0], 1000)
def forward(self, x):
x = self.stem(x)
x = self.stage1(x)
x = self.stage2(x)
x = self.stage3(x)
x = self.stage4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
def _make_stage(self, planes):
modules = []
for i in range(len(planes)):
in_planes = 64 if i == 0 else planes[i-1][0]
out_planes = planes[i][0]
num_blocks = planes[i][1]
stride = planes[i][2]
modules.append(VoVNetUnit(in_planes, out_planes, num_blocks, stride))
return nn.Sequential(*modules)
class VoVNetUnit(nn.Module):
def __init__(self, in_planes, out_planes, num_blocks, stride):
super(VoVNetUnit, self).__init__()
self.stride = stride
self.num_blocks = num_blocks
self.conv = nn.ModuleList()
self.bn = nn.ModuleList()
self.relu = nn.ReLU(inplace=True)
for i in range(num_blocks):
if i == 0:
self.conv.append(Conv2dBNReLU(in_planes, out_planes, kernel_size=3, stride=stride))
else:
self.conv.append(Conv2dBNReLU(out_planes, out_planes, kernel_size=3, stride=1))
self.bn.append(nn.BatchNorm2d(out_planes))
def forward(self, x):
residual = x
for i in range(self.num_blocks):
out = self.conv[i](x)
out = self.bn[i](out)
if i == 0 and self.stride != 1:
residual = out
elif i == 0:
residual = x
residual += out
x = self.relu(residual)
return x
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)