resnet.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import torch.nn as nn
  2. import torch.nn.functional as F
  3. def conv3x3(in_planes, out_planes, stride=1):
  4. """3x3 convolution with padding"""
  5. return nn.Conv2d(
  6. in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False
  7. )
  8. class BasicBlock(nn.Module):
  9. def __init__(self, in_chan, out_chan, stride=1):
  10. super(BasicBlock, self).__init__()
  11. self.conv1 = conv3x3(in_chan, out_chan, stride)
  12. self.bn1 = nn.BatchNorm2d(out_chan)
  13. self.conv2 = conv3x3(out_chan, out_chan)
  14. self.bn2 = nn.BatchNorm2d(out_chan)
  15. self.relu = nn.ReLU(inplace=True)
  16. self.downsample = None
  17. if in_chan != out_chan or stride != 1:
  18. self.downsample = nn.Sequential(
  19. nn.Conv2d(in_chan, out_chan, kernel_size=1, stride=stride, bias=False),
  20. nn.BatchNorm2d(out_chan),
  21. )
  22. def forward(self, x):
  23. residual = self.conv1(x)
  24. residual = F.relu(self.bn1(residual))
  25. residual = self.conv2(residual)
  26. residual = self.bn2(residual)
  27. shortcut = x
  28. if self.downsample is not None:
  29. shortcut = self.downsample(x)
  30. out = shortcut + residual
  31. out = self.relu(out)
  32. return out
  33. def create_layer_basic(in_chan, out_chan, bnum, stride=1):
  34. layers = [BasicBlock(in_chan, out_chan, stride=stride)]
  35. for i in range(bnum - 1):
  36. layers.append(BasicBlock(out_chan, out_chan, stride=1))
  37. return nn.Sequential(*layers)
  38. class ResNet18(nn.Module):
  39. def __init__(self):
  40. super(ResNet18, self).__init__()
  41. self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
  42. self.bn1 = nn.BatchNorm2d(64)
  43. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  44. self.layer1 = create_layer_basic(64, 64, bnum=2, stride=1)
  45. self.layer2 = create_layer_basic(64, 128, bnum=2, stride=2)
  46. self.layer3 = create_layer_basic(128, 256, bnum=2, stride=2)
  47. self.layer4 = create_layer_basic(256, 512, bnum=2, stride=2)
  48. def forward(self, x):
  49. x = self.conv1(x)
  50. x = F.relu(self.bn1(x))
  51. x = self.maxpool(x)
  52. x = self.layer1(x)
  53. feat8 = self.layer2(x) # 1/8
  54. feat16 = self.layer3(feat8) # 1/16
  55. feat32 = self.layer4(feat16) # 1/32
  56. return feat8, feat16, feat32