下圖為AlexNet的結構圖,
下圖也是AlexNet的結構圖,
只是把中間部分拆成兩半作處理,
主要由5層Convolution Layer+3層Fully-Connected Layer所組成
下面為AlexNet結構第一種寫法:
import torch.nn as nn class AlexNet(nn.Module): def __init__(self): super(AlexNet, self).__init__() self.conv1 = nn.Sequential( #227*227*3 nn.Conv2d(3, 96, 11, 4, 0), #55*55*96 nn.ReLU(), nn.MaxPool2d(3, 2)) #27*27*96 self.conv2 = nn.Sequential( nn.Conv2d(96, 256, 5, 1, 2), #27*27*256 nn.ReLU(), nn.MaxPool2d(3, 2)) #13*13*256 self.conv3 = nn.Sequential( nn.Conv2d(256, 384, 3, 1, 1), #13*13*384 nn.ReLU()) self.conv4 = nn.Sequential( nn.Conv2d(384, 384, 3, 1, 1), #13*13*384 nn.ReLU()) self.conv5 = nn.Sequential( nn.Conv2d(384, 256, 3, 1, 1), #13*13*256 nn.ReLU(), nn.MaxPool2d(3, 2)) #6*6*256 self.dense = nn.Sequential( nn.Linear(9216, 4096), nn.ReLU(), nn.Dropout(0.5), nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(0.5), nn.Linear(4096, 1000))
def forward(self, x): conv1_out = self.conv1(x) conv2_out = self.conv2(conv1_out) conv3_out = self.conv3(conv2_out) conv4_out = self.conv4(conv3_out) conv5_out = self.conv5(conv4_out) res = conv5_out.view(conv5_out.size(0), -1) out = self.dense(res) return out |
下圖為model輸出結果:
下面為AlexNet結構第二種寫法(使用官方預設的):
import torchvision model = torchvision.models.AlexNet() |
可以導入預訓練的model權重, 只要在()內填入pretrained參數即可, 如下:
import torchvision.models as models model = models.AlexNet(pretrained=True) |
下圖為model輸出結果:
留言列表