Repository navigation
车辆检测 #3533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
车辆检测 #3533
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
08a7a5f
为语句引入变量名
aasdar 819ebfd
Merge branch 'OpenHUTB:main' into main
aasdar f99ab3c
自动驾驶汽车车道与路径检测系统
aasdar 6d4a2c4
Merge branch 'OpenHUTB:main' into main
aasdar 1552d74
Merge branch 'OpenHUTB:main' into main
aasdar 64b17c0
Merge branch 'OpenHUTB:main' into main
aasdar 971af7f
车辆检测系统
aasdar f3cef2f
Merge branch 'OpenHUTB:main' into main
aasdar edc891f
车辆检测
aasdar 7250150
Merge branch 'main' of https://github.com/aasdar/nn
aasdar da4ab23
车辆检测
aasdar 8a09efe
Merge branch 'OpenHUTB:main' into main
aasdar 1855e6b
车辆检测
aasdar 8c3888e
Merge branch 'main' of https://github.com/aasdar/nn
aasdar c771bdc
Merge branch 'OpenHUTB:main' into main
aasdar ab3be99
车辆检测
aasdar df04de4
Merge branch 'main' of https://github.com/aasdar/nn
aasdar e58c1c1
Merge branch 'OpenHUTB:main' into main
aasdar be544fd
车辆检测系统
aasdar 24900a5
Merge branch 'main' of https://github.com/aasdar/nn
aasdar d0555db
Merge branch 'OpenHUTB:main' into main
aasdar 0412632
车辆检测
aasdar 69f8932
Merge branch 'main' of https://github.com/aasdar/nn
aasdar 6a01be7
车辆检测
aasdar bdb6561
Merge branch 'OpenHUTB:main' into main
aasdar 64bfcb3
车辆检测(添加模型训练代码)
aasdar 7d6811c
车辆检测
aasdar 282ee5c
Merge branch 'OpenHUTB:main' into 111
aasdar a9b81cf
车辆检测
aasdar 0e3dd61
Merge branch 'OpenHUTB:main' into 111
aasdar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| #-------------------------------------------------------------------------------------------------# | ||
| # kmeans虽然会对数据集中的框进行聚类,但是很多数据集由于框的大小相近,聚类出来的9个框相差不大, | ||
| # 这样的框反而不利于模型的训练。因为不同的特征层适合不同大小的先验框,越浅的特征层适合越大的先验框 | ||
| # 原始网络的先验框已经按大中小比例分配好了,不进行聚类也会有非常好的效果。 | ||
| #-------------------------------------------------------------------------------------------------# | ||
| import glob | ||
| import xml.etree.ElementTree as ET | ||
|
|
||
| import numpy as np | ||
|
|
||
| def cas_iou(box,cluster): | ||
| x = np.minimum(cluster[:,0],box[0]) | ||
| y = np.minimum(cluster[:,1],box[1]) | ||
|
|
||
| intersection = x * y | ||
| area1 = box[0] * box[1] | ||
|
|
||
| area2 = cluster[:,0] * cluster[:,1] | ||
| iou = intersection / (area1 + area2 -intersection) | ||
|
|
||
| return iou | ||
|
|
||
| def avg_iou(box,cluster): | ||
| return np.mean([np.max(cas_iou(box[i],cluster)) for i in range(box.shape[0])]) | ||
|
|
||
| def kmeans(box,k): | ||
| # 取出一共有多少框 | ||
| row = box.shape[0] | ||
|
|
||
| # 每个框各个点的位置 | ||
| distance = np.empty((row,k)) | ||
|
|
||
| # 最后的聚类位置 | ||
| last_clu = np.zeros((row,)) | ||
|
|
||
| np.random.seed() | ||
|
|
||
| # 随机选5个当聚类中心 | ||
| cluster = box[np.random.choice(row,k,replace = False)] | ||
| # cluster = random.sample(row, k) | ||
| while True: | ||
| # 计算每一行距离五个点的iou情况。 | ||
| for i in range(row): | ||
| distance[i] = 1 - cas_iou(box[i],cluster) | ||
|
|
||
| # 取出最小点 | ||
| near = np.argmin(distance,axis=1) | ||
|
|
||
| if (last_clu == near).all(): | ||
| break | ||
|
|
||
| # 求每一个类的中位点 | ||
| for j in range(k): | ||
| cluster[j] = np.median( | ||
| box[near == j],axis=0) | ||
|
|
||
| last_clu = near | ||
|
|
||
| return cluster | ||
|
|
||
| def load_data(path): | ||
| data = [] | ||
| # 对于每一个xml都寻找box | ||
| for xml_file in glob.glob('{}/*xml'.format(path)): | ||
| tree = ET.parse(xml_file) | ||
| height = int(tree.findtext('./size/height')) | ||
| width = int(tree.findtext('./size/width')) | ||
| if height<=0 or width<=0: | ||
| continue | ||
|
|
||
| # 对于每一个目标都获得它的宽高 | ||
| for obj in tree.iter('object'): | ||
| xmin = int(float(obj.findtext('bndbox/xmin'))) / width | ||
| ymin = int(float(obj.findtext('bndbox/ymin'))) / height | ||
| xmax = int(float(obj.findtext('bndbox/xmax'))) / width | ||
| ymax = int(float(obj.findtext('bndbox/ymax'))) / height | ||
|
|
||
| xmin = np.float64(xmin) | ||
| ymin = np.float64(ymin) | ||
| xmax = np.float64(xmax) | ||
| ymax = np.float64(ymax) | ||
| # 得到宽高 | ||
| data.append([xmax-xmin,ymax-ymin]) | ||
| return np.array(data) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| # 运行该程序会计算'./VOCdevkit/VOC2007/Annotations'的xml | ||
| # 会生成yolo_anchors.txt | ||
| SIZE = 416 | ||
| anchors_num = 9 | ||
| # 载入数据集,可以使用VOC的xml | ||
| path = r'./VOCdevkit/VOC2007/Annotations' | ||
|
|
||
| # 载入所有的xml | ||
| # 存储格式为转化为比例后的width,height | ||
| data = load_data(path) | ||
|
|
||
| # 使用k聚类算法 | ||
| out = kmeans(data,anchors_num) | ||
| out = out[np.argsort(out[:,0])] | ||
| print('acc:{:.2f}%'.format(avg_iou(data,out) * 100)) | ||
| print(out*SIZE) | ||
| data = out*SIZE | ||
| f = open("yolo_anchors.txt", 'w') | ||
| row = np.shape(data)[0] | ||
| for i in range(row): | ||
| if i == 0: | ||
| x_y = "%d,%d" % (data[i][0], data[i][1]) | ||
| else: | ||
| x_y = ", %d,%d" % (data[i][0], data[i][1]) | ||
| f.write(x_y) | ||
| f.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import os | ||
| import random | ||
| import shutil | ||
|
|
||
| # xml路径的地址 | ||
| XmlPath = r'xml_train' | ||
| # 原图片的地址 | ||
| pictureBasePath = r"E:\Insight-MVT_Annotation_Train" | ||
| # 保存图片的地址 | ||
| saveBasePath = r"E:\picture_train" | ||
|
|
||
| total_xml = os.listdir(XmlPath) | ||
| num = len(total_xml) | ||
| list = range(num) | ||
| if os.path.exists(saveBasePath) == False: # 判断文件夹是否存在 | ||
| os.makedirs(saveBasePath) | ||
| from tqdm import tqdm | ||
| for xml in tqdm(total_xml): | ||
| xml_temp = xml.split("__") | ||
| folder = xml_temp[0] | ||
| filename = xml_temp[1].split(".")[0] + ".jpg" | ||
| # print(folder) | ||
| # print(filename) | ||
| temp_pictureBasePath = os.path.join(pictureBasePath, folder) | ||
| filePath = os.path.join(temp_pictureBasePath, filename) | ||
| # print(filePath) | ||
| newfile = xml.split(".")[0] + ".jpg" | ||
| newfile_path = os.path.join(saveBasePath, newfile) | ||
| # print(newfile_path) | ||
| shutil.copyfile(filePath, newfile_path) | ||
| print("xml file total number", num) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #--------------------------------------------# | ||
| # 该部分代码只用于看网络结构,并非测试代码 | ||
| # map测试请看get_dr_txt.py、get_gt_txt.py | ||
| # 和get_map.py | ||
| #--------------------------------------------# | ||
| import torch | ||
| from torchsummary import summary | ||
|
|
||
| from nets.yolo4 import YoloBody | ||
|
|
||
| if __name__ == "__main__": | ||
| # 需要使用device来指定网络在GPU还是CPU运行 | ||
| device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | ||
| model = YoloBody(3,20).to(device) | ||
| summary(model, input_size=(3, 416, 416)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不能用绝对路径,其他地方也是