物流公司在哪做网站,小型公司注册资金写多少合适,云游戏免费平台,如何将网站提交给谷歌标注数据集很贵
网络架构
1.一般神经网络分为两块#xff0c;一是特征抽取原始像素变成容易线性分割的特征#xff0c;二是线性分类器来做分类
微调
1.原数据集不能直接使用#xff0c;因为标号发生改变#xff0c;通过微调可以仍然对我数据集做特征提取
2.pre-train源…标注数据集很贵
网络架构
1.一般神经网络分为两块一是特征抽取原始像素变成容易线性分割的特征二是线性分类器来做分类
微调
1.原数据集不能直接使用因为标号发生改变通过微调可以仍然对我数据集做特征提取
2.pre-train源数据集后迁移模型中的架构调整权重
3.是一个目标数据集上的正常训练任务但使用更强的正则化更小的学习率、更少的数据迭代
4.源数据集远复杂于目标数据通常微调效果更好
重用分类器权重
1.源数据集可能也有目标数据中的部分标号
2.可以使用预训练好的模型分类器中对应标号对应的向量来初始化
固定一些层
1.神经网络通常学习有层次的特征表达低层次的特征更加通用高层次的特征则和数据集相关
2.可以固定底部一些层的参数不参与更新更强的正则化
总结
1.微调通过使用在大数据上得到的预训练好的模型来初始化模型权重来提升精度
2.预训练模型质量很重要
3.微调通常速度更快、精度更高
代码实现——热狗识别
%matplotlib inline
import os
import torch
import torchvision
from torch import nn
from d2l import torch as d2l获取数据集
解压下载热狗数据集一共1400张热狗“正向”图片其他尽可能多的其他食物“负向”图片解压下载的数据集我们获得了两个文件夹hotdog/train和hotdog/test。 这两个文件夹都有hotdog有热狗和not-hotdog无热狗两个子文件夹 子文件夹内都包含相应类的图像。
d2l.DATA_HUB[hotdog] (d2l.DATA_URL hotdog.zip,fba480ffa8aa7e0febbb511d181409f899b9baa5)data_dir d2l.download_extract(hotdog)Downloading ../data/hotdog.zip from http://d2l-data.s3-accelerate.amazonaws.com/hotdog.zip...读取训练集和测试集的图片
train_imgs torchvision.datasets.ImageFolder(os.path.join(data_dir, train))
test_imgs torchvision.datasets.ImageFolder(os.path.join(data_dir, test))前8个正类、后8个负类图片的大小和高宽比不同。
hotdogs [train_imgs[i][0] for i in range(8)]
not_hotdogs [train_imgs[-i - 1][0] for i in range(8)]
d2l.show_images(hotdogs not_hotdogs, 2, 8, scale1.4);训练从图像中裁切随机大小和随机长宽比的区域然后将该区域缩放为224X224 输入图像。 测试我们将图像的高度和宽度都缩放到256像素然后裁剪中央224X224 区域作为输入。
此外对于RGB红、绿和蓝颜色通道我们分别标准化每个通道。 具体而言该通道的每个值减去该通道的平均值然后将结果除以该通道的标准差。
# 使用RGB通道的均值和标准差以标准化每个通道
normalize torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])train_augs torchvision.transforms.Compose([torchvision.transforms.RandomResizedCrop(224),torchvision.transforms.RandomHorizontalFlip(),torchvision.transforms.ToTensor(),normalize])test_augs torchvision.transforms.Compose([torchvision.transforms.Resize([256, 256]),torchvision.transforms.CenterCrop(224),torchvision.transforms.ToTensor(),normalize])初始化模型
使用在ImageNet数据集上预训练的ResNet-18作为源模型。 在这里我们指定pretrainedTrue以自动下载预训练的模型参数。 如果首次使用此模型则需要连接互联网才能下载。
pretrained_net torchvision.models.resnet18(pretrainedTrue)/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:208: UserWarning: The parameter pretrained is deprecated since 0.13 and may be removed in the future, please use weights instead.warnings.warn(
/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or None for weights are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing weightsResNet18_Weights.IMAGENET1K_V1. You can also use weightsResNet18_Weights.DEFAULT to get the most up-to-date weights.warnings.warn(msg)
Downloading: https://download.pytorch.org/models/resnet18-f37072fd.pth to /root/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth
100%|██████████| 44.7M/44.7M [00:0000:00, 111MB/s]fc预训练的源模型实例包含许多特征层和一个输出层fc。 此划分的主要目的是促进对除输出层以外所有层的模型参数进行微调。
# fc
pretrained_net.fcLinear(in_features512, out_features1000, biasTrue)目标模型finetune_net中成员变量features的参数被初始化为源模型相应层的模型参数。 由于模型参数是在ImageNet数据集上预训练的并且足够好因此通常只需要较小的学习率即可微调这些参数。
成员变量output的参数是随机初始化的通常需要更高的学习率才能从头开始训练。 假设Trainer实例中的学习率为n我们将成员变量output中参数的学习率设置为10n。
finetune_net torchvision.models.resnet18(pretrainedTrue)
finetune_net.fc nn.Linear(finetune_net.fc.in_features, 2)
nn.init.xavier_uniform_(finetune_net.fc.weight);训练模型微调
# 如果param_groupTrue输出层中的模型参数将使用十倍的学习率
def train_fine_tuning(net, learning_rate, batch_size128, num_epochs5,param_groupTrue):train_iter torch.utils.data.DataLoader(torchvision.datasets.ImageFolder(os.path.join(data_dir, train), transformtrain_augs),batch_sizebatch_size, shuffleTrue)test_iter torch.utils.data.DataLoader(torchvision.datasets.ImageFolder(os.path.join(data_dir, test), transformtest_augs),batch_sizebatch_size)devices d2l.try_all_gpus()loss nn.CrossEntropyLoss(reductionnone)if param_group:params_1x [param for name, param in net.named_parameters()if name not in [fc.weight, fc.bias]]trainer torch.optim.SGD([{params: params_1x},{params: net.fc.parameters(),lr: learning_rate * 10}],lrlearning_rate, weight_decay0.001)else:trainer torch.optim.SGD(net.parameters(), lrlearning_rate,weight_decay0.001)d2l.train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,devices)使用较小的学习率
train_fine_tuning(finetune_net, 5e-5)loss 0.238, train acc 0.910, test acc 0.941
360.1 examples/sec on [device(typecuda, index0)]为了比较定义相同的模型但需较大学习率
scratch_net torchvision.models.resnet18()
scratch_net.fc nn.Linear(scratch_net.fc.in_features, 2)
train_fine_tuning(scratch_net, 5e-4, param_groupFalse)loss 0.403, train acc 0.821, test acc 0.791
366.6 examples/sec on [device(typecuda, index0)]