用pytorch编写25633的卷积层连接两个包含SE残差块的残差层25633再连接12833的卷积层输入为116再连接并测试模型单集运行时间
以下是使用PyTorch编写的模型代码:
import torch
import torch.nn as nn
import torch.nn.functional as F
class SEBlock(nn.Module):
def __init__(self, channel, reduction=16):
super(SEBlock, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=False),
nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channels)
self.se = SEBlock(out_channels)
if stride != 1 or in_channels != out_channels:
self.shortcut = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False)
self.shortcut_bn = nn.BatchNorm2d(out_channels)
else:
self.shortcut = nn.Identity()
def forward(self, x):
out = self.conv1(x)
out = F.relu(self.bn1(out), inplace=True)
out = self.conv2(out)
out = self.bn2(out)
out = self.se(out)
out += self.shortcut_bn(self.shortcut(x))
out = F.relu(out, inplace=True)
return out
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 256, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(256)
self.residual1 = nn.Sequential(
ResidualBlock(256, 256),
ResidualBlock(256, 256),
)
self.residual2 = nn.Sequential(
ResidualBlock(256, 256),
ResidualBlock(256, 256),
)
self.conv2 = nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(128)
self.fc = nn.Linear(128 * 4 * 4, 10)
def forward(self, x):
out = self.conv1(x)
out = F.relu(self.bn1(out), inplace=True)
out = self.residual1(out)
out = self.residual2(out)
out = self.conv2(out)
out = F.relu(self.bn2(out), inplace=True)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
为了测试模型单集运行时间,我们可以使用以下代码:
import time
net = Net()
net.eval()
x = torch.randn(1, 1, 16, 16)
start_time = time.time()
y = net(x)
end_time = time.time()
print('Output size:', y.size())
print('Time elapsed:', end_time - start_time)
此代码将打印输出大小和模型单集运行时间。请注意,这个时间可能因为硬件、软件和输入数据的不同而有所变化。
原文地址: https://www.cveoy.top/t/topic/bzai 著作权归作者所有。请勿转载和采集!