卷积神经网络模型之ZFNet模型实现

import torch
from torch.nn import functional as F
from torch import nn

class ZFNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1=nn.Conv2d(in_channels=3,out_channels=96,kernel_size=7,stride=2,padding=1) #[110,110,96]
        self.maxpool1=nn.MaxPool2d(kernel_size=3,stride=2,padding=1) #[55,55,96]
        
        self.conv2=nn.Conv2d(in_channels=96,out_channels=256,kernel_size=5,stride=2)#[25,25,256]
        self.maxpool2=nn.MaxPool2d(kernel_size=3,stride=2,padding=1)#[13,13,256]
        
        self.conv3=nn.Conv2d(in_channels=256,out_channels=384,kernel_size=3,stride=1,padding=1)
        
        self.conv4=nn.Conv2d(in_channels=384,out_channels=384,kernel_size=3,stride=1,padding=1)
       
        self.conv5=nn.Conv2d(in_channels=384,out_channels=256,kernel_size=3,stride=1,padding=1)
        self.maxpool3=nn.MaxPool2d(kernel_size=3,stride=2)
        
        self.fc1=nn.Linear(in_features=6*6*256,out_features=4096)
        
        self.fc2=nn.Linear(in_features=4096,out_features=4096)
        
        self.fc3=nn.Linear(in_features=4096,out_features=1000)
        
        
    def forward(self,x):
        x=F.relu(self.conv1(x))
        print("conv1:",x.shape)
        x=self.maxpool1(x)
        print("maxpool1:",x.shape)
        
        x=F.relu(self.conv2(x))
        print("conv2:",x.shape)
        x=self.maxpool2(x)
        print("maxpool2:",x.shape)
        
        x=F.relu(self.conv3(x))
        print("conv3:",x.shape)
        x=F.relu(self.conv4(x))
        print("conv4:",x.shape)
        x=self.maxpool3(F.relu(self.conv5(x)))
        print("maxpool3:",x.shape)
        
        x=x.view(-1,6*6*256)
        print(x.shape)
        x=F.relu(self.fc1(x))
        print("fc1",x.shape)
        x=F.relu(self.fc2(x))
        print("fc2",x.shape)
        x=self.fc3(x)
        print("fc3",x.shape)
        return x

model=ZFNet()
model.cuda()
print(model)

from torch.autograd import Variable
x=torch.randn(1,3,224,224)
x=Variable(x).cuda()
pred=model(x)

print(pred.shape)

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值