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

合肥网站建设方案案例wordpress防止挂马

合肥网站建设方案案例,wordpress防止挂马,网上怎么推广产品,小说网站开发流程具体锚框的形状计算公式 假设原图的高为H,宽为W 锚框形状详细公式推导 以每个像素为中心生成不同形状的锚框 # s是缩放比#xff0c;ratio是宽高比 def multibox_prior(data, sizes, ratios):生成以每个像素为中心具有不同形状的锚框in_he…锚框的形状计算公式 假设原图的高为H,宽为W 锚框形状详细公式推导 以每个像素为中心生成不同形状的锚框 # s是缩放比ratio是宽高比 def multibox_prior(data, sizes, ratios):生成以每个像素为中心具有不同形状的锚框in_height,in_width data.shape[-2:] # 取出最后两个元素即h和wdevice,num_sizes,num_ratios data.device,len(sizes),len(ratios)boxes_per_pixel (num_sizesnum_ratios -1) # 以某个像素坐标为中心的锚框为nm-1size_tensor torch.tensor(sizes,devicedevice) # 将缩放比例列表sizes转为tensor, device参数指定设备ratio_tensor torch.tensor(ratios,devicedevice)# 为了将锚点移动到像素的中心,需要设置偏移量。# 因为一个像素的高为1且宽为1,我们选择偏移我们的中心0.5offset_h, offset_w 0.5, 0.5steps_h 1.0 / in_height # 在y轴上缩放步⻓steps_w 1.0 / in_width # 在x轴上缩放步⻓print(fsteps_h,steps_w {steps_h,steps_w})# 生成锚框的所有中心点center_h (torch.arange(in_height, devicedevice) offset_h) * steps_hcenter_w (torch.arange(in_width, devicedevice) offset_w) * steps_wprint(fcenter_h,center_w{center_h,center_w})#网格化中心点坐标shift_y,shift_x torch.meshgrid(center_h,center_w)#reshape成一维shift_y和shift_x坐标一一对应shift_y,shift_x shift_y.reshape(-1),shift_x.reshape(-1)print(fshift_y, shift_x{shift_y, shift_x}) ##norm√(H/W),这个就是个标号方便计算norm torch.sqrt(torch.tensor(in_height)/torch.tensor(in_width))# 生成“boxes_per_pixel”个高和宽,#只考虑包含s1或r1的组合,因此S*r1 与s1*R合并即为nm-1个锚框w torch.cat((size_tensor * torch.sqrt(ratio_tensor[0]),size_tensor[0] * torch.sqrt(ratio_tensor[1:]))) * normh torch.cat((size_tensor / torch.sqrt(ratio_tensor[0]),size_tensor[0] / torch.sqrt(ratio_tensor[1:]))) / norm# 获得归一化后的锚框的w,h的一半形成偏移量为了让归一化后的锚框根据中心点 偏移量找到 左上角和右下角坐标anchor_manipulations torch.stack((-w, -h, w, h)).T.repeat(in_height * in_width, 1) / 2# 每个中心点都将有“boxes_per_pixel”个锚框,# 所以生成含所有锚框中心的网格,重复了“boxes_per_pixel”次out_grid torch.stack([shift_x, shift_y, shift_x, shift_y],dim1).repeat_interleave(boxes_per_pixel, dim0)# 每个中心点都将有“boxes_per_pixel”个锚框# 所以生成含所有锚框中心的网格重复了“boxes_per_pixel”次out_grid torch.stack([shift_x, shift_y, shift_x, shift_y],dim1).repeat_interleave(boxes_per_pixel, dim0)#(x_min,y_min,x_max,y_max) 归一化后的锚框中心点 往左上角和右下角走的偏移量output out_grid anchor_manipulationsreturn output.unsqueeze(0)# 将锚框变量Y的形状更改为(图像高度,图像宽度,以同一像素为中心的锚框的数量,4) boxes Y.reshape(h, w, 5, 4)# 此处的5由 缩放的数量n 宽高比的数量m -1 而得 # 访问以(250,250)为中心的第一个锚框。它有四个元素:锚框左上⻆的(x, y)轴坐标和右下⻆的(x, y)轴坐标 boxes[250, 250, 0, :] # 输出的坐标是归一化后的即归一化前的锚框 w/in_weight 和 h/in_heightimg d2l.plt.imread(../data/images/cat_and_dog.jpg) h, w img.shape[:2] print(h, w) X torch.rand(size(1, 3, h, w)) # 返回的锚框变量Y的形状是(批量大小,锚框的数量,4 (表示锚框的左上角右下角坐标))。 Y multibox_prior(X, sizes[0.75, 0.5, 0.25], ratios[1, 2, 0.5]) Y.shape根据真实框来标注生成的锚框 # 计算IOU def box_iou(boxes1,boxes2)::param boxes1: shape (boxes1的数量,4):param boxes2: shape (boxes2的数量,4):param areas1: boxes1中每个框的面积 shape (boxes1的数量):param areas2: boxes2中每个框的面积 shape (boxes2的数量):return:# 定义一个Lambda函数输入boxes内容是计算得到框的面积box_area lambda boxes:((boxes[:,2] - boxes[:,0]) * (boxes[:,3] - boxes[:,0]))# 计算面积areas1 box_area(boxes1)areas2 box_area(boxes2)# 计算交集 要把所有锚框的左上角坐标 与 真实框的所有左上角坐标 作比较,大的就是交集的左上角 ,加个None 可以让锚框与所有真实框作对比inter_upperlefts torch.max(boxes1[:,None,:2],boxes2[:,:2])# 把所有锚框的右下角坐标 与 真实框的所有右下角坐标 作比较,小的就是交集的右下角坐标 ,加个None 可以让锚框与所有真实框作对比inter_lowerrights torch.min(boxes1[:,None,2:],boxes2[:,2:])# 如果右下角-左上角有元素小于0那就说明没有交集clamp(min-0)会将每个元素与0比较小于0的元素将会被替换成0inters (inter_lowerrights - inter_upperlefts).clamp(min0) # 得到w和hinter_areas inters[:,:,0] * inters[:,:,1] # 每个样本的 w*h# 求锚框与真实框的并集# 将所有锚框与真实框相加他们会多出来一个交集的面积所以要减一个交集的面积union_areas areas1[:,None] * areas2 - inter_areasreturn inter_areas/union_areas# 每个真实框都要跟所有锚框计算iouIou数量等于真实框数量 * 锚框的数量 def assign_anchor_to_bbox(ground_truth,anchors,devices,iou_threshold0.5):# 得到锚框和真实框的个数num_anchors,num_gt_boxes anchors.shape[0],ground_truth.shape[0]# jaccard是计算 所有锚框anchors和真实框ground_truth的交并比jaccard box_iou(anchors,ground_truth)# torch.full(size,fill_value,dtype,device)如下代码生产成一个一位数组长度为锚框的个数值为-1anchors_bbox_map torch.full((num_anchors,),-1,dtypetorch.long,devicedevices)# 对行取最大值得到每个真实框对应的最大IOU的锚框max_ious,indices torch.max(jaccard,dim1)# 返回张量中非0元素的索引即Max_iou设定的阈值位于第i行和第j列的元素x_ij是锚框i和真实边界框j的IoUanc_i torch.nonzero(max_iousiou_threshold).reshape(-1)box_j indices[max_iousiou_threshold]anchors_bbox_map[anc_i] box_jcol_discard torch.full((num_anchors,), -1)row_discard torch.full((num_gt_boxes,), -1)for _ in range(num_gt_boxes):max_idx torch.argmax(jaccard)box_idx (max_idx % num_gt_boxes).long()anc_idx (max_idx / num_gt_boxes).long()anchors_bbox_map[anc_idx] box_idxjaccard[:, box_idx] col_discardjaccard[anc_idx, :] row_discardreturn anchors_bbox_map# 标注类别和偏移量 def offset_boxes(anchors, assigned_bb, eps1e-6):对锚框偏移量的转换c_anc d2l.box_corner_to_center(anchors)c_assigned_bb d2l.box_corner_to_center(assigned_bb)offset_xy 10 * (c_assigned_bb[:, :2] - c_anc[:, :2]) / c_anc[:, 2:]offset_wh 5 * torch.log(eps c_assigned_bb[:, 2:] / c_anc[:, 2:])offset torch.cat([offset_xy, offset_wh], axis1)return offset如果一个锚框没有被分配真实边界框,将锚框的类别标记为背景。背景类别的锚框通常被称为负类锚框,其余的被称为正类锚框。我们使用真实边界框(labels参数)实现以下multibox_target函数,来标记锚框的类别和偏移量(anchors参数)。此函数将背景类别的索引设置为零,然后将新类别的整数索引递增一。def multibox_target(anchors, labels):使用真实边界框标记锚框batch_size, anchors labels.shape[0], anchors.squeeze(0)batch_offset, batch_mask, batch_class_labels [], [], []device, num_anchors anchors.device, anchors.shape[0]for i in range(batch_size):label labels[i, :, :]anchors_bbox_map assign_anchor_to_bbox(label[:, 1:], anchors, device)bbox_mask ((anchors_bbox_map 0).float().unsqueeze(-1)).repeat(1, 4)# 将类标签和分配的边界框坐标初始化为零class_labels torch.zeros(num_anchors, dtypetorch.long,devicedevice)assigned_bb torch.zeros((num_anchors, 4), dtypetorch.float32,devicedevice)# 使用真实边界框来标记锚框的类别。# 如果一个锚框没有被分配,标记其为背景(值为零)indices_true torch.nonzero(anchors_bbox_map 0)bb_idx anchors_bbox_map[indices_true]class_labels[indices_true] label[bb_idx, 0].long() 1assigned_bb[indices_true] label[bb_idx, 1:]# 使用真实边界框来标记锚框的类别。# 如果一个锚框没有被分配,标记其为背景(值为零)indices_true torch.nonzero(anchors_bbox_map 0)bb_idx anchors_bbox_map[indices_true]class_labels[indices_true] label[bb_idx, 0].long() 1assigned_bb[indices_true] label[bb_idx, 1:]# 偏移量转换offset offset_boxes(anchors, assigned_bb) * bbox_maskbatch_offset.append(offset.reshape(-1))batch_mask.append(bbox_mask.reshape(-1))batch_class_labels.append(class_labels)bbox_offset torch.stack(batch_offset)bbox_mask torch.stack(batch_mask)class_labels torch.stack(batch_class_labels)return (bbox_offset, bbox_mask, class_labels)# 第一个元素表示类别0代表狗1代表猫。其余四个元素是左下角坐标和右上角坐标(归一化后的介于0-1之间)归一化的方法是x坐标 / 宽y坐标/高 ground_truth torch.tensor([[0, 0.1, 0.08, 0.52, 0.92],[1, 0.55, 0.2, 0.9, 0.88]]) # 锚框 anchors torch.tensor([[0, 0.1, 0.2, 0.3], [0.15, 0.2, 0.4, 0.4],[0.63, 0.05, 0.88, 0.98], [0.66, 0.45, 0.8, 0.8],[0.57, 0.3, 0.92, 0.9]]) bbox_scale torch.tensor((w, h, w, h)) # img d2l.plt.imread(../data/images/cat_dog.png) img d2l.plt.imread(../data/images/cat_and_dog.jpg) fig d2l.plt.imshow(img) # 画出真实框 (坐标轴归一化*bbox_scale得到原图规模的坐标标签颜色) show_bboxes(fig.axes,ground_truth[:,1:] * bbox_scale,[dog,cat],k) # k最后画出来是黑色 # 画出设置的锚框把锚框类别标记为0-4 show_bboxes(fig.axes, anchors * bbox_scale, [0, 1, 2, 3, 4]); labels[0]:labels[1]:掩码形状为(批量大小锚框数的4倍)对应每个锚框的4个偏移量负类掩码为0通过元素乘法将负类的偏移量过滤掉labels[2]:锚框对应的标签labels multibox_target(anchors.unsqueeze(dim0),ground_truth.unsqueeze(dim0))非极大值抑制 在预测时,我们先为图像生成多个锚框,再为这些锚框一一预测类别和偏移量。一个预测好的边界框则根据其中某个带有预测偏移量的锚框而生成。下面我们实现了offset_inverse函数,该函数将锚框和偏移量预测作为输入,并应用逆偏移变换来返回预测的边界框坐标。输入: 锚框 和 偏移量预测输出根据锚框的原始坐标和预测的偏移量 计算出的 预测的边界框坐标def offset_inverse(anchors, offset_preds):根据带有预测偏移量的锚框来预测边界框anc d2l.box_corner_to_center(anchors)pred_bbox_xy (offset_preds[:, :2] * anc[:, 2:] / 10) anc[:, :2]pred_bbox_wh torch.exp(offset_preds[:, 2:] / 5) * anc[:, 2:]pred_bbox torch.cat((pred_bbox_xy, pred_bbox_wh), axis1)predicted_bbox d2l.box_center_to_corner(pred_bbox)return predicted_bbox按降序对置信度进行排序并返回其索引 #save def nms(boxes, scores, iou_threshold):对预测边界框的置信度进行排序B torch.argsort(scores, dim-1, descendingTrue)keep []# 保留预测边界框的指标while B.numel() 0:i B[0]keep.append(i)if B.numel() 1: breakiou box_iou(boxes[i, :].reshape(-1, 4),boxes[B[1:], :].reshape(-1, 4)).reshape(-1)inds torch.nonzero(iou iou_threshold).reshape(-1)B B[inds 1]return torch.tensor(keep, deviceboxes.device)def multibox_detection(cls_probs, offset_preds, anchors, nms_threshold0.5,pos_threshold0.009999999):使用非极大值抑制来预测边界框device, batch_size cls_probs.device, cls_probs.shape[0]anchors anchors.squeeze(0)num_classes, num_anchors cls_probs.shape[1], cls_probs.shape[2]out []for i in range(batch_size):cls_prob, offset_pred cls_probs[i], offset_preds[i].reshape(-1, 4)conf, class_id torch.max(cls_prob[1:], 0)调用offset_inversepredicted_bb offset_inverse(anchors, offset_pred)调用nmskeep nms(predicted_bb, conf, nms_threshold)# 找到所有的non_keep索引,并将类设置为背景all_idx torch.arange(num_anchors, dtypetorch.long, devicedevice)combined torch.cat((keep, all_idx))uniques, counts combined.unique(return_countsTrue)non_keep uniques[counts 1]all_id_sorted torch.cat((keep, non_keep))class_id[non_keep] -1class_id class_id[all_id_sorted]conf, predicted_bb conf[all_id_sorted], predicted_bb[all_id_sorted]# pos_threshold是一个用于非背景预测的阈值below_min_idx (conf pos_threshold)class_id[below_min_idx] -1conf[below_min_idx] 1 - conf[below_min_idx]pred_info torch.cat((class_id.unsqueeze(1),conf.unsqueeze(1),predicted_bb), dim1)out.append(pred_info)return torch.stack(out)
http://www.dnsts.com.cn/news/243286.html

相关文章:

  • seo网站建设接单dede网站婚纱模板
  • 网站设计南方企业网国外做游戏的视频网站
  • 洛阳做网站哪家便宜ppt哪个网站质量高
  • 哪个网站可以接活做外贸公司出口退税流程
  • 白羊女做网站网络规划设计师证书样本
  • 手机信息分类网站制作上海中小企业名录
  • 佛山网站建设维护苏州建网站哪家
  • 东莞网站的建设建设官方网站企业网站
  • 网站做数据统计上海app网络推广公司电话
  • phpcms 网站名称标签为什么要做企业网站
  • WordPress网站代码修改wordpress 打开好慢
  • 设计找版面网站网站推广费用怎么做分录
  • 怎样使自己做的网站上线wordpress刷新不管用
  • 西安知名的网站建设公司wordpress 模块插件
  • 网站建设背景怎么设置成怎么找网红推广自己的店
  • 运营一个网站的成本premium WordPress
  • 网站开发发布免费咨询医生男科
  • 哪些网站页面简洁桂林市建设工程造价管理站网站
  • 网站建设报价清单明细免费设计网站logo
  • 鄂州第一官方网站wordpress 不同分类目录调用不同模板的方法
  • 网站建设刂搜金手指下拉二四马卡龙网站建设方案
  • 国内精品网站建设被代运营骗了怎么追回
  • 学校门户网站群建设方案合肥专业网站建设公司哪家好
  • PHP网站建设视频免费金融股票类app网站开发
  • 厦门市建设局网站文件网站建设要学哪些方面
  • 开一家网站建设公司南昌做网站费用
  • 做网站学什么专业学校风采网站建设需求
  • 制作h5网站开发wordpress播入视频播放
  • 芜湖做网站优化国家建设标准发布网站在哪里
  • 钻石网站建设it外包公司怎么样