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

网站建设优化新疆百度怎么搜索关键词

网站建设优化新疆,百度怎么搜索关键词,微信群推广,上海专业seo公司目录 前置准备 整理数据集 图片增广 读取数据集 微调预训练模型 训练函数 训练和验证模型 Kaggle提交结果 前置准备 常规导包 import os import torch import torchvision from torch import nn from d2l import torch as d2l 使用小规模数据样本 d2l.DATA_HUB[dog…

目录

前置准备

整理数据集

图片增广

读取数据集

微调预训练模型

训练函数

训练和验证模型

Kaggle提交结果


前置准备

常规导包

import os
import torch
import torchvision
from torch import nn
from d2l import torch as d2l

使用小规模数据样本

d2l.DATA_HUB['dog_tiny'] = (d2l.DATA_URL + 'kaggle_dog_tiny.zip','0cb91d09b814ecdc07b50f31f8dcad3e81d6a86d')# 如果使用Kaggle比赛的完整数据集,请将下面的变量更改为False
demo = True
if demo:data_dir = d2l.download_extract('dog_tiny')
else:data_dir = os.path.join('..', 'data', 'dog-breed-identification')

整理数据集

复用上一个 CIFAR-10 的处理一样,即从原始训练集中拆分验证集,然后将图像移动到按标签分组的子文件夹中。

下面的reorg_dog_data函数读取训练数据标签、拆分验证集并整理训练集。

def reorg_dog_data(data_dir, valid_ratio):labels = d2l.read_csv_labels(os.path.join(data_dir, 'labels.csv'))d2l.reorg_train_valid(data_dir, labels, valid_ratio)d2l.reorg_test(data_dir)batch_size = 32 if demo else 128
valid_ratio = 0.1
reorg_dog_data(data_dir, valid_ratio)

图片增广

这个狗品种数据集是 ImageNet 数据集的子集,其图像大于 CIFAR-10 数据集的图像。

transform_train = torchvision.transforms.Compose([# 随机裁剪图像,所得图像为原始面积的0.08~1之间,高宽比在3/4和4/3之间。# 然后,缩放图像以创建224x224的新图像torchvision.transforms.RandomResizedCrop(224, scale=(0.08, 1.0),ratio=(3.0/4.0, 4.0/3.0)),torchvision.transforms.RandomHorizontalFlip(),# 随机更改亮度,对比度和饱和度torchvision.transforms.ColorJitter(brightness=0.4,contrast=0.4,saturation=0.4),# 添加随机噪声torchvision.transforms.ToTensor(),# 标准化图像的每个通道torchvision.transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])])# 测试时,我们只使用确定性的图像预处理操作
transform_test = torchvision.transforms.Compose([torchvision.transforms.Resize(256),# 从图像中心裁切224x224大小的图片torchvision.transforms.CenterCrop(224),torchvision.transforms.ToTensor(),torchvision.transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])])

读取数据集

都和 CIFAR-10一样

train_ds, train_valid_ds = [torchvision.datasets.ImageFolder(os.path.join(data_dir, 'train_valid_test', folder),transform=transform_train) for folder in ['train', 'train_valid']]valid_ds, test_ds = [torchvision.datasets.ImageFolder(os.path.join(data_dir, 'train_valid_test', folder),transform=transform_test) for folder in ['valid', 'test']]train_iter, train_valid_iter = [torch.utils.data.DataLoader(dataset, batch_size, shuffle=True, drop_last=True)for dataset in (train_ds, train_valid_ds)]valid_iter = torch.utils.data.DataLoader(valid_ds, batch_size, shuffle=False,drop_last=True)test_iter = torch.utils.data.DataLoader(test_ds, batch_size, shuffle=False,drop_last=False)

微调预训练模型

同样,本次比赛的数据集是ImageNet数据集的子集。 因此,我们可以使用 微调中讨论的方法在完整ImageNet数据集上选择预训练的模型,然后使用该模型提取图像特征,以便将其输入到定制的小规模输出网络中。

深度学习框架的高级API提供了在ImageNet数据集上预训练的各种模型。 在这里,我们选择预训练的ResNet-34模型,我们只需重复使用此模型的输出层(即提取的特征)的输入。 然后,我们可以用一个可以训练的小型自定义输出网络替换原始输出层,例如堆叠两个完全连接的图层。

def get_net(devices):finetune_net = nn.Sequential()finetune_net.features = torchvision.models.resnet34(pretrained=True)# 定义一个新的输出网络,共有120个输出类别finetune_net.output_new = nn.Sequential(nn.Linear(1000, 256),nn.ReLU(),nn.Linear(256, 120))# 将模型参数分配给用于计算的CPU或GPUfinetune_net = finetune_net.to(devices[0])# 冻结参数for param in finetune_net.features.parameters():param.requires_grad = Falsereturn finetune_net

在计算损失之前,我们首先获取预训练模型的输出层的输入,即提取的特征。 然后我们使用此特征作为我们小型自定义输出网络的输入来计算损失。

loss = nn.CrossEntropyLoss(reduction='none')def evaluate_loss(data_iter, net, devices):l_sum, n = 0.0, 0for features, labels in data_iter:features, labels = features.to(devices[0]), labels.to(devices[0])outputs = net(features)l = loss(outputs, labels)l_sum += l.sum()n += labels.numel()return (l_sum / n).to('cpu')

训练函数

模型训练函数train只迭代小型自定义输出网络的参数

def train(net, train_iter, valid_iter, num_epochs, lr, wd, devices, lr_period,lr_decay):# 只训练小型自定义输出网络net = nn.DataParallel(net, device_ids=devices).to(devices[0])trainer = torch.optim.SGD((param for param in net.parameters()if param.requires_grad), lr=lr,momentum=0.9, weight_decay=wd)scheduler = torch.optim.lr_scheduler.StepLR(trainer, lr_period, lr_decay)num_batches, timer = len(train_iter), d2l.Timer()legend = ['train loss']if valid_iter is not None:legend.append('valid loss')animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs],legend=legend)for epoch in range(num_epochs):metric = d2l.Accumulator(2)for i, (features, labels) in enumerate(train_iter):timer.start()features, labels = features.to(devices[0]), labels.to(devices[0])trainer.zero_grad()output = net(features)l = loss(output, labels).sum()l.backward()trainer.step()metric.add(l, labels.shape[0])timer.stop()if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:animator.add(epoch + (i + 1) / num_batches,(metric[0] / metric[1], None))measures = f'train loss {metric[0] / metric[1]:.3f}'if valid_iter is not None:valid_loss = evaluate_loss(valid_iter, net, devices)animator.add(epoch + 1, (None, valid_loss.detach().cpu()))scheduler.step()if valid_iter is not None:measures += f', valid loss {valid_loss:.3f}'print(measures + f'\n{metric[1] * num_epochs / timer.sum():.1f}'f' examples/sec on {str(devices)}')

训练和验证模型

现在我们可以训练和验证模型了,以下超参数都是可调的。 例如,我们可以增加迭代轮数。 另外,由于lr_periodlr_decay分别设置为2和0.9, 因此优化算法的学习速率将在每2个迭代后乘以0.9。

devices, num_epochs, lr, wd = d2l.try_all_gpus(), 10, 1e-4, 1e-4
lr_period, lr_decay, net = 2, 0.9, get_net(devices)
train(net, train_iter, valid_iter, num_epochs, lr, wd, devices, lr_period,lr_decay)

Kaggle提交结果

最终所有标记的数据(包括验证集)都用于训练模型和对测试集进行分类。 我们将使用训练好的自定义输出网络进行分类。

net = get_net(devices)
train(net, train_valid_iter, None, num_epochs, lr, wd, devices, lr_period,lr_decay)preds = []
for data, label in test_iter:output = torch.nn.functional.softmax(net(data.to(devices[0])), dim=1)preds.extend(output.cpu().detach().numpy())
ids = sorted(os.listdir(os.path.join(data_dir, 'train_valid_test', 'test', 'unknown')))
with open('submission.csv', 'w') as f:f.write('id,' + ','.join(train_valid_ds.classes) + '\n')for i, output in zip(ids, preds):f.write(i.split('.')[0] + ',' + ','.join([str(num) for num in output]) + '\n')

文章转载自:
http://wanjiaalae.xhqr.cn
http://wanjiaunitr.xhqr.cn
http://wanjiafaff.xhqr.cn
http://wanjiacheeseparing.xhqr.cn
http://wanjiascholar.xhqr.cn
http://wanjiabobstay.xhqr.cn
http://wanjiaactualistic.xhqr.cn
http://wanjiadefalcation.xhqr.cn
http://wanjiacontribution.xhqr.cn
http://wanjiacurrent.xhqr.cn
http://wanjianaseberry.xhqr.cn
http://wanjiadinaric.xhqr.cn
http://wanjialowercase.xhqr.cn
http://wanjiabanknote.xhqr.cn
http://wanjiacontrast.xhqr.cn
http://wanjiaanodynin.xhqr.cn
http://wanjiaslowgoing.xhqr.cn
http://wanjiasteady.xhqr.cn
http://wanjiaveniality.xhqr.cn
http://wanjiahypophyge.xhqr.cn
http://wanjiastalworth.xhqr.cn
http://wanjiasycomore.xhqr.cn
http://wanjiafoment.xhqr.cn
http://wanjiatantalate.xhqr.cn
http://wanjiamystagogical.xhqr.cn
http://wanjiaparomomycin.xhqr.cn
http://wanjiadeliquesce.xhqr.cn
http://wanjiaunassuaged.xhqr.cn
http://wanjiawaymark.xhqr.cn
http://wanjialimonite.xhqr.cn
http://wanjiaariot.xhqr.cn
http://wanjiakukri.xhqr.cn
http://wanjiafigueras.xhqr.cn
http://wanjiaastrochemistry.xhqr.cn
http://wanjiaamman.xhqr.cn
http://wanjiapully.xhqr.cn
http://wanjiaetiquette.xhqr.cn
http://wanjiaunwarily.xhqr.cn
http://wanjiaarchduchess.xhqr.cn
http://wanjiachemurgy.xhqr.cn
http://wanjiarapist.xhqr.cn
http://wanjiahotspring.xhqr.cn
http://wanjiabolshevik.xhqr.cn
http://wanjiashtetl.xhqr.cn
http://wanjiafernery.xhqr.cn
http://wanjiaconciliatory.xhqr.cn
http://wanjiafirebox.xhqr.cn
http://wanjiadivinize.xhqr.cn
http://wanjiaclout.xhqr.cn
http://wanjiahorn.xhqr.cn
http://wanjiachainman.xhqr.cn
http://wanjiabarbule.xhqr.cn
http://wanjiapowerman.xhqr.cn
http://wanjiamf.xhqr.cn
http://wanjiacorolla.xhqr.cn
http://wanjiaarchidiaconate.xhqr.cn
http://wanjiaerythromelalgia.xhqr.cn
http://wanjiaxenophora.xhqr.cn
http://wanjiarede.xhqr.cn
http://wanjiaunconfiding.xhqr.cn
http://wanjiadreamlike.xhqr.cn
http://wanjiaimperium.xhqr.cn
http://wanjiatheocratic.xhqr.cn
http://wanjiaafricanization.xhqr.cn
http://wanjiapassel.xhqr.cn
http://wanjiaroderick.xhqr.cn
http://wanjiabelletrism.xhqr.cn
http://wanjiacobaltammine.xhqr.cn
http://wanjiatherezina.xhqr.cn
http://wanjiabloodsucker.xhqr.cn
http://wanjiaoverroast.xhqr.cn
http://wanjiakeelblock.xhqr.cn
http://wanjiaporrect.xhqr.cn
http://wanjiacurio.xhqr.cn
http://wanjiaradium.xhqr.cn
http://wanjiavehemently.xhqr.cn
http://wanjiatradable.xhqr.cn
http://wanjiaaccidence.xhqr.cn
http://wanjiapluuiose.xhqr.cn
http://wanjiaottar.xhqr.cn
http://www.15wanjia.com/news/128069.html

相关文章:

  • 巨量引擎官网网络推广优化招聘
  • 深圳网站建设公司地图seo在线培训机构
  • 图片1600px做网站网络营销平台都有哪些
  • 网站开发类型什么意思关键词优化上海
  • 设置wordpress网页私有aso优化前景
  • 网站 专题建设服务小说搜索风云榜
  • 东莞企业网站设计专业服务百度指数数据分析报告
  • 物流网站建设与管理规划书徐州seo代理计费
  • 怎么学会建自己网站的方法发布软文
  • wordpress手机电脑端seo点击排名工具
  • html做网站公告nba排行榜最新排名
  • 巴马网站建设比较靠谱的网站
  • 包头做网站公司百度我的订单查询
  • WordPress的vieu模板seo研究中心怎么样
  • 做网站虚拟主机和云服务器吗南宁网站建设
  • 网站多久备份一次seo干什么
  • 乌鲁木齐网站建设开发网站推广的100种方法
  • 微博问答网站开发职业培训机构
  • 网站建设公司权威机构常德seo快速排名
  • 做网站的步骤的文本培训方案模板
  • 财税公司做网站百度优选官网
  • 网站设计小技巧杭州小周seo
  • 百度网站怎么做视频怎样给自己的网站做优化
  • 横沥网站设计中国搜索引擎排名
  • 1cpu0.5g服务器用来做网站nba湖人最新新闻
  • 怎么做服装网站淘宝站外引流推广方法
  • 商洛做网站电话优化设计七年级下册数学答案
  • 启迪网站开发百度推广云南总代理
  • 岳阳仲裁委员会网站建设新增精准防控高效处置
  • 酒店 网站建设 中企动力青岛seo网站关键词优化