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

怎么cms做网站vpswindows野外大全

怎么cms做网站,vpswindows野外大全,网站建设考级,创建wordpress小工具主体思路#xff1a; 1.生成一堆锚框 2.根据真实标签为每个锚框打标(类别、偏移、mask) 3.模型为每个锚框做一个预测(类别、偏移) 4.计算上述二者的差异损失#xff0c;以更新模型weights 先读取一张图像。 它的高度和宽度分别为561和728像素。 %matplotlib inline import …主体思路 1.生成一堆锚框 2.根据真实标签为每个锚框打标(类别、偏移、mask) 3.模型为每个锚框做一个预测(类别、偏移) 4.计算上述二者的差异损失以更新模型weights 先读取一张图像。 它的高度和宽度分别为561和728像素。 %matplotlib inline import torch from d2l import torch as d2limg d2l.plt.imread(catdog.jpg) h, w img.shape[:2] h, w(561, 728)display_anchors函数定义如下。 我们在特征图fmap上生成锚框anchors每个单位像素作为锚框的中心。 由于锚框中的x,y轴坐标值anchors已经被除以特征图fmap的宽度和高度因此这些值介于0和1之间表示特征图中锚框的相对位置。 def display_anchors(fmap_w, fmap_h, s):d2l.set_figsize()# 前两个维度上的值不影响输出fmap torch.zeros((1, 10, fmap_h, fmap_w))anchors d2l.multibox_prior(fmap, sizess, ratios[1, 2, 0.5])bbox_scale torch.tensor((w, h, w, h))d2l.show_bboxes(d2l.plt.imshow(img).axes,anchors[0] * bbox_scale)锚框的尺度设置为0.15特征图的高度和宽度设置为4。图像上4行和4列的锚框的中心是均匀分布的。 display_anchors(fmap_w4, fmap_h4, s[0.15])将特征图的高度和宽度减小一半然后使用较大的锚框来检测较大的目标。 当尺度设置为0.4时一些锚框将彼此重叠。 display_anchors(fmap_w2, fmap_h2, s[0.4])进一步将特征图的高度和宽度减小一半然后将锚框的尺度增加到0.8。 此时锚框的中心即是图像的中心 display_anchors(fmap_w1, fmap_h1, s[0.8])SSD的实现 单发多框检测 定义了这样一个类别预测层通过参数num_anchors和num_classes分别指定了a 和q。 该图层使用填充为1的3X3的卷积层。此卷积层的输入和输出的宽度和高度保持不变。 %matplotlib inline import torch import torchvision from torch import nn from torch.nn import functional as F from d2l import torch as d2ldef cls_predictor(num_inputs, num_anchors, num_classes):return nn.Conv2d(num_inputs, num_anchors * (num_classes 1),kernel_size3, padding1)边界框预测层 每个锚框预测4个偏移量而不是q1个类别。 def bbox_predictor(num_inputs, num_anchors):return nn.Conv2d(num_inputs, num_anchors * 4, kernel_size3, padding1)连接多尺度的预测 def forward(x, block):return block(x)Y1 forward(torch.zeros((2, 8, 20, 20)), cls_predictor(8, 5, 10)) Y2 forward(torch.zeros((2, 16, 10, 10)), cls_predictor(16, 3, 10)) Y1.shape, Y2.shape(torch.Size([2, 55, 20, 20]), torch.Size([2, 33, 10, 10]))将通道维移到最后一维。 因为不同尺度下批量大小仍保持不变我们可以将预测结果转成二维的批量大小高X宽X通道数的格式以方便之后在维度1上的连结 def flatten_pred(pred):return torch.flatten(pred.permute(0, 2, 3, 1), start_dim1)def concat_preds(preds):return torch.cat([flatten_pred(p) for p in preds], dim1)尽管Y1和Y2在通道数、高度和宽度方面具有不同的大小我们仍然可以在同一个小批量的两个不同尺度上连接这两个预测输出。 concat_preds([Y1, Y2]).shapetorch.Size([2, 25300])高和宽减半块 高和宽减半块down_sample_blk该模块将输入特征图的高度和宽度减半可以扩大每个单元在其输出特征图中的感受野。 def down_sample_blk(in_channels, out_channels):blk []for _ in range(2):blk.append(nn.Conv2d(in_channels, out_channels,kernel_size3, padding1))blk.append(nn.BatchNorm2d(out_channels))blk.append(nn.ReLU())in_channels out_channelsblk.append(nn.MaxPool2d(2))return nn.Sequential(*blk)在以下示例中我们构建的高和宽减半块会更改输入通道的数量并将输入特征图的高度和宽度减半。 forward(torch.zeros((2, 3, 20, 20)), down_sample_blk(3, 10)).shapetorch.Size([2, 10, 10, 10])基本网络块 基本网络块用于从输入图像中抽取特征输出特征形状32X32 def base_net():blk []num_filters [3, 16, 32, 64]for i in range(len(num_filters) - 1):blk.append(down_sample_blk(num_filters[i], num_filters[i1]))return nn.Sequential(*blk)forward(torch.zeros((2, 3, 256, 256)), base_net()).shapetorch.Size([2, 64, 32, 32])完整模型 完整的单发多框检测模型由五个模块组成。每个块生成的特征图既用于生成锚框又用于预测这些锚框的类别和偏移量。在这五个模块中第一个是基本网络块第二个到第四个是高和宽减半块最后一个模块使用全局最大池将高度和宽度都降到1。 def get_blk(i):if i 0:blk base_net()elif i 1:blk down_sample_blk(64, 128)elif i 4:blk nn.AdaptiveMaxPool2d((1,1))else:blk down_sample_blk(128, 128)return blk每个块定义前向传播。与图像分类任务不同此处的输出包括CNN特征图Y在当前尺度下根据Y生成的锚框预测的这些锚框的类别和偏移量基于Y。 def blk_forward(X, blk, size, ratio, cls_predictor, bbox_predictor):Y blk(X)anchors d2l.multibox_prior(Y, sizessize, ratiosratio)cls_preds cls_predictor(Y)bbox_preds bbox_predictor(Y)return (Y, anchors, cls_preds, bbox_preds)sizes [[0.2, 0.272], [0.37, 0.447], [0.54, 0.619], [0.71, 0.79],[0.88, 0.961]] ratios [[1, 2, 0.5]] * 5 num_anchors len(sizes[0]) len(ratios[0]) - 1完整的模型TinySSD class TinySSD(nn.Module):def __init__(self, num_classes, **kwargs):super(TinySSD, self).__init__(**kwargs)self.num_classes num_classesidx_to_in_channels [64, 128, 128, 128, 128]for i in range(5):# 即赋值语句self.blk_iget_blk(i)setattr(self, fblk_{i}, get_blk(i))setattr(self, fcls_{i}, cls_predictor(idx_to_in_channels[i],num_anchors, num_classes))setattr(self, fbbox_{i}, bbox_predictor(idx_to_in_channels[i],num_anchors))def forward(self, X):anchors, cls_preds, bbox_preds [None] * 5, [None] * 5, [None] * 5for i in range(5):# getattr(self,blk_%d%i)即访问self.blk_iX, anchors[i], cls_preds[i], bbox_preds[i] blk_forward(X, getattr(self, fblk_{i}), sizes[i], ratios[i],getattr(self, fcls_{i}), getattr(self, fbbox_{i}))anchors torch.cat(anchors, dim1)cls_preds concat_preds(cls_preds)cls_preds cls_preds.reshape(cls_preds.shape[0], -1, self.num_classes 1)bbox_preds concat_preds(bbox_preds)return anchors, cls_preds, bbox_preds创建一个模型实例然后使用它对一个256X256像素的小批量图像X执行前向传播.第一个模块输出特征图的形状为32X32。 回想一下第二到第四个模块为高和宽减半块第五个模块为全局汇聚层。 由于以特征图的每个单元为中心有4个锚框生成因此在所有五个尺度下每个图像总共生成5444 net TinySSD(num_classes1) X torch.zeros((32, 3, 256, 256)) anchors, cls_preds, bbox_preds net(X)print(output anchors:, anchors.shape) print(output class preds:, cls_preds.shape) print(output bbox preds:, bbox_preds.shape)output anchors: torch.Size([1, 5444, 4]) output class preds: torch.Size([32, 5444, 2]) output bbox preds: torch.Size([32, 21776])训练模型 读取数据集和初始化 batch_size 32 train_iter, _ d2l.load_data_bananas(batch_size)Downloading ../data/banana-detection.zip from http://d2l-data.s3-accelerate.amazonaws.com/banana-detection.zip... read 1000 training examples read 100 validation examples香蕉检测数据集中目标的类别数为1。 定义好模型后我们需要初始化其参数并定义优化算法。 device, net d2l.try_gpu(), TinySSD(num_classes1) trainer torch.optim.SGD(net.parameters(), lr0.2, weight_decay5e-4)定义损失和平均函数 使用L1范数损失即预测值和真实值之差的绝对值。 掩码变量bbox_masks令负类锚框和填充锚框不参与损失的计算。 最后我们将锚框类别和偏移量的损失相加以获得模型的最终损失函数。 cls_loss nn.CrossEntropyLoss(reductionnone) bbox_loss nn.L1Loss(reductionnone)def calc_loss(cls_preds, cls_labels, bbox_preds, bbox_labels, bbox_masks):batch_size, num_classes cls_preds.shape[0], cls_preds.shape[2]cls cls_loss(cls_preds.reshape(-1, num_classes),cls_labels.reshape(-1)).reshape(batch_size, -1).mean(dim1)bbox bbox_loss(bbox_preds * bbox_masks,bbox_labels * bbox_masks).mean(dim1)return cls bbox沿用准确率平均结果平均绝对误差来评价预测结果 def cls_eval(cls_preds, cls_labels):# 由于类别预测结果放在最后一维argmax需要指定最后一维。return float((cls_preds.argmax(dim-1).type(cls_labels.dtype) cls_labels).sum())def bbox_eval(bbox_preds, bbox_labels, bbox_masks):return float((torch.abs((bbox_labels - bbox_preds) * bbox_masks)).sum())训练模型 需要在模型的前向传播过程中生成多尺度锚框anchors并预测其类别cls_preds和偏移量bbox_preds。 然后我们根据标签信息Y为生成的锚框标记类别cls_labels和偏移量bbox_labels。 最后我们根据类别和偏移量的预测和标注值计算损失函数。 num_epochs, timer 20, d2l.Timer() animator d2l.Animator(xlabelepoch, xlim[1, num_epochs],legend[class error, bbox mae]) net net.to(device) for epoch in range(num_epochs):# 训练精确度的和训练精确度的和中的示例数# 绝对误差的和绝对误差的和中的示例数metric d2l.Accumulator(4)net.train()for features, target in train_iter:timer.start()trainer.zero_grad()X, Y features.to(device), target.to(device)# 生成多尺度的锚框为每个锚框预测类别和偏移量anchors, cls_preds, bbox_preds net(X)# 为每个锚框标注类别和偏移量bbox_labels, bbox_masks, cls_labels d2l.multibox_target(anchors, Y)# 根据类别和偏移量的预测和标注值计算损失函数l calc_loss(cls_preds, cls_labels, bbox_preds, bbox_labels,bbox_masks)l.mean().backward()trainer.step()metric.add(cls_eval(cls_preds, cls_labels), cls_labels.numel(),bbox_eval(bbox_preds, bbox_labels, bbox_masks),bbox_labels.numel())cls_err, bbox_mae 1 - metric[0] / metric[1], metric[2] / metric[3]animator.add(epoch 1, (cls_err, bbox_mae)) print(fclass err {cls_err:.2e}, bbox mae {bbox_mae:.2e}) print(f{len(train_iter.dataset) / timer.stop():.1f} examples/sec on f{str(device)})class err 3.32e-03, bbox mae 3.24e-03 4187.2 examples/sec on cuda:0预测目标 将图像中感兴趣的目标检测出来读取并调整测试图像的大小然后将其转成卷积层需要的四维格式。 X torchvision.io.read_image(banana.jpg).unsqueeze(0).float() img X.squeeze(0).permute(1, 2, 0).long()使用下面的multibox_detection函数我们可以根据锚框及其预测偏移量得到预测边界框。然后通过非极大值抑制来移除相似的预测边界框 def predict(X):net.eval()anchors, cls_preds, bbox_preds net(X.to(device))cls_probs F.softmax(cls_preds, dim2).permute(0, 2, 1)output d2l.multibox_detection(cls_probs, bbox_preds, anchors)idx [i for i, row in enumerate(output[0]) if row[0] ! -1]return output[0, idx]output predict(X)筛选所有置信度不低于0.9的边界框做为最终输出。 def display(img, output, threshold):d2l.set_figsize((5, 5))fig d2l.plt.imshow(img)for row in output:score float(row[1])if score threshold:continueh, w img.shape[0:2]bbox [row[2:6] * torch.tensor((w, h, w, h), devicerow.device)]d2l.show_bboxes(fig.axes, bbox, %.2f % score, w)display(img, output.cpu(), threshold0.9)
http://www.zqtcl.cn/news/190198/

相关文章:

  • 做网站时如何写接口文档上海网站设计建设公司
  • 网站小图标怎么制作平面设计素材网站推荐
  • 多元网络兰州网站建设惠州网页建站模板
  • 网站建设中首页模板下载网页制作模板保存
  • 宁夏做网站的江苏网站建设的案例展示
  • 网站功能需求文档如何免费域名注册
  • 推广网站的软件包头移动的网站建设
  • 自己制作音乐的软件免费上海seo怎么优化
  • 学vue可以做pc网站网站站长统计怎么弄
  • 做物流的可以在那些网站找客户大淘客网站建设app
  • 石家庄兼职做网站dedecms做视频网站
  • 优化公司怎么优化网站的网站 意义
  • 唯品会一家专门做特卖的网站手机版招聘网站开发技术维护
  • 做短租哪个网站wordpress 4.7
  • 网站换空间 site网站域没到期不能续费吗
  • 找别人做网站要考虑哪些网站导航条设计欣赏
  • mvc网站开发实例wordpress雪人主题2.0
  • 红色好看的网站中山网站建设工作室
  • 如何做喊单网站flask公司网站开发
  • 简单个人网站制作流程自己怎么做卖服装的网站
  • 网站开发公司创业做洁净的网站
  • 要建一个优惠卷网站怎么做企业开发小程序公司
  • 汕尾英文网站建设企业qq手机版
  • 重庆医院门户网站建设做百度网站电话号码
  • windows网站建设教程网站建设落地页
  • 新加坡做网站的价格网站正则表达式怎么做
  • 三门峡市住房的城乡建设局网站百度指数分析官网
  • 新网站外链怎么做陕西省煤炭建设第一中学官方网站
  • 学校网站建设方面汇报php网站开发和部署
  • 源码建站和模板建站区别商城网站功能