├── .gitignore ├── LICENSE ├── README.md ├── VOCdevkit └── VOC2007 │ ├── Annotations │ └── README.md │ ├── ImageSets │ └── Main │ │ └── README.md │ └── JPEGImages │ └── README.md ├── get_map.py ├── img └── street.jpg ├── kmeans_for_anchors.py ├── logs └── README.md ├── model_data ├── coco_classes.txt ├── simhei.ttf ├── voc_classes.txt └── yolo_anchors.txt ├── nets ├── __init__.py ├── darknet.py ├── yolo.py └── yolo_training.py ├── predict.py ├── requirements.txt ├── summary.py ├── train.py ├── utils ├── __init__.py ├── callbacks.py ├── dataloader.py ├── utils.py ├── utils_bbox.py ├── utils_fit.py └── utils_map.py ├── utils_coco ├── coco_annotation.py └── get_map_coco.py ├── voc_annotation.py ├── yolo.py └── 常见问题汇总.md /.gitignore: -------------------------------------------------------------------------------- 1 | # ignore map, miou, datasets 2 | map_out/ 3 | miou_out/ 4 | VOCdevkit/ 5 | datasets/ 6 | Medical_Datasets/ 7 | lfw/ 8 | logs/ 9 | model_data/ 10 | .temp_map_out/ 11 | 12 | # Byte-compiled / optimized / DLL files 13 | __pycache__/ 14 | *.py[cod] 15 | *$py.class 16 | 17 | # C extensions 18 | *.so 19 | 20 | # Distribution / packaging 21 | .Python 22 | build/ 23 | develop-eggs/ 24 | dist/ 25 | downloads/ 26 | eggs/ 27 | .eggs/ 28 | lib/ 29 | lib64/ 30 | parts/ 31 | sdist/ 32 | var/ 33 | wheels/ 34 | pip-wheel-metadata/ 35 | share/python-wheels/ 36 | *.egg-info/ 37 | .installed.cfg 38 | *.egg 39 | MANIFEST 40 | 41 | # PyInstaller 42 | # Usually these files are written by a python script from a template 43 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 44 | *.manifest 45 | *.spec 46 | 47 | # Installer logs 48 | pip-log.txt 49 | pip-delete-this-directory.txt 50 | 51 | # Unit test / coverage reports 52 | htmlcov/ 53 | .tox/ 54 | .nox/ 55 | .coverage 56 | .coverage.* 57 | .cache 58 | nosetests.xml 59 | coverage.xml 60 | *.cover 61 | *.py,cover 62 | .hypothesis/ 63 | .pytest_cache/ 64 | 65 | # Translations 66 | *.mo 67 | *.pot 68 | 69 | # Django stuff: 70 | *.log 71 | local_settings.py 72 | db.sqlite3 73 | db.sqlite3-journal 74 | 75 | # Flask stuff: 76 | instance/ 77 | .webassets-cache 78 | 79 | # Scrapy stuff: 80 | .scrapy 81 | 82 | # Sphinx documentation 83 | docs/_build/ 84 | 85 | # PyBuilder 86 | target/ 87 | 88 | # Jupyter Notebook 89 | .ipynb_checkpoints 90 | 91 | # IPython 92 | profile_default/ 93 | ipython_config.py 94 | 95 | # pyenv 96 | .python-version 97 | 98 | # pipenv 99 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 100 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 101 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 102 | # install all needed dependencies. 103 | #Pipfile.lock 104 | 105 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 106 | __pypackages__/ 107 | 108 | # Celery stuff 109 | celerybeat-schedule 110 | celerybeat.pid 111 | 112 | # SageMath parsed files 113 | *.sage.py 114 | 115 | # Environments 116 | .env 117 | .venv 118 | env/ 119 | venv/ 120 | ENV/ 121 | env.bak/ 122 | venv.bak/ 123 | 124 | # Spyder project settings 125 | .spyderproject 126 | .spyproject 127 | 128 | # Rope project settings 129 | .ropeproject 130 | 131 | # mkdocs documentation 132 | /site 133 | 134 | # mypy 135 | .mypy_cache/ 136 | .dmypy.json 137 | dmypy.json 138 | 139 | # Pyre type checker 140 | .pyre/ 141 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 JiaQi Xu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## YOLOV3:You Only Look Once目标检测模型在Pytorch当中的实现 2 | --- 3 | 4 | ## 目录 5 | 1. [仓库更新 Top News](#仓库更新) 6 | 2. [相关仓库 Related code](#相关仓库) 7 | 3. [性能情况 Performance](#性能情况) 8 | 4. [所需环境 Environment](#所需环境) 9 | 5. [文件下载 Download](#文件下载) 10 | 6. [训练步骤 How2train](#训练步骤) 11 | 7. [预测步骤 How2predict](#预测步骤) 12 | 8. [评估步骤 How2eval](#评估步骤) 13 | 9. [参考资料 Reference](#Reference) 14 | 15 | ## Top News 16 | **`2022-04`**:**支持多GPU训练,新增各个种类目标数量计算,新增heatmap。** 17 | 18 | **`2022-03`**:**进行了大幅度的更新,修改了loss组成,使得分类、目标、回归loss的比例合适、支持step、cos学习率下降法、支持adam、sgd优化器选择、支持学习率根据batch_size自适应调整、新增图片裁剪。** 19 | BiliBili视频中的原仓库地址为:https://github.com/bubbliiiing/yolo3-pytorch/tree/bilibili 20 | 21 | **`2021-10`**:**进行了大幅度的更新,增加了大量注释、增加了大量可调整参数、对代码的组成模块进行修改、增加fps、视频预测、批量预测等功能。** 22 | 23 | ## 相关仓库 24 | | 模型 | 路径 | 25 | | :----- | :----- | 26 | YoloV3 | https://github.com/bubbliiiing/yolo3-pytorch 27 | Efficientnet-Yolo3 | https://github.com/bubbliiiing/efficientnet-yolo3-pytorch 28 | YoloV4 | https://github.com/bubbliiiing/yolov4-pytorch 29 | YoloV4-tiny | https://github.com/bubbliiiing/yolov4-tiny-pytorch 30 | Mobilenet-Yolov4 | https://github.com/bubbliiiing/mobilenet-yolov4-pytorch 31 | YoloV5-V5.0 | https://github.com/bubbliiiing/yolov5-pytorch 32 | YoloV5-V6.1 | https://github.com/bubbliiiing/yolov5-v6.1-pytorch 33 | YoloX | https://github.com/bubbliiiing/yolox-pytorch 34 | YoloV7 | https://github.com/bubbliiiing/yolov7-pytorch 35 | YoloV7-tiny | https://github.com/bubbliiiing/yolov7-tiny-pytorch 36 | 37 | ## 性能情况 38 | | 训练数据集 | 权值文件名称 | 测试数据集 | 输入图片大小 | mAP 0.5:0.95 | mAP 0.5 | 39 | | :-----: | :-----: | :------: | :------: | :------: | :-----: | 40 | | COCO-Train2017 | [yolo_weights.pth](https://github.com/bubbliiiing/yolo3-pytorch/releases/download/v1.0/yolo_weights.pth) | COCO-Val2017 | 416x416 | 38.0 | 67.2 41 | 42 | ## 所需环境 43 | torch == 1.2.0 44 | 详情请看requirements.txt,文件具有一定兼容性,已测试pytorch1.7和1.7.1可以正常运行。 45 | 46 | ## 文件下载 47 | 训练所需的yolo_weights.pth可以在百度云下载。 48 | 链接: https://pan.baidu.com/s/1hCV4kg8NyStkywLiAeEr3g 49 | 提取码: 6da3 50 | 51 | VOC数据集下载地址如下,里面已经包括了训练集、测试集、验证集(与测试集一样),无需再次划分: 52 | 链接: https://pan.baidu.com/s/19Mw2u_df_nBzsC2lg20fQA 53 | 提取码: j5ge 54 | 55 | ## 训练步骤 56 | ### a、训练VOC07+12数据集 57 | 1. 数据集的准备 58 | **本文使用VOC格式进行训练,训练前需要下载好VOC07+12的数据集,解压后放在根目录** 59 | 60 | 2. 数据集的处理 61 | 修改voc_annotation.py里面的annotation_mode=2,运行voc_annotation.py生成根目录下的2007_train.txt和2007_val.txt。 62 | 63 | 3. 开始网络训练 64 | train.py的默认参数用于训练VOC数据集,直接运行train.py即可开始训练。 65 | 66 | 4. 训练结果预测 67 | 训练结果预测需要用到两个文件,分别是yolo.py和predict.py。我们首先需要去yolo.py里面修改model_path以及classes_path,这两个参数必须要修改。 68 | **model_path指向训练好的权值文件,在logs文件夹里。 69 | classes_path指向检测类别所对应的txt。** 70 | 完成修改后就可以运行predict.py进行检测了。运行后输入图片路径即可检测。 71 | 72 | ### b、训练自己的数据集 73 | 1. 数据集的准备 74 | **本文使用VOC格式进行训练,训练前需要自己制作好数据集,** 75 | 训练前将标签文件放在VOCdevkit文件夹下的VOC2007文件夹下的Annotation中。 76 | 训练前将图片文件放在VOCdevkit文件夹下的VOC2007文件夹下的JPEGImages中。 77 | 78 | 2. 数据集的处理 79 | 在完成数据集的摆放之后,我们需要利用voc_annotation.py获得训练用的2007_train.txt和2007_val.txt。 80 | 修改voc_annotation.py里面的参数。第一次训练可以仅修改classes_path,classes_path用于指向检测类别所对应的txt。 81 | 训练自己的数据集时,可以自己建立一个cls_classes.txt,里面写自己所需要区分的类别。 82 | model_data/cls_classes.txt文件内容为: 83 | ```python 84 | cat 85 | dog 86 | ... 87 | ``` 88 | 修改voc_annotation.py中的classes_path,使其对应cls_classes.txt,并运行voc_annotation.py。 89 | 90 | 3. 开始网络训练 91 | **训练的参数较多,均在train.py中,大家可以在下载库后仔细看注释,其中最重要的部分依然是train.py里的classes_path。** 92 | **classes_path用于指向检测类别所对应的txt,这个txt和voc_annotation.py里面的txt一样!训练自己的数据集必须要修改!** 93 | 修改完classes_path后就可以运行train.py开始训练了,在训练多个epoch后,权值会生成在logs文件夹中。 94 | 95 | 4. 训练结果预测 96 | 训练结果预测需要用到两个文件,分别是yolo.py和predict.py。在yolo.py里面修改model_path以及classes_path。 97 | **model_path指向训练好的权值文件,在logs文件夹里。 98 | classes_path指向检测类别所对应的txt。** 99 | 完成修改后就可以运行predict.py进行检测了。运行后输入图片路径即可检测。 100 | 101 | ## 预测步骤 102 | ### a、使用预训练权重 103 | 1. 下载完库后解压,在百度网盘下载yolo_weights.pth,放入model_data,运行predict.py,输入 104 | ```python 105 | img/street.jpg 106 | ``` 107 | 2. 在predict.py里面进行设置可以进行fps测试和video视频检测。 108 | ### b、使用自己训练的权重 109 | 1. 按照训练步骤训练。 110 | 2. 在yolo.py文件里面,在如下部分修改model_path和classes_path使其对应训练好的文件;**model_path对应logs文件夹下面的权值文件,classes_path是model_path对应分的类**。 111 | ```python 112 | _defaults = { 113 | #--------------------------------------------------------------------------# 114 | # 使用自己训练好的模型进行预测一定要修改model_path和classes_path! 115 | # model_path指向logs文件夹下的权值文件,classes_path指向model_data下的txt 116 | # 如果出现shape不匹配,同时要注意训练时的model_path和classes_path参数的修改 117 | #--------------------------------------------------------------------------# 118 | "model_path" : 'model_data/yolo_weights.pth', 119 | "classes_path" : 'model_data/coco_classes.txt', 120 | #---------------------------------------------------------------------# 121 | # anchors_path代表先验框对应的txt文件,一般不修改。 122 | # anchors_mask用于帮助代码找到对应的先验框,一般不修改。 123 | #---------------------------------------------------------------------# 124 | "anchors_path" : 'model_data/yolo_anchors.txt', 125 | "anchors_mask" : [[6, 7, 8], [3, 4, 5], [0, 1, 2]], 126 | #---------------------------------------------------------------------# 127 | # 输入图片的大小,必须为32的倍数。 128 | #---------------------------------------------------------------------# 129 | "input_shape" : [416, 416], 130 | #---------------------------------------------------------------------# 131 | # 只有得分大于置信度的预测框会被保留下来 132 | #---------------------------------------------------------------------# 133 | "confidence" : 0.5, 134 | #---------------------------------------------------------------------# 135 | # 非极大抑制所用到的nms_iou大小 136 | #---------------------------------------------------------------------# 137 | "nms_iou" : 0.3, 138 | #---------------------------------------------------------------------# 139 | # 该变量用于控制是否使用letterbox_image对输入图像进行不失真的resize, 140 | # 在多次测试后,发现关闭letterbox_image直接resize的效果更好 141 | #---------------------------------------------------------------------# 142 | "letterbox_image" : False, 143 | #-------------------------------# 144 | # 是否使用Cuda 145 | # 没有GPU可以设置成False 146 | #-------------------------------# 147 | "cuda" : True, 148 | } 149 | ``` 150 | 3. 运行predict.py,输入 151 | ```python 152 | img/street.jpg 153 | ``` 154 | 4. 在predict.py里面进行设置可以进行fps测试和video视频检测。 155 | 156 | ## 评估步骤 157 | ### a、评估VOC07+12的测试集 158 | 1. 本文使用VOC格式进行评估。VOC07+12已经划分好了测试集,无需利用voc_annotation.py生成ImageSets文件夹下的txt。 159 | 2. 在yolo.py里面修改model_path以及classes_path。**model_path指向训练好的权值文件,在logs文件夹里。classes_path指向检测类别所对应的txt。** 160 | 3. 运行get_map.py即可获得评估结果,评估结果会保存在map_out文件夹中。 161 | 162 | ### b、评估自己的数据集 163 | 1. 本文使用VOC格式进行评估。 164 | 2. 如果在训练前已经运行过voc_annotation.py文件,代码会自动将数据集划分成训练集、验证集和测试集。如果想要修改测试集的比例,可以修改voc_annotation.py文件下的trainval_percent。trainval_percent用于指定(训练集+验证集)与测试集的比例,默认情况下 (训练集+验证集):测试集 = 9:1。train_percent用于指定(训练集+验证集)中训练集与验证集的比例,默认情况下 训练集:验证集 = 9:1。 165 | 3. 利用voc_annotation.py划分测试集后,前往get_map.py文件修改classes_path,classes_path用于指向检测类别所对应的txt,这个txt和训练时的txt一样。评估自己的数据集必须要修改。 166 | 4. 在yolo.py里面修改model_path以及classes_path。**model_path指向训练好的权值文件,在logs文件夹里。classes_path指向检测类别所对应的txt。** 167 | 5. 运行get_map.py即可获得评估结果,评估结果会保存在map_out文件夹中。 168 | 169 | ## Reference 170 | https://github.com/qqwweee/pytorch-yolo3 171 | https://github.com/eriklindernoren/PyTorch-YOLOv3 172 | https://github.com/BobLiu20/YOLOv3_PyTorch 173 | -------------------------------------------------------------------------------- /VOCdevkit/VOC2007/Annotations/README.md: -------------------------------------------------------------------------------- 1 | 存放标签文件 -------------------------------------------------------------------------------- /VOCdevkit/VOC2007/ImageSets/Main/README.md: -------------------------------------------------------------------------------- 1 | 存放训练索引文件 -------------------------------------------------------------------------------- /VOCdevkit/VOC2007/JPEGImages/README.md: -------------------------------------------------------------------------------- 1 | 存放图片文件 -------------------------------------------------------------------------------- /get_map.py: -------------------------------------------------------------------------------- 1 | import os 2 | import xml.etree.ElementTree as ET 3 | 4 | from PIL import Image 5 | from tqdm import tqdm 6 | 7 | from utils.utils import get_classes 8 | from utils.utils_map import get_coco_map, get_map 9 | from yolo import YOLO 10 | 11 | if __name__ == "__main__": 12 | ''' 13 | Recall和Precision不像AP是一个面积的概念,因此在门限值(Confidence)不同时,网络的Recall和Precision值是不同的。 14 | 默认情况下,本代码计算的Recall和Precision代表的是当门限值(Confidence)为0.5时,所对应的Recall和Precision值。 15 | 16 | 受到mAP计算原理的限制,网络在计算mAP时需要获得近乎所有的预测框,这样才可以计算不同门限条件下的Recall和Precision值 17 | 因此,本代码获得的map_out/detection-results/里面的txt的框的数量一般会比直接predict多一些,目的是列出所有可能的预测框, 18 | ''' 19 | #------------------------------------------------------------------------------------------------------------------# 20 | # map_mode用于指定该文件运行时计算的内容 21 | # map_mode为0代表整个map计算流程,包括获得预测结果、获得真实框、计算VOC_map。 22 | # map_mode为1代表仅仅获得预测结果。 23 | # map_mode为2代表仅仅获得真实框。 24 | # map_mode为3代表仅仅计算VOC_map。 25 | # map_mode为4代表利用COCO工具箱计算当前数据集的0.50:0.95map。需要获得预测结果、获得真实框后并安装pycocotools才行 26 | #-------------------------------------------------------------------------------------------------------------------# 27 | map_mode = 0 28 | #--------------------------------------------------------------------------------------# 29 | # 此处的classes_path用于指定需要测量VOC_map的类别 30 | # 一般情况下与训练和预测所用的classes_path一致即可 31 | #--------------------------------------------------------------------------------------# 32 | classes_path = 'model_data/voc_classes.txt' 33 | #--------------------------------------------------------------------------------------# 34 | # MINOVERLAP用于指定想要获得的mAP0.x,mAP0.x的意义是什么请同学们百度一下。 35 | # 比如计算mAP0.75,可以设定MINOVERLAP = 0.75。 36 | # 37 | # 当某一预测框与真实框重合度大于MINOVERLAP时,该预测框被认为是正样本,否则为负样本。 38 | # 因此MINOVERLAP的值越大,预测框要预测的越准确才能被认为是正样本,此时算出来的mAP值越低, 39 | #--------------------------------------------------------------------------------------# 40 | MINOVERLAP = 0.5 41 | #--------------------------------------------------------------------------------------# 42 | # 受到mAP计算原理的限制,网络在计算mAP时需要获得近乎所有的预测框,这样才可以计算mAP 43 | # 因此,confidence的值应当设置的尽量小进而获得全部可能的预测框。 44 | # 45 | # 该值一般不调整。因为计算mAP需要获得近乎所有的预测框,此处的confidence不能随便更改。 46 | # 想要获得不同门限值下的Recall和Precision值,请修改下方的score_threhold。 47 | #--------------------------------------------------------------------------------------# 48 | confidence = 0.001 49 | #--------------------------------------------------------------------------------------# 50 | # 预测时使用到的非极大抑制值的大小,越大表示非极大抑制越不严格。 51 | # 52 | # 该值一般不调整。 53 | #--------------------------------------------------------------------------------------# 54 | nms_iou = 0.5 55 | #---------------------------------------------------------------------------------------------------------------# 56 | # Recall和Precision不像AP是一个面积的概念,因此在门限值不同时,网络的Recall和Precision值是不同的。 57 | # 58 | # 默认情况下,本代码计算的Recall和Precision代表的是当门限值为0.5(此处定义为score_threhold)时所对应的Recall和Precision值。 59 | # 因为计算mAP需要获得近乎所有的预测框,上面定义的confidence不能随便更改。 60 | # 这里专门定义一个score_threhold用于代表门限值,进而在计算mAP时找到门限值对应的Recall和Precision值。 61 | #---------------------------------------------------------------------------------------------------------------# 62 | score_threhold = 0.5 63 | #-------------------------------------------------------# 64 | # map_vis用于指定是否开启VOC_map计算的可视化 65 | #-------------------------------------------------------# 66 | map_vis = False 67 | #-------------------------------------------------------# 68 | # 指向VOC数据集所在的文件夹 69 | # 默认指向根目录下的VOC数据集 70 | #-------------------------------------------------------# 71 | VOCdevkit_path = 'VOCdevkit' 72 | #-------------------------------------------------------# 73 | # 结果输出的文件夹,默认为map_out 74 | #-------------------------------------------------------# 75 | map_out_path = 'map_out' 76 | 77 | image_ids = open(os.path.join(VOCdevkit_path, "VOC2007/ImageSets/Main/test.txt")).read().strip().split() 78 | 79 | if not os.path.exists(map_out_path): 80 | os.makedirs(map_out_path) 81 | if not os.path.exists(os.path.join(map_out_path, 'ground-truth')): 82 | os.makedirs(os.path.join(map_out_path, 'ground-truth')) 83 | if not os.path.exists(os.path.join(map_out_path, 'detection-results')): 84 | os.makedirs(os.path.join(map_out_path, 'detection-results')) 85 | if not os.path.exists(os.path.join(map_out_path, 'images-optional')): 86 | os.makedirs(os.path.join(map_out_path, 'images-optional')) 87 | 88 | class_names, _ = get_classes(classes_path) 89 | 90 | if map_mode == 0 or map_mode == 1: 91 | print("Load model.") 92 | yolo = YOLO(confidence = confidence, nms_iou = nms_iou) 93 | print("Load model done.") 94 | 95 | print("Get predict result.") 96 | for image_id in tqdm(image_ids): 97 | image_path = os.path.join(VOCdevkit_path, "VOC2007/JPEGImages/"+image_id+".jpg") 98 | image = Image.open(image_path) 99 | if map_vis: 100 | image.save(os.path.join(map_out_path, "images-optional/" + image_id + ".jpg")) 101 | yolo.get_map_txt(image_id, image, class_names, map_out_path) 102 | print("Get predict result done.") 103 | 104 | if map_mode == 0 or map_mode == 2: 105 | print("Get ground truth result.") 106 | for image_id in tqdm(image_ids): 107 | with open(os.path.join(map_out_path, "ground-truth/"+image_id+".txt"), "w") as new_f: 108 | root = ET.parse(os.path.join(VOCdevkit_path, "VOC2007/Annotations/"+image_id+".xml")).getroot() 109 | for obj in root.findall('object'): 110 | difficult_flag = False 111 | if obj.find('difficult')!=None: 112 | difficult = obj.find('difficult').text 113 | if int(difficult)==1: 114 | difficult_flag = True 115 | obj_name = obj.find('name').text 116 | if obj_name not in class_names: 117 | continue 118 | bndbox = obj.find('bndbox') 119 | left = bndbox.find('xmin').text 120 | top = bndbox.find('ymin').text 121 | right = bndbox.find('xmax').text 122 | bottom = bndbox.find('ymax').text 123 | 124 | if difficult_flag: 125 | new_f.write("%s %s %s %s %s difficult\n" % (obj_name, left, top, right, bottom)) 126 | else: 127 | new_f.write("%s %s %s %s %s\n" % (obj_name, left, top, right, bottom)) 128 | print("Get ground truth result done.") 129 | 130 | if map_mode == 0 or map_mode == 3: 131 | print("Get map.") 132 | get_map(MINOVERLAP, True, score_threhold = score_threhold, path = map_out_path) 133 | print("Get map done.") 134 | 135 | if map_mode == 4: 136 | print("Get map.") 137 | get_coco_map(class_names = class_names, path = map_out_path) 138 | print("Get map done.") 139 | -------------------------------------------------------------------------------- /img/street.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bubbliiiing/yolo3-pytorch/8fbb81d3377b7a404c8634ad26ff3f1193da2cad/img/street.jpg -------------------------------------------------------------------------------- /kmeans_for_anchors.py: -------------------------------------------------------------------------------- 1 | #-------------------------------------------------------------------------------------------------------# 2 | # kmeans虽然会对数据集中的框进行聚类,但是很多数据集由于框的大小相近,聚类出来的9个框相差不大, 3 | # 这样的框反而不利于模型的训练。因为不同的特征层适合不同大小的先验框,shape越小的特征层适合越大的先验框 4 | # 原始网络的先验框已经按大中小比例分配好了,不进行聚类也会有非常好的效果。 5 | #-------------------------------------------------------------------------------------------------------# 6 | import glob 7 | import xml.etree.ElementTree as ET 8 | 9 | import matplotlib.pyplot as plt 10 | import numpy as np 11 | from tqdm import tqdm 12 | 13 | 14 | def cas_iou(box, cluster): 15 | x = np.minimum(cluster[:, 0], box[0]) 16 | y = np.minimum(cluster[:, 1], box[1]) 17 | 18 | intersection = x * y 19 | area1 = box[0] * box[1] 20 | 21 | area2 = cluster[:,0] * cluster[:,1] 22 | iou = intersection / (area1 + area2 - intersection) 23 | 24 | return iou 25 | 26 | def avg_iou(box, cluster): 27 | return np.mean([np.max(cas_iou(box[i], cluster)) for i in range(box.shape[0])]) 28 | 29 | def kmeans(box, k): 30 | #-------------------------------------------------------------# 31 | # 取出一共有多少框 32 | #-------------------------------------------------------------# 33 | row = box.shape[0] 34 | 35 | #-------------------------------------------------------------# 36 | # 每个框各个点的位置 37 | #-------------------------------------------------------------# 38 | distance = np.empty((row, k)) 39 | 40 | #-------------------------------------------------------------# 41 | # 最后的聚类位置 42 | #-------------------------------------------------------------# 43 | last_clu = np.zeros((row, )) 44 | 45 | np.random.seed() 46 | 47 | #-------------------------------------------------------------# 48 | # 随机选5个当聚类中心 49 | #-------------------------------------------------------------# 50 | cluster = box[np.random.choice(row, k, replace = False)] 51 | 52 | iter = 0 53 | while True: 54 | #-------------------------------------------------------------# 55 | # 计算当前框和先验框的宽高比例 56 | #-------------------------------------------------------------# 57 | for i in range(row): 58 | distance[i] = 1 - cas_iou(box[i], cluster) 59 | 60 | #-------------------------------------------------------------# 61 | # 取出最小点 62 | #-------------------------------------------------------------# 63 | near = np.argmin(distance, axis=1) 64 | 65 | if (last_clu == near).all(): 66 | break 67 | 68 | #-------------------------------------------------------------# 69 | # 求每一个类的中位点 70 | #-------------------------------------------------------------# 71 | for j in range(k): 72 | cluster[j] = np.median( 73 | box[near == j],axis=0) 74 | 75 | last_clu = near 76 | if iter % 5 == 0: 77 | print('iter: {:d}. avg_iou:{:.2f}'.format(iter, avg_iou(box, cluster))) 78 | iter += 1 79 | 80 | return cluster, near 81 | 82 | def load_data(path): 83 | data = [] 84 | #-------------------------------------------------------------# 85 | # 对于每一个xml都寻找box 86 | #-------------------------------------------------------------# 87 | for xml_file in tqdm(glob.glob('{}/*xml'.format(path))): 88 | tree = ET.parse(xml_file) 89 | height = int(tree.findtext('./size/height')) 90 | width = int(tree.findtext('./size/width')) 91 | if height<=0 or width<=0: 92 | continue 93 | 94 | #-------------------------------------------------------------# 95 | # 对于每一个目标都获得它的宽高 96 | #-------------------------------------------------------------# 97 | for obj in tree.iter('object'): 98 | xmin = int(float(obj.findtext('bndbox/xmin'))) / width 99 | ymin = int(float(obj.findtext('bndbox/ymin'))) / height 100 | xmax = int(float(obj.findtext('bndbox/xmax'))) / width 101 | ymax = int(float(obj.findtext('bndbox/ymax'))) / height 102 | 103 | xmin = np.float64(xmin) 104 | ymin = np.float64(ymin) 105 | xmax = np.float64(xmax) 106 | ymax = np.float64(ymax) 107 | # 得到宽高 108 | data.append([xmax - xmin, ymax - ymin]) 109 | return np.array(data) 110 | 111 | if __name__ == '__main__': 112 | np.random.seed(0) 113 | #-------------------------------------------------------------# 114 | # 运行该程序会计算'./VOCdevkit/VOC2007/Annotations'的xml 115 | # 会生成yolo_anchors.txt 116 | #-------------------------------------------------------------# 117 | input_shape = [416, 416] 118 | anchors_num = 9 119 | #-------------------------------------------------------------# 120 | # 载入数据集,可以使用VOC的xml 121 | #-------------------------------------------------------------# 122 | path = 'VOCdevkit/VOC2007/Annotations' 123 | 124 | #-------------------------------------------------------------# 125 | # 载入所有的xml 126 | # 存储格式为转化为比例后的width,height 127 | #-------------------------------------------------------------# 128 | print('Load xmls.') 129 | data = load_data(path) 130 | print('Load xmls done.') 131 | 132 | #-------------------------------------------------------------# 133 | # 使用k聚类算法 134 | #-------------------------------------------------------------# 135 | print('K-means boxes.') 136 | cluster, near = kmeans(data, anchors_num) 137 | print('K-means boxes done.') 138 | data = data * np.array([input_shape[1], input_shape[0]]) 139 | cluster = cluster * np.array([input_shape[1], input_shape[0]]) 140 | 141 | #-------------------------------------------------------------# 142 | # 绘图 143 | #-------------------------------------------------------------# 144 | for j in range(anchors_num): 145 | plt.scatter(data[near == j][:,0], data[near == j][:,1]) 146 | plt.scatter(cluster[j][0], cluster[j][1], marker='x', c='black') 147 | plt.savefig("kmeans_for_anchors.jpg") 148 | plt.show() 149 | print('Save kmeans_for_anchors.jpg in root dir.') 150 | 151 | cluster = cluster[np.argsort(cluster[:, 0] * cluster[:, 1])] 152 | print('avg_ratio:{:.2f}'.format(avg_iou(data, cluster))) 153 | print(cluster) 154 | 155 | f = open("yolo_anchors.txt", 'w') 156 | row = np.shape(cluster)[0] 157 | for i in range(row): 158 | if i == 0: 159 | x_y = "%d,%d" % (cluster[i][0], cluster[i][1]) 160 | else: 161 | x_y = ", %d,%d" % (cluster[i][0], cluster[i][1]) 162 | f.write(x_y) 163 | f.close() 164 | -------------------------------------------------------------------------------- /logs/README.md: -------------------------------------------------------------------------------- 1 | 用于存放训练好的文件 -------------------------------------------------------------------------------- /model_data/coco_classes.txt: -------------------------------------------------------------------------------- 1 | person 2 | bicycle 3 | car 4 | motorbike 5 | aeroplane 6 | bus 7 | train 8 | truck 9 | boat 10 | traffic light 11 | fire hydrant 12 | stop sign 13 | parking meter 14 | bench 15 | bird 16 | cat 17 | dog 18 | horse 19 | sheep 20 | cow 21 | elephant 22 | bear 23 | zebra 24 | giraffe 25 | backpack 26 | umbrella 27 | handbag 28 | tie 29 | suitcase 30 | frisbee 31 | skis 32 | snowboard 33 | sports ball 34 | kite 35 | baseball bat 36 | baseball glove 37 | skateboard 38 | surfboard 39 | tennis racket 40 | bottle 41 | wine glass 42 | cup 43 | fork 44 | knife 45 | spoon 46 | bowl 47 | banana 48 | apple 49 | sandwich 50 | orange 51 | broccoli 52 | carrot 53 | hot dog 54 | pizza 55 | donut 56 | cake 57 | chair 58 | sofa 59 | pottedplant 60 | bed 61 | diningtable 62 | toilet 63 | tvmonitor 64 | laptop 65 | mouse 66 | remote 67 | keyboard 68 | cell phone 69 | microwave 70 | oven 71 | toaster 72 | sink 73 | refrigerator 74 | book 75 | clock 76 | vase 77 | scissors 78 | teddy bear 79 | hair drier 80 | toothbrush 81 | -------------------------------------------------------------------------------- /model_data/simhei.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bubbliiiing/yolo3-pytorch/8fbb81d3377b7a404c8634ad26ff3f1193da2cad/model_data/simhei.ttf -------------------------------------------------------------------------------- /model_data/voc_classes.txt: -------------------------------------------------------------------------------- 1 | aeroplane 2 | bicycle 3 | bird 4 | boat 5 | bottle 6 | bus 7 | car 8 | cat 9 | chair 10 | cow 11 | diningtable 12 | dog 13 | horse 14 | motorbike 15 | person 16 | pottedplant 17 | sheep 18 | sofa 19 | train 20 | tvmonitor -------------------------------------------------------------------------------- /model_data/yolo_anchors.txt: -------------------------------------------------------------------------------- 1 | 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326 -------------------------------------------------------------------------------- /nets/__init__.py: -------------------------------------------------------------------------------- 1 | # -------------------------------------------------------------------------------- /nets/darknet.py: -------------------------------------------------------------------------------- 1 | import math 2 | from collections import OrderedDict 3 | 4 | import torch.nn as nn 5 | 6 | 7 | #---------------------------------------------------------------------# 8 | # 残差结构 9 | # 利用一个1x1卷积下降通道数,然后利用一个3x3卷积提取特征并且上升通道数 10 | # 最后接上一个残差边 11 | #---------------------------------------------------------------------# 12 | class BasicBlock(nn.Module): 13 | def __init__(self, inplanes, planes): 14 | super(BasicBlock, self).__init__() 15 | self.conv1 = nn.Conv2d(inplanes, planes[0], kernel_size=1, stride=1, padding=0, bias=False) 16 | self.bn1 = nn.BatchNorm2d(planes[0]) 17 | self.relu1 = nn.LeakyReLU(0.1) 18 | 19 | self.conv2 = nn.Conv2d(planes[0], planes[1], kernel_size=3, stride=1, padding=1, bias=False) 20 | self.bn2 = nn.BatchNorm2d(planes[1]) 21 | self.relu2 = nn.LeakyReLU(0.1) 22 | 23 | def forward(self, x): 24 | residual = x 25 | 26 | out = self.conv1(x) 27 | out = self.bn1(out) 28 | out = self.relu1(out) 29 | 30 | out = self.conv2(out) 31 | out = self.bn2(out) 32 | out = self.relu2(out) 33 | 34 | out += residual 35 | return out 36 | 37 | class DarkNet(nn.Module): 38 | def __init__(self, layers): 39 | super(DarkNet, self).__init__() 40 | self.inplanes = 32 41 | # 416,416,3 -> 416,416,32 42 | self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=3, stride=1, padding=1, bias=False) 43 | self.bn1 = nn.BatchNorm2d(self.inplanes) 44 | self.relu1 = nn.LeakyReLU(0.1) 45 | 46 | # 416,416,32 -> 208,208,64 47 | self.layer1 = self._make_layer([32, 64], layers[0]) 48 | # 208,208,64 -> 104,104,128 49 | self.layer2 = self._make_layer([64, 128], layers[1]) 50 | # 104,104,128 -> 52,52,256 51 | self.layer3 = self._make_layer([128, 256], layers[2]) 52 | # 52,52,256 -> 26,26,512 53 | self.layer4 = self._make_layer([256, 512], layers[3]) 54 | # 26,26,512 -> 13,13,1024 55 | self.layer5 = self._make_layer([512, 1024], layers[4]) 56 | 57 | self.layers_out_filters = [64, 128, 256, 512, 1024] 58 | 59 | # 进行权值初始化 60 | for m in self.modules(): 61 | if isinstance(m, nn.Conv2d): 62 | n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels 63 | m.weight.data.normal_(0, math.sqrt(2. / n)) 64 | elif isinstance(m, nn.BatchNorm2d): 65 | m.weight.data.fill_(1) 66 | m.bias.data.zero_() 67 | 68 | #---------------------------------------------------------------------# 69 | # 在每一个layer里面,首先利用一个步长为2的3x3卷积进行下采样 70 | # 然后进行残差结构的堆叠 71 | #---------------------------------------------------------------------# 72 | def _make_layer(self, planes, blocks): 73 | layers = [] 74 | # 下采样,步长为2,卷积核大小为3 75 | layers.append(("ds_conv", nn.Conv2d(self.inplanes, planes[1], kernel_size=3, stride=2, padding=1, bias=False))) 76 | layers.append(("ds_bn", nn.BatchNorm2d(planes[1]))) 77 | layers.append(("ds_relu", nn.LeakyReLU(0.1))) 78 | # 加入残差结构 79 | self.inplanes = planes[1] 80 | for i in range(0, blocks): 81 | layers.append(("residual_{}".format(i), BasicBlock(self.inplanes, planes))) 82 | return nn.Sequential(OrderedDict(layers)) 83 | 84 | def forward(self, x): 85 | x = self.conv1(x) 86 | x = self.bn1(x) 87 | x = self.relu1(x) 88 | 89 | x = self.layer1(x) 90 | x = self.layer2(x) 91 | out3 = self.layer3(x) 92 | out4 = self.layer4(out3) 93 | out5 = self.layer5(out4) 94 | 95 | return out3, out4, out5 96 | 97 | def darknet53(): 98 | model = DarkNet([1, 2, 8, 8, 4]) 99 | return model 100 | -------------------------------------------------------------------------------- /nets/yolo.py: -------------------------------------------------------------------------------- 1 | from collections import OrderedDict 2 | 3 | import torch 4 | import torch.nn as nn 5 | 6 | from nets.darknet import darknet53 7 | 8 | def conv2d(filter_in, filter_out, kernel_size): 9 | pad = (kernel_size - 1) // 2 if kernel_size else 0 10 | return nn.Sequential(OrderedDict([ 11 | ("conv", nn.Conv2d(filter_in, filter_out, kernel_size=kernel_size, stride=1, padding=pad, bias=False)), 12 | ("bn", nn.BatchNorm2d(filter_out)), 13 | ("relu", nn.LeakyReLU(0.1)), 14 | ])) 15 | 16 | #------------------------------------------------------------------------# 17 | # make_last_layers里面一共有七个卷积,前五个用于提取特征。 18 | # 后两个用于获得yolo网络的预测结果 19 | #------------------------------------------------------------------------# 20 | def make_last_layers(filters_list, in_filters, out_filter): 21 | m = nn.Sequential( 22 | conv2d(in_filters, filters_list[0], 1), 23 | conv2d(filters_list[0], filters_list[1], 3), 24 | conv2d(filters_list[1], filters_list[0], 1), 25 | conv2d(filters_list[0], filters_list[1], 3), 26 | conv2d(filters_list[1], filters_list[0], 1), 27 | conv2d(filters_list[0], filters_list[1], 3), 28 | nn.Conv2d(filters_list[1], out_filter, kernel_size=1, stride=1, padding=0, bias=True) 29 | ) 30 | return m 31 | 32 | class YoloBody(nn.Module): 33 | def __init__(self, anchors_mask, num_classes, pretrained = False): 34 | super(YoloBody, self).__init__() 35 | #---------------------------------------------------# 36 | # 生成darknet53的主干模型 37 | # 获得三个有效特征层,他们的shape分别是: 38 | # 52,52,256 39 | # 26,26,512 40 | # 13,13,1024 41 | #---------------------------------------------------# 42 | self.backbone = darknet53() 43 | if pretrained: 44 | self.backbone.load_state_dict(torch.load("model_data/darknet53_backbone_weights.pth")) 45 | 46 | #---------------------------------------------------# 47 | # out_filters : [64, 128, 256, 512, 1024] 48 | #---------------------------------------------------# 49 | out_filters = self.backbone.layers_out_filters 50 | 51 | #------------------------------------------------------------------------# 52 | # 计算yolo_head的输出通道数,对于voc数据集而言 53 | # final_out_filter0 = final_out_filter1 = final_out_filter2 = 75 54 | #------------------------------------------------------------------------# 55 | self.last_layer0 = make_last_layers([512, 1024], out_filters[-1], len(anchors_mask[0]) * (num_classes + 5)) 56 | 57 | self.last_layer1_conv = conv2d(512, 256, 1) 58 | self.last_layer1_upsample = nn.Upsample(scale_factor=2, mode='nearest') 59 | self.last_layer1 = make_last_layers([256, 512], out_filters[-2] + 256, len(anchors_mask[1]) * (num_classes + 5)) 60 | 61 | self.last_layer2_conv = conv2d(256, 128, 1) 62 | self.last_layer2_upsample = nn.Upsample(scale_factor=2, mode='nearest') 63 | self.last_layer2 = make_last_layers([128, 256], out_filters[-3] + 128, len(anchors_mask[2]) * (num_classes + 5)) 64 | 65 | def forward(self, x): 66 | #---------------------------------------------------# 67 | # 获得三个有效特征层,他们的shape分别是: 68 | # 52,52,256;26,26,512;13,13,1024 69 | #---------------------------------------------------# 70 | x2, x1, x0 = self.backbone(x) 71 | 72 | #---------------------------------------------------# 73 | # 第一个特征层 74 | # out0 = (batch_size,255,13,13) 75 | #---------------------------------------------------# 76 | # 13,13,1024 -> 13,13,512 -> 13,13,1024 -> 13,13,512 -> 13,13,1024 -> 13,13,512 77 | out0_branch = self.last_layer0[:5](x0) 78 | out0 = self.last_layer0[5:](out0_branch) 79 | 80 | # 13,13,512 -> 13,13,256 -> 26,26,256 81 | x1_in = self.last_layer1_conv(out0_branch) 82 | x1_in = self.last_layer1_upsample(x1_in) 83 | 84 | # 26,26,256 + 26,26,512 -> 26,26,768 85 | x1_in = torch.cat([x1_in, x1], 1) 86 | #---------------------------------------------------# 87 | # 第二个特征层 88 | # out1 = (batch_size,255,26,26) 89 | #---------------------------------------------------# 90 | # 26,26,768 -> 26,26,256 -> 26,26,512 -> 26,26,256 -> 26,26,512 -> 26,26,256 91 | out1_branch = self.last_layer1[:5](x1_in) 92 | out1 = self.last_layer1[5:](out1_branch) 93 | 94 | # 26,26,256 -> 26,26,128 -> 52,52,128 95 | x2_in = self.last_layer2_conv(out1_branch) 96 | x2_in = self.last_layer2_upsample(x2_in) 97 | 98 | # 52,52,128 + 52,52,256 -> 52,52,384 99 | x2_in = torch.cat([x2_in, x2], 1) 100 | #---------------------------------------------------# 101 | # 第一个特征层 102 | # out3 = (batch_size,255,52,52) 103 | #---------------------------------------------------# 104 | # 52,52,384 -> 52,52,128 -> 52,52,256 -> 52,52,128 -> 52,52,256 -> 52,52,128 105 | out2 = self.last_layer2(x2_in) 106 | return out0, out1, out2 -------------------------------------------------------------------------------- /nets/yolo_training.py: -------------------------------------------------------------------------------- 1 | import math 2 | from functools import partial 3 | 4 | import numpy as np 5 | import torch 6 | import torch.nn as nn 7 | 8 | class YOLOLoss(nn.Module): 9 | def __init__(self, anchors, num_classes, input_shape, cuda, anchors_mask = [[6,7,8], [3,4,5], [0,1,2]]): 10 | super(YOLOLoss, self).__init__() 11 | #-----------------------------------------------------------# 12 | # 13x13的特征层对应的anchor是[116,90],[156,198],[373,326] 13 | # 26x26的特征层对应的anchor是[30,61],[62,45],[59,119] 14 | # 52x52的特征层对应的anchor是[10,13],[16,30],[33,23] 15 | #-----------------------------------------------------------# 16 | self.anchors = anchors 17 | self.num_classes = num_classes 18 | self.bbox_attrs = 5 + num_classes 19 | self.input_shape = input_shape 20 | self.anchors_mask = anchors_mask 21 | 22 | self.giou = True 23 | self.balance = [0.4, 1.0, 4] 24 | self.box_ratio = 0.05 25 | self.obj_ratio = 5 * (input_shape[0] * input_shape[1]) / (416 ** 2) 26 | self.cls_ratio = 1 * (num_classes / 80) 27 | 28 | self.ignore_threshold = 0.5 29 | self.cuda = cuda 30 | 31 | def clip_by_tensor(self, t, t_min, t_max): 32 | t = t.float() 33 | result = (t >= t_min).float() * t + (t < t_min).float() * t_min 34 | result = (result <= t_max).float() * result + (result > t_max).float() * t_max 35 | return result 36 | 37 | def MSELoss(self, pred, target): 38 | return torch.pow(pred - target, 2) 39 | 40 | def BCELoss(self, pred, target): 41 | epsilon = 1e-7 42 | pred = self.clip_by_tensor(pred, epsilon, 1.0 - epsilon) 43 | output = - target * torch.log(pred) - (1.0 - target) * torch.log(1.0 - pred) 44 | return output 45 | 46 | def box_giou(self, b1, b2): 47 | """ 48 | 输入为: 49 | ---------- 50 | b1: tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh 51 | b2: tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh 52 | 53 | 返回为: 54 | ------- 55 | giou: tensor, shape=(batch, feat_w, feat_h, anchor_num, 1) 56 | """ 57 | #----------------------------------------------------# 58 | # 求出预测框左上角右下角 59 | #----------------------------------------------------# 60 | b1_xy = b1[..., :2] 61 | b1_wh = b1[..., 2:4] 62 | b1_wh_half = b1_wh/2. 63 | b1_mins = b1_xy - b1_wh_half 64 | b1_maxes = b1_xy + b1_wh_half 65 | #----------------------------------------------------# 66 | # 求出真实框左上角右下角 67 | #----------------------------------------------------# 68 | b2_xy = b2[..., :2] 69 | b2_wh = b2[..., 2:4] 70 | b2_wh_half = b2_wh/2. 71 | b2_mins = b2_xy - b2_wh_half 72 | b2_maxes = b2_xy + b2_wh_half 73 | 74 | #----------------------------------------------------# 75 | # 求真实框和预测框所有的iou 76 | #----------------------------------------------------# 77 | intersect_mins = torch.max(b1_mins, b2_mins) 78 | intersect_maxes = torch.min(b1_maxes, b2_maxes) 79 | intersect_wh = torch.max(intersect_maxes - intersect_mins, torch.zeros_like(intersect_maxes)) 80 | intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] 81 | b1_area = b1_wh[..., 0] * b1_wh[..., 1] 82 | b2_area = b2_wh[..., 0] * b2_wh[..., 1] 83 | union_area = b1_area + b2_area - intersect_area 84 | iou = intersect_area / union_area 85 | 86 | #----------------------------------------------------# 87 | # 找到包裹两个框的最小框的左上角和右下角 88 | #----------------------------------------------------# 89 | enclose_mins = torch.min(b1_mins, b2_mins) 90 | enclose_maxes = torch.max(b1_maxes, b2_maxes) 91 | enclose_wh = torch.max(enclose_maxes - enclose_mins, torch.zeros_like(intersect_maxes)) 92 | #----------------------------------------------------# 93 | # 计算对角线距离 94 | #----------------------------------------------------# 95 | enclose_area = enclose_wh[..., 0] * enclose_wh[..., 1] 96 | giou = iou - (enclose_area - union_area) / enclose_area 97 | 98 | return giou 99 | 100 | def forward(self, l, input, targets=None): 101 | #----------------------------------------------------# 102 | # l代表的是,当前输入进来的有效特征层,是第几个有效特征层 103 | # input的shape为 bs, 3*(5+num_classes), 13, 13 104 | # bs, 3*(5+num_classes), 26, 26 105 | # bs, 3*(5+num_classes), 52, 52 106 | # targets代表的是真实框。 107 | #----------------------------------------------------# 108 | #--------------------------------# 109 | # 获得图片数量,特征层的高和宽 110 | # 13和13 111 | #--------------------------------# 112 | bs = input.size(0) 113 | in_h = input.size(2) 114 | in_w = input.size(3) 115 | #-----------------------------------------------------------------------# 116 | # 计算步长 117 | # 每一个特征点对应原来的图片上多少个像素点 118 | # 如果特征层为13x13的话,一个特征点就对应原来的图片上的32个像素点 119 | # 如果特征层为26x26的话,一个特征点就对应原来的图片上的16个像素点 120 | # 如果特征层为52x52的话,一个特征点就对应原来的图片上的8个像素点 121 | # stride_h = stride_w = 32、16、8 122 | # stride_h和stride_w都是32。 123 | #-----------------------------------------------------------------------# 124 | stride_h = self.input_shape[0] / in_h 125 | stride_w = self.input_shape[1] / in_w 126 | #-------------------------------------------------# 127 | # 此时获得的scaled_anchors大小是相对于特征层的 128 | #-------------------------------------------------# 129 | scaled_anchors = [(a_w / stride_w, a_h / stride_h) for a_w, a_h in self.anchors] 130 | #-----------------------------------------------# 131 | # 输入的input一共有三个,他们的shape分别是 132 | # bs, 3*(5+num_classes), 13, 13 => batch_size, 3, 13, 13, 5 + num_classes 133 | # batch_size, 3, 26, 26, 5 + num_classes 134 | # batch_size, 3, 52, 52, 5 + num_classes 135 | #-----------------------------------------------# 136 | prediction = input.view(bs, len(self.anchors_mask[l]), self.bbox_attrs, in_h, in_w).permute(0, 1, 3, 4, 2).contiguous() 137 | 138 | #-----------------------------------------------# 139 | # 先验框的中心位置的调整参数 140 | #-----------------------------------------------# 141 | x = torch.sigmoid(prediction[..., 0]) 142 | y = torch.sigmoid(prediction[..., 1]) 143 | #-----------------------------------------------# 144 | # 先验框的宽高调整参数 145 | #-----------------------------------------------# 146 | w = prediction[..., 2] 147 | h = prediction[..., 3] 148 | #-----------------------------------------------# 149 | # 获得置信度,是否有物体 150 | #-----------------------------------------------# 151 | conf = torch.sigmoid(prediction[..., 4]) 152 | #-----------------------------------------------# 153 | # 种类置信度 154 | #-----------------------------------------------# 155 | pred_cls = torch.sigmoid(prediction[..., 5:]) 156 | 157 | #-----------------------------------------------# 158 | # 获得网络应该有的预测结果 159 | #-----------------------------------------------# 160 | y_true, noobj_mask, box_loss_scale = self.get_target(l, targets, scaled_anchors, in_h, in_w) 161 | 162 | #---------------------------------------------------------------# 163 | # 将预测结果进行解码,判断预测结果和真实值的重合程度 164 | # 如果重合程度过大则忽略,因为这些特征点属于预测比较准确的特征点 165 | # 作为负样本不合适 166 | #----------------------------------------------------------------# 167 | noobj_mask, pred_boxes = self.get_ignore(l, x, y, h, w, targets, scaled_anchors, in_h, in_w, noobj_mask) 168 | 169 | if self.cuda: 170 | y_true = y_true.type_as(x) 171 | noobj_mask = noobj_mask.type_as(x) 172 | box_loss_scale = box_loss_scale.type_as(x) 173 | #--------------------------------------------------------------------------# 174 | # box_loss_scale是真实框宽高的乘积,宽高均在0-1之间,因此乘积也在0-1之间。 175 | # 2-宽高的乘积代表真实框越大,比重越小,小框的比重更大。 176 | #--------------------------------------------------------------------------# 177 | box_loss_scale = 2 - box_loss_scale 178 | 179 | loss = 0 180 | obj_mask = y_true[..., 4] == 1 181 | n = torch.sum(obj_mask) 182 | if n != 0: 183 | if self.giou: 184 | #---------------------------------------------------------------# 185 | # 计算预测结果和真实结果的giou 186 | #----------------------------------------------------------------# 187 | giou = self.box_giou(pred_boxes, y_true[..., :4]).type_as(x) 188 | loss_loc = torch.mean((1 - giou)[obj_mask]) 189 | else: 190 | #-----------------------------------------------------------# 191 | # 计算中心偏移情况的loss,使用BCELoss效果好一些 192 | #-----------------------------------------------------------# 193 | loss_x = torch.mean(self.BCELoss(x[obj_mask], y_true[..., 0][obj_mask]) * box_loss_scale[obj_mask]) 194 | loss_y = torch.mean(self.BCELoss(y[obj_mask], y_true[..., 1][obj_mask]) * box_loss_scale[obj_mask]) 195 | #-----------------------------------------------------------# 196 | # 计算宽高调整值的loss 197 | #-----------------------------------------------------------# 198 | loss_w = torch.mean(self.MSELoss(w[obj_mask], y_true[..., 2][obj_mask]) * box_loss_scale[obj_mask]) 199 | loss_h = torch.mean(self.MSELoss(h[obj_mask], y_true[..., 3][obj_mask]) * box_loss_scale[obj_mask]) 200 | loss_loc = (loss_x + loss_y + loss_h + loss_w) * 0.1 201 | 202 | loss_cls = torch.mean(self.BCELoss(pred_cls[obj_mask], y_true[..., 5:][obj_mask])) 203 | loss += loss_loc * self.box_ratio + loss_cls * self.cls_ratio 204 | 205 | loss_conf = torch.mean(self.BCELoss(conf, obj_mask.type_as(conf))[noobj_mask.bool() | obj_mask]) 206 | loss += loss_conf * self.balance[l] * self.obj_ratio 207 | # if n != 0: 208 | # print(loss_loc * self.box_ratio, loss_cls * self.cls_ratio, loss_conf * self.balance[l] * self.obj_ratio) 209 | return loss 210 | 211 | def calculate_iou(self, _box_a, _box_b): 212 | #-----------------------------------------------------------# 213 | # 计算真实框的左上角和右下角 214 | #-----------------------------------------------------------# 215 | b1_x1, b1_x2 = _box_a[:, 0] - _box_a[:, 2] / 2, _box_a[:, 0] + _box_a[:, 2] / 2 216 | b1_y1, b1_y2 = _box_a[:, 1] - _box_a[:, 3] / 2, _box_a[:, 1] + _box_a[:, 3] / 2 217 | #-----------------------------------------------------------# 218 | # 计算先验框获得的预测框的左上角和右下角 219 | #-----------------------------------------------------------# 220 | b2_x1, b2_x2 = _box_b[:, 0] - _box_b[:, 2] / 2, _box_b[:, 0] + _box_b[:, 2] / 2 221 | b2_y1, b2_y2 = _box_b[:, 1] - _box_b[:, 3] / 2, _box_b[:, 1] + _box_b[:, 3] / 2 222 | 223 | #-----------------------------------------------------------# 224 | # 将真实框和预测框都转化成左上角右下角的形式 225 | #-----------------------------------------------------------# 226 | box_a = torch.zeros_like(_box_a) 227 | box_b = torch.zeros_like(_box_b) 228 | box_a[:, 0], box_a[:, 1], box_a[:, 2], box_a[:, 3] = b1_x1, b1_y1, b1_x2, b1_y2 229 | box_b[:, 0], box_b[:, 1], box_b[:, 2], box_b[:, 3] = b2_x1, b2_y1, b2_x2, b2_y2 230 | 231 | #-----------------------------------------------------------# 232 | # A为真实框的数量,B为先验框的数量 233 | #-----------------------------------------------------------# 234 | A = box_a.size(0) 235 | B = box_b.size(0) 236 | 237 | #-----------------------------------------------------------# 238 | # 计算交的面积 239 | #-----------------------------------------------------------# 240 | max_xy = torch.min(box_a[:, 2:].unsqueeze(1).expand(A, B, 2), box_b[:, 2:].unsqueeze(0).expand(A, B, 2)) 241 | min_xy = torch.max(box_a[:, :2].unsqueeze(1).expand(A, B, 2), box_b[:, :2].unsqueeze(0).expand(A, B, 2)) 242 | inter = torch.clamp((max_xy - min_xy), min=0) 243 | inter = inter[:, :, 0] * inter[:, :, 1] 244 | #-----------------------------------------------------------# 245 | # 计算预测框和真实框各自的面积 246 | #-----------------------------------------------------------# 247 | area_a = ((box_a[:, 2]-box_a[:, 0]) * (box_a[:, 3]-box_a[:, 1])).unsqueeze(1).expand_as(inter) # [A,B] 248 | area_b = ((box_b[:, 2]-box_b[:, 0]) * (box_b[:, 3]-box_b[:, 1])).unsqueeze(0).expand_as(inter) # [A,B] 249 | #-----------------------------------------------------------# 250 | # 求IOU 251 | #-----------------------------------------------------------# 252 | union = area_a + area_b - inter 253 | return inter / union # [A,B] 254 | 255 | def get_target(self, l, targets, anchors, in_h, in_w): 256 | #-----------------------------------------------------# 257 | # 计算一共有多少张图片 258 | #-----------------------------------------------------# 259 | bs = len(targets) 260 | #-----------------------------------------------------# 261 | # 用于选取哪些先验框不包含物体 262 | #-----------------------------------------------------# 263 | noobj_mask = torch.ones(bs, len(self.anchors_mask[l]), in_h, in_w, requires_grad = False) 264 | #-----------------------------------------------------# 265 | # 让网络更加去关注小目标 266 | #-----------------------------------------------------# 267 | box_loss_scale = torch.zeros(bs, len(self.anchors_mask[l]), in_h, in_w, requires_grad = False) 268 | #-----------------------------------------------------# 269 | # batch_size, 3, 13, 13, 5 + num_classes 270 | #-----------------------------------------------------# 271 | y_true = torch.zeros(bs, len(self.anchors_mask[l]), in_h, in_w, self.bbox_attrs, requires_grad = False) 272 | for b in range(bs): 273 | if len(targets[b])==0: 274 | continue 275 | batch_target = torch.zeros_like(targets[b]) 276 | #-------------------------------------------------------# 277 | # 计算出正样本在特征层上的中心点 278 | #-------------------------------------------------------# 279 | batch_target[:, [0,2]] = targets[b][:, [0,2]] * in_w 280 | batch_target[:, [1,3]] = targets[b][:, [1,3]] * in_h 281 | batch_target[:, 4] = targets[b][:, 4] 282 | batch_target = batch_target.cpu() 283 | 284 | #-------------------------------------------------------# 285 | # 将真实框转换一个形式 286 | # num_true_box, 4 287 | #-------------------------------------------------------# 288 | gt_box = torch.FloatTensor(torch.cat((torch.zeros((batch_target.size(0), 2)), batch_target[:, 2:4]), 1)) 289 | #-------------------------------------------------------# 290 | # 将先验框转换一个形式 291 | # 9, 4 292 | #-------------------------------------------------------# 293 | anchor_shapes = torch.FloatTensor(torch.cat((torch.zeros((len(anchors), 2)), torch.FloatTensor(anchors)), 1)) 294 | #-------------------------------------------------------# 295 | # 计算交并比 296 | # self.calculate_iou(gt_box, anchor_shapes) = [num_true_box, 9]每一个真实框和9个先验框的重合情况 297 | # best_ns: 298 | # [每个真实框最大的重合度max_iou, 每一个真实框最重合的先验框的序号] 299 | #-------------------------------------------------------# 300 | best_ns = torch.argmax(self.calculate_iou(gt_box, anchor_shapes), dim=-1) 301 | 302 | for t, best_n in enumerate(best_ns): 303 | if best_n not in self.anchors_mask[l]: 304 | continue 305 | #----------------------------------------# 306 | # 判断这个先验框是当前特征点的哪一个先验框 307 | #----------------------------------------# 308 | k = self.anchors_mask[l].index(best_n) 309 | #----------------------------------------# 310 | # 获得真实框属于哪个网格点 311 | #----------------------------------------# 312 | i = torch.floor(batch_target[t, 0]).long() 313 | j = torch.floor(batch_target[t, 1]).long() 314 | #----------------------------------------# 315 | # 取出真实框的种类 316 | #----------------------------------------# 317 | c = batch_target[t, 4].long() 318 | 319 | #----------------------------------------# 320 | # noobj_mask代表无目标的特征点 321 | #----------------------------------------# 322 | noobj_mask[b, k, j, i] = 0 323 | #----------------------------------------# 324 | # tx、ty代表中心调整参数的真实值 325 | #----------------------------------------# 326 | if not self.giou: 327 | #----------------------------------------# 328 | # tx、ty代表中心调整参数的真实值 329 | #----------------------------------------# 330 | y_true[b, k, j, i, 0] = batch_target[t, 0] - i.float() 331 | y_true[b, k, j, i, 1] = batch_target[t, 1] - j.float() 332 | y_true[b, k, j, i, 2] = math.log(batch_target[t, 2] / anchors[best_n][0]) 333 | y_true[b, k, j, i, 3] = math.log(batch_target[t, 3] / anchors[best_n][1]) 334 | y_true[b, k, j, i, 4] = 1 335 | y_true[b, k, j, i, c + 5] = 1 336 | else: 337 | #----------------------------------------# 338 | # tx、ty代表中心调整参数的真实值 339 | #----------------------------------------# 340 | y_true[b, k, j, i, 0] = batch_target[t, 0] 341 | y_true[b, k, j, i, 1] = batch_target[t, 1] 342 | y_true[b, k, j, i, 2] = batch_target[t, 2] 343 | y_true[b, k, j, i, 3] = batch_target[t, 3] 344 | y_true[b, k, j, i, 4] = 1 345 | y_true[b, k, j, i, c + 5] = 1 346 | #----------------------------------------# 347 | # 用于获得xywh的比例 348 | # 大目标loss权重小,小目标loss权重大 349 | #----------------------------------------# 350 | box_loss_scale[b, k, j, i] = batch_target[t, 2] * batch_target[t, 3] / in_w / in_h 351 | return y_true, noobj_mask, box_loss_scale 352 | 353 | def get_ignore(self, l, x, y, h, w, targets, scaled_anchors, in_h, in_w, noobj_mask): 354 | #-----------------------------------------------------# 355 | # 计算一共有多少张图片 356 | #-----------------------------------------------------# 357 | bs = len(targets) 358 | 359 | #-----------------------------------------------------# 360 | # 生成网格,先验框中心,网格左上角 361 | #-----------------------------------------------------# 362 | grid_x = torch.linspace(0, in_w - 1, in_w).repeat(in_h, 1).repeat( 363 | int(bs * len(self.anchors_mask[l])), 1, 1).view(x.shape).type_as(x) 364 | grid_y = torch.linspace(0, in_h - 1, in_h).repeat(in_w, 1).t().repeat( 365 | int(bs * len(self.anchors_mask[l])), 1, 1).view(y.shape).type_as(x) 366 | 367 | # 生成先验框的宽高 368 | scaled_anchors_l = np.array(scaled_anchors)[self.anchors_mask[l]] 369 | anchor_w = torch.Tensor(scaled_anchors_l).index_select(1, torch.LongTensor([0])).type_as(x) 370 | anchor_h = torch.Tensor(scaled_anchors_l).index_select(1, torch.LongTensor([1])).type_as(x) 371 | 372 | anchor_w = anchor_w.repeat(bs, 1).repeat(1, 1, in_h * in_w).view(w.shape) 373 | anchor_h = anchor_h.repeat(bs, 1).repeat(1, 1, in_h * in_w).view(h.shape) 374 | #-------------------------------------------------------# 375 | # 计算调整后的先验框中心与宽高 376 | #-------------------------------------------------------# 377 | pred_boxes_x = torch.unsqueeze(x + grid_x, -1) 378 | pred_boxes_y = torch.unsqueeze(y + grid_y, -1) 379 | pred_boxes_w = torch.unsqueeze(torch.exp(w) * anchor_w, -1) 380 | pred_boxes_h = torch.unsqueeze(torch.exp(h) * anchor_h, -1) 381 | pred_boxes = torch.cat([pred_boxes_x, pred_boxes_y, pred_boxes_w, pred_boxes_h], dim = -1) 382 | 383 | for b in range(bs): 384 | #-------------------------------------------------------# 385 | # 将预测结果转换一个形式 386 | # pred_boxes_for_ignore num_anchors, 4 387 | #-------------------------------------------------------# 388 | pred_boxes_for_ignore = pred_boxes[b].view(-1, 4) 389 | #-------------------------------------------------------# 390 | # 计算真实框,并把真实框转换成相对于特征层的大小 391 | # gt_box num_true_box, 4 392 | #-------------------------------------------------------# 393 | if len(targets[b]) > 0: 394 | batch_target = torch.zeros_like(targets[b]) 395 | #-------------------------------------------------------# 396 | # 计算出正样本在特征层上的中心点 397 | #-------------------------------------------------------# 398 | batch_target[:, [0,2]] = targets[b][:, [0,2]] * in_w 399 | batch_target[:, [1,3]] = targets[b][:, [1,3]] * in_h 400 | batch_target = batch_target[:, :4].type_as(x) 401 | #-------------------------------------------------------# 402 | # 计算交并比 403 | # anch_ious num_true_box, num_anchors 404 | #-------------------------------------------------------# 405 | anch_ious = self.calculate_iou(batch_target, pred_boxes_for_ignore) 406 | #-------------------------------------------------------# 407 | # 每个先验框对应真实框的最大重合度 408 | # anch_ious_max num_anchors 409 | #-------------------------------------------------------# 410 | anch_ious_max, _ = torch.max(anch_ious, dim = 0) 411 | anch_ious_max = anch_ious_max.view(pred_boxes[b].size()[:3]) 412 | noobj_mask[b][anch_ious_max > self.ignore_threshold] = 0 413 | return noobj_mask, pred_boxes 414 | 415 | def weights_init(net, init_type='normal', init_gain = 0.02): 416 | def init_func(m): 417 | classname = m.__class__.__name__ 418 | if hasattr(m, 'weight') and classname.find('Conv') != -1: 419 | if init_type == 'normal': 420 | torch.nn.init.normal_(m.weight.data, 0.0, init_gain) 421 | elif init_type == 'xavier': 422 | torch.nn.init.xavier_normal_(m.weight.data, gain=init_gain) 423 | elif init_type == 'kaiming': 424 | torch.nn.init.kaiming_normal_(m.weight.data, a=0, mode='fan_in') 425 | elif init_type == 'orthogonal': 426 | torch.nn.init.orthogonal_(m.weight.data, gain=init_gain) 427 | else: 428 | raise NotImplementedError('initialization method [%s] is not implemented' % init_type) 429 | elif classname.find('BatchNorm2d') != -1: 430 | torch.nn.init.normal_(m.weight.data, 1.0, 0.02) 431 | torch.nn.init.constant_(m.bias.data, 0.0) 432 | print('initialize network with %s type' % init_type) 433 | net.apply(init_func) 434 | 435 | def get_lr_scheduler(lr_decay_type, lr, min_lr, total_iters, warmup_iters_ratio = 0.05, warmup_lr_ratio = 0.1, no_aug_iter_ratio = 0.05, step_num = 10): 436 | def yolox_warm_cos_lr(lr, min_lr, total_iters, warmup_total_iters, warmup_lr_start, no_aug_iter, iters): 437 | if iters <= warmup_total_iters: 438 | # lr = (lr - warmup_lr_start) * iters / float(warmup_total_iters) + warmup_lr_start 439 | lr = (lr - warmup_lr_start) * pow(iters / float(warmup_total_iters), 2) + warmup_lr_start 440 | elif iters >= total_iters - no_aug_iter: 441 | lr = min_lr 442 | else: 443 | lr = min_lr + 0.5 * (lr - min_lr) * ( 444 | 1.0 + math.cos(math.pi* (iters - warmup_total_iters) / (total_iters - warmup_total_iters - no_aug_iter)) 445 | ) 446 | return lr 447 | 448 | def step_lr(lr, decay_rate, step_size, iters): 449 | if step_size < 1: 450 | raise ValueError("step_size must above 1.") 451 | n = iters // step_size 452 | out_lr = lr * decay_rate ** n 453 | return out_lr 454 | 455 | if lr_decay_type == "cos": 456 | warmup_total_iters = min(max(warmup_iters_ratio * total_iters, 1), 3) 457 | warmup_lr_start = max(warmup_lr_ratio * lr, 1e-6) 458 | no_aug_iter = min(max(no_aug_iter_ratio * total_iters, 1), 15) 459 | func = partial(yolox_warm_cos_lr ,lr, min_lr, total_iters, warmup_total_iters, warmup_lr_start, no_aug_iter) 460 | else: 461 | decay_rate = (min_lr / lr) ** (1 / (step_num - 1)) 462 | step_size = total_iters / step_num 463 | func = partial(step_lr, lr, decay_rate, step_size) 464 | 465 | return func 466 | 467 | def set_optimizer_lr(optimizer, lr_scheduler_func, epoch): 468 | lr = lr_scheduler_func(epoch) 469 | for param_group in optimizer.param_groups: 470 | param_group['lr'] = lr 471 | -------------------------------------------------------------------------------- /predict.py: -------------------------------------------------------------------------------- 1 | #-----------------------------------------------------------------------# 2 | # predict.py将单张图片预测、摄像头检测、FPS测试和目录遍历检测等功能 3 | # 整合到了一个py文件中,通过指定mode进行模式的修改。 4 | #-----------------------------------------------------------------------# 5 | import time 6 | 7 | import cv2 8 | import numpy as np 9 | from PIL import Image 10 | 11 | from yolo import YOLO 12 | 13 | if __name__ == "__main__": 14 | yolo = YOLO() 15 | #----------------------------------------------------------------------------------------------------------# 16 | # mode用于指定测试的模式: 17 | # 'predict' 表示单张图片预测,如果想对预测过程进行修改,如保存图片,截取对象等,可以先看下方详细的注释 18 | # 'video' 表示视频检测,可调用摄像头或者视频进行检测,详情查看下方注释。 19 | # 'fps' 表示测试fps,使用的图片是img里面的street.jpg,详情查看下方注释。 20 | # 'dir_predict' 表示遍历文件夹进行检测并保存。默认遍历img文件夹,保存img_out文件夹,详情查看下方注释。 21 | # 'heatmap' 表示进行预测结果的热力图可视化,详情查看下方注释。 22 | # 'export_onnx' 表示将模型导出为onnx,需要pytorch1.7.1以上。 23 | #----------------------------------------------------------------------------------------------------------# 24 | mode = "predict" 25 | #-------------------------------------------------------------------------# 26 | # crop 指定了是否在单张图片预测后对目标进行截取 27 | # count 指定了是否进行目标的计数 28 | # crop、count仅在mode='predict'时有效 29 | #-------------------------------------------------------------------------# 30 | crop = False 31 | count = False 32 | #----------------------------------------------------------------------------------------------------------# 33 | # video_path 用于指定视频的路径,当video_path=0时表示检测摄像头 34 | # 想要检测视频,则设置如video_path = "xxx.mp4"即可,代表读取出根目录下的xxx.mp4文件。 35 | # video_save_path 表示视频保存的路径,当video_save_path=""时表示不保存 36 | # 想要保存视频,则设置如video_save_path = "yyy.mp4"即可,代表保存为根目录下的yyy.mp4文件。 37 | # video_fps 用于保存的视频的fps 38 | # 39 | # video_path、video_save_path和video_fps仅在mode='video'时有效 40 | # 保存视频时需要ctrl+c退出或者运行到最后一帧才会完成完整的保存步骤。 41 | #----------------------------------------------------------------------------------------------------------# 42 | video_path = 0 43 | video_save_path = "" 44 | video_fps = 25.0 45 | #----------------------------------------------------------------------------------------------------------# 46 | # test_interval 用于指定测量fps的时候,图片检测的次数。理论上test_interval越大,fps越准确。 47 | # fps_image_path 用于指定测试的fps图片 48 | # 49 | # test_interval和fps_image_path仅在mode='fps'有效 50 | #----------------------------------------------------------------------------------------------------------# 51 | test_interval = 100 52 | fps_image_path = "img/street.jpg" 53 | #-------------------------------------------------------------------------# 54 | # dir_origin_path 指定了用于检测的图片的文件夹路径 55 | # dir_save_path 指定了检测完图片的保存路径 56 | # 57 | # dir_origin_path和dir_save_path仅在mode='dir_predict'时有效 58 | #-------------------------------------------------------------------------# 59 | dir_origin_path = "img/" 60 | dir_save_path = "img_out/" 61 | #-------------------------------------------------------------------------# 62 | # heatmap_save_path 热力图的保存路径,默认保存在model_data下 63 | # 64 | # heatmap_save_path仅在mode='heatmap'有效 65 | #-------------------------------------------------------------------------# 66 | heatmap_save_path = "model_data/heatmap_vision.png" 67 | #-------------------------------------------------------------------------# 68 | # simplify 使用Simplify onnx 69 | # onnx_save_path 指定了onnx的保存路径 70 | #-------------------------------------------------------------------------# 71 | simplify = True 72 | onnx_save_path = "model_data/models.onnx" 73 | 74 | if mode == "predict": 75 | ''' 76 | 1、如果想要进行检测完的图片的保存,利用r_image.save("img.jpg")即可保存,直接在predict.py里进行修改即可。 77 | 2、如果想要获得预测框的坐标,可以进入yolo.detect_image函数,在绘图部分读取top,left,bottom,right这四个值。 78 | 3、如果想要利用预测框截取下目标,可以进入yolo.detect_image函数,在绘图部分利用获取到的top,left,bottom,right这四个值 79 | 在原图上利用矩阵的方式进行截取。 80 | 4、如果想要在预测图上写额外的字,比如检测到的特定目标的数量,可以进入yolo.detect_image函数,在绘图部分对predicted_class进行判断, 81 | 比如判断if predicted_class == 'car': 即可判断当前目标是否为车,然后记录数量即可。利用draw.text即可写字。 82 | ''' 83 | while True: 84 | img = input('Input image filename:') 85 | try: 86 | image = Image.open(img) 87 | except: 88 | print('Open Error! Try again!') 89 | continue 90 | else: 91 | r_image = yolo.detect_image(image, crop = crop, count=count) 92 | r_image.show() 93 | 94 | elif mode == "video": 95 | capture = cv2.VideoCapture(video_path) 96 | if video_save_path!="": 97 | fourcc = cv2.VideoWriter_fourcc(*'XVID') 98 | size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) 99 | out = cv2.VideoWriter(video_save_path, fourcc, video_fps, size) 100 | 101 | ref, frame = capture.read() 102 | if not ref: 103 | raise ValueError("未能正确读取摄像头(视频),请注意是否正确安装摄像头(是否正确填写视频路径)。") 104 | 105 | fps = 0.0 106 | while(True): 107 | t1 = time.time() 108 | # 读取某一帧 109 | ref, frame = capture.read() 110 | if not ref: 111 | break 112 | # 格式转变,BGRtoRGB 113 | frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB) 114 | # 转变成Image 115 | frame = Image.fromarray(np.uint8(frame)) 116 | # 进行检测 117 | frame = np.array(yolo.detect_image(frame)) 118 | # RGBtoBGR满足opencv显示格式 119 | frame = cv2.cvtColor(frame,cv2.COLOR_RGB2BGR) 120 | 121 | fps = ( fps + (1./(time.time()-t1)) ) / 2 122 | print("fps= %.2f"%(fps)) 123 | frame = cv2.putText(frame, "fps= %.2f"%(fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) 124 | 125 | cv2.imshow("video",frame) 126 | c= cv2.waitKey(1) & 0xff 127 | if video_save_path!="": 128 | out.write(frame) 129 | 130 | if c==27: 131 | capture.release() 132 | break 133 | 134 | print("Video Detection Done!") 135 | capture.release() 136 | if video_save_path!="": 137 | print("Save processed video to the path :" + video_save_path) 138 | out.release() 139 | cv2.destroyAllWindows() 140 | 141 | elif mode == "fps": 142 | img = Image.open(fps_image_path) 143 | tact_time = yolo.get_FPS(img, test_interval) 144 | print(str(tact_time) + ' seconds, ' + str(1/tact_time) + 'FPS, @batch_size 1') 145 | 146 | elif mode == "dir_predict": 147 | import os 148 | 149 | from tqdm import tqdm 150 | 151 | img_names = os.listdir(dir_origin_path) 152 | for img_name in tqdm(img_names): 153 | if img_name.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')): 154 | image_path = os.path.join(dir_origin_path, img_name) 155 | image = Image.open(image_path) 156 | r_image = yolo.detect_image(image) 157 | if not os.path.exists(dir_save_path): 158 | os.makedirs(dir_save_path) 159 | r_image.save(os.path.join(dir_save_path, img_name.replace(".jpg", ".png")), quality=95, subsampling=0) 160 | 161 | elif mode == "heatmap": 162 | while True: 163 | img = input('Input image filename:') 164 | try: 165 | image = Image.open(img) 166 | except: 167 | print('Open Error! Try again!') 168 | continue 169 | else: 170 | yolo.detect_heatmap(image, heatmap_save_path) 171 | 172 | elif mode == "export_onnx": 173 | yolo.convert_to_onnx(simplify, onnx_save_path) 174 | 175 | else: 176 | raise AssertionError("Please specify the correct mode: 'predict', 'video', 'fps', 'heatmap', 'export_onnx', 'dir_predict'.") 177 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | torch 2 | torchvision 3 | tensorboard 4 | scipy==1.2.1 5 | numpy==1.17.0 6 | matplotlib==3.1.2 7 | opencv_python==4.1.2.30 8 | tqdm==4.60.0 9 | Pillow==8.2.0 10 | h5py==2.10.0 -------------------------------------------------------------------------------- /summary.py: -------------------------------------------------------------------------------- 1 | #--------------------------------------------# 2 | # 该部分代码用于看网络结构 3 | #--------------------------------------------# 4 | import torch 5 | from thop import clever_format, profile 6 | from torchsummary import summary 7 | 8 | from nets.yolo import YoloBody 9 | 10 | if __name__ == "__main__": 11 | input_shape = [416, 416] 12 | anchors_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]] 13 | num_classes = 80 14 | 15 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 16 | m = YoloBody(anchors_mask, num_classes).to(device) 17 | summary(m, (3, input_shape[0], input_shape[1])) 18 | 19 | dummy_input = torch.randn(1, 3, input_shape[0], input_shape[1]).to(device) 20 | flops, params = profile(m.to(device), (dummy_input, ), verbose=False) 21 | #--------------------------------------------------------# 22 | # flops * 2是因为profile没有将卷积作为两个operations 23 | # 有些论文将卷积算乘法、加法两个operations。此时乘2 24 | # 有些论文只考虑乘法的运算次数,忽略加法。此时不乘2 25 | # 本代码选择乘2,参考YOLOX。 26 | #--------------------------------------------------------# 27 | flops = flops * 2 28 | flops, params = clever_format([flops, params], "%.3f") 29 | print('Total GFLOPS: %s' % (flops)) 30 | print('Total params: %s' % (params)) 31 | -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | #-------------------------------------# 2 | # 对数据集进行训练 3 | #-------------------------------------# 4 | import datetime 5 | import os 6 | from functools import partial 7 | 8 | import numpy as np 9 | import torch 10 | import torch.backends.cudnn as cudnn 11 | import torch.distributed as dist 12 | import torch.nn as nn 13 | import torch.optim as optim 14 | from torch import nn 15 | from torch.utils.data import DataLoader 16 | 17 | from nets.yolo import YoloBody 18 | from nets.yolo_training import (YOLOLoss, get_lr_scheduler, set_optimizer_lr, 19 | weights_init) 20 | from utils.callbacks import EvalCallback, LossHistory 21 | from utils.dataloader import YoloDataset, yolo_dataset_collate 22 | from utils.utils import (get_anchors, get_classes, seed_everything, 23 | show_config, worker_init_fn) 24 | from utils.utils_fit import fit_one_epoch 25 | 26 | ''' 27 | 训练自己的目标检测模型一定需要注意以下几点: 28 | 1、训练前仔细检查自己的格式是否满足要求,该库要求数据集格式为VOC格式,需要准备好的内容有输入图片和标签 29 | 输入图片为.jpg图片,无需固定大小,传入训练前会自动进行resize。 30 | 灰度图会自动转成RGB图片进行训练,无需自己修改。 31 | 输入图片如果后缀非jpg,需要自己批量转成jpg后再开始训练。 32 | 33 | 标签为.xml格式,文件中会有需要检测的目标信息,标签文件和输入图片文件相对应。 34 | 35 | 2、损失值的大小用于判断是否收敛,比较重要的是有收敛的趋势,即验证集损失不断下降,如果验证集损失基本上不改变的话,模型基本上就收敛了。 36 | 损失值的具体大小并没有什么意义,大和小只在于损失的计算方式,并不是接近于0才好。如果想要让损失好看点,可以直接到对应的损失函数里面除上10000。 37 | 训练过程中的损失值会保存在logs文件夹下的loss_%Y_%m_%d_%H_%M_%S文件夹中 38 | 39 | 3、训练好的权值文件保存在logs文件夹中,每个训练世代(Epoch)包含若干训练步长(Step),每个训练步长(Step)进行一次梯度下降。 40 | 如果只是训练了几个Step是不会保存的,Epoch和Step的概念要捋清楚一下。 41 | ''' 42 | if __name__ == "__main__": 43 | #---------------------------------# 44 | # Cuda 是否使用Cuda 45 | # 没有GPU可以设置成False 46 | #---------------------------------# 47 | Cuda = True 48 | #----------------------------------------------# 49 | # Seed 用于固定随机种子 50 | # 使得每次独立训练都可以获得一样的结果 51 | #----------------------------------------------# 52 | seed = 11 53 | #---------------------------------------------------------------------# 54 | # distributed 用于指定是否使用单机多卡分布式运行 55 | # 终端指令仅支持Ubuntu。CUDA_VISIBLE_DEVICES用于在Ubuntu下指定显卡。 56 | # Windows系统下默认使用DP模式调用所有显卡,不支持DDP。 57 | # DP模式: 58 | # 设置 distributed = False 59 | # 在终端中输入 CUDA_VISIBLE_DEVICES=0,1 python train.py 60 | # DDP模式: 61 | # 设置 distributed = True 62 | # 在终端中输入 CUDA_VISIBLE_DEVICES=0,1 python -m torch.distributed.launch --nproc_per_node=2 train.py 63 | #---------------------------------------------------------------------# 64 | distributed = False 65 | #---------------------------------------------------------------------# 66 | # sync_bn 是否使用sync_bn,DDP模式多卡可用 67 | #---------------------------------------------------------------------# 68 | sync_bn = False 69 | #---------------------------------------------------------------------# 70 | # fp16 是否使用混合精度训练 71 | # 可减少约一半的显存、需要pytorch1.7.1以上 72 | #---------------------------------------------------------------------# 73 | fp16 = False 74 | #---------------------------------------------------------------------# 75 | # classes_path 指向model_data下的txt,与自己训练的数据集相关 76 | # 训练前一定要修改classes_path,使其对应自己的数据集 77 | #---------------------------------------------------------------------# 78 | classes_path = 'model_data/voc_classes.txt' 79 | #---------------------------------------------------------------------# 80 | # anchors_path 代表先验框对应的txt文件,一般不修改。 81 | # anchors_mask 用于帮助代码找到对应的先验框,一般不修改。 82 | #---------------------------------------------------------------------# 83 | anchors_path = 'model_data/yolo_anchors.txt' 84 | anchors_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]] 85 | #----------------------------------------------------------------------------------------------------------------------------# 86 | # 权值文件的下载请看README,可以通过网盘下载。模型的 预训练权重 对不同数据集是通用的,因为特征是通用的。 87 | # 模型的 预训练权重 比较重要的部分是 主干特征提取网络的权值部分,用于进行特征提取。 88 | # 预训练权重对于99%的情况都必须要用,不用的话主干部分的权值太过随机,特征提取效果不明显,网络训练的结果也不会好 89 | # 90 | # 如果训练过程中存在中断训练的操作,可以将model_path设置成logs文件夹下的权值文件,将已经训练了一部分的权值再次载入。 91 | # 同时修改下方的 冻结阶段 或者 解冻阶段 的参数,来保证模型epoch的连续性。 92 | # 93 | # 当model_path = ''的时候不加载整个模型的权值。 94 | # 95 | # 此处使用的是整个模型的权重,因此是在train.py进行加载的,下面的pretrain不影响此处的权值加载。 96 | # 如果想要让模型从主干的预训练权值开始训练,则设置model_path = '',下面的pretrain = True,此时仅加载主干。 97 | # 如果想要让模型从0开始训练,则设置model_path = '',下面的pretrain = Fasle,Freeze_Train = Fasle,此时从0开始训练,且没有冻结主干的过程。 98 | # 99 | # 一般来讲,网络从0开始的训练效果会很差,因为权值太过随机,特征提取效果不明显,因此非常、非常、非常不建议大家从0开始训练! 100 | # 如果一定要从0开始,可以了解imagenet数据集,首先训练分类模型,获得网络的主干部分权值,分类模型的 主干部分 和该模型通用,基于此进行训练。 101 | #----------------------------------------------------------------------------------------------------------------------------# 102 | model_path = 'model_data/yolo_weights.pth' 103 | #------------------------------------------------------# 104 | # input_shape 输入的shape大小,一定要是32的倍数 105 | #------------------------------------------------------# 106 | input_shape = [416, 416] 107 | #----------------------------------------------------------------------------------------------------------------------------# 108 | # pretrained 是否使用主干网络的预训练权重,此处使用的是主干的权重,因此是在模型构建的时候进行加载的。 109 | # 如果设置了model_path,则主干的权值无需加载,pretrained的值无意义。 110 | # 如果不设置model_path,pretrained = True,此时仅加载主干开始训练。 111 | # 如果不设置model_path,pretrained = False,Freeze_Train = Fasle,此时从0开始训练,且没有冻结主干的过程。 112 | #----------------------------------------------------------------------------------------------------------------------------# 113 | pretrained = False 114 | 115 | #----------------------------------------------------------------------------------------------------------------------------# 116 | # 训练分为两个阶段,分别是冻结阶段和解冻阶段。设置冻结阶段是为了满足机器性能不足的同学的训练需求。 117 | # 冻结训练需要的显存较小,显卡非常差的情况下,可设置Freeze_Epoch等于UnFreeze_Epoch,此时仅仅进行冻结训练。 118 | # 119 | # 在此提供若干参数设置建议,各位训练者根据自己的需求进行灵活调整: 120 | # (一)从整个模型的预训练权重开始训练: 121 | # Adam: 122 | # Init_Epoch = 0,Freeze_Epoch = 50,UnFreeze_Epoch = 100,Freeze_Train = True,optimizer_type = 'adam',Init_lr = 1e-3,weight_decay = 0。(冻结) 123 | # Init_Epoch = 0,UnFreeze_Epoch = 100,Freeze_Train = False,optimizer_type = 'adam',Init_lr = 1e-3,weight_decay = 0。(不冻结) 124 | # SGD: 125 | # Init_Epoch = 0,Freeze_Epoch = 50,UnFreeze_Epoch = 300,Freeze_Train = True,optimizer_type = 'sgd',Init_lr = 1e-2,weight_decay = 5e-4。(冻结) 126 | # Init_Epoch = 0,UnFreeze_Epoch = 300,Freeze_Train = False,optimizer_type = 'sgd',Init_lr = 1e-2,weight_decay = 5e-4。(不冻结) 127 | # 其中:UnFreeze_Epoch可以在100-300之间调整。 128 | # (二)从主干网络的预训练权重开始训练: 129 | # Adam: 130 | # Init_Epoch = 0,Freeze_Epoch = 50,UnFreeze_Epoch = 100,Freeze_Train = True,optimizer_type = 'adam',Init_lr = 1e-3,weight_decay = 0。(冻结) 131 | # Init_Epoch = 0,UnFreeze_Epoch = 100,Freeze_Train = False,optimizer_type = 'adam',Init_lr = 1e-3,weight_decay = 0。(不冻结) 132 | # SGD: 133 | # Init_Epoch = 0,Freeze_Epoch = 50,UnFreeze_Epoch = 300,Freeze_Train = True,optimizer_type = 'sgd',Init_lr = 1e-2,weight_decay = 5e-4。(冻结) 134 | # Init_Epoch = 0,UnFreeze_Epoch = 300,Freeze_Train = False,optimizer_type = 'sgd',Init_lr = 1e-2,weight_decay = 5e-4。(不冻结) 135 | # 其中:由于从主干网络的预训练权重开始训练,主干的权值不一定适合目标检测,需要更多的训练跳出局部最优解。 136 | # UnFreeze_Epoch可以在150-300之间调整,YOLOV5和YOLOX均推荐使用300。 137 | # Adam相较于SGD收敛的快一些。因此UnFreeze_Epoch理论上可以小一点,但依然推荐更多的Epoch。 138 | # (三)batch_size的设置: 139 | # 在显卡能够接受的范围内,以大为好。显存不足与数据集大小无关,提示显存不足(OOM或者CUDA out of memory)请调小batch_size。 140 | # 受到BatchNorm层影响,batch_size最小为2,不能为1。 141 | # 正常情况下Freeze_batch_size建议为Unfreeze_batch_size的1-2倍。不建议设置的差距过大,因为关系到学习率的自动调整。 142 | #----------------------------------------------------------------------------------------------------------------------------# 143 | #------------------------------------------------------------------# 144 | # 冻结阶段训练参数 145 | # 此时模型的主干被冻结了,特征提取网络不发生改变 146 | # 占用的显存较小,仅对网络进行微调 147 | # Init_Epoch 模型当前开始的训练世代,其值可以大于Freeze_Epoch,如设置: 148 | # Init_Epoch = 60、Freeze_Epoch = 50、UnFreeze_Epoch = 100 149 | # 会跳过冻结阶段,直接从60代开始,并调整对应的学习率。 150 | # (断点续练时使用) 151 | # Freeze_Epoch 模型冻结训练的Freeze_Epoch 152 | # (当Freeze_Train=False时失效) 153 | # Freeze_batch_size 模型冻结训练的batch_size 154 | # (当Freeze_Train=False时失效) 155 | #------------------------------------------------------------------# 156 | Init_Epoch = 0 157 | Freeze_Epoch = 50 158 | Freeze_batch_size = 16 159 | #------------------------------------------------------------------# 160 | # 解冻阶段训练参数 161 | # 此时模型的主干不被冻结了,特征提取网络会发生改变 162 | # 占用的显存较大,网络所有的参数都会发生改变 163 | # UnFreeze_Epoch 模型总共训练的epoch 164 | # SGD需要更长的时间收敛,因此设置较大的UnFreeze_Epoch 165 | # Adam可以使用相对较小的UnFreeze_Epoch 166 | # Unfreeze_batch_size 模型在解冻后的batch_size 167 | #------------------------------------------------------------------# 168 | UnFreeze_Epoch = 300 169 | Unfreeze_batch_size = 8 170 | #------------------------------------------------------------------# 171 | # Freeze_Train 是否进行冻结训练 172 | # 默认先冻结主干训练后解冻训练。 173 | #------------------------------------------------------------------# 174 | Freeze_Train = True 175 | 176 | #------------------------------------------------------------------# 177 | # 其它训练参数:学习率、优化器、学习率下降有关 178 | #------------------------------------------------------------------# 179 | #------------------------------------------------------------------# 180 | # Init_lr 模型的最大学习率 181 | # Min_lr 模型的最小学习率,默认为最大学习率的0.01 182 | #------------------------------------------------------------------# 183 | Init_lr = 1e-2 184 | Min_lr = Init_lr * 0.01 185 | #------------------------------------------------------------------# 186 | # optimizer_type 使用到的优化器种类,可选的有adam、sgd 187 | # 当使用Adam优化器时建议设置 Init_lr=1e-3 188 | # 当使用SGD优化器时建议设置 Init_lr=1e-2 189 | # momentum 优化器内部使用到的momentum参数 190 | # weight_decay 权值衰减,可防止过拟合 191 | # adam会导致weight_decay错误,使用adam时建议设置为0。 192 | #------------------------------------------------------------------# 193 | optimizer_type = "sgd" 194 | momentum = 0.937 195 | weight_decay = 5e-4 196 | #------------------------------------------------------------------# 197 | # lr_decay_type 使用到的学习率下降方式,可选的有step、cos 198 | #------------------------------------------------------------------# 199 | lr_decay_type = "cos" 200 | #------------------------------------------------------------------# 201 | # save_period 多少个epoch保存一次权值 202 | #------------------------------------------------------------------# 203 | save_period = 10 204 | #------------------------------------------------------------------# 205 | # save_dir 权值与日志文件保存的文件夹 206 | #------------------------------------------------------------------# 207 | save_dir = 'logs' 208 | #------------------------------------------------------------------# 209 | # eval_flag 是否在训练时进行评估,评估对象为验证集 210 | # 安装pycocotools库后,评估体验更佳。 211 | # eval_period 代表多少个epoch评估一次,不建议频繁的评估 212 | # 评估需要消耗较多的时间,频繁评估会导致训练非常慢 213 | # 此处获得的mAP会与get_map.py获得的会有所不同,原因有二: 214 | # (一)此处获得的mAP为验证集的mAP。 215 | # (二)此处设置评估参数较为保守,目的是加快评估速度。 216 | #------------------------------------------------------------------# 217 | eval_flag = True 218 | eval_period = 10 219 | #------------------------------------------------------------------# 220 | # num_workers 用于设置是否使用多线程读取数据 221 | # 开启后会加快数据读取速度,但是会占用更多内存 222 | # 内存较小的电脑可以设置为2或者0 223 | #------------------------------------------------------------------# 224 | num_workers = 4 225 | 226 | #----------------------------------------------------# 227 | # 获得图片路径和标签 228 | #----------------------------------------------------# 229 | train_annotation_path = '2007_train.txt' 230 | val_annotation_path = '2007_val.txt' 231 | 232 | seed_everything(seed) 233 | #------------------------------------------------------# 234 | # 设置用到的显卡 235 | #------------------------------------------------------# 236 | ngpus_per_node = torch.cuda.device_count() 237 | if distributed: 238 | dist.init_process_group(backend="nccl") 239 | local_rank = int(os.environ["LOCAL_RANK"]) 240 | rank = int(os.environ["RANK"]) 241 | device = torch.device("cuda", local_rank) 242 | if local_rank == 0: 243 | print(f"[{os.getpid()}] (rank = {rank}, local_rank = {local_rank}) training...") 244 | print("Gpu Device Count : ", ngpus_per_node) 245 | else: 246 | device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') 247 | local_rank = 0 248 | rank = 0 249 | 250 | #----------------------------------------------------# 251 | # 获取classes和anchor 252 | #----------------------------------------------------# 253 | class_names, num_classes = get_classes(classes_path) 254 | anchors, num_anchors = get_anchors(anchors_path) 255 | 256 | #------------------------------------------------------# 257 | # 创建yolo模型 258 | #------------------------------------------------------# 259 | model = YoloBody(anchors_mask, num_classes, pretrained=pretrained) 260 | if not pretrained: 261 | weights_init(model) 262 | if model_path != '': 263 | #------------------------------------------------------# 264 | # 权值文件请看README,百度网盘下载 265 | #------------------------------------------------------# 266 | if local_rank == 0: 267 | print('Load weights {}.'.format(model_path)) 268 | 269 | #------------------------------------------------------# 270 | # 根据预训练权重的Key和模型的Key进行加载 271 | #------------------------------------------------------# 272 | model_dict = model.state_dict() 273 | pretrained_dict = torch.load(model_path, map_location = device) 274 | load_key, no_load_key, temp_dict = [], [], {} 275 | for k, v in pretrained_dict.items(): 276 | if k in model_dict.keys() and np.shape(model_dict[k]) == np.shape(v): 277 | temp_dict[k] = v 278 | load_key.append(k) 279 | else: 280 | no_load_key.append(k) 281 | model_dict.update(temp_dict) 282 | model.load_state_dict(model_dict) 283 | #------------------------------------------------------# 284 | # 显示没有匹配上的Key 285 | #------------------------------------------------------# 286 | if local_rank == 0: 287 | print("\nSuccessful Load Key:", str(load_key)[:500], "……\nSuccessful Load Key Num:", len(load_key)) 288 | print("\nFail To Load Key:", str(no_load_key)[:500], "……\nFail To Load Key num:", len(no_load_key)) 289 | print("\n\033[1;33;44m温馨提示,head部分没有载入是正常现象,Backbone部分没有载入是错误的。\033[0m") 290 | 291 | #----------------------# 292 | # 获得损失函数 293 | #----------------------# 294 | yolo_loss = YOLOLoss(anchors, num_classes, input_shape, Cuda, anchors_mask) 295 | #----------------------# 296 | # 记录Loss 297 | #----------------------# 298 | if local_rank == 0: 299 | time_str = datetime.datetime.strftime(datetime.datetime.now(),'%Y_%m_%d_%H_%M_%S') 300 | log_dir = os.path.join(save_dir, "loss_" + str(time_str)) 301 | loss_history = LossHistory(log_dir, model, input_shape=input_shape) 302 | else: 303 | loss_history = None 304 | 305 | #------------------------------------------------------------------# 306 | # torch 1.2不支持amp,建议使用torch 1.7.1及以上正确使用fp16 307 | # 因此torch1.2这里显示"could not be resolve" 308 | #------------------------------------------------------------------# 309 | if fp16: 310 | from torch.cuda.amp import GradScaler as GradScaler 311 | scaler = GradScaler() 312 | else: 313 | scaler = None 314 | 315 | model_train = model.train() 316 | #----------------------------# 317 | # 多卡同步Bn 318 | #----------------------------# 319 | if sync_bn and ngpus_per_node > 1 and distributed: 320 | model_train = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model_train) 321 | elif sync_bn: 322 | print("Sync_bn is not support in one gpu or not distributed.") 323 | 324 | if Cuda: 325 | if distributed: 326 | #----------------------------# 327 | # 多卡平行运行 328 | #----------------------------# 329 | model_train = model_train.cuda(local_rank) 330 | model_train = torch.nn.parallel.DistributedDataParallel(model_train, device_ids=[local_rank], find_unused_parameters=True) 331 | else: 332 | model_train = torch.nn.DataParallel(model) 333 | cudnn.benchmark = True 334 | model_train = model_train.cuda() 335 | 336 | #---------------------------# 337 | # 读取数据集对应的txt 338 | #---------------------------# 339 | with open(train_annotation_path) as f: 340 | train_lines = f.readlines() 341 | with open(val_annotation_path) as f: 342 | val_lines = f.readlines() 343 | num_train = len(train_lines) 344 | num_val = len(val_lines) 345 | 346 | if local_rank == 0: 347 | show_config( 348 | classes_path = classes_path, anchors_path = anchors_path, anchors_mask = anchors_mask, model_path = model_path, input_shape = input_shape, \ 349 | Init_Epoch = Init_Epoch, Freeze_Epoch = Freeze_Epoch, UnFreeze_Epoch = UnFreeze_Epoch, Freeze_batch_size = Freeze_batch_size, Unfreeze_batch_size = Unfreeze_batch_size, Freeze_Train = Freeze_Train, \ 350 | Init_lr = Init_lr, Min_lr = Min_lr, optimizer_type = optimizer_type, momentum = momentum, lr_decay_type = lr_decay_type, \ 351 | save_period = save_period, save_dir = save_dir, num_workers = num_workers, num_train = num_train, num_val = num_val 352 | ) 353 | #---------------------------------------------------------# 354 | # 总训练世代指的是遍历全部数据的总次数 355 | # 总训练步长指的是梯度下降的总次数 356 | # 每个训练世代包含若干训练步长,每个训练步长进行一次梯度下降。 357 | # 此处仅建议最低训练世代,上不封顶,计算时只考虑了解冻部分 358 | #----------------------------------------------------------# 359 | wanted_step = 5e4 if optimizer_type == "sgd" else 1.5e4 360 | total_step = num_train // Unfreeze_batch_size * UnFreeze_Epoch 361 | if total_step <= wanted_step: 362 | if num_train // Unfreeze_batch_size == 0: 363 | raise ValueError('数据集过小,无法进行训练,请扩充数据集。') 364 | wanted_epoch = wanted_step // (num_train // Unfreeze_batch_size) + 1 365 | print("\n\033[1;33;44m[Warning] 使用%s优化器时,建议将训练总步长设置到%d以上。\033[0m"%(optimizer_type, wanted_step)) 366 | print("\033[1;33;44m[Warning] 本次运行的总训练数据量为%d,Unfreeze_batch_size为%d,共训练%d个Epoch,计算出总训练步长为%d。\033[0m"%(num_train, Unfreeze_batch_size, UnFreeze_Epoch, total_step)) 367 | print("\033[1;33;44m[Warning] 由于总训练步长为%d,小于建议总步长%d,建议设置总世代为%d。\033[0m"%(total_step, wanted_step, wanted_epoch)) 368 | 369 | #------------------------------------------------------# 370 | # 主干特征提取网络特征通用,冻结训练可以加快训练速度 371 | # 也可以在训练初期防止权值被破坏。 372 | # Init_Epoch为起始世代 373 | # Freeze_Epoch为冻结训练的世代 374 | # UnFreeze_Epoch总训练世代 375 | # 提示OOM或者显存不足请调小Batch_size 376 | #------------------------------------------------------# 377 | if True: 378 | UnFreeze_flag = False 379 | #------------------------------------# 380 | # 冻结一定部分训练 381 | #------------------------------------# 382 | if Freeze_Train: 383 | for param in model.backbone.parameters(): 384 | param.requires_grad = False 385 | 386 | #-------------------------------------------------------------------# 387 | # 如果不冻结训练的话,直接设置batch_size为Unfreeze_batch_size 388 | #-------------------------------------------------------------------# 389 | batch_size = Freeze_batch_size if Freeze_Train else Unfreeze_batch_size 390 | 391 | #-------------------------------------------------------------------# 392 | # 判断当前batch_size,自适应调整学习率 393 | #-------------------------------------------------------------------# 394 | nbs = 64 395 | lr_limit_max = 1e-3 if optimizer_type == 'adam' else 5e-2 396 | lr_limit_min = 3e-4 if optimizer_type == 'adam' else 5e-4 397 | Init_lr_fit = min(max(batch_size / nbs * Init_lr, lr_limit_min), lr_limit_max) 398 | Min_lr_fit = min(max(batch_size / nbs * Min_lr, lr_limit_min * 1e-2), lr_limit_max * 1e-2) 399 | 400 | #---------------------------------------# 401 | # 根据optimizer_type选择优化器 402 | #---------------------------------------# 403 | pg0, pg1, pg2 = [], [], [] 404 | for k, v in model.named_modules(): 405 | if hasattr(v, "bias") and isinstance(v.bias, nn.Parameter): 406 | pg2.append(v.bias) 407 | if isinstance(v, nn.BatchNorm2d) or "bn" in k: 408 | pg0.append(v.weight) 409 | elif hasattr(v, "weight") and isinstance(v.weight, nn.Parameter): 410 | pg1.append(v.weight) 411 | optimizer = { 412 | 'adam' : optim.Adam(pg0, Init_lr_fit, betas = (momentum, 0.999)), 413 | 'sgd' : optim.SGD(pg0, Init_lr_fit, momentum = momentum, nesterov=True) 414 | }[optimizer_type] 415 | optimizer.add_param_group({"params": pg1, "weight_decay": weight_decay}) 416 | optimizer.add_param_group({"params": pg2}) 417 | 418 | #---------------------------------------# 419 | # 获得学习率下降的公式 420 | #---------------------------------------# 421 | lr_scheduler_func = get_lr_scheduler(lr_decay_type, Init_lr_fit, Min_lr_fit, UnFreeze_Epoch) 422 | 423 | #---------------------------------------# 424 | # 判断每一个世代的长度 425 | #---------------------------------------# 426 | epoch_step = num_train // batch_size 427 | epoch_step_val = num_val // batch_size 428 | 429 | if epoch_step == 0 or epoch_step_val == 0: 430 | raise ValueError("数据集过小,无法继续进行训练,请扩充数据集。") 431 | 432 | #---------------------------------------# 433 | # 构建数据集加载器。 434 | #---------------------------------------# 435 | train_dataset = YoloDataset(train_lines, input_shape, num_classes, train = True) 436 | val_dataset = YoloDataset(val_lines, input_shape, num_classes, train = False) 437 | 438 | if distributed: 439 | train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset, shuffle=True,) 440 | val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset, shuffle=False,) 441 | batch_size = batch_size // ngpus_per_node 442 | shuffle = False 443 | else: 444 | train_sampler = None 445 | val_sampler = None 446 | shuffle = True 447 | 448 | gen = DataLoader(train_dataset, shuffle = shuffle, batch_size = batch_size, num_workers = num_workers, pin_memory=True, 449 | drop_last=True, collate_fn=yolo_dataset_collate, sampler=train_sampler, 450 | worker_init_fn=partial(worker_init_fn, rank=rank, seed=seed)) 451 | gen_val = DataLoader(val_dataset , shuffle = shuffle, batch_size = batch_size, num_workers = num_workers, pin_memory=True, 452 | drop_last=True, collate_fn=yolo_dataset_collate, sampler=val_sampler, 453 | worker_init_fn=partial(worker_init_fn, rank=rank, seed=seed)) 454 | 455 | #----------------------# 456 | # 记录eval的map曲线 457 | #----------------------# 458 | if local_rank == 0: 459 | eval_callback = EvalCallback(model, input_shape, anchors, anchors_mask, class_names, num_classes, val_lines, log_dir, Cuda, \ 460 | eval_flag=eval_flag, period=eval_period) 461 | else: 462 | eval_callback = None 463 | 464 | #---------------------------------------# 465 | # 开始模型训练 466 | #---------------------------------------# 467 | for epoch in range(Init_Epoch, UnFreeze_Epoch): 468 | #---------------------------------------# 469 | # 如果模型有冻结学习部分 470 | # 则解冻,并设置参数 471 | #---------------------------------------# 472 | if epoch >= Freeze_Epoch and not UnFreeze_flag and Freeze_Train: 473 | batch_size = Unfreeze_batch_size 474 | 475 | #-------------------------------------------------------------------# 476 | # 判断当前batch_size,自适应调整学习率 477 | #-------------------------------------------------------------------# 478 | nbs = 64 479 | lr_limit_max = 1e-3 if optimizer_type == 'adam' else 5e-2 480 | lr_limit_min = 3e-4 if optimizer_type == 'adam' else 5e-4 481 | Init_lr_fit = min(max(batch_size / nbs * Init_lr, lr_limit_min), lr_limit_max) 482 | Min_lr_fit = min(max(batch_size / nbs * Min_lr, lr_limit_min * 1e-2), lr_limit_max * 1e-2) 483 | #---------------------------------------# 484 | # 获得学习率下降的公式 485 | #---------------------------------------# 486 | lr_scheduler_func = get_lr_scheduler(lr_decay_type, Init_lr_fit, Min_lr_fit, UnFreeze_Epoch) 487 | 488 | for param in model.backbone.parameters(): 489 | param.requires_grad = True 490 | 491 | epoch_step = num_train // batch_size 492 | epoch_step_val = num_val // batch_size 493 | 494 | if epoch_step == 0 or epoch_step_val == 0: 495 | raise ValueError("数据集过小,无法继续进行训练,请扩充数据集。") 496 | 497 | if distributed: 498 | batch_size = batch_size // ngpus_per_node 499 | 500 | gen = DataLoader(train_dataset, shuffle = shuffle, batch_size = batch_size, num_workers = num_workers, pin_memory=True, 501 | drop_last=True, collate_fn=yolo_dataset_collate, sampler=train_sampler, 502 | worker_init_fn=partial(worker_init_fn, rank=rank, seed=seed)) 503 | gen_val = DataLoader(val_dataset , shuffle = shuffle, batch_size = batch_size, num_workers = num_workers, pin_memory=True, 504 | drop_last=True, collate_fn=yolo_dataset_collate, sampler=val_sampler, 505 | worker_init_fn=partial(worker_init_fn, rank=rank, seed=seed)) 506 | 507 | UnFreeze_flag = True 508 | 509 | if distributed: 510 | train_sampler.set_epoch(epoch) 511 | set_optimizer_lr(optimizer, lr_scheduler_func, epoch) 512 | 513 | fit_one_epoch(model_train, model, yolo_loss, loss_history, eval_callback, optimizer, epoch, epoch_step, epoch_step_val, gen, gen_val, UnFreeze_Epoch, Cuda, fp16, scaler, save_period, save_dir, local_rank) 514 | 515 | if distributed: 516 | dist.barrier() 517 | 518 | if local_rank == 0: 519 | loss_history.writer.close() 520 | -------------------------------------------------------------------------------- /utils/__init__.py: -------------------------------------------------------------------------------- 1 | # -------------------------------------------------------------------------------- /utils/callbacks.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import os 3 | 4 | import torch 5 | import matplotlib 6 | matplotlib.use('Agg') 7 | import scipy.signal 8 | from matplotlib import pyplot as plt 9 | from torch.utils.tensorboard import SummaryWriter 10 | 11 | import shutil 12 | import numpy as np 13 | 14 | from PIL import Image 15 | from tqdm import tqdm 16 | from .utils import cvtColor, preprocess_input, resize_image 17 | from .utils_bbox import DecodeBox 18 | from .utils_map import get_coco_map, get_map 19 | 20 | 21 | class LossHistory(): 22 | def __init__(self, log_dir, model, input_shape): 23 | self.log_dir = log_dir 24 | self.losses = [] 25 | self.val_loss = [] 26 | 27 | os.makedirs(self.log_dir) 28 | self.writer = SummaryWriter(self.log_dir) 29 | try: 30 | dummy_input = torch.randn(2, 3, input_shape[0], input_shape[1]) 31 | self.writer.add_graph(model, dummy_input) 32 | except: 33 | pass 34 | 35 | def append_loss(self, epoch, loss, val_loss): 36 | if not os.path.exists(self.log_dir): 37 | os.makedirs(self.log_dir) 38 | 39 | self.losses.append(loss) 40 | self.val_loss.append(val_loss) 41 | 42 | with open(os.path.join(self.log_dir, "epoch_loss.txt"), 'a') as f: 43 | f.write(str(loss)) 44 | f.write("\n") 45 | with open(os.path.join(self.log_dir, "epoch_val_loss.txt"), 'a') as f: 46 | f.write(str(val_loss)) 47 | f.write("\n") 48 | 49 | self.writer.add_scalar('loss', loss, epoch) 50 | self.writer.add_scalar('val_loss', val_loss, epoch) 51 | self.loss_plot() 52 | 53 | def loss_plot(self): 54 | iters = range(len(self.losses)) 55 | 56 | plt.figure() 57 | plt.plot(iters, self.losses, 'red', linewidth = 2, label='train loss') 58 | plt.plot(iters, self.val_loss, 'coral', linewidth = 2, label='val loss') 59 | try: 60 | if len(self.losses) < 25: 61 | num = 5 62 | else: 63 | num = 15 64 | 65 | plt.plot(iters, scipy.signal.savgol_filter(self.losses, num, 3), 'green', linestyle = '--', linewidth = 2, label='smooth train loss') 66 | plt.plot(iters, scipy.signal.savgol_filter(self.val_loss, num, 3), '#8B4513', linestyle = '--', linewidth = 2, label='smooth val loss') 67 | except: 68 | pass 69 | 70 | plt.grid(True) 71 | plt.xlabel('Epoch') 72 | plt.ylabel('Loss') 73 | plt.legend(loc="upper right") 74 | 75 | plt.savefig(os.path.join(self.log_dir, "epoch_loss.png")) 76 | 77 | plt.cla() 78 | plt.close("all") 79 | 80 | class EvalCallback(): 81 | def __init__(self, net, input_shape, anchors, anchors_mask, class_names, num_classes, val_lines, log_dir, cuda, \ 82 | map_out_path=".temp_map_out", max_boxes=100, confidence=0.05, nms_iou=0.5, letterbox_image=True, MINOVERLAP=0.5, eval_flag=True, period=1): 83 | super(EvalCallback, self).__init__() 84 | 85 | self.net = net 86 | self.input_shape = input_shape 87 | self.anchors = anchors 88 | self.anchors_mask = anchors_mask 89 | self.class_names = class_names 90 | self.num_classes = num_classes 91 | self.val_lines = val_lines 92 | self.log_dir = log_dir 93 | self.cuda = cuda 94 | self.map_out_path = map_out_path 95 | self.max_boxes = max_boxes 96 | self.confidence = confidence 97 | self.nms_iou = nms_iou 98 | self.letterbox_image = letterbox_image 99 | self.MINOVERLAP = MINOVERLAP 100 | self.eval_flag = eval_flag 101 | self.period = period 102 | 103 | self.bbox_util = DecodeBox(self.anchors, self.num_classes, (self.input_shape[0], self.input_shape[1]), self.anchors_mask) 104 | 105 | self.maps = [0] 106 | self.epoches = [0] 107 | if self.eval_flag: 108 | with open(os.path.join(self.log_dir, "epoch_map.txt"), 'a') as f: 109 | f.write(str(0)) 110 | f.write("\n") 111 | 112 | def get_map_txt(self, image_id, image, class_names, map_out_path): 113 | f = open(os.path.join(map_out_path, "detection-results/"+image_id+".txt"), "w", encoding='utf-8') 114 | image_shape = np.array(np.shape(image)[0:2]) 115 | #---------------------------------------------------------# 116 | # 在这里将图像转换成RGB图像,防止灰度图在预测时报错。 117 | # 代码仅仅支持RGB图像的预测,所有其它类型的图像都会转化成RGB 118 | #---------------------------------------------------------# 119 | image = cvtColor(image) 120 | #---------------------------------------------------------# 121 | # 给图像增加灰条,实现不失真的resize 122 | # 也可以直接resize进行识别 123 | #---------------------------------------------------------# 124 | image_data = resize_image(image, (self.input_shape[1], self.input_shape[0]), self.letterbox_image) 125 | #---------------------------------------------------------# 126 | # 添加上batch_size维度 127 | #---------------------------------------------------------# 128 | image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, dtype='float32')), (2, 0, 1)), 0) 129 | 130 | with torch.no_grad(): 131 | images = torch.from_numpy(image_data) 132 | if self.cuda: 133 | images = images.cuda() 134 | #---------------------------------------------------------# 135 | # 将图像输入网络当中进行预测! 136 | #---------------------------------------------------------# 137 | outputs = self.net(images) 138 | outputs = self.bbox_util.decode_box(outputs) 139 | #---------------------------------------------------------# 140 | # 将预测框进行堆叠,然后进行非极大抑制 141 | #---------------------------------------------------------# 142 | results = self.bbox_util.non_max_suppression(torch.cat(outputs, 1), self.num_classes, self.input_shape, 143 | image_shape, self.letterbox_image, conf_thres = self.confidence, nms_thres = self.nms_iou) 144 | 145 | if results[0] is None: 146 | return 147 | 148 | top_label = np.array(results[0][:, 6], dtype = 'int32') 149 | top_conf = results[0][:, 4] * results[0][:, 5] 150 | top_boxes = results[0][:, :4] 151 | 152 | top_100 = np.argsort(top_conf)[::-1][:self.max_boxes] 153 | top_boxes = top_boxes[top_100] 154 | top_conf = top_conf[top_100] 155 | top_label = top_label[top_100] 156 | 157 | for i, c in list(enumerate(top_label)): 158 | predicted_class = self.class_names[int(c)] 159 | box = top_boxes[i] 160 | score = str(top_conf[i]) 161 | 162 | top, left, bottom, right = box 163 | if predicted_class not in class_names: 164 | continue 165 | 166 | f.write("%s %s %s %s %s %s\n" % (predicted_class, score[:6], str(int(left)), str(int(top)), str(int(right)),str(int(bottom)))) 167 | 168 | f.close() 169 | return 170 | 171 | def on_epoch_end(self, epoch, model_eval): 172 | if epoch % self.period == 0 and self.eval_flag: 173 | self.net = model_eval 174 | if not os.path.exists(self.map_out_path): 175 | os.makedirs(self.map_out_path) 176 | if not os.path.exists(os.path.join(self.map_out_path, "ground-truth")): 177 | os.makedirs(os.path.join(self.map_out_path, "ground-truth")) 178 | if not os.path.exists(os.path.join(self.map_out_path, "detection-results")): 179 | os.makedirs(os.path.join(self.map_out_path, "detection-results")) 180 | print("Get map.") 181 | for annotation_line in tqdm(self.val_lines): 182 | line = annotation_line.split() 183 | image_id = os.path.basename(line[0]).split('.')[0] 184 | #------------------------------# 185 | # 读取图像并转换成RGB图像 186 | #------------------------------# 187 | image = Image.open(line[0]) 188 | #------------------------------# 189 | # 获得预测框 190 | #------------------------------# 191 | gt_boxes = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]]) 192 | #------------------------------# 193 | # 获得预测txt 194 | #------------------------------# 195 | self.get_map_txt(image_id, image, self.class_names, self.map_out_path) 196 | 197 | #------------------------------# 198 | # 获得真实框txt 199 | #------------------------------# 200 | with open(os.path.join(self.map_out_path, "ground-truth/"+image_id+".txt"), "w") as new_f: 201 | for box in gt_boxes: 202 | left, top, right, bottom, obj = box 203 | obj_name = self.class_names[obj] 204 | new_f.write("%s %s %s %s %s\n" % (obj_name, left, top, right, bottom)) 205 | 206 | print("Calculate Map.") 207 | try: 208 | temp_map = get_coco_map(class_names = self.class_names, path = self.map_out_path)[1] 209 | except: 210 | temp_map = get_map(self.MINOVERLAP, False, path = self.map_out_path) 211 | self.maps.append(temp_map) 212 | self.epoches.append(epoch) 213 | 214 | with open(os.path.join(self.log_dir, "epoch_map.txt"), 'a') as f: 215 | f.write(str(temp_map)) 216 | f.write("\n") 217 | 218 | plt.figure() 219 | plt.plot(self.epoches, self.maps, 'red', linewidth = 2, label='train map') 220 | 221 | plt.grid(True) 222 | plt.xlabel('Epoch') 223 | plt.ylabel('Map %s'%str(self.MINOVERLAP)) 224 | plt.title('A Map Curve') 225 | plt.legend(loc="upper right") 226 | 227 | plt.savefig(os.path.join(self.log_dir, "epoch_map.png")) 228 | plt.cla() 229 | plt.close("all") 230 | 231 | print("Get map done.") 232 | shutil.rmtree(self.map_out_path) 233 | -------------------------------------------------------------------------------- /utils/dataloader.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import torch 4 | from PIL import Image 5 | from torch.utils.data.dataset import Dataset 6 | 7 | from utils.utils import cvtColor, preprocess_input 8 | 9 | 10 | class YoloDataset(Dataset): 11 | def __init__(self, annotation_lines, input_shape, num_classes, train): 12 | super(YoloDataset, self).__init__() 13 | self.annotation_lines = annotation_lines 14 | self.input_shape = input_shape 15 | self.num_classes = num_classes 16 | self.length = len(self.annotation_lines) 17 | self.train = train 18 | 19 | def __len__(self): 20 | return self.length 21 | 22 | def __getitem__(self, index): 23 | index = index % self.length 24 | #---------------------------------------------------# 25 | # 训练时进行数据的随机增强 26 | # 验证时不进行数据的随机增强 27 | #---------------------------------------------------# 28 | image, box = self.get_random_data(self.annotation_lines[index], self.input_shape[0:2], random = self.train) 29 | image = np.transpose(preprocess_input(np.array(image, dtype=np.float32)), (2, 0, 1)) 30 | box = np.array(box, dtype=np.float32) 31 | if len(box) != 0: 32 | box[:, [0, 2]] = box[:, [0, 2]] / self.input_shape[1] 33 | box[:, [1, 3]] = box[:, [1, 3]] / self.input_shape[0] 34 | 35 | box[:, 2:4] = box[:, 2:4] - box[:, 0:2] 36 | box[:, 0:2] = box[:, 0:2] + box[:, 2:4] / 2 37 | return image, box 38 | 39 | def rand(self, a=0, b=1): 40 | return np.random.rand()*(b-a) + a 41 | 42 | def get_random_data(self, annotation_line, input_shape, jitter=.3, hue=.1, sat=0.7, val=0.4, random=True): 43 | line = annotation_line.split() 44 | #------------------------------# 45 | # 读取图像并转换成RGB图像 46 | #------------------------------# 47 | image = Image.open(line[0]) 48 | image = cvtColor(image) 49 | #------------------------------# 50 | # 获得图像的高宽与目标高宽 51 | #------------------------------# 52 | iw, ih = image.size 53 | h, w = input_shape 54 | #------------------------------# 55 | # 获得预测框 56 | #------------------------------# 57 | box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]]) 58 | 59 | if not random: 60 | scale = min(w/iw, h/ih) 61 | nw = int(iw*scale) 62 | nh = int(ih*scale) 63 | dx = (w-nw)//2 64 | dy = (h-nh)//2 65 | 66 | #---------------------------------# 67 | # 将图像多余的部分加上灰条 68 | #---------------------------------# 69 | image = image.resize((nw,nh), Image.BICUBIC) 70 | new_image = Image.new('RGB', (w,h), (128,128,128)) 71 | new_image.paste(image, (dx, dy)) 72 | image_data = np.array(new_image, np.float32) 73 | 74 | #---------------------------------# 75 | # 对真实框进行调整 76 | #---------------------------------# 77 | if len(box)>0: 78 | np.random.shuffle(box) 79 | box[:, [0,2]] = box[:, [0,2]]*nw/iw + dx 80 | box[:, [1,3]] = box[:, [1,3]]*nh/ih + dy 81 | box[:, 0:2][box[:, 0:2]<0] = 0 82 | box[:, 2][box[:, 2]>w] = w 83 | box[:, 3][box[:, 3]>h] = h 84 | box_w = box[:, 2] - box[:, 0] 85 | box_h = box[:, 3] - box[:, 1] 86 | box = box[np.logical_and(box_w>1, box_h>1)] # discard invalid box 87 | 88 | return image_data, box 89 | 90 | #------------------------------------------# 91 | # 对图像进行缩放并且进行长和宽的扭曲 92 | #------------------------------------------# 93 | new_ar = iw/ih * self.rand(1-jitter,1+jitter) / self.rand(1-jitter,1+jitter) 94 | scale = self.rand(.25, 2) 95 | if new_ar < 1: 96 | nh = int(scale*h) 97 | nw = int(nh*new_ar) 98 | else: 99 | nw = int(scale*w) 100 | nh = int(nw/new_ar) 101 | image = image.resize((nw,nh), Image.BICUBIC) 102 | 103 | #------------------------------------------# 104 | # 将图像多余的部分加上灰条 105 | #------------------------------------------# 106 | dx = int(self.rand(0, w-nw)) 107 | dy = int(self.rand(0, h-nh)) 108 | new_image = Image.new('RGB', (w,h), (128,128,128)) 109 | new_image.paste(image, (dx, dy)) 110 | image = new_image 111 | 112 | #------------------------------------------# 113 | # 翻转图像 114 | #------------------------------------------# 115 | flip = self.rand()<.5 116 | if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT) 117 | 118 | image_data = np.array(image, np.uint8) 119 | #---------------------------------# 120 | # 对图像进行色域变换 121 | # 计算色域变换的参数 122 | #---------------------------------# 123 | r = np.random.uniform(-1, 1, 3) * [hue, sat, val] + 1 124 | #---------------------------------# 125 | # 将图像转到HSV上 126 | #---------------------------------# 127 | hue, sat, val = cv2.split(cv2.cvtColor(image_data, cv2.COLOR_RGB2HSV)) 128 | dtype = image_data.dtype 129 | #---------------------------------# 130 | # 应用变换 131 | #---------------------------------# 132 | x = np.arange(0, 256, dtype=r.dtype) 133 | lut_hue = ((x * r[0]) % 180).astype(dtype) 134 | lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) 135 | lut_val = np.clip(x * r[2], 0, 255).astype(dtype) 136 | 137 | image_data = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))) 138 | image_data = cv2.cvtColor(image_data, cv2.COLOR_HSV2RGB) 139 | 140 | #---------------------------------# 141 | # 对真实框进行调整 142 | #---------------------------------# 143 | if len(box)>0: 144 | np.random.shuffle(box) 145 | box[:, [0,2]] = box[:, [0,2]]*nw/iw + dx 146 | box[:, [1,3]] = box[:, [1,3]]*nh/ih + dy 147 | if flip: box[:, [0,2]] = w - box[:, [2,0]] 148 | box[:, 0:2][box[:, 0:2]<0] = 0 149 | box[:, 2][box[:, 2]>w] = w 150 | box[:, 3][box[:, 3]>h] = h 151 | box_w = box[:, 2] - box[:, 0] 152 | box_h = box[:, 3] - box[:, 1] 153 | box = box[np.logical_and(box_w>1, box_h>1)] 154 | 155 | return image_data, box 156 | 157 | # DataLoader中collate_fn使用 158 | def yolo_dataset_collate(batch): 159 | images = [] 160 | bboxes = [] 161 | for img, box in batch: 162 | images.append(img) 163 | bboxes.append(box) 164 | images = torch.from_numpy(np.array(images)).type(torch.FloatTensor) 165 | bboxes = [torch.from_numpy(ann).type(torch.FloatTensor) for ann in bboxes] 166 | return images, bboxes 167 | -------------------------------------------------------------------------------- /utils/utils.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | import numpy as np 4 | import torch 5 | from PIL import Image 6 | 7 | #---------------------------------------------------------# 8 | # 将图像转换成RGB图像,防止灰度图在预测时报错。 9 | # 代码仅仅支持RGB图像的预测,所有其它类型的图像都会转化成RGB 10 | #---------------------------------------------------------# 11 | def cvtColor(image): 12 | if len(np.shape(image)) == 3 and np.shape(image)[2] == 3: 13 | return image 14 | else: 15 | image = image.convert('RGB') 16 | return image 17 | 18 | #---------------------------------------------------# 19 | # 对输入图像进行resize 20 | #---------------------------------------------------# 21 | def resize_image(image, size, letterbox_image): 22 | iw, ih = image.size 23 | w, h = size 24 | if letterbox_image: 25 | scale = min(w/iw, h/ih) 26 | nw = int(iw*scale) 27 | nh = int(ih*scale) 28 | 29 | image = image.resize((nw,nh), Image.BICUBIC) 30 | new_image = Image.new('RGB', size, (128,128,128)) 31 | new_image.paste(image, ((w-nw)//2, (h-nh)//2)) 32 | else: 33 | new_image = image.resize((w, h), Image.BICUBIC) 34 | return new_image 35 | 36 | #---------------------------------------------------# 37 | # 获得类 38 | #---------------------------------------------------# 39 | def get_classes(classes_path): 40 | with open(classes_path, encoding='utf-8') as f: 41 | class_names = f.readlines() 42 | class_names = [c.strip() for c in class_names] 43 | return class_names, len(class_names) 44 | 45 | #---------------------------------------------------# 46 | # 获得先验框 47 | #---------------------------------------------------# 48 | def get_anchors(anchors_path): 49 | '''loads the anchors from a file''' 50 | with open(anchors_path, encoding='utf-8') as f: 51 | anchors = f.readline() 52 | anchors = [float(x) for x in anchors.split(',')] 53 | anchors = np.array(anchors).reshape(-1, 2) 54 | return anchors, len(anchors) 55 | 56 | #---------------------------------------------------# 57 | # 获得学习率 58 | #---------------------------------------------------# 59 | def get_lr(optimizer): 60 | for param_group in optimizer.param_groups: 61 | return param_group['lr'] 62 | 63 | #---------------------------------------------------# 64 | # 设置种子 65 | #---------------------------------------------------# 66 | def seed_everything(seed=11): 67 | random.seed(seed) 68 | np.random.seed(seed) 69 | torch.manual_seed(seed) 70 | torch.cuda.manual_seed(seed) 71 | torch.cuda.manual_seed_all(seed) 72 | torch.backends.cudnn.deterministic = True 73 | torch.backends.cudnn.benchmark = False 74 | 75 | #---------------------------------------------------# 76 | # 设置Dataloader的种子 77 | #---------------------------------------------------# 78 | def worker_init_fn(worker_id, rank, seed): 79 | worker_seed = rank + seed 80 | random.seed(worker_seed) 81 | np.random.seed(worker_seed) 82 | torch.manual_seed(worker_seed) 83 | 84 | def preprocess_input(image): 85 | image /= 255.0 86 | return image 87 | 88 | def show_config(**kwargs): 89 | print('Configurations:') 90 | print('-' * 70) 91 | print('|%25s | %40s|' % ('keys', 'values')) 92 | print('-' * 70) 93 | for key, value in kwargs.items(): 94 | print('|%25s | %40s|' % (str(key), str(value))) 95 | print('-' * 70) 96 | -------------------------------------------------------------------------------- /utils/utils_bbox.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torchvision.ops import nms 4 | import numpy as np 5 | 6 | class DecodeBox(): 7 | def __init__(self, anchors, num_classes, input_shape, anchors_mask = [[6,7,8], [3,4,5], [0,1,2]]): 8 | super(DecodeBox, self).__init__() 9 | self.anchors = anchors 10 | self.num_classes = num_classes 11 | self.bbox_attrs = 5 + num_classes 12 | self.input_shape = input_shape 13 | #-----------------------------------------------------------# 14 | # 13x13的特征层对应的anchor是[116,90],[156,198],[373,326] 15 | # 26x26的特征层对应的anchor是[30,61],[62,45],[59,119] 16 | # 52x52的特征层对应的anchor是[10,13],[16,30],[33,23] 17 | #-----------------------------------------------------------# 18 | self.anchors_mask = anchors_mask 19 | 20 | def decode_box(self, inputs): 21 | outputs = [] 22 | for i, input in enumerate(inputs): 23 | #-----------------------------------------------# 24 | # 输入的input一共有三个,他们的shape分别是 25 | # batch_size, 255, 13, 13 26 | # batch_size, 255, 26, 26 27 | # batch_size, 255, 52, 52 28 | #-----------------------------------------------# 29 | batch_size = input.size(0) 30 | input_height = input.size(2) 31 | input_width = input.size(3) 32 | 33 | #-----------------------------------------------# 34 | # 输入为416x416时 35 | # stride_h = stride_w = 32、16、8 36 | #-----------------------------------------------# 37 | stride_h = self.input_shape[0] / input_height 38 | stride_w = self.input_shape[1] / input_width 39 | #-------------------------------------------------# 40 | # 此时获得的scaled_anchors大小是相对于特征层的 41 | #-------------------------------------------------# 42 | scaled_anchors = [(anchor_width / stride_w, anchor_height / stride_h) for anchor_width, anchor_height in self.anchors[self.anchors_mask[i]]] 43 | 44 | #-----------------------------------------------# 45 | # 输入的input一共有三个,他们的shape分别是 46 | # batch_size, 3, 13, 13, 85 47 | # batch_size, 3, 26, 26, 85 48 | # batch_size, 3, 52, 52, 85 49 | #-----------------------------------------------# 50 | prediction = input.view(batch_size, len(self.anchors_mask[i]), 51 | self.bbox_attrs, input_height, input_width).permute(0, 1, 3, 4, 2).contiguous() 52 | 53 | #-----------------------------------------------# 54 | # 先验框的中心位置的调整参数 55 | #-----------------------------------------------# 56 | x = torch.sigmoid(prediction[..., 0]) 57 | y = torch.sigmoid(prediction[..., 1]) 58 | #-----------------------------------------------# 59 | # 先验框的宽高调整参数 60 | #-----------------------------------------------# 61 | w = prediction[..., 2] 62 | h = prediction[..., 3] 63 | #-----------------------------------------------# 64 | # 获得置信度,是否有物体 65 | #-----------------------------------------------# 66 | conf = torch.sigmoid(prediction[..., 4]) 67 | #-----------------------------------------------# 68 | # 种类置信度 69 | #-----------------------------------------------# 70 | pred_cls = torch.sigmoid(prediction[..., 5:]) 71 | 72 | FloatTensor = torch.cuda.FloatTensor if x.is_cuda else torch.FloatTensor 73 | LongTensor = torch.cuda.LongTensor if x.is_cuda else torch.LongTensor 74 | 75 | #----------------------------------------------------------# 76 | # 生成网格,先验框中心,网格左上角 77 | # batch_size,3,13,13 78 | #----------------------------------------------------------# 79 | grid_x = torch.linspace(0, input_width - 1, input_width).repeat(input_height, 1).repeat( 80 | batch_size * len(self.anchors_mask[i]), 1, 1).view(x.shape).type(FloatTensor) 81 | grid_y = torch.linspace(0, input_height - 1, input_height).repeat(input_width, 1).t().repeat( 82 | batch_size * len(self.anchors_mask[i]), 1, 1).view(y.shape).type(FloatTensor) 83 | 84 | #----------------------------------------------------------# 85 | # 按照网格格式生成先验框的宽高 86 | # batch_size,3,13,13 87 | #----------------------------------------------------------# 88 | anchor_w = FloatTensor(scaled_anchors).index_select(1, LongTensor([0])) 89 | anchor_h = FloatTensor(scaled_anchors).index_select(1, LongTensor([1])) 90 | anchor_w = anchor_w.repeat(batch_size, 1).repeat(1, 1, input_height * input_width).view(w.shape) 91 | anchor_h = anchor_h.repeat(batch_size, 1).repeat(1, 1, input_height * input_width).view(h.shape) 92 | 93 | #----------------------------------------------------------# 94 | # 利用预测结果对先验框进行调整 95 | # 首先调整先验框的中心,从先验框中心向右下角偏移 96 | # 再调整先验框的宽高。 97 | #----------------------------------------------------------# 98 | pred_boxes = FloatTensor(prediction[..., :4].shape) 99 | pred_boxes[..., 0] = x.data + grid_x 100 | pred_boxes[..., 1] = y.data + grid_y 101 | pred_boxes[..., 2] = torch.exp(w.data) * anchor_w 102 | pred_boxes[..., 3] = torch.exp(h.data) * anchor_h 103 | 104 | #----------------------------------------------------------# 105 | # 将输出结果归一化成小数的形式 106 | #----------------------------------------------------------# 107 | _scale = torch.Tensor([input_width, input_height, input_width, input_height]).type(FloatTensor) 108 | output = torch.cat((pred_boxes.view(batch_size, -1, 4) / _scale, 109 | conf.view(batch_size, -1, 1), pred_cls.view(batch_size, -1, self.num_classes)), -1) 110 | outputs.append(output.data) 111 | return outputs 112 | 113 | def yolo_correct_boxes(self, box_xy, box_wh, input_shape, image_shape, letterbox_image): 114 | #-----------------------------------------------------------------# 115 | # 把y轴放前面是因为方便预测框和图像的宽高进行相乘 116 | #-----------------------------------------------------------------# 117 | box_yx = box_xy[..., ::-1] 118 | box_hw = box_wh[..., ::-1] 119 | input_shape = np.array(input_shape) 120 | image_shape = np.array(image_shape) 121 | 122 | if letterbox_image: 123 | #-----------------------------------------------------------------# 124 | # 这里求出来的offset是图像有效区域相对于图像左上角的偏移情况 125 | # new_shape指的是宽高缩放情况 126 | #-----------------------------------------------------------------# 127 | new_shape = np.round(image_shape * np.min(input_shape/image_shape)) 128 | offset = (input_shape - new_shape)/2./input_shape 129 | scale = input_shape/new_shape 130 | 131 | box_yx = (box_yx - offset) * scale 132 | box_hw *= scale 133 | 134 | box_mins = box_yx - (box_hw / 2.) 135 | box_maxes = box_yx + (box_hw / 2.) 136 | boxes = np.concatenate([box_mins[..., 0:1], box_mins[..., 1:2], box_maxes[..., 0:1], box_maxes[..., 1:2]], axis=-1) 137 | boxes *= np.concatenate([image_shape, image_shape], axis=-1) 138 | return boxes 139 | 140 | def non_max_suppression(self, prediction, num_classes, input_shape, image_shape, letterbox_image, conf_thres=0.5, nms_thres=0.4): 141 | #----------------------------------------------------------# 142 | # 将预测结果的格式转换成左上角右下角的格式。 143 | # prediction [batch_size, num_anchors, 85] 144 | #----------------------------------------------------------# 145 | box_corner = prediction.new(prediction.shape) 146 | box_corner[:, :, 0] = prediction[:, :, 0] - prediction[:, :, 2] / 2 147 | box_corner[:, :, 1] = prediction[:, :, 1] - prediction[:, :, 3] / 2 148 | box_corner[:, :, 2] = prediction[:, :, 0] + prediction[:, :, 2] / 2 149 | box_corner[:, :, 3] = prediction[:, :, 1] + prediction[:, :, 3] / 2 150 | prediction[:, :, :4] = box_corner[:, :, :4] 151 | 152 | output = [None for _ in range(len(prediction))] 153 | for i, image_pred in enumerate(prediction): 154 | #----------------------------------------------------------# 155 | # 对种类预测部分取max。 156 | # class_conf [num_anchors, 1] 种类置信度 157 | # class_pred [num_anchors, 1] 种类 158 | #----------------------------------------------------------# 159 | class_conf, class_pred = torch.max(image_pred[:, 5:5 + num_classes], 1, keepdim=True) 160 | 161 | #----------------------------------------------------------# 162 | # 利用置信度进行第一轮筛选 163 | #----------------------------------------------------------# 164 | conf_mask = (image_pred[:, 4] * class_conf[:, 0] >= conf_thres).squeeze() 165 | 166 | #----------------------------------------------------------# 167 | # 根据置信度进行预测结果的筛选 168 | #----------------------------------------------------------# 169 | image_pred = image_pred[conf_mask] 170 | class_conf = class_conf[conf_mask] 171 | class_pred = class_pred[conf_mask] 172 | if not image_pred.size(0): 173 | continue 174 | #-------------------------------------------------------------------------# 175 | # detections [num_anchors, 7] 176 | # 7的内容为:x1, y1, x2, y2, obj_conf, class_conf, class_pred 177 | #-------------------------------------------------------------------------# 178 | detections = torch.cat((image_pred[:, :5], class_conf.float(), class_pred.float()), 1) 179 | 180 | #------------------------------------------# 181 | # 获得预测结果中包含的所有种类 182 | #------------------------------------------# 183 | unique_labels = detections[:, -1].cpu().unique() 184 | 185 | if prediction.is_cuda: 186 | unique_labels = unique_labels.cuda() 187 | detections = detections.cuda() 188 | 189 | for c in unique_labels: 190 | #------------------------------------------# 191 | # 获得某一类得分筛选后全部的预测结果 192 | #------------------------------------------# 193 | detections_class = detections[detections[:, -1] == c] 194 | 195 | #------------------------------------------# 196 | # 使用官方自带的非极大抑制会速度更快一些! 197 | #------------------------------------------# 198 | keep = nms( 199 | detections_class[:, :4], 200 | detections_class[:, 4] * detections_class[:, 5], 201 | nms_thres 202 | ) 203 | max_detections = detections_class[keep] 204 | 205 | # # 按照存在物体的置信度排序 206 | # _, conf_sort_index = torch.sort(detections_class[:, 4]*detections_class[:, 5], descending=True) 207 | # detections_class = detections_class[conf_sort_index] 208 | # # 进行非极大抑制 209 | # max_detections = [] 210 | # while detections_class.size(0): 211 | # # 取出这一类置信度最高的,一步一步往下判断,判断重合程度是否大于nms_thres,如果是则去除掉 212 | # max_detections.append(detections_class[0].unsqueeze(0)) 213 | # if len(detections_class) == 1: 214 | # break 215 | # ious = bbox_iou(max_detections[-1], detections_class[1:]) 216 | # detections_class = detections_class[1:][ious < nms_thres] 217 | # # 堆叠 218 | # max_detections = torch.cat(max_detections).data 219 | 220 | # Add max detections to outputs 221 | output[i] = max_detections if output[i] is None else torch.cat((output[i], max_detections)) 222 | 223 | if output[i] is not None: 224 | output[i] = output[i].cpu().numpy() 225 | box_xy, box_wh = (output[i][:, 0:2] + output[i][:, 2:4])/2, output[i][:, 2:4] - output[i][:, 0:2] 226 | output[i][:, :4] = self.yolo_correct_boxes(box_xy, box_wh, input_shape, image_shape, letterbox_image) 227 | return output 228 | -------------------------------------------------------------------------------- /utils/utils_fit.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import torch 4 | from tqdm import tqdm 5 | 6 | from utils.utils import get_lr 7 | 8 | 9 | def fit_one_epoch(model_train, model, yolo_loss, loss_history, eval_callback, optimizer, epoch, epoch_step, epoch_step_val, gen, gen_val, Epoch, cuda, fp16, scaler, save_period, save_dir, local_rank=0): 10 | loss = 0 11 | val_loss = 0 12 | 13 | if local_rank == 0: 14 | print('Start Train') 15 | pbar = tqdm(total=epoch_step,desc=f'Epoch {epoch + 1}/{Epoch}',postfix=dict,mininterval=0.3) 16 | model_train.train() 17 | for iteration, batch in enumerate(gen): 18 | if iteration >= epoch_step: 19 | break 20 | 21 | images, targets = batch[0], batch[1] 22 | with torch.no_grad(): 23 | if cuda: 24 | images = images.cuda(local_rank) 25 | targets = [ann.cuda(local_rank) for ann in targets] 26 | #----------------------# 27 | # 清零梯度 28 | #----------------------# 29 | optimizer.zero_grad() 30 | if not fp16: 31 | #----------------------# 32 | # 前向传播 33 | #----------------------# 34 | outputs = model_train(images) 35 | 36 | loss_value_all = 0 37 | #----------------------# 38 | # 计算损失 39 | #----------------------# 40 | for l in range(len(outputs)): 41 | loss_item = yolo_loss(l, outputs[l], targets) 42 | loss_value_all += loss_item 43 | loss_value = loss_value_all 44 | 45 | #----------------------# 46 | # 反向传播 47 | #----------------------# 48 | loss_value.backward() 49 | optimizer.step() 50 | else: 51 | from torch.cuda.amp import autocast 52 | with autocast(): 53 | #----------------------# 54 | # 前向传播 55 | #----------------------# 56 | outputs = model_train(images) 57 | 58 | loss_value_all = 0 59 | #----------------------# 60 | # 计算损失 61 | #----------------------# 62 | for l in range(len(outputs)): 63 | loss_item = yolo_loss(l, outputs[l], targets) 64 | loss_value_all += loss_item 65 | loss_value = loss_value_all 66 | 67 | #----------------------# 68 | # 反向传播 69 | #----------------------# 70 | scaler.scale(loss_value).backward() 71 | scaler.step(optimizer) 72 | scaler.update() 73 | 74 | loss += loss_value.item() 75 | 76 | if local_rank == 0: 77 | pbar.set_postfix(**{'loss' : loss / (iteration + 1), 78 | 'lr' : get_lr(optimizer)}) 79 | pbar.update(1) 80 | 81 | if local_rank == 0: 82 | pbar.close() 83 | print('Finish Train') 84 | print('Start Validation') 85 | pbar = tqdm(total=epoch_step_val, desc=f'Epoch {epoch + 1}/{Epoch}',postfix=dict,mininterval=0.3) 86 | 87 | model_train.eval() 88 | for iteration, batch in enumerate(gen_val): 89 | if iteration >= epoch_step_val: 90 | break 91 | images, targets = batch[0], batch[1] 92 | with torch.no_grad(): 93 | if cuda: 94 | images = images.cuda(local_rank) 95 | targets = [ann.cuda(local_rank) for ann in targets] 96 | #----------------------# 97 | # 清零梯度 98 | #----------------------# 99 | optimizer.zero_grad() 100 | #----------------------# 101 | # 前向传播 102 | #----------------------# 103 | outputs = model_train(images) 104 | 105 | loss_value_all = 0 106 | #----------------------# 107 | # 计算损失 108 | #----------------------# 109 | for l in range(len(outputs)): 110 | loss_item = yolo_loss(l, outputs[l], targets) 111 | loss_value_all += loss_item 112 | loss_value = loss_value_all 113 | 114 | val_loss += loss_value.item() 115 | if local_rank == 0: 116 | pbar.set_postfix(**{'val_loss': val_loss / (iteration + 1)}) 117 | pbar.update(1) 118 | 119 | if local_rank == 0: 120 | pbar.close() 121 | print('Finish Validation') 122 | loss_history.append_loss(epoch + 1, loss / epoch_step, val_loss / epoch_step_val) 123 | eval_callback.on_epoch_end(epoch + 1, model_train) 124 | print('Epoch:'+ str(epoch + 1) + '/' + str(Epoch)) 125 | print('Total Loss: %.3f || Val Loss: %.3f ' % (loss / epoch_step, val_loss / epoch_step_val)) 126 | 127 | #-----------------------------------------------# 128 | # 保存权值 129 | #-----------------------------------------------# 130 | if (epoch + 1) % save_period == 0 or epoch + 1 == Epoch: 131 | torch.save(model.state_dict(), os.path.join(save_dir, "ep%03d-loss%.3f-val_loss%.3f.pth" % (epoch + 1, loss / epoch_step, val_loss / epoch_step_val))) 132 | 133 | if len(loss_history.val_loss) <= 1 or (val_loss / epoch_step_val) <= min(loss_history.val_loss): 134 | print('Save best model to best_epoch_weights.pth') 135 | torch.save(model.state_dict(), os.path.join(save_dir, "best_epoch_weights.pth")) 136 | 137 | torch.save(model.state_dict(), os.path.join(save_dir, "last_epoch_weights.pth")) -------------------------------------------------------------------------------- /utils_coco/coco_annotation.py: -------------------------------------------------------------------------------- 1 | #-------------------------------------------------------# 2 | # 用于处理COCO数据集,根据json文件生成txt文件用于训练 3 | #-------------------------------------------------------# 4 | import json 5 | import os 6 | from collections import defaultdict 7 | 8 | #-------------------------------------------------------# 9 | # 指向了COCO训练集与验证集图片的路径 10 | #-------------------------------------------------------# 11 | train_datasets_path = "coco_dataset/train2017" 12 | val_datasets_path = "coco_dataset/val2017" 13 | 14 | #-------------------------------------------------------# 15 | # 指向了COCO训练集与验证集标签的路径 16 | #-------------------------------------------------------# 17 | train_annotation_path = "coco_dataset/annotations/instances_train2017.json" 18 | val_annotation_path = "coco_dataset/annotations/instances_val2017.json" 19 | 20 | #-------------------------------------------------------# 21 | # 生成的txt文件路径 22 | #-------------------------------------------------------# 23 | train_output_path = "coco_train.txt" 24 | val_output_path = "coco_val.txt" 25 | 26 | if __name__ == "__main__": 27 | name_box_id = defaultdict(list) 28 | id_name = dict() 29 | f = open(train_annotation_path, encoding='utf-8') 30 | data = json.load(f) 31 | 32 | annotations = data['annotations'] 33 | for ant in annotations: 34 | id = ant['image_id'] 35 | name = os.path.join(train_datasets_path, '%012d.jpg' % id) 36 | cat = ant['category_id'] 37 | if cat >= 1 and cat <= 11: 38 | cat = cat - 1 39 | elif cat >= 13 and cat <= 25: 40 | cat = cat - 2 41 | elif cat >= 27 and cat <= 28: 42 | cat = cat - 3 43 | elif cat >= 31 and cat <= 44: 44 | cat = cat - 5 45 | elif cat >= 46 and cat <= 65: 46 | cat = cat - 6 47 | elif cat == 67: 48 | cat = cat - 7 49 | elif cat == 70: 50 | cat = cat - 9 51 | elif cat >= 72 and cat <= 82: 52 | cat = cat - 10 53 | elif cat >= 84 and cat <= 90: 54 | cat = cat - 11 55 | name_box_id[name].append([ant['bbox'], cat]) 56 | 57 | f = open(train_output_path, 'w') 58 | for key in name_box_id.keys(): 59 | f.write(key) 60 | box_infos = name_box_id[key] 61 | for info in box_infos: 62 | x_min = int(info[0][0]) 63 | y_min = int(info[0][1]) 64 | x_max = x_min + int(info[0][2]) 65 | y_max = y_min + int(info[0][3]) 66 | 67 | box_info = " %d,%d,%d,%d,%d" % ( 68 | x_min, y_min, x_max, y_max, int(info[1])) 69 | f.write(box_info) 70 | f.write('\n') 71 | f.close() 72 | 73 | name_box_id = defaultdict(list) 74 | id_name = dict() 75 | f = open(val_annotation_path, encoding='utf-8') 76 | data = json.load(f) 77 | 78 | annotations = data['annotations'] 79 | for ant in annotations: 80 | id = ant['image_id'] 81 | name = os.path.join(val_datasets_path, '%012d.jpg' % id) 82 | cat = ant['category_id'] 83 | if cat >= 1 and cat <= 11: 84 | cat = cat - 1 85 | elif cat >= 13 and cat <= 25: 86 | cat = cat - 2 87 | elif cat >= 27 and cat <= 28: 88 | cat = cat - 3 89 | elif cat >= 31 and cat <= 44: 90 | cat = cat - 5 91 | elif cat >= 46 and cat <= 65: 92 | cat = cat - 6 93 | elif cat == 67: 94 | cat = cat - 7 95 | elif cat == 70: 96 | cat = cat - 9 97 | elif cat >= 72 and cat <= 82: 98 | cat = cat - 10 99 | elif cat >= 84 and cat <= 90: 100 | cat = cat - 11 101 | name_box_id[name].append([ant['bbox'], cat]) 102 | 103 | f = open(val_output_path, 'w') 104 | for key in name_box_id.keys(): 105 | f.write(key) 106 | box_infos = name_box_id[key] 107 | for info in box_infos: 108 | x_min = int(info[0][0]) 109 | y_min = int(info[0][1]) 110 | x_max = x_min + int(info[0][2]) 111 | y_max = y_min + int(info[0][3]) 112 | 113 | box_info = " %d,%d,%d,%d,%d" % ( 114 | x_min, y_min, x_max, y_max, int(info[1])) 115 | f.write(box_info) 116 | f.write('\n') 117 | f.close() 118 | -------------------------------------------------------------------------------- /utils_coco/get_map_coco.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | 4 | import numpy as np 5 | import torch 6 | from PIL import Image 7 | from pycocotools.coco import COCO 8 | from pycocotools.cocoeval import COCOeval 9 | from tqdm import tqdm 10 | 11 | from utils.utils import cvtColor, preprocess_input, resize_image 12 | from yolo import YOLO 13 | 14 | #---------------------------------------------------------------------------# 15 | # map_mode用于指定该文件运行时计算的内容 16 | # map_mode为0代表整个map计算流程,包括获得预测结果、计算map。 17 | # map_mode为1代表仅仅获得预测结果。 18 | # map_mode为2代表仅仅获得计算map。 19 | #---------------------------------------------------------------------------# 20 | map_mode = 0 21 | #-------------------------------------------------------# 22 | # 指向了验证集标签与图片路径 23 | #-------------------------------------------------------# 24 | cocoGt_path = 'coco_dataset/annotations/instances_val2017.json' 25 | dataset_img_path = 'coco_dataset/val2017' 26 | #-------------------------------------------------------# 27 | # 结果输出的文件夹,默认为map_out 28 | #-------------------------------------------------------# 29 | temp_save_path = 'map_out/coco_eval' 30 | 31 | class mAP_YOLO(YOLO): 32 | #---------------------------------------------------# 33 | # 检测图片 34 | #---------------------------------------------------# 35 | def detect_image(self, image_id, image, results): 36 | #---------------------------------------------------# 37 | # 计算输入图片的高和宽 38 | #---------------------------------------------------# 39 | image_shape = np.array(np.shape(image)[0:2]) 40 | #---------------------------------------------------------# 41 | # 在这里将图像转换成RGB图像,防止灰度图在预测时报错。 42 | # 代码仅仅支持RGB图像的预测,所有其它类型的图像都会转化成RGB 43 | #---------------------------------------------------------# 44 | image = cvtColor(image) 45 | #---------------------------------------------------------# 46 | # 给图像增加灰条,实现不失真的resize 47 | # 也可以直接resize进行识别 48 | #---------------------------------------------------------# 49 | image_data = resize_image(image, (self.input_shape[1],self.input_shape[0]), self.letterbox_image) 50 | #---------------------------------------------------------# 51 | # 添加上batch_size维度 52 | #---------------------------------------------------------# 53 | image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, dtype='float32')), (2, 0, 1)), 0) 54 | 55 | with torch.no_grad(): 56 | images = torch.from_numpy(image_data) 57 | if self.cuda: 58 | images = images.cuda() 59 | #---------------------------------------------------------# 60 | # 将图像输入网络当中进行预测! 61 | #---------------------------------------------------------# 62 | outputs = self.net(images) 63 | outputs = self.bbox_util.decode_box(outputs) 64 | #---------------------------------------------------------# 65 | # 将预测框进行堆叠,然后进行非极大抑制 66 | #---------------------------------------------------------# 67 | outputs = self.bbox_util.non_max_suppression(torch.cat(outputs, 1), self.num_classes, self.input_shape, 68 | image_shape, self.letterbox_image, conf_thres = self.confidence, nms_thres = self.nms_iou) 69 | 70 | if outputs[0] is None: 71 | return results 72 | 73 | top_label = np.array(outputs[0][:, 6], dtype = 'int32') 74 | top_conf = outputs[0][:, 4] * outputs[0][:, 5] 75 | top_boxes = outputs[0][:, :4] 76 | 77 | for i, c in enumerate(top_label): 78 | result = {} 79 | top, left, bottom, right = top_boxes[i] 80 | 81 | result["image_id"] = int(image_id) 82 | result["category_id"] = clsid2catid[c] 83 | result["bbox"] = [float(left),float(top),float(right-left),float(bottom-top)] 84 | result["score"] = float(top_conf[i]) 85 | results.append(result) 86 | return results 87 | 88 | if __name__ == "__main__": 89 | if not os.path.exists(temp_save_path): 90 | os.makedirs(temp_save_path) 91 | 92 | cocoGt = COCO(cocoGt_path) 93 | ids = list(cocoGt.imgToAnns.keys()) 94 | clsid2catid = cocoGt.getCatIds() 95 | 96 | if map_mode == 0 or map_mode == 1: 97 | yolo = mAP_YOLO(confidence = 0.001, nms_iou = 0.65) 98 | 99 | with open(os.path.join(temp_save_path, 'eval_results.json'),"w") as f: 100 | results = [] 101 | for image_id in tqdm(ids): 102 | image_path = os.path.join(dataset_img_path, cocoGt.loadImgs(image_id)[0]['file_name']) 103 | image = Image.open(image_path) 104 | results = yolo.detect_image(image_id, image, results) 105 | json.dump(results, f) 106 | 107 | if map_mode == 0 or map_mode == 2: 108 | cocoDt = cocoGt.loadRes(os.path.join(temp_save_path, 'eval_results.json')) 109 | cocoEval = COCOeval(cocoGt, cocoDt, 'bbox') 110 | cocoEval.evaluate() 111 | cocoEval.accumulate() 112 | cocoEval.summarize() 113 | print("Get map done.") 114 | -------------------------------------------------------------------------------- /voc_annotation.py: -------------------------------------------------------------------------------- 1 | import os 2 | import random 3 | import xml.etree.ElementTree as ET 4 | 5 | import numpy as np 6 | 7 | from utils.utils import get_classes 8 | 9 | #--------------------------------------------------------------------------------------------------------------------------------# 10 | # annotation_mode用于指定该文件运行时计算的内容 11 | # annotation_mode为0代表整个标签处理过程,包括获得VOCdevkit/VOC2007/ImageSets里面的txt以及训练用的2007_train.txt、2007_val.txt 12 | # annotation_mode为1代表获得VOCdevkit/VOC2007/ImageSets里面的txt 13 | # annotation_mode为2代表获得训练用的2007_train.txt、2007_val.txt 14 | #--------------------------------------------------------------------------------------------------------------------------------# 15 | annotation_mode = 0 16 | #-------------------------------------------------------------------# 17 | # 必须要修改,用于生成2007_train.txt、2007_val.txt的目标信息 18 | # 与训练和预测所用的classes_path一致即可 19 | # 如果生成的2007_train.txt里面没有目标信息 20 | # 那么就是因为classes没有设定正确 21 | # 仅在annotation_mode为0和2的时候有效 22 | #-------------------------------------------------------------------# 23 | classes_path = 'model_data/voc_classes.txt' 24 | #--------------------------------------------------------------------------------------------------------------------------------# 25 | # trainval_percent用于指定(训练集+验证集)与测试集的比例,默认情况下 (训练集+验证集):测试集 = 9:1 26 | # train_percent用于指定(训练集+验证集)中训练集与验证集的比例,默认情况下 训练集:验证集 = 9:1 27 | # 仅在annotation_mode为0和1的时候有效 28 | #--------------------------------------------------------------------------------------------------------------------------------# 29 | trainval_percent = 0.9 30 | train_percent = 0.9 31 | #-------------------------------------------------------# 32 | # 指向VOC数据集所在的文件夹 33 | # 默认指向根目录下的VOC数据集 34 | #-------------------------------------------------------# 35 | VOCdevkit_path = 'VOCdevkit' 36 | 37 | VOCdevkit_sets = [('2007', 'train'), ('2007', 'val')] 38 | classes, _ = get_classes(classes_path) 39 | 40 | #-------------------------------------------------------# 41 | # 统计目标数量 42 | #-------------------------------------------------------# 43 | photo_nums = np.zeros(len(VOCdevkit_sets)) 44 | nums = np.zeros(len(classes)) 45 | def convert_annotation(year, image_id, list_file): 46 | in_file = open(os.path.join(VOCdevkit_path, 'VOC%s/Annotations/%s.xml'%(year, image_id)), encoding='utf-8') 47 | tree=ET.parse(in_file) 48 | root = tree.getroot() 49 | 50 | for obj in root.iter('object'): 51 | difficult = 0 52 | if obj.find('difficult')!=None: 53 | difficult = obj.find('difficult').text 54 | cls = obj.find('name').text 55 | if cls not in classes or int(difficult)==1: 56 | continue 57 | cls_id = classes.index(cls) 58 | xmlbox = obj.find('bndbox') 59 | b = (int(float(xmlbox.find('xmin').text)), int(float(xmlbox.find('ymin').text)), int(float(xmlbox.find('xmax').text)), int(float(xmlbox.find('ymax').text))) 60 | list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id)) 61 | 62 | nums[classes.index(cls)] = nums[classes.index(cls)] + 1 63 | 64 | if __name__ == "__main__": 65 | random.seed(0) 66 | if " " in os.path.abspath(VOCdevkit_path): 67 | raise ValueError("数据集存放的文件夹路径与图片名称中不可以存在空格,否则会影响正常的模型训练,请注意修改。") 68 | 69 | if annotation_mode == 0 or annotation_mode == 1: 70 | print("Generate txt in ImageSets.") 71 | xmlfilepath = os.path.join(VOCdevkit_path, 'VOC2007/Annotations') 72 | saveBasePath = os.path.join(VOCdevkit_path, 'VOC2007/ImageSets/Main') 73 | temp_xml = os.listdir(xmlfilepath) 74 | total_xml = [] 75 | for xml in temp_xml: 76 | if xml.endswith(".xml"): 77 | total_xml.append(xml) 78 | 79 | num = len(total_xml) 80 | list = range(num) 81 | tv = int(num*trainval_percent) 82 | tr = int(tv*train_percent) 83 | trainval= random.sample(list,tv) 84 | train = random.sample(trainval,tr) 85 | 86 | print("train and val size",tv) 87 | print("train size",tr) 88 | ftrainval = open(os.path.join(saveBasePath,'trainval.txt'), 'w') 89 | ftest = open(os.path.join(saveBasePath,'test.txt'), 'w') 90 | ftrain = open(os.path.join(saveBasePath,'train.txt'), 'w') 91 | fval = open(os.path.join(saveBasePath,'val.txt'), 'w') 92 | 93 | for i in list: 94 | name=total_xml[i][:-4]+'\n' 95 | if i in trainval: 96 | ftrainval.write(name) 97 | if i in train: 98 | ftrain.write(name) 99 | else: 100 | fval.write(name) 101 | else: 102 | ftest.write(name) 103 | 104 | ftrainval.close() 105 | ftrain.close() 106 | fval.close() 107 | ftest.close() 108 | print("Generate txt in ImageSets done.") 109 | 110 | if annotation_mode == 0 or annotation_mode == 2: 111 | print("Generate 2007_train.txt and 2007_val.txt for train.") 112 | type_index = 0 113 | for year, image_set in VOCdevkit_sets: 114 | image_ids = open(os.path.join(VOCdevkit_path, 'VOC%s/ImageSets/Main/%s.txt'%(year, image_set)), encoding='utf-8').read().strip().split() 115 | list_file = open('%s_%s.txt'%(year, image_set), 'w', encoding='utf-8') 116 | for image_id in image_ids: 117 | list_file.write('%s/VOC%s/JPEGImages/%s.jpg'%(os.path.abspath(VOCdevkit_path), year, image_id)) 118 | 119 | convert_annotation(year, image_id, list_file) 120 | list_file.write('\n') 121 | photo_nums[type_index] = len(image_ids) 122 | type_index += 1 123 | list_file.close() 124 | print("Generate 2007_train.txt and 2007_val.txt for train done.") 125 | 126 | def printTable(List1, List2): 127 | for i in range(len(List1[0])): 128 | print("|", end=' ') 129 | for j in range(len(List1)): 130 | print(List1[j][i].rjust(int(List2[j])), end=' ') 131 | print("|", end=' ') 132 | print() 133 | 134 | str_nums = [str(int(x)) for x in nums] 135 | tableData = [ 136 | classes, str_nums 137 | ] 138 | colWidths = [0]*len(tableData) 139 | len1 = 0 140 | for i in range(len(tableData)): 141 | for j in range(len(tableData[i])): 142 | if len(tableData[i][j]) > colWidths[i]: 143 | colWidths[i] = len(tableData[i][j]) 144 | printTable(tableData, colWidths) 145 | 146 | if photo_nums[0] <= 500: 147 | print("训练集数量小于500,属于较小的数据量,请注意设置较大的训练世代(Epoch)以满足足够的梯度下降次数(Step)。") 148 | 149 | if np.sum(nums) == 0: 150 | print("在数据集中并未获得任何目标,请注意修改classes_path对应自己的数据集,并且保证标签名字正确,否则训练将会没有任何效果!") 151 | print("在数据集中并未获得任何目标,请注意修改classes_path对应自己的数据集,并且保证标签名字正确,否则训练将会没有任何效果!") 152 | print("在数据集中并未获得任何目标,请注意修改classes_path对应自己的数据集,并且保证标签名字正确,否则训练将会没有任何效果!") 153 | print("(重要的事情说三遍)。") 154 | -------------------------------------------------------------------------------- /yolo.py: -------------------------------------------------------------------------------- 1 | import colorsys 2 | import os 3 | import time 4 | 5 | import numpy as np 6 | import torch 7 | import torch.nn as nn 8 | from PIL import ImageDraw, ImageFont 9 | 10 | from nets.yolo import YoloBody 11 | from utils.utils import (cvtColor, get_anchors, get_classes, preprocess_input, 12 | resize_image, show_config) 13 | from utils.utils_bbox import DecodeBox 14 | 15 | ''' 16 | 训练自己的数据集必看注释! 17 | ''' 18 | class YOLO(object): 19 | _defaults = { 20 | #--------------------------------------------------------------------------# 21 | # 使用自己训练好的模型进行预测一定要修改model_path和classes_path! 22 | # model_path指向logs文件夹下的权值文件,classes_path指向model_data下的txt 23 | # 24 | # 训练好后logs文件夹下存在多个权值文件,选择验证集损失较低的即可。 25 | # 验证集损失较低不代表mAP较高,仅代表该权值在验证集上泛化性能较好。 26 | # 如果出现shape不匹配,同时要注意训练时的model_path和classes_path参数的修改 27 | #--------------------------------------------------------------------------# 28 | "model_path" : 'model_data/yolo_weights.pth', 29 | "classes_path" : 'model_data/coco_classes.txt', 30 | #---------------------------------------------------------------------# 31 | # anchors_path代表先验框对应的txt文件,一般不修改。 32 | # anchors_mask用于帮助代码找到对应的先验框,一般不修改。 33 | #---------------------------------------------------------------------# 34 | "anchors_path" : 'model_data/yolo_anchors.txt', 35 | "anchors_mask" : [[6, 7, 8], [3, 4, 5], [0, 1, 2]], 36 | #---------------------------------------------------------------------# 37 | # 输入图片的大小,必须为32的倍数。 38 | #---------------------------------------------------------------------# 39 | "input_shape" : [416, 416], 40 | #---------------------------------------------------------------------# 41 | # 只有得分大于置信度的预测框会被保留下来 42 | #---------------------------------------------------------------------# 43 | "confidence" : 0.5, 44 | #---------------------------------------------------------------------# 45 | # 非极大抑制所用到的nms_iou大小 46 | #---------------------------------------------------------------------# 47 | "nms_iou" : 0.3, 48 | #---------------------------------------------------------------------# 49 | # 该变量用于控制是否使用letterbox_image对输入图像进行不失真的resize, 50 | # 在多次测试后,发现关闭letterbox_image直接resize的效果更好 51 | #---------------------------------------------------------------------# 52 | "letterbox_image" : False, 53 | #-------------------------------# 54 | # 是否使用Cuda 55 | # 没有GPU可以设置成False 56 | #-------------------------------# 57 | "cuda" : True, 58 | } 59 | 60 | @classmethod 61 | def get_defaults(cls, n): 62 | if n in cls._defaults: 63 | return cls._defaults[n] 64 | else: 65 | return "Unrecognized attribute name '" + n + "'" 66 | 67 | #---------------------------------------------------# 68 | # 初始化YOLO 69 | #---------------------------------------------------# 70 | def __init__(self, **kwargs): 71 | self.__dict__.update(self._defaults) 72 | for name, value in kwargs.items(): 73 | setattr(self, name, value) 74 | self._defaults[name] = value 75 | 76 | #---------------------------------------------------# 77 | # 获得种类和先验框的数量 78 | #---------------------------------------------------# 79 | self.class_names, self.num_classes = get_classes(self.classes_path) 80 | self.anchors, self.num_anchors = get_anchors(self.anchors_path) 81 | self.bbox_util = DecodeBox(self.anchors, self.num_classes, (self.input_shape[0], self.input_shape[1]), self.anchors_mask) 82 | 83 | #---------------------------------------------------# 84 | # 画框设置不同的颜色 85 | #---------------------------------------------------# 86 | hsv_tuples = [(x / self.num_classes, 1., 1.) for x in range(self.num_classes)] 87 | self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) 88 | self.colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors)) 89 | self.generate() 90 | 91 | show_config(**self._defaults) 92 | 93 | #---------------------------------------------------# 94 | # 生成模型 95 | #---------------------------------------------------# 96 | def generate(self, onnx=False): 97 | #---------------------------------------------------# 98 | # 建立yolov3模型,载入yolov3模型的权重 99 | #---------------------------------------------------# 100 | self.net = YoloBody(self.anchors_mask, self.num_classes) 101 | device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') 102 | self.net.load_state_dict(torch.load(self.model_path, map_location=device)) 103 | self.net = self.net.eval() 104 | print('{} model, anchors, and classes loaded.'.format(self.model_path)) 105 | if not onnx: 106 | if self.cuda: 107 | self.net = nn.DataParallel(self.net) 108 | self.net = self.net.cuda() 109 | 110 | #---------------------------------------------------# 111 | # 检测图片 112 | #---------------------------------------------------# 113 | def detect_image(self, image, crop = False, count = False): 114 | image_shape = np.array(np.shape(image)[0:2]) 115 | #---------------------------------------------------------# 116 | # 在这里将图像转换成RGB图像,防止灰度图在预测时报错。 117 | # 代码仅仅支持RGB图像的预测,所有其它类型的图像都会转化成RGB 118 | #---------------------------------------------------------# 119 | image = cvtColor(image) 120 | #---------------------------------------------------------# 121 | # 给图像增加灰条,实现不失真的resize 122 | # 也可以直接resize进行识别 123 | #---------------------------------------------------------# 124 | image_data = resize_image(image, (self.input_shape[1],self.input_shape[0]), self.letterbox_image) 125 | #---------------------------------------------------------# 126 | # 添加上batch_size维度 127 | #---------------------------------------------------------# 128 | image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, dtype='float32')), (2, 0, 1)), 0) 129 | 130 | with torch.no_grad(): 131 | images = torch.from_numpy(image_data) 132 | if self.cuda: 133 | images = images.cuda() 134 | #---------------------------------------------------------# 135 | # 将图像输入网络当中进行预测! 136 | #---------------------------------------------------------# 137 | outputs = self.net(images) 138 | outputs = self.bbox_util.decode_box(outputs) 139 | #---------------------------------------------------------# 140 | # 将预测框进行堆叠,然后进行非极大抑制 141 | #---------------------------------------------------------# 142 | results = self.bbox_util.non_max_suppression(torch.cat(outputs, 1), self.num_classes, self.input_shape, 143 | image_shape, self.letterbox_image, conf_thres = self.confidence, nms_thres = self.nms_iou) 144 | 145 | if results[0] is None: 146 | return image 147 | 148 | top_label = np.array(results[0][:, 6], dtype = 'int32') 149 | top_conf = results[0][:, 4] * results[0][:, 5] 150 | top_boxes = results[0][:, :4] 151 | #---------------------------------------------------------# 152 | # 设置字体与边框厚度 153 | #---------------------------------------------------------# 154 | font = ImageFont.truetype(font='model_data/simhei.ttf', size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32')) 155 | thickness = int(max((image.size[0] + image.size[1]) // np.mean(self.input_shape), 1)) 156 | #---------------------------------------------------------# 157 | # 计数 158 | #---------------------------------------------------------# 159 | if count: 160 | print("top_label:", top_label) 161 | classes_nums = np.zeros([self.num_classes]) 162 | for i in range(self.num_classes): 163 | num = np.sum(top_label == i) 164 | if num > 0: 165 | print(self.class_names[i], " : ", num) 166 | classes_nums[i] = num 167 | print("classes_nums:", classes_nums) 168 | #---------------------------------------------------------# 169 | # 是否进行目标的裁剪 170 | #---------------------------------------------------------# 171 | if crop: 172 | for i, c in list(enumerate(top_label)): 173 | top, left, bottom, right = top_boxes[i] 174 | top = max(0, np.floor(top).astype('int32')) 175 | left = max(0, np.floor(left).astype('int32')) 176 | bottom = min(image.size[1], np.floor(bottom).astype('int32')) 177 | right = min(image.size[0], np.floor(right).astype('int32')) 178 | 179 | dir_save_path = "img_crop" 180 | if not os.path.exists(dir_save_path): 181 | os.makedirs(dir_save_path) 182 | crop_image = image.crop([left, top, right, bottom]) 183 | crop_image.save(os.path.join(dir_save_path, "crop_" + str(i) + ".png"), quality=95, subsampling=0) 184 | print("save crop_" + str(i) + ".png to " + dir_save_path) 185 | #---------------------------------------------------------# 186 | # 图像绘制 187 | #---------------------------------------------------------# 188 | for i, c in list(enumerate(top_label)): 189 | predicted_class = self.class_names[int(c)] 190 | box = top_boxes[i] 191 | score = top_conf[i] 192 | 193 | top, left, bottom, right = box 194 | 195 | top = max(0, np.floor(top).astype('int32')) 196 | left = max(0, np.floor(left).astype('int32')) 197 | bottom = min(image.size[1], np.floor(bottom).astype('int32')) 198 | right = min(image.size[0], np.floor(right).astype('int32')) 199 | 200 | label = '{} {:.2f}'.format(predicted_class, score) 201 | draw = ImageDraw.Draw(image) 202 | label_size = draw.textsize(label, font) 203 | label = label.encode('utf-8') 204 | print(label, top, left, bottom, right) 205 | 206 | if top - label_size[1] >= 0: 207 | text_origin = np.array([left, top - label_size[1]]) 208 | else: 209 | text_origin = np.array([left, top + 1]) 210 | 211 | for i in range(thickness): 212 | draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[c]) 213 | draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c]) 214 | draw.text(text_origin, str(label,'UTF-8'), fill=(0, 0, 0), font=font) 215 | del draw 216 | 217 | return image 218 | 219 | def get_FPS(self, image, test_interval): 220 | image_shape = np.array(np.shape(image)[0:2]) 221 | #---------------------------------------------------------# 222 | # 在这里将图像转换成RGB图像,防止灰度图在预测时报错。 223 | # 代码仅仅支持RGB图像的预测,所有其它类型的图像都会转化成RGB 224 | #---------------------------------------------------------# 225 | image = cvtColor(image) 226 | #---------------------------------------------------------# 227 | # 给图像增加灰条,实现不失真的resize 228 | # 也可以直接resize进行识别 229 | #---------------------------------------------------------# 230 | image_data = resize_image(image, (self.input_shape[1],self.input_shape[0]), self.letterbox_image) 231 | #---------------------------------------------------------# 232 | # 添加上batch_size维度 233 | #---------------------------------------------------------# 234 | image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, dtype='float32')), (2, 0, 1)), 0) 235 | 236 | with torch.no_grad(): 237 | images = torch.from_numpy(image_data) 238 | if self.cuda: 239 | images = images.cuda() 240 | #---------------------------------------------------------# 241 | # 将图像输入网络当中进行预测! 242 | #---------------------------------------------------------# 243 | outputs = self.net(images) 244 | outputs = self.bbox_util.decode_box(outputs) 245 | #---------------------------------------------------------# 246 | # 将预测框进行堆叠,然后进行非极大抑制 247 | #---------------------------------------------------------# 248 | results = self.bbox_util.non_max_suppression(torch.cat(outputs, 1), self.num_classes, self.input_shape, 249 | image_shape, self.letterbox_image, conf_thres=self.confidence, nms_thres=self.nms_iou) 250 | 251 | t1 = time.time() 252 | for _ in range(test_interval): 253 | with torch.no_grad(): 254 | #---------------------------------------------------------# 255 | # 将图像输入网络当中进行预测! 256 | #---------------------------------------------------------# 257 | outputs = self.net(images) 258 | outputs = self.bbox_util.decode_box(outputs) 259 | #---------------------------------------------------------# 260 | # 将预测框进行堆叠,然后进行非极大抑制 261 | #---------------------------------------------------------# 262 | results = self.bbox_util.non_max_suppression(torch.cat(outputs, 1), self.num_classes, self.input_shape, 263 | image_shape, self.letterbox_image, conf_thres=self.confidence, nms_thres=self.nms_iou) 264 | 265 | t2 = time.time() 266 | tact_time = (t2 - t1) / test_interval 267 | return tact_time 268 | 269 | def detect_heatmap(self, image, heatmap_save_path): 270 | import cv2 271 | import matplotlib.pyplot as plt 272 | def sigmoid(x): 273 | y = 1.0 / (1.0 + np.exp(-x)) 274 | return y 275 | #---------------------------------------------------------# 276 | # 在这里将图像转换成RGB图像,防止灰度图在预测时报错。 277 | # 代码仅仅支持RGB图像的预测,所有其它类型的图像都会转化成RGB 278 | #---------------------------------------------------------# 279 | image = cvtColor(image) 280 | #---------------------------------------------------------# 281 | # 给图像增加灰条,实现不失真的resize 282 | # 也可以直接resize进行识别 283 | #---------------------------------------------------------# 284 | image_data = resize_image(image, (self.input_shape[1],self.input_shape[0]), self.letterbox_image) 285 | #---------------------------------------------------------# 286 | # 添加上batch_size维度 287 | #---------------------------------------------------------# 288 | image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, dtype='float32')), (2, 0, 1)), 0) 289 | 290 | with torch.no_grad(): 291 | images = torch.from_numpy(image_data) 292 | if self.cuda: 293 | images = images.cuda() 294 | #---------------------------------------------------------# 295 | # 将图像输入网络当中进行预测! 296 | #---------------------------------------------------------# 297 | outputs = self.net(images) 298 | 299 | plt.imshow(image, alpha=1) 300 | plt.axis('off') 301 | mask = np.zeros((image.size[1], image.size[0])) 302 | for sub_output in outputs: 303 | sub_output = sub_output.cpu().numpy() 304 | b, c, h, w = np.shape(sub_output) 305 | sub_output = np.transpose(np.reshape(sub_output, [b, 3, -1, h, w]), [0, 3, 4, 1, 2])[0] 306 | score = np.max(sigmoid(sub_output[..., 4]), -1) 307 | score = cv2.resize(score, (image.size[0], image.size[1])) 308 | normed_score = (score * 255).astype('uint8') 309 | mask = np.maximum(mask, normed_score) 310 | 311 | plt.imshow(mask, alpha=0.5, interpolation='nearest', cmap="jet") 312 | 313 | plt.axis('off') 314 | plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) 315 | plt.margins(0, 0) 316 | plt.savefig(heatmap_save_path, dpi=200, bbox_inches='tight', pad_inches = -0.1) 317 | print("Save to the " + heatmap_save_path) 318 | plt.show() 319 | 320 | def convert_to_onnx(self, simplify, model_path): 321 | import onnx 322 | self.generate(onnx=True) 323 | 324 | im = torch.zeros(1, 3, *self.input_shape).to('cpu') # image size(1, 3, 512, 512) BCHW 325 | input_layer_names = ["images"] 326 | output_layer_names = ["output"] 327 | 328 | # Export the model 329 | print(f'Starting export with onnx {onnx.__version__}.') 330 | torch.onnx.export(self.net, 331 | im, 332 | f = model_path, 333 | verbose = False, 334 | opset_version = 12, 335 | training = torch.onnx.TrainingMode.EVAL, 336 | do_constant_folding = True, 337 | input_names = input_layer_names, 338 | output_names = output_layer_names, 339 | dynamic_axes = None) 340 | 341 | # Checks 342 | model_onnx = onnx.load(model_path) # load onnx model 343 | onnx.checker.check_model(model_onnx) # check onnx model 344 | 345 | # Simplify onnx 346 | if simplify: 347 | import onnxsim 348 | print(f'Simplifying with onnx-simplifier {onnxsim.__version__}.') 349 | model_onnx, check = onnxsim.simplify( 350 | model_onnx, 351 | dynamic_input_shape=False, 352 | input_shapes=None) 353 | assert check, 'assert check failed' 354 | onnx.save(model_onnx, model_path) 355 | 356 | print('Onnx model save as {}'.format(model_path)) 357 | 358 | def get_map_txt(self, image_id, image, class_names, map_out_path): 359 | f = open(os.path.join(map_out_path, "detection-results/"+image_id+".txt"),"w") 360 | image_shape = np.array(np.shape(image)[0:2]) 361 | #---------------------------------------------------------# 362 | # 在这里将图像转换成RGB图像,防止灰度图在预测时报错。 363 | # 代码仅仅支持RGB图像的预测,所有其它类型的图像都会转化成RGB 364 | #---------------------------------------------------------# 365 | image = cvtColor(image) 366 | #---------------------------------------------------------# 367 | # 给图像增加灰条,实现不失真的resize 368 | # 也可以直接resize进行识别 369 | #---------------------------------------------------------# 370 | image_data = resize_image(image, (self.input_shape[1],self.input_shape[0]), self.letterbox_image) 371 | #---------------------------------------------------------# 372 | # 添加上batch_size维度 373 | #---------------------------------------------------------# 374 | image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, dtype='float32')), (2, 0, 1)), 0) 375 | 376 | with torch.no_grad(): 377 | images = torch.from_numpy(image_data) 378 | if self.cuda: 379 | images = images.cuda() 380 | #---------------------------------------------------------# 381 | # 将图像输入网络当中进行预测! 382 | #---------------------------------------------------------# 383 | outputs = self.net(images) 384 | outputs = self.bbox_util.decode_box(outputs) 385 | #---------------------------------------------------------# 386 | # 将预测框进行堆叠,然后进行非极大抑制 387 | #---------------------------------------------------------# 388 | results = self.bbox_util.non_max_suppression(torch.cat(outputs, 1), self.num_classes, self.input_shape, 389 | image_shape, self.letterbox_image, conf_thres = self.confidence, nms_thres = self.nms_iou) 390 | 391 | if results[0] is None: 392 | return 393 | 394 | top_label = np.array(results[0][:, 6], dtype = 'int32') 395 | top_conf = results[0][:, 4] * results[0][:, 5] 396 | top_boxes = results[0][:, :4] 397 | 398 | for i, c in list(enumerate(top_label)): 399 | predicted_class = self.class_names[int(c)] 400 | box = top_boxes[i] 401 | score = str(top_conf[i]) 402 | 403 | top, left, bottom, right = box 404 | if predicted_class not in class_names: 405 | continue 406 | 407 | f.write("%s %s %s %s %s %s\n" % (predicted_class, score[:6], str(int(left)), str(int(top)), str(int(right)),str(int(bottom)))) 408 | 409 | f.close() 410 | return 411 | -------------------------------------------------------------------------------- /常见问题汇总.md: -------------------------------------------------------------------------------- 1 | 问题汇总的博客地址为[https://blog.csdn.net/weixin_44791964/article/details/107517428](https://blog.csdn.net/weixin_44791964/article/details/107517428)。 2 | 3 | # 问题汇总 4 | ## 1、下载问题 5 | ### a、代码下载 6 | **问:up主,可以给我发一份代码吗,代码在哪里下载啊? 7 | 答:Github上的地址就在视频简介里。复制一下就能进去下载了。** 8 | 9 | **问:up主,为什么我下载的代码提示压缩包损坏? 10 | 答:重新去Github下载。** 11 | 12 | **问:up主,为什么我下载的代码和你在视频以及博客上的代码不一样? 13 | 答:我常常会对代码进行更新,最终以实际的代码为准。** 14 | 15 | ### b、 权值下载 16 | **问:up主,为什么我下载的代码里面,model_data下面没有.pth或者.h5文件? 17 | 答:我一般会把权值上传到Github和百度网盘,在GITHUB的README里面就能找到。** 18 | 19 | ### c、 数据集下载 20 | **问:up主,XXXX数据集在哪里下载啊? 21 | 答:一般数据集的下载地址我会放在README里面,基本上都有,没有的话请及时联系我添加,直接发github的issue即可**。 22 | 23 | ## 2、环境配置问题 24 | ### a、20系列及以下显卡环境配置 25 | **pytorch代码对应的pytorch版本为1.2,博客地址对应**[https://blog.csdn.net/weixin_44791964/article/details/106037141](https://blog.csdn.net/weixin_44791964/article/details/106037141)。 26 | 27 | **keras代码对应的tensorflow版本为1.13.2,keras版本是2.1.5,博客地址对应**[https://blog.csdn.net/weixin_44791964/article/details/104702142](https://blog.csdn.net/weixin_44791964/article/details/104702142)。 28 | 29 | **tf2代码对应的tensorflow版本为2.2.0,无需安装keras,博客地址对应**[https://blog.csdn.net/weixin_44791964/article/details/109161493](https://blog.csdn.net/weixin_44791964/article/details/109161493)。 30 | 31 | **问:你的代码某某某版本的tensorflow和pytorch能用嘛? 32 | 答:最好按照我推荐的配置,配置教程也有!其它版本的我没有试过!可能出现问题但是一般问题不大。仅需要改少量代码即可。** 33 | 34 | ### b、30系列显卡环境配置 35 | 30系显卡由于框架更新不可使用上述环境配置教程。 36 | 当前我已经测试的可以用的30显卡配置如下: 37 | **pytorch代码对应的pytorch版本为1.7.0,cuda为11.0,cudnn为8.0.5,博客地址对应**[https://blog.csdn.net/weixin_44791964/article/details/120668551](https://blog.csdn.net/weixin_44791964/article/details/120668551)。 38 | 39 | **keras代码无法在win10下配置cuda11,在ubuntu下可以百度查询一下,配置tensorflow版本为1.15.4,keras版本是2.1.5或者2.3.1(少量函数接口不同,代码可能还需要少量调整。)** 40 | 41 | **tf2代码对应的tensorflow版本为2.4.0,cuda为11.0,cudnn为8.0.5,博客地址对应为**[https://blog.csdn.net/weixin_44791964/article/details/120657664](https://blog.csdn.net/weixin_44791964/article/details/120657664)。 42 | 43 | ### c、CPU环境配置 44 | **pytorch代码对应的pytorch-cpu版本为1.2,博客地址对应**[https://blog.csdn.net/weixin_44791964/article/details/120655098](https://blog.csdn.net/weixin_44791964/article/details/120655098) 45 | 46 | **keras代码对应的tensorflow-cpu版本为1.13.2,keras版本是2.1.5,博客地址对应**[https://blog.csdn.net/weixin_44791964/article/details/120653717](https://blog.csdn.net/weixin_44791964/article/details/120653717)。 47 | 48 | **tf2代码对应的tensorflow-cpu版本为2.2.0,无需安装keras,博客地址对应**[https://blog.csdn.net/weixin_44791964/article/details/120656291](https://blog.csdn.net/weixin_44791964/article/details/120656291)。 49 | 50 | 51 | ### d、GPU利用问题与环境使用问题 52 | **问:为什么我安装了tensorflow-gpu但是却没用利用GPU进行训练呢? 53 | 答:确认tensorflow-gpu已经装好,利用pip list查看tensorflow版本,然后查看任务管理器或者利用nvidia命令看看是否使用了gpu进行训练,任务管理器的话要看显存使用情况。** 54 | 55 | **问:up主,我好像没有在用gpu进行训练啊,怎么看是不是用了GPU进行训练? 56 | 答:查看是否使用GPU进行训练一般使用NVIDIA在命令行的查看命令。在windows电脑中打开cmd然后利用nvidia-smi指令查看GPU利用情况** 57 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/f88ef794c9a341918f000eb2b1c67af6.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQnViYmxpaWlpbmc=,size_20,color_FFFFFF,t_70,g_se,x_16) 58 | **如果要一定看任务管理器的话,请看性能部分GPU的显存是否利用,或者查看任务管理器的Cuda,而非Copy。** 59 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201013234241524.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5MTk2NA==,size_16,color_FFFFFF,t_70#pic_center) 60 | 61 | ### e、DLL load failed: 找不到指定的模块 62 | **问:出现如下错误** 63 | ```python 64 | Traceback (most recent call last): 65 | File "C:\Users\focus\Anaconda3\ana\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in 66 | from tensorflow.python.pywrap_tensorflow_internal import * 67 | File "C:\Users\focus\Anaconda3\ana\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in 68 | pywrap_tensorflow_internal = swig_import_helper() 69 | File "C:\Users\focus\Anaconda3\ana\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper 70 | _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description) 71 | File "C:\Users\focus\Anaconda3\ana\envs\tensorflow-gpu\lib\imp.py", line 243, in load_modulereturn load_dynamic(name, filename, file) 72 | File "C:\Users\focus\Anaconda3\ana\envs\tensorflow-gpu\lib\imp.py", line 343, in load_dynamic 73 | return _load(spec) 74 | ImportError: DLL load failed: 找不到指定的模块。 75 | ``` 76 | **答:如果没重启过就重启一下,否则重新按照步骤安装,还无法解决则把你的GPU、CUDA、CUDNN、TF版本以及PYTORCH版本私聊告诉我。** 77 | 78 | ### f、no module问题(no module name utils.utils、no module named 'matplotlib' ) 79 | **问:为什么提示说no module name utils.utils(no module name nets.yolo、no module name nets.ssd等一系列问题)啊? 80 | 答:utils并不需要用pip装,它就在我上传的仓库的根目录,出现这个问题的原因是根目录不对,查查相对目录和根目录的概念。查了基本上就明白了。** 81 | 82 | **问:为什么提示说no module name matplotlib(no module name PIL,no module name cv2等等)? 83 | 答:这个库没安装打开命令行安装就好。pip install matplotlib** 84 | 85 | **问:为什么我已经用pip装了opencv(pillow、matplotlib等),还是提示no module name cv2? 86 | 答:没有激活环境装,要激活对应的conda环境进行安装才可以正常使用** 87 | 88 | **问:为什么提示说No module named 'torch' ? 89 | 答:其实我也真的很想知道为什么会有这个问题……这个pytorch没装是什么情况?一般就俩情况,一个是真的没装,还有一个是装到其它环境了,当前激活的环境不是自己装的环境。** 90 | 91 | **问:为什么提示说No module named 'tensorflow' ? 92 | 答:同上。** 93 | 94 | ### g、cuda安装失败问题 95 | 一般cuda安装前需要安装Visual Studio,装个2017版本即可。 96 | 97 | ### h、Ubuntu系统问题 98 | **所有代码在Ubuntu下可以使用,我两个系统都试过。** 99 | 100 | ### i、VSCODE提示错误的问题 101 | **问:为什么在VSCODE里面提示一大堆的错误啊? 102 | 答:我也提示一大堆的错误,但是不影响,是VSCODE的问题,如果不想看错误的话就装Pycharm。 103 | 最好将设置里面的Python:Language Server,调整为Pylance。** 104 | 105 | ### j、使用cpu进行训练与预测的问题 106 | **对于keras和tf2的代码而言,如果想用cpu进行训练和预测,直接装cpu版本的tensorflow就可以了。** 107 | 108 | **对于pytorch的代码而言,如果想用cpu进行训练和预测,需要将cuda=True修改成cuda=False。** 109 | 110 | ### k、tqdm没有pos参数问题 111 | **问:运行代码提示'tqdm' object has no attribute 'pos'。 112 | 答:重装tqdm,换个版本就可以了。** 113 | 114 | ### l、提示decode(“utf-8”)的问题 115 | **由于h5py库的更新,安装过程中会自动安装h5py=3.0.0以上的版本,会导致decode("utf-8")的错误! 116 | 各位一定要在安装完tensorflow后利用命令装h5py=2.10.0!** 117 | ``` 118 | pip install h5py==2.10.0 119 | ``` 120 | 121 | ### m、提示TypeError: __array__() takes 1 positional argument but 2 were given错误 122 | 可以修改pillow版本解决。 123 | ``` 124 | pip install pillow==8.2.0 125 | ``` 126 | ### n、如何查看当前cuda和cudnn 127 | **window下cuda版本查看方式如下: 128 | 1、打开cmd窗口。 129 | 2、输入nvcc -V。 130 | 3、Cuda compilation tools, release XXXXXXXX中的XXXXXXXX即cuda版本。** 131 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/0389ea35107a408a80ab5cb6590d5a74.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQnViYmxpaWlpbmc=,size_20,color_FFFFFF,t_70,g_se,x_16) 132 | window下cudnn版本查看方式如下: 133 | 1、进入cuda安装目录,进入incude文件夹。 134 | 2、找到cudnn.h文件。 135 | 3、右键文本打开,下拉,看到#define处可获得cudnn版本。 136 | ```python 137 | #define CUDNN_MAJOR 7 138 | #define CUDNN_MINOR 4 139 | #define CUDNN_PATCHLEVEL 1 140 | ``` 141 | 代表cudnn为7.4.1。 142 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/7a86b68b17c84feaa6fa95780d4ae4b4.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQnViYmxpaWlpbmc=,size_20,color_FFFFFF,t_70,g_se,x_16) 143 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/81bb7c3e13cc492292530e4b69df86a9.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQnViYmxpaWlpbmc=,size_20,color_FFFFFF,t_70,g_se,x_16) 144 | 145 | ### o、为什么按照你的环境配置后还是不能使用 146 | **问:up主,为什么我按照你的环境配置后还是不能使用? 147 | 答:请把你的GPU、CUDA、CUDNN、TF版本以及PYTORCH版本B站私聊告诉我。** 148 | 149 | ### p、其它问题 150 | **问:为什么提示TypeError: cat() got an unexpected keyword argument 'axis',Traceback (most recent call last),AttributeError: 'Tensor' object has no attribute 'bool'? 151 | 答:这是版本问题,建议使用torch1.2以上版本** 152 | 153 | **其它有很多稀奇古怪的问题,很多是版本问题,建议按照我的视频教程安装Keras和tensorflow。比如装的是tensorflow2,就不用问我说为什么我没法运行Keras-yolo啥的。那是必然不行的。** 154 | 155 | ## 3、目标检测库问题汇总(人脸检测和分类库也可参考) 156 | ### a、shape不匹配问题。 157 | #### 1)、训练时shape不匹配问题。 158 | **问:up主,为什么运行train.py会提示shape不匹配啊? 159 | 答:在keras环境中,因为你训练的种类和原始的种类不同,网络结构会变化,所以最尾部的shape会有少量不匹配。** 160 | 161 | #### 2)、预测时shape不匹配问题。 162 | **问:为什么我运行predict.py会提示我说shape不匹配呀。** 163 | ##### i、copying a param with shape torch.Size([75, 704, 1, 1]) from checkpoint 164 | 在Pytorch里面是这样的: 165 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200722171631901.png) 166 | ##### ii、Shapes are [1,1,1024,75] and [255,1024,1,1]. for 'Assign_360' (op: 'Assign') with input shapes: [1,1,1024,75], [255,1024,1,1]. 167 | 在Keras里面是这样的: 168 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200722171523380.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5MTk2NA==,size_16,color_FFFFFF,t_70) 169 | **答:原因主要有仨: 170 | 1、训练的classes_path没改,就开始训练了。 171 | 2、训练的model_path没改。 172 | 3、训练的classes_path没改。 173 | 请检查清楚了!确定自己所用的model_path和classes_path是对应的!训练的时候用到的num_classes或者classes_path也需要检查!** 174 | 175 | ### b、显存不足问题(OOM、RuntimeError: CUDA out of memory)。 176 | **问:为什么我运行train.py下面的命令行闪的贼快,还提示OOM啥的? 177 | 答:这是在keras中出现的,爆显存了,可以改小batch_size,SSD的显存占用率是最小的,建议用SSD; 178 | 2G显存:SSD、YOLOV4-TINY 179 | 4G显存:YOLOV3 180 | 6G显存:YOLOV4、Retinanet、M2det、Efficientdet、Faster RCNN等 181 | 8G+显存:随便选吧。** 182 | **需要注意的是,受到BatchNorm2d影响,batch_size不可为1,至少为2。** 183 | 184 | **问:为什么提示 RuntimeError: CUDA out of memory. Tried to allocate 52.00 MiB (GPU 0; 15.90 GiB total capacity; 14.85 GiB already allocated; 51.88 MiB free; 15.07 GiB reserved in total by PyTorch)? 185 | 答:这是pytorch中出现的,爆显存了,同上。** 186 | 187 | **问:为什么我显存都没利用,就直接爆显存了? 188 | 答:都爆显存了,自然就不利用了,模型没有开始训练。** 189 | ### c、为什么要进行冻结训练与解冻训练,不进行行吗? 190 | **问:为什么要冻结训练和解冻训练呀? 191 | 答:可以不进行,本质上是为了保证性能不足的同学的训练,如果电脑性能完全不够,可以将Freeze_Epoch和UnFreeze_Epoch设置成一样,只进行冻结训练。** 192 | 193 | **同时这也是迁移学习的思想,因为神经网络主干特征提取部分所提取到的特征是通用的,我们冻结起来训练可以加快训练效率,也可以防止权值被破坏。** 194 | 在冻结阶段,模型的主干被冻结了,特征提取网络不发生改变。占用的显存较小,仅对网络进行微调。 195 | 在解冻阶段,模型的主干不被冻结了,特征提取网络会发生改变。占用的显存较大,网络所有的参数都会发生改变。 196 | 197 | ### d、我的LOSS好大啊,有问题吗?(我的LOSS好小啊,有问题吗?) 198 | **问:为什么我的网络不收敛啊,LOSS是XXXX。 199 | 答:不同网络的LOSS不同,LOSS只是一个参考指标,用于查看网络是否收敛,而非评价网络好坏,我的yolo代码都没有归一化,所以LOSS值看起来比较高,LOSS的值不重要,重要的是是否在变小,预测是否有效果。** 200 | 201 | ### e、为什么我训练出来的模型没有预测结果? 202 | **问:为什么我的训练效果不好?预测了没有框(框不准)。 203 | 答:** 204 | 考虑几个问题: 205 | 1、目标信息问题,查看2007_train.txt文件是否有目标信息,没有的话请修改voc_annotation.py。 206 | 2、数据集问题,小于500的自行考虑增加数据集,同时测试不同的模型,确认数据集是好的。 207 | 3、是否解冻训练,如果数据集分布与常规画面差距过大需要进一步解冻训练,调整主干,加强特征提取能力。 208 | 4、网络问题,比如SSD不适合小目标,因为先验框固定了。 209 | 5、训练时长问题,有些同学只训练了几代表示没有效果,按默认参数训练完。 210 | 6、确认自己是否按照步骤去做了,如果比如voc_annotation.py里面的classes是否修改了等。 211 | 7、不同网络的LOSS不同,LOSS只是一个参考指标,用于查看网络是否收敛,而非评价网络好坏,LOSS的值不重要,重要的是是否收敛。 212 | 8、是否修改了网络的主干,如果修改了没有预训练权重,网络不容易收敛,自然效果不好。 213 | 214 | ### f、为什么我计算出来的map是0? 215 | **问:为什么我的训练效果不好?没有map? 216 | 答:** 217 | 首先尝试利用predict.py预测一下,如果有效果的话应该是get_map.py里面的classes_path设置错误。如果没有预测结果的话,解决方法同e问题,对下面几点进行检查: 218 | 1、目标信息问题,查看2007_train.txt文件是否有目标信息,没有的话请修改voc_annotation.py。 219 | 2、数据集问题,小于500的自行考虑增加数据集,同时测试不同的模型,确认数据集是好的。 220 | 3、是否解冻训练,如果数据集分布与常规画面差距过大需要进一步解冻训练,调整主干,加强特征提取能力。 221 | 4、网络问题,比如SSD不适合小目标,因为先验框固定了。 222 | 5、训练时长问题,有些同学只训练了几代表示没有效果,按默认参数训练完。 223 | 6、确认自己是否按照步骤去做了,如果比如voc_annotation.py里面的classes是否修改了等。 224 | 7、不同网络的LOSS不同,LOSS只是一个参考指标,用于查看网络是否收敛,而非评价网络好坏,LOSS的值不重要,重要的是是否收敛。 225 | 8、是否修改了网络的主干,如果修改了没有预训练权重,网络不容易收敛,自然效果不好。 226 | 227 | ### g、gbk编码错误('gbk' codec can't decode byte)。 228 | **问:我怎么出现了gbk什么的编码错误啊:** 229 | ```python 230 | UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 446: illegal multibyte sequence 231 | ``` 232 | **答:标签和路径不要使用中文,如果一定要使用中文,请注意处理的时候编码的问题,改成打开文件的encoding方式改为utf-8。** 233 | 234 | ### h、我的图片是xxx*xxx的分辨率的,可以用吗? 235 | **问:我的图片是xxx*xxx的分辨率的,可以用吗!** 236 | **答:可以用,代码里面会自动进行resize与数据增强。** 237 | 238 | ### i、我想进行数据增强!怎么增强? 239 | **问:我想要进行数据增强!怎么做呢?** 240 | **答:可以用,代码里面会自动进行resize与数据增强。** 241 | 242 | ### j、多GPU训练。 243 | **问:怎么进行多GPU训练? 244 | 答:pytorch的大多数代码可以直接使用gpu训练,keras的话直接百度就好了,实现并不复杂,我没有多卡没法详细测试,还需要各位同学自己努力了。** 245 | 246 | ### k、能不能训练灰度图? 247 | **问:能不能训练灰度图(预测灰度图)啊? 248 | 答:我的大多数库会将灰度图转化成RGB进行训练和预测,如果遇到代码不能训练或者预测灰度图的情况,可以尝试一下在get_random_data里面将Image.open后的结果转换成RGB,预测的时候也这样试试。(仅供参考)** 249 | 250 | ### l、断点续练问题。 251 | **问:我已经训练过几个世代了,能不能从这个基础上继续开始训练 252 | 答:可以,你在训练前,和载入预训练权重一样载入训练过的权重就行了。一般训练好的权重会保存在logs文件夹里面,将model_path修改成你要开始的权值的路径即可。** 253 | 254 | ### m、我要训练其它的数据集,预训练权重能不能用? 255 | **问:如果我要训练其它的数据集,预训练权重要怎么办啊?** 256 | **答:数据的预训练权重对不同数据集是通用的,因为特征是通用的,预训练权重对于99%的情况都必须要用,不用的话权值太过随机,特征提取效果不明显,网络训练的结果也不会好。** 257 | 258 | ### n、网络如何从0开始训练? 259 | **问:我要怎么不使用预训练权重啊? 260 | 答:看一看注释、大多数代码是model_path = '',Freeze_Train = Fasle**,如果设置model_path无用,**那么把载入预训练权重的代码注释了就行。** 261 | 262 | ### o、为什么从0开始训练效果这么差(修改了网络主干,效果不好怎么办)? 263 | **问:为什么我不使用预训练权重效果这么差啊? 264 | 答:因为随机初始化的权值不好,提取的特征不好,也就导致了模型训练的效果不好,voc07+12、coco+voc07+12效果都不一样,预训练权重还是非常重要的。** 265 | 266 | **问:up,我修改了网络,预训练权重还能用吗? 267 | 答:修改了主干的话,如果不是用的现有的网络,基本上预训练权重是不能用的,要么就自己判断权值里卷积核的shape然后自己匹配,要么只能自己预训练去了;修改了后半部分的话,前半部分的主干部分的预训练权重还是可以用的,如果是pytorch代码的话,需要自己修改一下载入权值的方式,判断shape后载入,如果是keras代码,直接by_name=True,skip_mismatch=True即可。** 268 | 权值匹配的方式可以参考如下: 269 | ```python 270 | # 加快模型训练的效率 271 | print('Loading weights into state dict...') 272 | device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') 273 | model_dict = model.state_dict() 274 | pretrained_dict = torch.load(model_path, map_location=device) 275 | a = {} 276 | for k, v in pretrained_dict.items(): 277 | try: 278 | if np.shape(model_dict[k]) == np.shape(v): 279 | a[k]=v 280 | except: 281 | pass 282 | model_dict.update(a) 283 | model.load_state_dict(model_dict) 284 | print('Finished!') 285 | ``` 286 | 287 | **问:为什么从0开始训练效果这么差(我修改了网络主干,效果不好怎么办)? 288 | 答:一般来讲,网络从0开始的训练效果会很差,因为权值太过随机,特征提取效果不明显,因此非常、非常、非常不建议大家从0开始训练!如果一定要从0开始,可以了解imagenet数据集,首先训练分类模型,获得网络的主干部分权值,分类模型的 主干部分 和该模型通用,基于此进行训练。 289 | 网络修改了主干之后也是同样的问题,随机的权值效果很差。** 290 | 291 | **问:怎么在模型上从0开始训练? 292 | 答:在算力不足与调参能力不足的情况下从0开始训练毫无意义。模型特征提取能力在随机初始化参数的情况下非常差。没有好的参数调节能力和算力,无法使得网络正常收敛。** 293 | 如果一定要从0开始,那么训练的时候请注意几点: 294 | - 不载入预训练权重。 295 | - 不要进行冻结训练,注释冻结模型的代码。 296 | 297 | **问:为什么我不使用预训练权重效果这么差啊? 298 | 答:因为随机初始化的权值不好,提取的特征不好,也就导致了模型训练的效果不好,voc07+12、coco+voc07+12效果都不一样,预训练权重还是非常重要的。** 299 | 300 | ### p、你的权值都是哪里来的? 301 | **问:如果网络不能从0开始训练的话你的权值哪里来的? 302 | 答:有些权值是官方转换过来的,有些权值是自己训练出来的,我用到的主干的imagenet的权值都是官方的。** 303 | 304 | ### q、视频检测与摄像头检测 305 | **问:怎么用摄像头检测呀? 306 | 答:predict.py修改参数可以进行摄像头检测,也有视频详细解释了摄像头检测的思路。** 307 | 308 | **问:怎么用视频检测呀? 309 | 答:同上** 310 | 311 | ### r、如何保存检测出的图片 312 | **问:检测完的图片怎么保存? 313 | 答:一般目标检测用的是Image,所以查询一下PIL库的Image如何进行保存。详细看看predict.py文件的注释。** 314 | 315 | **问:怎么用视频保存呀? 316 | 答:详细看看predict.py文件的注释。** 317 | 318 | ### s、遍历问题 319 | **问:如何对一个文件夹的图片进行遍历? 320 | 答:一般使用os.listdir先找出文件夹里面的所有图片,然后根据predict.py文件里面的执行思路检测图片就行了,详细看看predict.py文件的注释。** 321 | 322 | **问:如何对一个文件夹的图片进行遍历?并且保存。 323 | 答:遍历的话一般使用os.listdir先找出文件夹里面的所有图片,然后根据predict.py文件里面的执行思路检测图片就行了。保存的话一般目标检测用的是Image,所以查询一下PIL库的Image如何进行保存。如果有些库用的是cv2,那就是查一下cv2怎么保存图片。详细看看predict.py文件的注释。** 324 | 325 | ### t、路径问题(No such file or directory、StopIteration: [Errno 13] Permission denied: 'XXXXXX') 326 | **问:我怎么出现了这样的错误呀:** 327 | ```python 328 | FileNotFoundError: 【Errno 2】 No such file or directory 329 | StopIteration: [Errno 13] Permission denied: 'D:\\Study\\Collection\\Dataset\\VOC07+12+test\\VOCdevkit/VOC2007' 330 | …………………………………… 331 | …………………………………… 332 | ``` 333 | **答:去检查一下文件夹路径,查看是否有对应文件;并且检查一下2007_train.txt,其中文件路径是否有错。** 334 | 关于路径有几个重要的点: 335 | **文件夹名称中一定不要有空格。 336 | 注意相对路径和绝对路径。 337 | 多百度路径相关的知识。** 338 | 339 | **所有的路径问题基本上都是根目录问题,好好查一下相对目录的概念!** 340 | ### u、和原版比较问题,你怎么和原版不一样啊? 341 | **问:原版的代码是XXX,为什么你的代码是XXX? 342 | 答:是啊……这要不怎么说我不是原版呢……** 343 | 344 | **问:你这个代码和原版比怎么样,可以达到原版的效果么? 345 | 答:基本上可以达到,我都用voc数据测过,我没有好显卡,没有能力在coco上测试与训练。** 346 | 347 | **问:你有没有实现yolov4所有的tricks,和原版差距多少? 348 | 答:并没有实现全部的改进部分,由于YOLOV4使用的改进实在太多了,很难完全实现与列出来,这里只列出来了一些我比较感兴趣,而且非常有效的改进。论文中提到的SAM(注意力机制模块),作者自己的源码也没有使用。还有其它很多的tricks,不是所有的tricks都有提升,我也没法实现全部的tricks。至于和原版的比较,我没有能力训练coco数据集,根据使用过的同学反应差距不大。** 349 | 350 | ### v、我的检测速度是xxx正常吗?我的检测速度还能增快吗? 351 | **问:你这个FPS可以到达多少,可以到 XX FPS么? 352 | 答:FPS和机子的配置有关,配置高就快,配置低就慢。** 353 | 354 | **问:我的检测速度是xxx正常吗?我的检测速度还能增快吗? 355 | 答:看配置,配置好速度就快,如果想要配置不变的情况下加快速度,就要修改网络了。** 356 | 357 | **问:为什么我用服务器去测试yolov4(or others)的FPS只有十几? 358 | 答:检查是否正确安装了tensorflow-gpu或者pytorch的gpu版本,如果已经正确安装,可以去利用time.time()的方法查看detect_image里面,哪一段代码耗时更长(不仅只有网络耗时长,其它处理部分也会耗时,如绘图等)。** 359 | 360 | **问:为什么论文中说速度可以达到XX,但是这里却没有? 361 | 答:检查是否正确安装了tensorflow-gpu或者pytorch的gpu版本,如果已经正确安装,可以去利用time.time()的方法查看detect_image里面,哪一段代码耗时更长(不仅只有网络耗时长,其它处理部分也会耗时,如绘图等)。有些论文还会使用多batch进行预测,我并没有去实现这个部分。** 362 | 363 | ### w、预测图片不显示问题 364 | **问:为什么你的代码在预测完成后不显示图片?只是在命令行告诉我有什么目标。 365 | 答:给系统安装一个图片查看器就行了。** 366 | 367 | ### x、算法评价问题(目标检测的map、PR曲线、Recall、Precision等) 368 | **问:怎么计算map? 369 | 答:看map视频,都一个流程。** 370 | 371 | **问:计算map的时候,get_map.py里面有一个MINOVERLAP是什么用的,是iou吗? 372 | 答:是iou,它的作用是判断预测框和真实框的重合成度,如果重合程度大于MINOVERLAP,则预测正确。** 373 | 374 | **问:为什么get_map.py里面的self.confidence(self.score)要设置的那么小? 375 | 答:看一下map的视频的原理部分,要知道所有的结果然后再进行pr曲线的绘制。** 376 | 377 | **问:能不能说说怎么绘制PR曲线啥的呀。 378 | 答:可以看mAP视频,结果里面有PR曲线。** 379 | 380 | **问:怎么计算Recall、Precision指标。 381 | 答:这俩指标应该是相对于特定的置信度的,计算map的时候也会获得。** 382 | 383 | ### y、coco数据集训练问题 384 | **问:目标检测怎么训练COCO数据集啊?。 385 | 答:coco数据训练所需要的txt文件可以参考qqwweee的yolo3的库,格式都是一样的。** 386 | 387 | ### z、UP,怎么优化模型啊?我想提升效果 388 | **问:up,怎么修改模型啊,我想发个小论文! 389 | 答:建议看看yolov3和yolov4的区别,然后看看yolov4的论文,作为一个大型调参现场非常有参考意义,使用了很多tricks。我能给的建议就是多看一些经典模型,然后拆解里面的亮点结构并使用。** 390 | 391 | ### aa、UP,有Focal LOSS的代码吗?怎么改啊? 392 | **问:up,YOLO系列使用Focal LOSS的代码你有吗,有提升吗? 393 | 答:很多人试过,提升效果也不大(甚至变的更Low),它自己有自己的正负样本的平衡方式**。改代码的事情,还是自己好好看看代码吧。 394 | 395 | ### ab、部署问题(ONNX、TensorRT等) 396 | 我没有具体部署到手机等设备上过,所以很多部署问题我并不了解…… 397 | 398 | ## 4、语义分割库问题汇总 399 | ### a、shape不匹配问题 400 | #### 1)、训练时shape不匹配问题 401 | **问:up主,为什么运行train.py会提示shape不匹配啊? 402 | 答:在keras环境中,因为你训练的种类和原始的种类不同,网络结构会变化,所以最尾部的shape会有少量不匹配。** 403 | 404 | #### 2)、预测时shape不匹配问题 405 | **问:为什么我运行predict.py会提示我说shape不匹配呀。** 406 | ##### i、copying a param with shape torch.Size([75, 704, 1, 1]) from checkpoint 407 | 在Pytorch里面是这样的: 408 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200722171631901.png) 409 | ##### ii、Shapes are [1,1,1024,75] and [255,1024,1,1]. for 'Assign_360' (op: 'Assign') with input shapes: [1,1,1024,75], [255,1024,1,1]. 410 | 在Keras里面是这样的: 411 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200722171523380.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5MTk2NA==,size_16,color_FFFFFF,t_70) 412 | **答:原因主要有二: 413 | 1、train.py里面的num_classes没改。 414 | 2、预测时num_classes没改。 415 | 3、预测时model_path没改。 416 | 请检查清楚!训练和预测的时候用到的num_classes都需要检查!** 417 | 418 | ### b、显存不足问题(OOM、RuntimeError: CUDA out of memory)。 419 | **问:为什么我运行train.py下面的命令行闪的贼快,还提示OOM啥的? 420 | 答:这是在keras中出现的,爆显存了,可以改小batch_size。** 421 | 422 | **需要注意的是,受到BatchNorm2d影响,batch_size不可为1,至少为2。** 423 | 424 | **问:为什么提示 RuntimeError: CUDA out of memory. Tried to allocate 52.00 MiB (GPU 0; 15.90 GiB total capacity; 14.85 GiB already allocated; 51.88 MiB free; 15.07 GiB reserved in total by PyTorch)? 425 | 答:这是pytorch中出现的,爆显存了,同上。** 426 | 427 | **问:为什么我显存都没利用,就直接爆显存了? 428 | 答:都爆显存了,自然就不利用了,模型没有开始训练。** 429 | 430 | ### c、为什么要进行冻结训练与解冻训练,不进行行吗? 431 | **问:为什么要冻结训练和解冻训练呀? 432 | 答:可以不进行,本质上是为了保证性能不足的同学的训练,如果电脑性能完全不够,可以将Freeze_Epoch和UnFreeze_Epoch设置成一样,只进行冻结训练。** 433 | 434 | **同时这也是迁移学习的思想,因为神经网络主干特征提取部分所提取到的特征是通用的,我们冻结起来训练可以加快训练效率,也可以防止权值被破坏。** 435 | 在冻结阶段,模型的主干被冻结了,特征提取网络不发生改变。占用的显存较小,仅对网络进行微调。 436 | 在解冻阶段,模型的主干不被冻结了,特征提取网络会发生改变。占用的显存较大,网络所有的参数都会发生改变。 437 | 438 | ### d、我的LOSS好大啊,有问题吗?(我的LOSS好小啊,有问题吗?) 439 | **问:为什么我的网络不收敛啊,LOSS是XXXX。 440 | 答:不同网络的LOSS不同,LOSS只是一个参考指标,用于查看网络是否收敛,而非评价网络好坏,我的yolo代码都没有归一化,所以LOSS值看起来比较高,LOSS的值不重要,重要的是是否在变小,预测是否有效果。** 441 | 442 | ### e、为什么我训练出来的模型没有预测结果? 443 | **问:为什么我的训练效果不好?预测了没有框(框不准)。 444 | 答:** 445 | **考虑几个问题: 446 | 1、数据集问题,这是最重要的问题。小于500的自行考虑增加数据集;一定要检查数据集的标签,视频中详细解析了VOC数据集的格式,但并不是有输入图片有输出标签即可,还需要确认标签的每一个像素值是否为它对应的种类。很多同学的标签格式不对,最常见的错误格式就是标签的背景为黑,目标为白,此时目标的像素点值为255,无法正常训练,目标需要为1才行。 447 | 2、是否解冻训练,如果数据集分布与常规画面差距过大需要进一步解冻训练,调整主干,加强特征提取能力。 448 | 3、网络问题,可以尝试不同的网络。 449 | 4、训练时长问题,有些同学只训练了几代表示没有效果,按默认参数训练完。 450 | 5、确认自己是否按照步骤去做了。 451 | 6、不同网络的LOSS不同,LOSS只是一个参考指标,用于查看网络是否收敛,而非评价网络好坏,LOSS的值不重要,重要的是是否收敛。** 452 | 453 | **问:为什么我的训练效果不好?对小目标预测不准确。 454 | 答:对于deeplab和pspnet而言,可以修改一下downsample_factor,当downsample_factor为16的时候下采样倍数过多,效果不太好,可以修改为8。** 455 | 456 | ### f、为什么我计算出来的miou是0? 457 | **问:为什么我的训练效果不好?计算出来的miou是0?。** 458 | 答: 459 | 与e类似,**考虑几个问题: 460 | 1、数据集问题,这是最重要的问题。小于500的自行考虑增加数据集;一定要检查数据集的标签,视频中详细解析了VOC数据集的格式,但并不是有输入图片有输出标签即可,还需要确认标签的每一个像素值是否为它对应的种类。很多同学的标签格式不对,最常见的错误格式就是标签的背景为黑,目标为白,此时目标的像素点值为255,无法正常训练,目标需要为1才行。 461 | 2、是否解冻训练,如果数据集分布与常规画面差距过大需要进一步解冻训练,调整主干,加强特征提取能力。 462 | 3、网络问题,可以尝试不同的网络。 463 | 4、训练时长问题,有些同学只训练了几代表示没有效果,按默认参数训练完。 464 | 5、确认自己是否按照步骤去做了。 465 | 6、不同网络的LOSS不同,LOSS只是一个参考指标,用于查看网络是否收敛,而非评价网络好坏,LOSS的值不重要,重要的是是否收敛。** 466 | 467 | ### g、gbk编码错误('gbk' codec can't decode byte)。 468 | **问:我怎么出现了gbk什么的编码错误啊:** 469 | ```python 470 | UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 446: illegal multibyte sequence 471 | ``` 472 | **答:标签和路径不要使用中文,如果一定要使用中文,请注意处理的时候编码的问题,改成打开文件的encoding方式改为utf-8。** 473 | 474 | ### h、我的图片是xxx*xxx的分辨率的,可以用吗? 475 | **问:我的图片是xxx*xxx的分辨率的,可以用吗!** 476 | **答:可以用,代码里面会自动进行resize与数据增强。** 477 | 478 | ### i、我想进行数据增强!怎么增强? 479 | **问:我想要进行数据增强!怎么做呢?** 480 | **答:可以用,代码里面会自动进行resize与数据增强。** 481 | 482 | ### j、多GPU训练。 483 | **问:怎么进行多GPU训练? 484 | 答:pytorch的大多数代码可以直接使用gpu训练,keras的话直接百度就好了,实现并不复杂,我没有多卡没法详细测试,还需要各位同学自己努力了。** 485 | 486 | ### k、能不能训练灰度图? 487 | **问:能不能训练灰度图(预测灰度图)啊? 488 | 答:我的大多数库会将灰度图转化成RGB进行训练和预测,如果遇到代码不能训练或者预测灰度图的情况,可以尝试一下在get_random_data里面将Image.open后的结果转换成RGB,预测的时候也这样试试。(仅供参考)** 489 | 490 | ### l、断点续练问题。 491 | **问:我已经训练过几个世代了,能不能从这个基础上继续开始训练 492 | 答:可以,你在训练前,和载入预训练权重一样载入训练过的权重就行了。一般训练好的权重会保存在logs文件夹里面,将model_path修改成你要开始的权值的路径即可。** 493 | 494 | ### m、我要训练其它的数据集,预训练权重能不能用? 495 | **问:如果我要训练其它的数据集,预训练权重要怎么办啊?** 496 | **答:数据的预训练权重对不同数据集是通用的,因为特征是通用的,预训练权重对于99%的情况都必须要用,不用的话权值太过随机,特征提取效果不明显,网络训练的结果也不会好。** 497 | 498 | ### n、网络如何从0开始训练? 499 | **问:我要怎么不使用预训练权重啊? 500 | 答:看一看注释、大多数代码是model_path = '',Freeze_Train = Fasle**,如果设置model_path无用,**那么把载入预训练权重的代码注释了就行。** 501 | 502 | ### o、为什么从0开始训练效果这么差(修改了网络主干,效果不好怎么办)? 503 | **问:为什么我不使用预训练权重效果这么差啊? 504 | 答:因为随机初始化的权值不好,提取的特征不好,也就导致了模型训练的效果不好,预训练权重还是非常重要的。** 505 | 506 | **问:up,我修改了网络,预训练权重还能用吗? 507 | 答:修改了主干的话,如果不是用的现有的网络,基本上预训练权重是不能用的,要么就自己判断权值里卷积核的shape然后自己匹配,要么只能自己预训练去了;修改了后半部分的话,前半部分的主干部分的预训练权重还是可以用的,如果是pytorch代码的话,需要自己修改一下载入权值的方式,判断shape后载入,如果是keras代码,直接by_name=True,skip_mismatch=True即可。** 508 | 权值匹配的方式可以参考如下: 509 | ```python 510 | # 加快模型训练的效率 511 | print('Loading weights into state dict...') 512 | device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') 513 | model_dict = model.state_dict() 514 | pretrained_dict = torch.load(model_path, map_location=device) 515 | a = {} 516 | for k, v in pretrained_dict.items(): 517 | try: 518 | if np.shape(model_dict[k]) == np.shape(v): 519 | a[k]=v 520 | except: 521 | pass 522 | model_dict.update(a) 523 | model.load_state_dict(model_dict) 524 | print('Finished!') 525 | ``` 526 | 527 | **问:为什么从0开始训练效果这么差(我修改了网络主干,效果不好怎么办)? 528 | 答:一般来讲,网络从0开始的训练效果会很差,因为权值太过随机,特征提取效果不明显,因此非常、非常、非常不建议大家从0开始训练!如果一定要从0开始,可以了解imagenet数据集,首先训练分类模型,获得网络的主干部分权值,分类模型的 主干部分 和该模型通用,基于此进行训练。 529 | 网络修改了主干之后也是同样的问题,随机的权值效果很差。** 530 | 531 | **问:怎么在模型上从0开始训练? 532 | 答:在算力不足与调参能力不足的情况下从0开始训练毫无意义。模型特征提取能力在随机初始化参数的情况下非常差。没有好的参数调节能力和算力,无法使得网络正常收敛。** 533 | 如果一定要从0开始,那么训练的时候请注意几点: 534 | - 不载入预训练权重。 535 | - 不要进行冻结训练,注释冻结模型的代码。 536 | 537 | **问:为什么我不使用预训练权重效果这么差啊? 538 | 答:因为随机初始化的权值不好,提取的特征不好,也就导致了模型训练的效果不好,voc07+12、coco+voc07+12效果都不一样,预训练权重还是非常重要的。** 539 | 540 | ### p、你的权值都是哪里来的? 541 | **问:如果网络不能从0开始训练的话你的权值哪里来的? 542 | 答:有些权值是官方转换过来的,有些权值是自己训练出来的,我用到的主干的imagenet的权值都是官方的。** 543 | 544 | 545 | ### q、视频检测与摄像头检测 546 | **问:怎么用摄像头检测呀? 547 | 答:predict.py修改参数可以进行摄像头检测,也有视频详细解释了摄像头检测的思路。** 548 | 549 | **问:怎么用视频检测呀? 550 | 答:同上** 551 | 552 | ### r、如何保存检测出的图片 553 | **问:检测完的图片怎么保存? 554 | 答:一般目标检测用的是Image,所以查询一下PIL库的Image如何进行保存。详细看看predict.py文件的注释。** 555 | 556 | **问:怎么用视频保存呀? 557 | 答:详细看看predict.py文件的注释。** 558 | 559 | ### s、遍历问题 560 | **问:如何对一个文件夹的图片进行遍历? 561 | 答:一般使用os.listdir先找出文件夹里面的所有图片,然后根据predict.py文件里面的执行思路检测图片就行了,详细看看predict.py文件的注释。** 562 | 563 | **问:如何对一个文件夹的图片进行遍历?并且保存。 564 | 答:遍历的话一般使用os.listdir先找出文件夹里面的所有图片,然后根据predict.py文件里面的执行思路检测图片就行了。保存的话一般目标检测用的是Image,所以查询一下PIL库的Image如何进行保存。如果有些库用的是cv2,那就是查一下cv2怎么保存图片。详细看看predict.py文件的注释。** 565 | 566 | ### t、路径问题(No such file or directory、StopIteration: [Errno 13] Permission denied: 'XXXXXX') 567 | **问:我怎么出现了这样的错误呀:** 568 | ```python 569 | FileNotFoundError: 【Errno 2】 No such file or directory 570 | StopIteration: [Errno 13] Permission denied: 'D:\\Study\\Collection\\Dataset\\VOC07+12+test\\VOCdevkit/VOC2007' 571 | …………………………………… 572 | …………………………………… 573 | ``` 574 | **答:去检查一下文件夹路径,查看是否有对应文件;并且检查一下2007_train.txt,其中文件路径是否有错。** 575 | 关于路径有几个重要的点: 576 | **文件夹名称中一定不要有空格。 577 | 注意相对路径和绝对路径。 578 | 多百度路径相关的知识。** 579 | 580 | **所有的路径问题基本上都是根目录问题,好好查一下相对目录的概念!** 581 | ### u、和原版比较问题,你怎么和原版不一样啊? 582 | **问:原版的代码是XXX,为什么你的代码是XXX? 583 | 答:是啊……这要不怎么说我不是原版呢……** 584 | 585 | **问:你这个代码和原版比怎么样,可以达到原版的效果么? 586 | 答:基本上可以达到,我都用voc数据测过,我没有好显卡,没有能力在coco上测试与训练。** 587 | 588 | ### v、我的检测速度是xxx正常吗?我的检测速度还能增快吗? 589 | **问:你这个FPS可以到达多少,可以到 XX FPS么? 590 | 答:FPS和机子的配置有关,配置高就快,配置低就慢。** 591 | 592 | **问:我的检测速度是xxx正常吗?我的检测速度还能增快吗? 593 | 答:看配置,配置好速度就快,如果想要配置不变的情况下加快速度,就要修改网络了。** 594 | 595 | **问:为什么论文中说速度可以达到XX,但是这里却没有? 596 | 答:检查是否正确安装了tensorflow-gpu或者pytorch的gpu版本,如果已经正确安装,可以去利用time.time()的方法查看detect_image里面,哪一段代码耗时更长(不仅只有网络耗时长,其它处理部分也会耗时,如绘图等)。有些论文还会使用多batch进行预测,我并没有去实现这个部分。** 597 | 598 | ### w、预测图片不显示问题 599 | **问:为什么你的代码在预测完成后不显示图片?只是在命令行告诉我有什么目标。 600 | 答:给系统安装一个图片查看器就行了。** 601 | 602 | ### x、算法评价问题(miou) 603 | **问:怎么计算miou? 604 | 答:参考视频里的miou测量部分。** 605 | 606 | **问:怎么计算Recall、Precision指标。 607 | 答:现有的代码还无法获得,需要各位同学理解一下混淆矩阵的概念,然后自行计算一下。** 608 | 609 | ### y、UP,怎么优化模型啊?我想提升效果 610 | **问:up,怎么修改模型啊,我想发个小论文! 611 | 答:建议目标检测中的yolov4论文,作为一个大型调参现场非常有参考意义,使用了很多tricks。我能给的建议就是多看一些经典模型,然后拆解里面的亮点结构并使用。** 612 | 613 | ### z、部署问题(ONNX、TensorRT等) 614 | 我没有具体部署到手机等设备上过,所以很多部署问题我并不了解…… 615 | 616 | ## 5、交流群问题 617 | **问:up,有没有QQ群啥的呢? 618 | 答:没有没有,我没有时间管理QQ群……** 619 | 620 | ## 6、怎么学习的问题 621 | **问:up,你的学习路线怎么样的?我是个小白我要怎么学? 622 | 答:这里有几点需要注意哈 623 | 1、我不是高手,很多东西我也不会,我的学习路线也不一定适用所有人。 624 | 2、我实验室不做深度学习,所以我很多东西都是自学,自己摸索,正确与否我也不知道。 625 | 3、我个人觉得学习更靠自学** 626 | 学习路线的话,我是先学习了莫烦的python教程,从tensorflow、keras、pytorch入门,入门完之后学的SSD,YOLO,然后了解了很多经典的卷积网,后面就开始学很多不同的代码了,我的学习方法就是一行一行的看,了解整个代码的执行流程,特征层的shape变化等,花了很多时间也没有什么捷径,就是要花时间吧。 627 | --------------------------------------------------------------------------------