深度学习-卷积神经网络-目标检测之YOLOV3模型-代码运行图片检测实践1

系列文章

深度学习-卷积神经网络(目标检测环境搭建)-TensorFlow及Keras环境搭建&详细安装教程

相关文章

深度学习-卷积神经网络-实例及代码0.8—基于最小均方误差的线性判别函数参数拟合训练

深度学习-卷积神经网络-实例及代码0.9—MNIST数据集介绍、下载及基本操作

深度学习-卷积神经网络-实例及代码1(入门)—利用Tensorflow和mnist数据集训练单层前馈神经网络/感知机实现手写数字识别

深度学习-卷积神经网络-实例及代码2(初级)—利用Tensorflow和mnist数据集训练简单的深度网络模型实现手写数字识别

深度学习-卷积神经网络-实例及代码3(图像分类LeNet5模型)—利用Tensorflow和mnist数据集训练LeNet5-CNN模型实现手写数字识别

 

最近看了计算机视觉—目标检测领域中的YOLOV3模型及代码

这里使用的代码版本是github上的qqwweee/keras-yolo3 Keras实现版本

在Windows环境下使用CPU进行检测,看看效果如何

(操作系统:Windows 10专业版 64位)

(CPU:Intel(R) Core(TM) i5-4590 CPU@3.30GHz,内存8G)

执行下面YOLOV3的检测程序前,需要完成前序文章中的TensorFlow和Keras环境搭建和安装

(如果要执行视频检测还要安装opencv的python版本等)

1,下载YOLOV3-Keras项目

从这里下载原始项目文件:Github上qqwweee-keras版本的YOLOV3实现

文后附上了该项目的具体说明内容

使用Pycharm软件打开下载解压后的keras-yolov3项目

(我这里换了个项目名字:KerasYolo3Test)
2,修改代码——yolo_video.py

# 添加的代码块
import tensorflow as tf
import keras
from keras.backend.tensorflow_backend import set_session
config=tf.ConfigProto()
config.gpu_options.allow_growth=True
sess=tf.Session(config=config)
set_session(sess)
keras.backend.clear_session()

3,修改代码——yolo.py

# 添加的代码块
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

4,下载YOLOV3官网上在COCO数据集上训练完成的权重文件yolov3.weights

进入YOLOV3官网链接:https://pjreddie.com/darknet/yolo/

找到 Performance on the COCO Dataset,其中有一行如下:
YOLOv3-416    COCO trainval    test-dev    55.3    65.86 Bn    35    cfg    weights

点击weights链接完成权重文件yolov3.weights的下载
5,执行代码convert.py,根据下载的权重文件生成模型
在pycharm中右键项目或项目文件,选择Open in Terminal
输入python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5

完成模型的生成,在项目的model_data文件夹下可以看到yolo.h5

6,执行图片检测
继续输入以下执行检测的命令

python yolo_video.py --image

根据提示输入所需的检测图片路径:

Input image filename:image/car3.jpg

检测过程及结果:

E:\PythonProject2020-PCnew\MyPythonTest\KerasYolo3Test>python yolo_video.py --image
Using TensorFlow backend.
2020-10-09 20:50:42.050119: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_100.dll'; dlerror: cudart64_100.
dll not found
2020-10-09 20:50:42.055319: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
WARNING:tensorflow:From yolo_video.py:10: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

WARNING:tensorflow:From yolo_video.py:12: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

2020-10-09 20:50:56.288179: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use:
AVX2
2020-10-09 20:50:56.306861: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not foun
d
2020-10-09 20:50:56.311911: E tensorflow/stream_executor/cuda/cuda_driver.cc:318] failed call to cuInit: UNKNOWN ERROR (303)
2020-10-09 20:50:56.405559: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: USER-PC
2020-10-09 20:50:56.408309: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: USER-PC
Image detection mode
 Ignoring remaining command line arguments: ./path2your_video,
WARNING:tensorflow:From C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py:1630: calling Base
ResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
model_data/yolo.h5 model, anchors, and classes loaded.
WARNING:tensorflow:From C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow_core\python\ops\array_ops.py:1475: where (from tensorflow.p
ython.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Input image filename:image/car3.jpg
(416, 416, 3)
2020-10-09 20:53:08.198728: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:533] remapper failed: Invalid argument: Subshape must have computed start >= end
since stride is negative, but is 0 and 2 (computed from start 0 and end 9223372036854775807 over shape with rank 2 and stride-1)
2020-10-09 20:53:08.729473: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:533] remapper failed: Invalid argument: Subshape must have computed start >= end
since stride is negative, but is 0 and 2 (computed from start 0 and end 9223372036854775807 over shape with rank 2 and stride-1)
Found 4 boxes for img
truck 0.31 (261, 415) (328, 495)
truck 0.96 (267, 353) (363, 489)
car 0.70 (603, 229) (633, 267)
car 0.88 (437, 376) (470, 414)
4.0813527999999994

 

以上修改后完整的可执行代码下载地址:KerasYolo3Test含权重文件及模型

 

附Github中关于qqwweee/keras-yolov3的使用说明:


Introduction
A Keras implementation of YOLOv3 (Tensorflow backend) inspired by allanzelener/YAD2K.

Quick Start
Download YOLOv3 weights from YOLO website.
Convert the Darknet YOLO model to a Keras model.
Run YOLO detection.
wget https://pjreddie.com/media/files/yolov3.weights
python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5
python yolo_video.py [OPTIONS...] --image, for image detection mode, OR
python yolo_video.py [video_path] [output_path (optional)]
For Tiny YOLOv3, just do in a similar way, just specify model path and anchor path with --model model_file and --anchors anchor_file.

Usage
Use --help to see usage of yolo_video.py:

usage: yolo_video.py [-h] [--model MODEL] [--anchors ANCHORS]
                     [--classes CLASSES] [--gpu_num GPU_NUM] [--image]
                     [--input] [--output]

positional arguments:
  --input        Video input path
  --output       Video output path

optional arguments:
  -h, --help         show this help message and exit
  --model MODEL      path to model weight file, default model_data/yolo.h5
  --anchors ANCHORS  path to anchor definitions, default
                     model_data/yolo_anchors.txt
  --classes CLASSES  path to class definitions, default
                     model_data/coco_classes.txt
  --gpu_num GPU_NUM  Number of GPU to use, default 1
  --image            Image detection mode, will ignore all positional arguments
MultiGPU usage: use --gpu_num N to use N GPUs. It is passed to the Keras multi_gpu_model().
Training
Generate your own annotation file and class names file.
One row for one image;
Row format: image_file_path box1 box2 ... boxN;
Box format: x_min,y_min,x_max,y_max,class_id (no space).
For VOC dataset, try python voc_annotation.py
Here is an example:

path/to/img1.jpg 50,100,150,200,0 30,50,200,120,3
path/to/img2.jpg 120,300,250,600,2
...
Make sure you have run python convert.py -w yolov3.cfg yolov3.weights model_data/yolo_weights.h5
The file model_data/yolo_weights.h5 is used to load pretrained weights.

Modify train.py and start training.
python train.py
Use your trained weights or checkpoint weights with command line option --model model_file when using yolo_video.py Remember to modify class path or anchor path, with --classes class_file and --anchors anchor_file.

If you want to use original pretrained weights for YOLOv3:
1. wget https://pjreddie.com/media/files/darknet53.conv.74
2. rename it as darknet53.weights
3. python convert.py -w darknet53.cfg darknet53.weights model_data/darknet53_weights.h5
4. use model_data/darknet53_weights.h5 in train.py

Some issues to know
The test environment is

Python 3.5.2
Keras 2.1.5
tensorflow 1.6.0
Default anchors are used. If you use your own anchors, probably some changes are needed.

The inference result is not totally the same as Darknet but the difference is small.

The speed is slower than Darknet. Replacing PIL with opencv may help a little.

Always load pretrained weights and freeze layers in the first stage of training. Or try Darknet training. It's OK if there is a mismatch warning.

The training strategy is for reference only. Adjust it according to your dataset and your goal. And add further strategy if needed.

For speeding up the training process with frozen layers train_bottleneck.py can be used. It will compute the bottleneck features of the frozen model first and then only trains the last layers. This makes training on CPU possible in a reasonable time. See this for more information on bottleneck features.

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: YOLOv5是一个用于对象检测的开源软件,它可以帮助你识别图像或视频中的物体。 如果你想要实现YOLOv5的代码,你需要学习一些基本的机器学习知识,例如卷积神经网络(CNN)。你还需要了解目标检测的基本原理,例如滑动窗口,锚点,非极大值抑制等。 在开始实现YOLOv5的代码之前,你可以先学习一些有关机器学习的基本概念,例如神经网络,损失函数,优化器等。你还可以尝试使用一些机器学习框架,例如TensorFlow,PyTorch等,来编写机器学习模型代码。 当你对这些概念有了一定的了解之后,你可以通过以下步骤来实现YOLOv5的代码: 1. 设计YOLOv5的模型结构。YOLOv5使用卷积神经网络(CNN)来检测图像中的物体。你可以参考YOLOv5的文献或者其他资料,来设计你自己的模型结构。 2. 获取训练数据。YOLOv5需要大量的训练数据来学习如何识别图像中的物体。你可以使用公开的数据集,例如C ### 回答2: YOLOv5是一种用于目标检测深度学习模型,该模型在实现上采用了PyTorch框架。以下是YOLOv5的代码实现的概述。 首先,YOLOv5的代码实现可以在GitHub上的ultralytics/yolov5仓库中找到。你可以通过克隆或下载该仓库来获取YOLOv5的代码。 在代码实现中,YOLOv5模型被分为了五个部分:数据准备、模型构建、模型训练、模型评估和模型推理。 在数据准备阶段,你需要准备目标检测所需的数据集。通常,数据集包括图像和对应的标签,标签包含了目标类别和边界框信息。 在模型构建阶段,YOLOv5使用了一个由变量nb和nc定义的网络结构。nb定义了边界框的数量,nc定义了目标类别的数量。该网络结构通常包括卷积层、池化层、残差块等。YOLOv5模型中的网络结构被设计为轻量级的,以便在移动设备或资源受限环境中高效运行。 在模型训练阶段,YOLOv5使用了交叉熵损失函数和IOU损失函数。这些损失函数用于度量模型预测的类别和边界框与真实值之间的差异。训练过程通常包括加载数据集、前向传播、损失计算、反向传播和参数更新等步骤。 在模型评估阶段,YOLOv5使用了mAP(Mean Average Precision)指标来评估模型的性能。mAP是一种常用的目标检测性能指标,它综合考虑了精确率和召回率。 在模型推理阶段,YOLOv5可以用于对图像或视频进行目标检测。推理过程通常包括加载模型、输入图像、前向传播、非极大抑制和绘制边界框等步骤。 总之,YOLOv5代码实现通过数据准备、模型构建、模型训练、模型评估和模型推理等步骤实现目标检测功能,并使用PyTorch框架来实现模型的训练和推理过程。 ### 回答3: YOLOv5是一种目标检测算法,其代码实现可以分为以下几个步骤。 首先,我们需要准备数据集。通过收集并标注一组带有边界框的图像,可以创建训练集和验证集。这些图像将用于训练和测试模型。 接下来,我们可以下载YOLOv5的源代码并将其部署到本地环境中。可以通过GitHub等网站找到YOLOv5代码的开源版本。解压源代码并配置Python环境所需的依赖项。 然后,我们需要进行模型训练。在训练之前,需要进行一些配置,例如选择要使用的模型架构、设定训练的超参数等等。通过运行训练脚本,模型将根据用于训练的数据进行学习,并逐渐提高其在目标检测任务上的能力。 训练过程完成后,可以进行模型的评估。将验证集输入已经训练好的模型,并计算出模型目标检测任务上的精度、召回率、平均精度等指标。 最后,我们可以使用训练好的模型进行目标检测。将待检测的图像输入模型,并根据模型的输出结果绘制出边界框,从而标识出图像中的目标物体。 总之,YOLOv5的代码实现可以通过准备数据集、配置环境、训练模型、评估模型以及进行目标检测等步骤完成。通过这些步骤,我们可以利用YOLOv5算法来实现对图像中目标物体的快速而准确的检测

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值