Jupyter笔记[4]-目标检测

需求

在 python 中进行目标检测

Haar级联

Haar 级联 是一种基于特征的对象检测算法,用于从图像中检测对象。Cascade 函数在大量正 负图像上进行训练以进行检测。
该算法不需要大量计算并且可以实时运行。我们可以为动物、汽⻋、自行⻋等自定义对象训练自己的级联函数。
Haar 级联使用 Cascade 函数和 Cascade 窗口。它尝试计算每个窗口的特征并进行正负分类。如果窗口可以是对象的一部分,则为正,否则为负。
Haar 级联可以理解为二进制分类器。它为那些可以成为我们对象一部分的级联窗口指定正 值,为那些不能成为我们对象的一部分的窗口指定负值。

YOLO算法系列

[https://zhuanlan.zhihu.com/p/538808226]
YOLO:v1,v2,v3,v4,v5,v6,v7
YOLO 算法是基于 one-stage 的主流目标检测算法,它不需要 region proposal阶段,可以直接产生目标物体的类别概率和位置坐标值,即输入待检测图像输出就是含有预测框的目标物体的类别概率和位置坐标值信息,真正实现了目标检测端到端的流程。YOLO 算法为了追求更快的检测速度,在检测准确率上做了一定的让步,但随着 YOLO 算法的不断更新迭代和优化,汲取了目前一些优秀的检测算法的优势,YOLO 算法在保持更快速度的同时也取得了较高的检测精度。
YOLOv7在5FPS到 160 FPS 范围内的速度和准确度都超过了所有已知的物体检测器,YOLOv7 在 5 FPS 到 160 FPS 范围内的速度和准确度都超过了所有已知的目标检测器,并且在 GPU V100 上 30 FPS 或更高的所有已知实时目标检测器中具有最高的准确度 56.8% AP。
YOLOv7论文:[https://arxiv.org/abs/2207.02696]
YOLOv7源码:[https://github.com/WongKinYiu/yolov7]
YOLOv7架构图[https://zhuanlan.zhihu.com/p/543743278]

配置环境

[https://blog.csdn.net/qq_44824148/article/details/125736620]

#换源
pip install pqi
pqi use ustc
#正式安装
pip install yolov7
mkdir -p ~/work/temp/YOLOv7
cd ~/work/temp/YOLOv7
git clone https://github.com/WongKinYiu/yolov7.git

新建weights文件夹,放入[https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt]权重文件
修改yolov7-main/utils/wandb_logging/wandb_util.py

'''
try:
    import wandb
    from wandb import init, finish
except ImportError:
    wandb = None
'''
#不要登录wandb
wandb=None


测试

下载COCO数据集

由于网络不畅,所以需要手动操作
按照yolov7-main/scripts/get_coco.sh的指示,

#!/bin/bash
# COCO 2017 dataset http://cocodataset.org
# Download command: bash ./scripts/get_coco.sh

# Download/unzip labels
d='./' # unzip directory
url=https://github.com/ultralytics/yolov5/releases/download/v1.0/
f='coco2017labels-segments.zip' # or 'coco2017labels.zip', 68 MB
echo 'Downloading' $url$f ' ...'
curl -L $url$f -o $f && unzip -q $f -d $d && rm $f & # download, unzip, remove in background

# Download/unzip images
d='./coco/images' # unzip directory
url=http://images.cocodataset.org/zips/
f1='train2017.zip' # 19G, 118k images
f2='val2017.zip'   # 1G, 5k images
f3='test2017.zip'  # 7G, 41k images (optional)
for f in $f1 $f2 $f3; do
  echo 'Downloading' $url$f '...'
  curl -L $url$f -o $f && unzip -q $f -d $d && rm $f & # download, unzip, remove in background
done
wait # finish background tasks

还有就是根据data/coco.yaml
下载[https://github.com/ultralytics/yolov5/releases/download/v1.0/coco2017labels-segments.zip]
解压到yolov7-main/
下载[http://images.cocodataset.org/zips/train2017.zip]
下载[http://images.cocodataset.org/zips/val2017.zip]
下载[http://images.cocodataset.org/zips/test2017.zip]
解压到yolov7-main/coco/images

使用自己的数据集

新建data.yaml文件,配置yolov7的数据集,数据集为 YOLO格式。

train: ~/work/dataset/YOLO-Dataset/train
val: ~/work/dataset/YOLO-Dataset/val 
test: ~/work/dataset/YOLO-Dataset/test

# number of classes
nc: 20
# class names
names: ["Akita_Dog", "Basset_Hound", "Beagle_Dog", "Border_Collie", "Chinese_Shar-pei", "Corgi","English_Cocker_Spaniel","English_Sheepdog","German_Shepherd_Dog","Golden_Hair","Labrador","Pomeranian","Redbone_Coonhound","Saint_Bernard","Samoyed","Schnauzer","Schnauzer","Siberian_Husky","Springer_Spaniel","Tibetan_Mastiff"]

使用--data data.yaml指定使用的数据集

检测detect

cd ~/work/temp/YOLOv7/yolov7-main
git config --global --add safe.directory /home/jovyan/work
#使用--device cpu指定使用cpu
python detect.py --weights weights/yolov7.pt --conf 0.25 --device cpu --img-size 640 --source inference/images/horses.jpg

效果

在线体验(无需配置环境):[https://huggingface.co/spaces/akhaliq/yolov7]

报错处理

(base) root@e83132972abd:~/work/temp/YOLOv7/yolov7-main# python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg
Namespace(weights=['yolov7.pt'], source='inference/images/horses.jpg', img_size=640, conf_thres=0.25, iou_thres=0.45, device='', view_img=False, save_txt=False, save_conf=False, nosave=False, classes=None, agnostic_nms=False, augment=False, update=False, project='runs/detect', name='exp', exist_ok=False, no_trace=False)
YOLOR 🚀 7a9a04b torch 1.13.1+cu117 CPU

Traceback (most recent call last):
  File "/home/jovyan/work/temp/YOLOv7/yolov7-main/utils/google_utils.py", line 26, in attempt_download
    assets = [x['name'] for x in response['assets']]  # release assets
KeyError: 'assets'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jovyan/work/temp/YOLOv7/yolov7-main/detect.py", line 196, in <module>
    detect()
  File "/home/jovyan/work/temp/YOLOv7/yolov7-main/detect.py", line 34, in detect
    model = attempt_load(weights, map_location=device)  # load FP32 model
  File "/home/jovyan/work/temp/YOLOv7/yolov7-main/models/experimental.py", line 251, in attempt_load
    attempt_download(w)
  File "/home/jovyan/work/temp/YOLOv7/yolov7-main/utils/google_utils.py", line 31, in attempt_download
    tag = subprocess.check_output('git tag', shell=True).decode().split()[-1]
IndexError: list index out of range

命令改为:python detect.py --weights weights/yolov7.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg

(base) root@e83132972abd:~/work/temp/YOLOv7/yolov7-main# python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg
Namespace(weights=['yolov7.pt'], source='inference/images/horses.jpg', img_size=640, conf_thres=0.25, iou_thres=0.45, device='', view_img=False, save_txt=False, save_conf=False, nosave=False, classes=None, agnostic_nms=False, augment=False, update=False, project='runs/detect', name='exp', exist_ok=False, no_trace=False)
YOLOR 🚀 7a9a04b torch 1.13.1+cu117 CPU

Traceback (most recent call last):
  File "/home/jovyan/work/temp/YOLOv7/yolov7-main/detect.py", line 196, in <module>
    detect()
  File "/home/jovyan/work/temp/YOLOv7/yolov7-main/detect.py", line 34, in detect
    model = attempt_load(weights, map_location=device)  # load FP32 model
  File "/home/jovyan/work/temp/YOLOv7/yolov7-main/models/experimental.py", line 252, in attempt_load
    ckpt = torch.load(w, map_location=map_location)  # load
  File "/opt/conda/lib/python3.9/site-packages/torch/serialization.py", line 789, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
  File "/opt/conda/lib/python3.9/site-packages/torch/serialization.py", line 1131, in _load
    result = unpickler.load()
  File "/opt/conda/lib/python3.9/site-packages/torch/serialization.py", line 1124, in find_class
    return super().find_class(mod_name, name)
  File "/home/jovyan/work/temp/YOLOv7/yolov7-main/models/yolo.py", line 15, in <module>
    from utils.loss import SigmoidBin
  File "/home/jovyan/work/temp/YOLOv7/yolov7-main/utils/loss.py", line 687
    fg_pred = pi[b, a, gj, gi]                
    ^
SyntaxError: invalid syntax

暂无处理方式

posted @ 2022-12-29 15:13  qsBye  阅读(468)  评论(0编辑  收藏  举报