• 手写实现卷积和NMS


    手写实现卷积python

    1. import numpy as np
    2. #k是卷积核假设n*n,feature是特征图假设是w*h
    3. def convolution(feature,k):
    4. w,h=feature.shape
    5. n=len(k)
    6. new_feature=[]
    7. for i in range(w-n):
    8. line=[]
    9. for j in range(h-n):
    10. a=feature[i:i+n,j:j+n]
    11. #np.multiply()求两个矩阵的内积
    12. line.append(np.sum(np.multiply(k,a)))
    13. new_feature.append(line)
    14. return np.array(new_feature)

    手写NMS

    pytorch版本

    1. import torch
    2. #坐标原点在左上角,水平x,竖直y
    3. #计算一个框与一堆框的iou
    4. def iou(box,boxes):
    5. #先计算box的面积[x1,y1,x2,y2]
    6. box_area=(box[2]-box[0])*(box[3]-box[1])
    7. boxes_area=(boxes[:,2]-boxes[:,0])*(boxes[:,3]-boxes[:,1])
    8. #求交集,左上角最大,右下角最小
    9. xx1=torch.maximum(box[0],boxes[:,0])
    10. yy1=torch.maximum(box[1],boxes[:,1])
    11. xx2=torch.minimum(box[2],boxes[:,2])
    12. yy2=torch.minimum(box[3],boxes[:,3])
    13. w,h=torch.maximum(torch.Tensor([0]),xx2-xx1),torch.maximum(torch.Tensor([0]),yy2-yy1)
    14. over_area=w*h
    15. return over_area/(box_area+boxes_area-over_area)
    16. def NMS(boxes,thresh=0.3):
    17. #根据boxes的置信度进行排序,假设置信度在第一列
    18. #argsort将numpy数组进行排序,返回索引,descending表示从大到小返回
    19. new_boxes=boxes[boxes[:,0].argsort(descending=True)]
    20. keep_boxes=[]
    21. while len(new_boxes)>0:
    22. max_box=new_boxes[0]
    23. keep_boxes.append(max_box)
    24. if len(new_boxes)>1:#除了评分最高的外,还有其他框
    25. other_boxes=new_boxes[1:]
    26. #torch.where返回满足条件的索引
    27. new_boxes=other_boxes[torch.where(iou(max_box[1:],other_boxes[:,1:])
    28. else:
    29. break
    30. #将一个列表里面的张量数据进行扩维拼接,本来1行5列,现在n行5列
    31. return torch.stack(keep_boxes)
    32. boxes=torch.Tensor([[0.8,0,0,2,2],[0.6,1,1,4,4],[0.5,-1,-1,2,2],[0.75,-0.5,-0.5,2.5,2.5]])
    33. print(NMS(boxes,0.3))

    numpy版本

    1. def nms(boxes, scores, threshold):
    2. # 根据得分降序排序
    3. indices = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)
    4. # 初始化选中的边界框列表
    5. selected_boxes = []
    6. while indices:
    7. # 选择得分最高的边界框
    8. i = indices[0]
    9. selected_boxes.append(boxes[i])
    10. # 计算当前边界框与其他边界框的IoU
    11. ious = [calculate_iou(boxes[i], boxes[j]) for j in indices[1:]]
    12. # 仅保留IoU低于阈值的边界框
    13. indices = [indices[j + 1] for j, iou in enumerate(ious) if iou < threshold]
    14. return selected_boxes
    15. def calculate_iou(box1, box2):
    16. # 计算两个边界框的相交区域
    17. x1 = max(box1[0], box2[0])
    18. y1 = max(box1[1], box2[1])
    19. x2 = min(box1[2], box2[2])
    20. y2 = min(box1[3], box2[3])
    21. intersection = max(0, x2 - x1) * max(0, y2 - y1)
    22. # 计算两个边界框的联合区域
    23. area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
    24. area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
    25. union = area1 + area2 - intersection
    26. # 计算IoU(交并比)
    27. iou = intersection / union
    28. return iou

  • 相关阅读:
    2022Flink大数据比赛项目-焦点科技大数据编程大赛
    关于数字化转型的know how
    数据结构 —— 栈(超详细图解 & 接口函数实现)
    【JUC系列】Fork/Join框架之概览
    Understanding the Users and Videos by Mining a Novel Danmu Dataset
    无线互动会议室解决方案-总控室部署
    c语言之函数篇
    Linux内核开发基础0--实模式,保护模式,长模式
    【FAQ】HarmonyOS SDK 闭源开放能力 —Account Kit
    以太网数据链路层、Ethernet_II帧格式、IEEE802.3帧格式,以太网的MAC地址的组成,ARP地址解析协议的工作原理,单播帧、组播帧、广播帧的区别
  • 原文地址:https://blog.csdn.net/slamer111/article/details/127650934