#------------------------------用50行代码搭建ResNet-------------------------------------------
from torch import nn
import torch as t
from torch.nn import functional as F
class ResidualBlock(nn.Module):
#实现子module: Residual Block
def __init__(self,inchannel,outchannel,stride=1,shortcut=None):
super(ResidualBlock,self).__init__()
self.left=nn.Sequential(
nn.Conv2d(inchannel,outchannel,3,stride,1,bias=False),
nn.BatchNorm2d(outchannel),
nn.ReLU(inplace=True),
nn.Conv2d(outchannel,outchannel,3,1,1,bias=False),
nn.BatchNorm2d(outchannel)
)
self.right=shortcut
def forward(self,x):
out=self.left(x)
residual=x if self.right is None else self.right(x)
out+=residual
return F.relu(out)
class ResNet(nn.Module):
#实现主module:ResNet34
#ResNet34
Pytorch学习(三)--用50行代码搭建ResNet
最新推荐文章于 2024-03-02 14:45:43 发布