torchvision.models
官方预训练模型调用代码:https://github.com/pytorch/vision/tree/master/torchvision/models
官方文档地址 :https://pytorch.org/docs/master/torchvision/models.html
预训练模型下载:
Classification 分类
AlexNet
model_urls = {
'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth',
}
VGG
model_urls = {
'vgg11': 'https://download.pytorch.org/models/vgg11-bbd30ac9.pth',
'vgg13': 'https://download.pytorch.org/models/vgg13-c768596a.pth',
'vgg16': 'https://download.pytorch.org/models/vgg16-397923af.pth',
'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth',
'vgg11_bn': 'https://download.pytorch.org/models/vgg11_bn-6002323d.pth',
'vgg13_bn': 'https://download.pytorch.org/models/vgg13_bn-abd245e5.pth',
'vgg16_bn': 'https://download.pytorch.org/models/vgg16_bn-6c64b313.pth',
'vgg19_bn': 'https://download.pytorch.org/models/vgg19_bn-c79401a0.pth',
}
ResNet
model_urls = {
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
}
ResNeXt
model_urls = {
'resnext50_32x4d': 'https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth',
'resnext101_32x8d': 'https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth',
'resnext101_32x16d': 'https://download.pytorch.org/models/ig_resnext101_32x16-c6f796b0.pth'
}
Wide ResNet
model_urls = {
'wide_resnet50_2': 'https://download.pytorch.org/models/wide_resnet50_2-95faca4d.pth',
'wide_resnet101_2': 'https://download.pytorch.org/models/wide_resnet101_2-32ee1156.pth'
}
SqueezeNet
model_urls = {
'squeezenet1_0': 'https://download.pytorch.org/models/squeezenet1_0-a815701f.pth',
'squeezenet1_1': 'https://download.pytorch.org/models/squeezenet1_1-f364aa15.pth',
}
DenseNet
model_urls = {
'densenet121': 'https://download.pytorch.org/models/densenet121-a639ec97.pth',
'densenet169': 'https://download.pytorch.org/models/densenet169-b2777c0a.pth',
'densenet201': 'https://download.pytorch.org/models/densenet201-c1103571.pth',
'densenet161': 'https://download.pytorch.org/models/densenet161-8d451a50.pth',
}
Inception v3
model_urls = {
## Inception v3 ported from TensorFlow
'inception_v3_google': 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth',
}
GoogLeNet
model_urls = {
# GoogLeNet ported from TensorFlow
'googlenet': 'https://download.pytorch.org/models/googlenet-1378be20.pth',
}
ShuffleNet v2
model_urls = {
'shufflenetv2_x0.5': 'https://download.pytorch.org/models/shufflenetv2_x0.5-f707e7126e.pth',
'shufflenetv2_x1.0': 'https://download.pytorch.org/models/shufflenetv2_x1-5666bf0f80.pth',
'shufflenetv2_x1.5': None,
'shufflenetv2_x2.0': None,
}
MobileNet v2
model_urls = {
'mobilenet_v2': 'https://download.pytorch.org/models/mobilenet_v2-b0353104.pth',
}
MobileNet v3
model_urls = {
'mobilenet_v3_small': "https://download.pytorch.org/models/mobilenet_v3_small-047dcff4.pth",
'mobilenet_v3_large': "https://download.pytorch.org/models/mobilenet_v3_large-5c1a4163.pth"
}
MNASNet
_MODEL_URLS = {
"mnasnet0_5":
"https://download.pytorch.org/models/mnasnet0.5_top1_67.823-3ffadce67e.pth",
"mnasnet0_75": None,
"mnasnet1_0":
"https://download.pytorch.org/models/mnasnet1.0_top1_73.512-f206786ef8.pth",
"mnasnet1_3": None
}
CONVNEXT
_MODEL_URLS = {
"convnext_tiny": "https://download.pytorch.org/models/convnext_tiny-983f1562.pth",
"convnext_small": "https://download.pytorch.org/models/convnext_small-0c510722.pth",
"convnext_base": "https://download.pytorch.org/models/convnext_base-6075fbad.pth",
"convnext_large": "https://download.pytorch.org/models/convnext_large-ea097f82.pth",
}
REGNET
_MODEL_URLS = {
"regnet_y_400mf": "https://download.pytorch.org/models/regnet_y_400mf-e6988f5f.pth",
"regnet_y_800mf": "https://download.pytorch.org/models/regnet_y_800mf-58fc7688.pth",
"regnet_y_1_6gf": "https://download.pytorch.org/models/regnet_y_1_6gf-0d7bc02a.pth",
"regnet_y_3_2gf": "https://download.pytorch.org/models/regnet_y_3_2gf-9180c971.pth",
"regnet_y_8gf": "https://download.pytorch.org/models/regnet_y_8gf-dc2b1b54.pth",
"regnet_y_16gf": "https://download.pytorch.org/models/regnet_y_16gf-3e4a00f9.pth",
"regnet_y_32gf": "https://download.pytorch.org/models/regnet_y_32gf-8db6d4b5.pth",
"regnet_y_128gf": "https://download.pytorch.org/models/regnet_y_128gf_swag-c8ce3e52.pth",
"regnet_x_400mf": "https://download.pytorch.org/models/regnet_x_400mf-62229a5f.pth",
"regnet_x_800mf": "https://download.pytorch.org/models/regnet_x_800mf-94a99ebd.pth",
"regnet_x_1_6gf": "https://download.pytorch.org/models/regnet_x_1_6gf-a12f2b72.pth",
"regnet_x_3_2gf": "https://download.pytorch.org/models/regnet_x_3_2gf-7071aa85.pth",
"regnet_x_8gf": "https://download.pytorch.org/models/regnet_x_8gf-2b70d774.pth",
"regnet_x_16gf": "https://download.pytorch.org/models/regnet_x_16gf-ba3796d7.pth",
"regnet_x_32gf": "https://download.pytorch.org/models/regnet_x_32gf-6eb8fdc6.pth",
}
Semantic Segmentation 语义分割
FCN ResNet50, ResNet101
model_urls = {
'fcn_resnet50_coco': 'https://download.pytorch.org/models/fcn_resnet50_coco-1167a1af.pth',
'fcn_resnet101_coco': 'https://download.pytorch.org/models/fcn_resnet101_coco-7ecb50ca.pth'
}
DeepLabV3 ResNet50, ResNet101
model_urls = {
'deeplabv3_resnet50_coco': 'https://download.pytorch.org/models/deeplabv3_resnet50_coco-cd0a2569.pth',
'deeplabv3_resnet101_coco': 'https://download.pytorch.org/models/deeplabv3_resnet101_coco-586e9e4e.pth'
}
Object Detection 目标检测
Faster R-CNN ResNet-50 FPN
model_urls = {
'fasterrcnn_resnet50_fpn_coco':
'https://download.pytorch.org/models/fasterrcnn_resnet50_fpn_coco-258fb6c6.pth',
}
Instance Segmentation 实例分割
Mask R-CNN ResNet-50 FPN
model_urls = {
'maskrcnn_resnet50_fpn_coco':
'https://download.pytorch.org/models/maskrcnn_resnet50_fpn_coco-bf2d0c1e.pth',
}
Person Keypoint Detection 人体关键点
Keypoint R-CNN ResNet-50 FPN
model_urls = {
# legacy model for BC reasons, see https://github.com/pytorch/vision/issues/1606
'keypointrcnn_resnet50_fpn_coco_legacy':
'https://download.pytorch.org/models/keypointrcnn_resnet50_fpn_coco-9f466800.pth',
'keypointrcnn_resnet50_fpn_coco':
'https://download.pytorch.org/models/keypointrcnn_resnet50_fpn_coco-fc266e95.pth',
}
Video classification 视频分类
ResNet 3D
model_urls = {
## ResNet 3D
'r3d_18': 'https://download.pytorch.org/models/r3d_18-b3b3357e.pth'
}
ResNet Mixed Convolution
model_urls = {
## ResNet Mixed Convolution
'mc3_18': 'https://download.pytorch.org/models/mc3_18-a90a0ba3.pth'
}
ResNet (2+1)D
model_urls = {
## ResNet (2+1)D
'r2plus1d_18': 'https://download.pytorch.org/models/r2plus1d_18-91a641e6.pth'
}
预训练模型加载调用:
Classification 分类
import torchvision.models as models
## 如果只需要网络结构,不需要用预训练模型的参数来初始化,pretrained=False:
resnet18 = models.resnet18(pretrained=True)
alexnet = models.alexnet(pretrained=True)
squeezenet = models.squeezenet1_0(pretrained=True)
vgg16 = models.vgg16(pretrained=True)
densenet = models.densenet161(pretrained=True)
inception = models.inception_v3(pretrained=True)
googlenet = models.googlenet(pretrained=True)
shufflenet = models.shufflenet_v2_x1_0(pretrained=True)
mobilenet = models.mobilenet_v2(pretrained=True)
resnext50_32x4d = models.resnext50_32x4d(pretrained=True)
wide_resnet50_2 = models.wide_resnet50_2(pretrained=True)
mnasnet = models.mnasnet1_0(pretrained=True)
Semantic Segmentation 语义分割
fcn_resnet50 = torchvision.models.segmentation.fcn_resnet50(pretrained=False, progress=True, num_classes=21, aux_loss=None)
fcn_resnet101 = torchvision.models.segmentation.fcn_resnet101(pretrained=False, progress=True, num_classes=21, aux_loss=None)
deeplabv3_resnet50 = torchvision.models.segmentation.deeplabv3_resnet50(pretrained=False, progress=True, num_classes=21, aux_loss=None)
deeplabv3_resnet101 = torchvision.models.segmentation.deeplabv3_resnet101(pretrained=False, progress=True, num_classes=21, aux_loss=None)
Object Detection 目标检测
fasterrcnn_resnet50_fpn = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False, progress=True, num_classes=91, pretrained_backbone=True, trainable_backbone_layers=3)
Instance Segmentation 实例分割
maskrcnn_resnet50_fpn = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=False, progress=True, num_classes=91, pretrained_backbone=True)
Person Keypoint Detection 人体关键点
keypointrcnn_resnet50_fpn = torchvision.models.detection.keypointrcnn_resnet50_fpn(pretrained=False, progress=True, num_classes=2, num_keypoints=17, pretrained_backbone=True)
Video classification 视频分类
r3d_18 = torchvision.models.video.r3d_18(pretrained=False, progress=True)
完整调用代码请移步一>官方预训练模型调用代码:
https://github.com/pytorch/vision/tree/master/torchvision/models
完整理解代码请移步一>官方文档地址:
https://pytorch.org/docs/master/torchvision/models.html
调用例子:
import os
import torch
from torch.utils.data import DataLoader
from torch import nn
from torch import optim
import torchvision.models as models
import time
# use res18
# from resnet.resnetmini import ClassificModel as Model
from datasets.read_data_sleep import PlayPhoneData
def train(data_path=r"E:\Datasets\sleep_traindata"):
# 设置超参数
batch_size = 1 # 每次训练的数据量
LR = 0.01 # 学习率
STEP_SIZE = 5 # 控制学习率变化
MAX_EPOCH = 20 # 总的训练次数
num_print = 100 # 每n个batch打印一次
playPhoneData = PlayPhoneData(data_path)
# 利用dataloader加载数据集
train_loader = torch.utils.data.DataLoader(playPhoneData, batch_size=batch_size, shuffle=True, drop_last=True)
# 生成驱动器
use_gpu = torch.cuda.is_available()
if use_gpu:
print('congratulation! You can use gpu to support acceleration')
else:
print('oppps, please use a small batch size')
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# 初始化模型
model = models.resnet18()
# 修改网络结构,将fc层1000个输出改为9个输出
fc_input_feature = model.fc.in_features
model.fc = nn.Linear(fc_input_feature, 9)
# load除最后一层的预训练权重
pretrained_weight = torch.hub.load_state_dict_from_url(
url='https://download.pytorch.org/models/resnet18-5c106cde.pth', progress=True)
del pretrained_weight['fc.weight']
del pretrained_weight['fc.bias']
model.load_state_dict(pretrained_weight, strict=False)
model.to(device)
# net = Model(8).to(device) # class_num=8分八类:睡岗(趴着睡,躺着睡,仰着睡,低头睡),玩手机(俯视玩手机,平视玩手机,侧视玩手机),其他=[0,1,2,3,4,5,6,7]
# net = Model(9).to(device) # class_num=9分九类:睡岗(趴着睡,躺着睡,低头睡),站立,半蹲,坐(背坐,正坐,侧坐),其他=[0,1,2,3,4,5,6,7,8]
# 损失函数
get_loss = nn.CrossEntropyLoss() #交叉熵损失函数
# SGD优化器 第一个参数是输入需要优化的参数,第二个是学习率,第三个是动量,大致就是借助上一次导数结果,加快收敛速度。
'''
这一行代码里面实际上包含了多种优化:
一个是动量优化,增加了一个关于上一次迭代得到的系数的偏置,借助上一次的指导,减小梯度震荡,加快收敛速度
一个是权重衰减,通过对权重增加一个(正则项),该正则项会使得迭代公式中的权重按照比例缩减,这么做的原因是,过拟合的表现一般为参数浮动大,使用小参数可以防止过拟合
'''
optimizer = optim.SGD(model.parameters(), lr=LR, momentum=0.9, weight_decay=0.001)
# optimizer = optim.Adam(net.parameters(), lr=learn_rate)
# 动态调整学习率 StepLR 是等间隔调整学习率,每step_size 令lr=lr*gamma
# 学习率衰减,随着训练的加深,目前的权重也越来越接近最优权重,原本的学习率会使得,loss上下震荡,逐步减小学习率能加快收敛速度。
# scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=STEP_SIZE, gamma=0.5, last_epoch=-1)
# Step:设置学习率下降策略
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)
loss_list = []
start = time.time()
for epoch in range(MAX_EPOCH):
running_loss = 0.0
# enumerate()是python自带的函数,用于迭代字典。参数1,是需要迭代的对象,第二参数是迭代的起始位置
for i, (inputs, labels) in enumerate(train_loader, 0):
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs) # 前向传播求出预测的值
optimizer.zero_grad() # 将梯度初始化为0
loss = get_loss(outputs, labels.long())
loss.backward() # 反向传播求梯度
optimizer.step() # 更新所有参数
running_loss += loss.item() # loss是张量,访问值时需要使用item()
loss_list.append(loss.item())
if i % num_print == num_print - 1: # 每num_print打印平均loss
print('[%d epoch, %d] loss: %.6f' % (epoch + 1, i + 1, running_loss / num_print))
running_loss = 0.0
lr = optimizer.param_groups[0]['lr'] # 查看目前的学习率
print('learn_rate : %.5f' % lr)
scheduler.step() # 根据迭代epoch更新学习率
end = time.time()
print('time:{}'.format(end - start))
torch.save(model, f'E:/model/playphone+sleepthepose/model_resnetmini_睡岗9分类{end}.pth')
if __name__ == "__main__":
train()