当前位置: 首页 > news >正文

深度学习速成:模型的使用与修改,保存与读取

1.使用与修改

VGG16为例

import torchvision import torch #trian_data=torchvision.datasets.imagenet("../data_imgnet",train=True,transform=torchvision.transforms.ToTensor(),download=True) vgg16_false=torchvision.models.vgg16(pretrained=False) vgg16_true=torchvision.models.vgg16(weights=torchvision.models.VGG16_Weights.IMAGENET1K_V1) print(vgg16_true) #vgg16_true.add_module("new_fc",torch.nn.Linear(1000,10))#追加全結合層 vgg16_true.classifier.add_module("new_fc",torch.nn.Linear(1000,10))#追加全結合層 print(vgg16_true) print(vgg16_false) vgg16_false.classifier[6]=torch.nn.Linear(4096,10)#在(6)那里修改全连接层 print(vgg16_false)

修改完的输出

2.保存与读取

2.1保存

import torch import torchvision import torch.nn as nn vgg16=torchvision.models.vgg16(weights=None) #保存1 保存整个模型(结构+参数) torch.save(vgg16,"vgg16.pth") #保存2 只保存模型参数(官方推荐,内存小) torch.save(vgg16.state_dict(),"vgg16_params.pth") class tudui(nn.Module): def __init__(self): super().__init__() self.conv1=nn.Conv2d(3,32,5,padding=2)# self.maxpool1=nn.MaxPool2d(2)# self.conv2=nn.Conv2d(32,32,5,padding=2) self.maxpool2=nn.MaxPool2d(2) self.conv3=nn.Conv2d(32,64,5,padding=2) self.maxpool3=nn.MaxPool2d(2) self.flatten=nn.Flatten()# self.linear1=nn.Linear(1024,64)# self.linear2=nn.Linear(64,10)# def forward(self,x): x=self.conv1(x) x=self.maxpool1(x) x=self.conv2(x) x=self.maxpool2(x) x=self.conv3(x) x=self.maxpool3(x) x=self.flatten(x) x=self.linear1(x) x=self.linear2(x) return x tudui_model=tudui() torch.save(tudui_model,"tudui_params.pth")#方法一保存

2.2 读取

import torch import torchvision from model_save import * #方式一 加载模型 """ vgg16=torch.load("vgg16.pth") print(vgg16) """ #方式二 加载模型参数 vgg16=torchvision.models.vgg16(weights=None) vgg16.load_state_dict(torch.load("vgg16_params.pth")) print(vgg16) """ vgg16_params=torch.load("vgg16_params.pth")#字典形式 print(vgg16_params) """ #方式一有陷阱 需要能访问到save时的类定义 im model=torch.load("tudui_params.pth") print(model)

2.3输出结果

(base) PS E:\desktop\deeplearning> & D:\miniconda3\envs\pytorch_py312\python.exe e:/desktop/deeplearning/src/model_load.py e:\desktop\deeplearning\src\model_load.py:10: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. vgg16.load_state_dict(torch.load("vgg16_params.pth")) VGG( (features): Sequential( (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (1): ReLU(inplace=True) (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (3): ReLU(inplace=True) (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (6): ReLU(inplace=True) (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (8): ReLU(inplace=True) (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (11): ReLU(inplace=True) (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (13): ReLU(inplace=True) (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (15): ReLU(inplace=True) (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (18): ReLU(inplace=True) (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (20): ReLU(inplace=True) (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (22): ReLU(inplace=True) (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (25): ReLU(inplace=True) (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (27): ReLU(inplace=True) (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (29): ReLU(inplace=True) (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) ) (avgpool): AdaptiveAvgPool2d(output_size=(7, 7)) (classifier): Sequential( (0): Linear(in_features=25088, out_features=4096, bias=True) (1): ReLU(inplace=True) (2): Dropout(p=0.5, inplace=False) (3): Linear(in_features=4096, out_features=4096, bias=True) (4): ReLU(inplace=True) (5): Dropout(p=0.5, inplace=False) (6): Linear(in_features=4096, out_features=1000, bias=True) ) ) e:\desktop\deeplearning\src\model_load.py:19: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. model=torch.load("tudui_params.pth") tudui( (conv1): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)) (maxpool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) (conv2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)) (maxpool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) (conv3): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)) (maxpool3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) (flatten): Flatten(start_dim=1, end_dim=-1) (linear1): Linear(in_features=1024, out_features=64, bias=True) (linear2): Linear(in_features=64, out_features=10, bias=True) )
http://www.jsqmd.com/news/344179/

相关文章:

  • 2026年开年,湖北企业如何甄选靠谱的污水处理药剂供应商? - 2026年企业推荐榜
  • 2026年湖南污水处理制造厂综合实力与服务信誉深度评析 - 2026年企业推荐榜
  • 2026年亳州报废车回收实力厂家深度评估与推荐 - 2026年企业推荐榜
  • 2026年知名的长春AI数字人制作/数字人制作口碑推荐 - 行业平台推荐
  • 2026年知名的吉林数字人制作精品推荐 - 行业平台推荐
  • 2026年佛山空冷器供货可靠厂家综合评估与推荐 - 2026年企业推荐榜
  • 2026年佛山闭式塔空冷器制造厂综合评估与精选推荐 - 2026年企业推荐榜
  • 演示下 一个qkv网络逐步趋向出局部加法结构的过程的每个详细的子步骤 -----来自deepseek的回答
  • 2026年口碑好的长春AI数字人制作精选排行推荐 - 行业平台推荐
  • 2026现阶段河北知名的半导体封装甲酸真空回流焊生产商精选 - 2026年企业推荐榜
  • 2026年宜兴MBBR填料选购指南:深度解析五家优质服务商 - 2026年企业推荐榜
  • 2026年Q1徐州儿童房窗帘品牌专业解析与五强推荐 - 2026年企业推荐榜
  • 2026年靠谱的数字人制作/长春数字人制作优选推荐 - 行业平台推荐
  • 2026年比较好的去氧化皮去毛刺机靠谱厂家盘点 - 行业平台推荐
  • Granite-4.0-H-350M实战:轻松实现多语言问答与文本摘要
  • 2026上海全屋定制服务商数据盘点与选购指南 - 2026年企业推荐榜
  • 2026年临沂高性价比干洗服务商深度测评与推荐 - 2026年企业推荐榜
  • 实测CTC语音唤醒模型:93%准确率的‘小云小云‘识别
  • 2026年Q1临沂干洗服务商综合测评与选择指南 - 2026年企业推荐榜
  • Linux常用命令大全:RMBG-2.0运维指南
  • 2026年成都餐桌石材厂家评测:如何选择优质服务商? - 2026年企业推荐榜
  • RexUniNLU部署案例:中小企业客服对话意图+情感双任务解析
  • MusePublic Art Studio效果实测:长尾提示词对复杂场景理解能力
  • ollama部署embeddinggemma-300m:300M参数模型如何实现媲美大模型的语义精度
  • Chord与VSCode完美搭配:C++视频分析开发环境配置
  • 5分钟搭建私有AI股票分析:Ollama镜像完全指南
  • 2026年口碑好的三维动画长期口碑汇总 - 行业平台推荐
  • 测试用例库怎么搭体系?结合7款工具能力,讲清沉淀与复用路径
  • MyBatis体系结构与工作原理 下篇
  • 2026年银烧结设备服务商综合实力与选型指南 - 2026年企业推荐榜