1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| 残差网络构建 import torch from torch import nn
from model.resdual import Resdual
class Resnet(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Sequential( nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2), nn.BatchNorm2d(64), nn.ReLU() )
self.conv2 = nn.Sequential( nn.MaxPool2d(kernel_size=3, stride=2), Resdual(64,64), Resdual(64,64), Resdual(64,64) )
self.conv3 = nn.Sequential( Resdual(64,128,stride=2), Resdual(128,128), Resdual(128, 128), Resdual(128, 128) )
self.conv4 = nn.Sequential( Resdual(128,256,stride=2), Resdual(256,256), Resdual(256, 256), Resdual(256, 256), Resdual(256, 256), Resdual(256, 256) )
self.conv5 = nn.Sequential( Resdual(256,512,stride=2), Resdual(512,512), Resdual(512, 512) )
self.average_pool = nn.AdaptiveAvgPool2d(1) self.flatten = nn.Flatten() self.fc = nn.Linear(512,6)
def forward(self,x): x = self.conv1(x) x = self.conv2(x) x = self.conv3(x) x = self.conv4(x) x = self.conv5(x) x = self.average_pool(x) x = self.flatten(x) x = self.fc(x) return x
|