torchvision 加载模型/查看计算参数/更改输出层

import torch
from torchvision.models import resnet18,resnet34,resnet50,vgg11_bn,inception_v3
from thop import profile
from torchsummary import summary

#加载并查看参数与运算复杂度
model = vgg11_bn(pretrained=False).cuda() #加载模型,未预训练
input = torch.randn(1, 3, 128, 128).cuda()
flops, params = profile(model, inputs=(input, )) #计算参数与计算复杂度
flops, params = clever_format([flops, params], "%.8f")  #按格式输出
print(flops, params)


#查看模型具体结构
print(model) #直接打印模型
summary(model,input_size=(3,128,128)) #格式化打印,input_size = (channel,H,W)


#更改输出层,默认输出为[1000,],通过上面打印的信息查看模型最后一层的名字,可能是fc/classifier[-1]
#model.fc = torch.nn.Linear(model.fc.in_features,num_class).cuda() 
#model.fc.in_features是最后一层的输入大小,num_class为希望的输出大小
model.classifier[-1] = torch.nn.Linear(4096,2).cuda() #以VGG 为例
summary(model,input_size=(3,128,128)) #查看更改后的模型
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值