YOLOv3 object/target detection practical chapter (Windows system, Python3, TensorFlow2 version)

Preface

Based on YOLO for object detection and object recognition, after setting up the development environment, we will first conduct practical applications with everyone to experience the object/target detection effect and charm of YOLOv3; at the same time, we will gradually understand the shortcomings and optimization ideas of YOLOv3.

 

Development environment parameters

system:Windows      programming language:Python 3.8           

deep learning framework:TensorFlow 2.3        integrated development environment:Anaconda        Development code IDE:PyCharm

Mainly use TensorFlow2.3, ​​opencv-python4.4.0, Pillow, matplotlib and other dependent libraries.

For details, please refer to my other blog:YOLO practical application building development environment (Windows system, Python 3.8, TensorFlow 2.3 version)

 

Object/target detection effect of YOLOv3:

1) Four kittens were detected:

Use a light blue frame to frame the location of the kitten, and annotate the label (category confidence) above the frame. For example, the label detected for the first kitten is cat, and the confidence level is 0.95, that is, it is 95% sure that it is cat.

 

 

2) A puppy and a kitten are detected at the same time:

The kitten is detected to be a cat, 1.00; there is 100% confidence that it is a cat;

The puppy is detected as a dog, 0.97; it is 97% confident that it is a cat;

 

 

3) At complex intersections, many pedestrians and vehicles are detected:

You can see that most pedestrians, cars and buses have been detected, and a small number have not been detected. If you are doing target detection in a specific scene, it is recommended that you purchase data for the specific scene and retrain the network. Generate a stable and high-precision model and save the weight file for subsequent use.

 

 

Table of contents

Preface

Development environment parameters

Experience YOLOv3 object/target detection

1) Download the code and open the project

2) Download the weight file

3) Apply the weight file to the project

4) Perform target detection

Call the core code of the model


 

Experience YOLOv3 object/target detection

1) Download the code and open the project

First go to githug to download the code, then unzip the project, and then use the PyCharm tool to open the project;

Githug code download address:https://github.com/guo-pu/yolov3-tf2

illustrate:This warehouse code comes fromzzh8829/yolov3-tf2 For modification, the zzh8829/yolov3-tf2 code warehouse address:https://github.com/zzh8829/yolov3-tf2 

Open the project using PyCharm tool:

The page that opens looks like this:

[Select development environment]

File -> Settings -> Project -> Project Interpreters Select the development environment to build;

Then click Apply first, wait for the loading to complete, and then click OK;

 

 

2) Download the weight file

Method 1: Use wget to download

Prerequisite: need to support wget command;

yolov3.weights and yolov3-tiny.weights are pre-trained Darknet network weights;

yolov3.weights is the default weight, which supports more and more accurate target categories;

yolov3-tiny.weights is a weight applied to lightweight devices. The performance requirements of the device are not so high, and the response speed is faster than yolov3.weights;

Enter the windows administrator command window:

[Download yolov3.weights weight file]

Enter the directory where the data is stored, such as the data directory of the e-disk, and then execute the following command to download the weight value:

wget https://pjreddie.com/media/files/yolov3.weights  -O  .\yolov3.weights

Then the download will start;


【Download yolov3-tiny.weightsWeight file]

wget https://pjreddie.com/media/files/yolov3-tiny.weights  -O .\yolov3-tiny.weights

After downloading, go to the storage directory to check whether the download is successful and complete;

 

Method 2: Extract from my network disk

Link: https://pan.baidu.com/s/1TK4EEWsCHPyunNkJ98Mhjw

Extraction code: urad

 

Then copy the data to the downloaded project package, yolov3-tf2-master\data

 

3) Apply the weight file to the project

Execute the following command to convert the trained weights and apply them to the project.

Enter the YOLO3-GPU-TensorFlow2 development environment in the Pycharm command terminal:

conda activate YOLO3-GPU-TensorFlow2

【yolov3.weights】

python convert.py –weights ./data/yolov3.weights –output ./checkpoints/yolov3.tf

After executing the command successfully, you can see three new files in the checkpoints directory.

 

 

【yolov3-tiny.weights】(optional)

python convert.py –weights ./data/yolov3-tiny.weights –output ./checkpoints/yolov3-tiny.tf –tiny

 

4) Perform target detection

Detect objects in pictures:

python detect.py –image ./data/cat.jpg

Four kittens were detected: Use a light blue frame to frame the location of the kitten, and annotate the label (category confidence) above the frame. For example, the label detected for the first kitten is cat, and the confidence level is 0.95, that is, it is 95% sure that it is cat.

 

We can specify the image generated after target detection:

 python detect.py –image ./data/cat.jpg

A puppy and a kitten are detected at the same time: the kitten is detected as a cat, 1.00; there is 100% confidence that it is a cat; the puppy is detected as a dog, 0.97; there is 97% confidence that it is a cat It’s a cat;

 

We can also try to use the camera for real-time target detection, or perform target detection on video files. For details, see below:

Summary of target detection execution commands:

#yolov3 Detect objects in images
python detect.py --image ./data/cat.jpg

# yolov3-tiny
python detect.py --weights ./checkpoints/yolov3-tiny.tf --tiny --image ./data/street.jpg

# webcam camera detects objects in real time
python detect_video.py --video 0

# video file object to detect video files
python detect_video.py --video path_to_file.mp4 --weights ./checkpoints/yolov3-tiny.tf --tiny

# video file with output
python detect_video.py --video path_to_file.mp4 --output ./output.avi

 

Call the core code of the model

detect.py code:#yolov3 Detect objects in images

import time
from absl import app, flags, logging
from absl.flags import FLAGS
import cv2
import numpy as np
import tensorflow as tf
from yolov3_tf2.models import (
    YoloV3, YoloV3Tiny
)
from yolov3_tf2.dataset import transform_images, load_tfrecord_dataset
from yolov3_tf2.utils import draw_outputs

flags.DEFINE_string('classes', './data/coco.names', 'path to classes file')
flags.DEFINE_string('weights', './checkpoints/yolov3.tf',
                    'path to weights file')
flags.DEFINE_boolean('tiny', False, 'yolov3 or yolov3-tiny')
flags.DEFINE_integer('size', 416, 'resize images to')
flags.DEFINE_string('image', './data/girl.png', 'path to input image')
flags.DEFINE_string('tfrecord', None, 'tfrecord instead of image')
flags.DEFINE_string('output', './output.jpg', 'path to output image')
flags.DEFINE_integer('num_classes', 80, 'number of classes in the model')


def main(_argv):
    physical_devices = tf.config.experimental.list_physical_devices('GPU')
    for physical_device in physical_devices:
        tf.config.experimental.set_memory_growth(physical_device, True)

    if FLAGS.tiny:
        yolo = YoloV3Tiny(classes=FLAGS.num_classes)
    else:
        yolo = YoloV3(classes=FLAGS.num_classes)

    yolo.load_weights(FLAGS.weights).expect_partial()
    logging.info('weights loaded')

    class_names = [c.strip() for c in open(FLAGS.classes).readlines()]
    logging.info('classes loaded')

    if FLAGS.tfrecord:
        dataset = load_tfrecord_dataset(
            FLAGS.tfrecord, FLAGS.classes, FLAGS.size)
        dataset = dataset.shuffle(512)
        img_raw, _label = next(iter(dataset.take(1)))
    else:
        img_raw = tf.image.decode_image(
            open(FLAGS.image, 'rb').read(), channels=3)

    img = tf.expand_dims(img_raw, 0)
    img = transform_images(img, FLAGS.size)

    t1 = time.time()
    boxes, scores, classes, nums = yolo(img)
    t2 = time.time()
    logging.info('time: {}'.format(t2 - t1))

    logging.info('detections:')
    for i in range(nums[0]):
        logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])],
                                           np.array(scores[0][i]),
                                           np.array(boxes[0][i])))

    img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR)
    img = draw_outputs(img, (boxes, scores, classes, nums), class_names)
    cv2.imwrite(FLAGS.output, img)
    logging.info('output saved to: {}'.format(FLAGS.output))


if __name__ == '__main__':
    try:
        app.run(main)
    except SystemExit:
        pass

 

Hope it helps you. ( •̀ ω •́ )✧

 

 

 

Related Posts

The application cannot start because the parallel configuration of the application is incorrect

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*