深度学习-卷积神经网络-目标检测之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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值