[PYTHON] How to make a model for object detection using YOLO in 3 hours

I will teach you how to make a model for object detection using YOLO in 3 hours.

It's a mess, but I'll show you how to detect an object very easily.

The tool used to create this time

Referenced article (really grateful)

--Learning YOLO original data http://takesan.hatenablog.com/entry/2018/08/16/013452 --Learning YOLO v3 original data on Google Colaboratory https://qiita.com/emi-cd/items/60e8fe877dbedb2cfae7 --Counting objects using darknet (YOLO) on Google Colab [Image recognition] https://wakuphas.hatenablog.com/entry/2018/09/19/025941

How to make a model

What is YOLO in the first place? What is object detection? Please refer to the above for answers to questions such as. This article is also included in the 3 hours of creation, so please forgive me though there are some places where it is broken.

① Prepare teacher data and perform annotation work with labelImg

Prepare simple teacher data. In my case, I wanted to detect the greenhouse this time, so I prepared 30 images for the time being. Like this. The aerial photograph of Google Maps is so beautiful![Screenshot 2019-12-20 20.37.09.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/285361 /aa7b62c1-f85c-4b13-b543-35e0fe032768.png)

We will perform annotation work (labeling) on it. This time I used a tool called labelImg. There are other tools called VoTT and BBox-Label-Tool, but VoTT supports YOLO, but if it doesn't work, it's difficult to start over, and BBox is troublesome to set up when trying to create multiple classes. So I chose labelImg.

Execute the following command to make labelImg ready for use. If you have Python3 installed, you can skip line 3.4.

git clone https://github.com/tzutalin/labelImg.git sudo apt install pyqt5-dev-tools sudo apt install python3-pip sudo pip3 install lxml cd labelImg make qt5py3

Create a folder with any name in the labelimg folder and move the prepared image there. (Vinyl, etc. for greenhouses) Then modify predefined_class.txt in labelimg / data. At first, about 20 classes are written in advance, but this time, delete all the class names that have already been written, and write the class name with a line break with your favorite name. I wrote a line with vinyl.

python3 labelimg.py Run labelImg by. Cut for internal work. Roughly speaking

  1. Open the directory you want to use
  2. Use Change Save Dir to specify a text file that describes the coordinate information of the label (created a folder such as vinyl_txt in the labelimg folder).
  3. Click Pascal VOC under Save and change to YOLO
  4. After that, D-> drag the mouse to enclose the object-> w press to confirm the label position-> Save to save-> D to press the next image ...

I will label it like this. 30 sheets is an instant. This completes the teacher data preparation.

② Preparation for learning

We will prepare for learning. To use YOLO, install a framework called darknet. This framework works pretty well with YOLO. (Rather, is it used by YOLO developers?) Install from the following command.

git clone https://github.com/pjreddie/darknet.git

Next, move the images used with labelImg to darknet / data / images and the text files created with labelImg to darknet / data / labels. In that state at / darknet python3 process.py To execute. This separates the teacher data into a teacher file and a learning file.

We will do various work from here. Enumerate. -Create /darknet/data/images/obj.names and enter the name of the label you want to identify (vinyl) -Do the same for /darknet/data/images/obj.list -Set the classes on line 244 of /darknet/cfg/yolo-obj.cfg to 1 as above, and set the filters on line 237 to (classes + coord + 1) * 5. In this case it is 30. -Max_batches on the 20th line of /darknet/cfg/yolo-obj.cfg is the number of times to actually learn. The ideal is classes * 2000, so in this case 2000.

If you don't have /darknet/cfg/yolo-obj.cfg, copy and edit /darknet/cfg/yolov2-voc.cfg. 3rd line: Set batch = 64. The number of images used for each learning step. You can comment out and turn on line 6 4th line: Set subdivisions = 8. The batch is divided by 8. You can comment out and turn on line 7

Finally, modify /darknet/cfg/obj.data as follows: classes=1 train = data/images/train.txt valid = data/images/test.txt labels = data/images/obj.names backup = backup/

https://pjreddie.com/media/files/darknet19_448.conv.23 The preparation is completed by downloading.

③ Start learning using Google Colabatory

It's finally learning. In the current state ./darknet detector train cfg/obj.data cfg/yolo-obj.cfg darknet19_448.conv.23 It's okay to start learning as a CPU, but it takes an unbelievably long time with a CPU and I'm desperate. Therefore, here, we will learn by GPU using Google Colaboratory.

Google Colaboratory is a tool that allows you to use the GPU for free, and it is very friendly because it is basically equipped with tools for deep learning. AI beginners should definitely use it. (Only when you do not use information that cannot be disclosed because it is open source.)

Upload the darknet folder you were working on to drive. After that, save the file related to Google Collaboration from the following and do what is written in .ipynb. git clone https://github.com/foifoi1201/yolo.git

If your post is correct, this should complete the model creation. The rest is based on the downloaded .weights ./darknet detect cfg/obj.cfg obj_final.weights data/***.jpg I think that it can be detected by executing.

result!

If you try to create an object detection model with 30 teacher data within 3 hours, the result will be like this.

スクリーンショット 2019-12-21 2.42.53.png

I can confirm it. .. ..

Object detection using YOLO is very troublesome to create, and in fact, if you do it this way, you may not be able to do everything well. Besides, Google Colabatory is constantly updated, so it is possible that the same creation method will not work over time. (Actually, it seems that the update was done this time as well, and it took more time than expected to create it. Thank you very much for the references ,,,,,,)

I hope it will be of some help to the same AI beginners as myself.

Recommended Posts

How to make a model for object detection using YOLO in 3 hours
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
How to make a Python package using VS Code
How to execute a command using subprocess in Python
How to define multiple variables in a python for statement
How to make a Python package (written for an intern)
How to generate a query using the IN operator in Django
I tried to make a stopwatch using tkinter in python
How to make a Japanese-English translation
How to make a slack bot
How to make a crawler --Advanced
How to make a recursive function
How to make a deadman's switch
[Blender] How to make a Blender plugin
A light introduction to object detection
How to make a crawler --Basic
How to make a string into an array or an array into a string in Python
I tried to implement anomaly detection using a hidden Markov model
[Introduction to Python] How to use the in operator in a for statement?
[For beginners] How to register a library created in Python in PyPI
Spigot (Paper) Introduction to how to make a plug-in for 2020 # 01 (Environment construction)
How to make a unit test Part.1 Design pattern for introduction
[Python] How to make a class iterable
How to draw a graph using Matplotlib
How to install a package using a repository
How to get a stacktrace in python
How to make a Backtrader custom indicator
How to make a Pelican site map
How to move BufferImageStim object in PsychoPy
How to generate a new loggroup in CloudWatch using python within Lambda
I learned scraping using selenium to make a horse racing prediction model.
How to get a value from a parameter store in lambda (using python)
How to make a container name a subdomain and make it accessible in Docker
How to run a quickstart for Google Cloud Text-to-Speech .NET in Unity
How to make a request to bitFlyer Lightning's Private API in Go language
I tried to make PyTorch model API in Azure environment using TorchServe
How to make a dialogue system dedicated to beginners
How to code a drone using image recognition
How to make Spigot plugin (for Java beginners)
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a function object from a string
If you want to display values using choices in a template in a Django model
How to create a JSON file in Python
How to make a dictionary with a hierarchical structure.
How to generate a Python object from JSON
How to implement a gradient picker in Houdini
How to make a QGIS plugin (package generation)
[Cloudian # 10] Try to generate a signed URL for object publishing in Python (boto3)
Continue to make stock price forecast AI for 10 hours a day 1st month
How to write a ShellScript Bash for statement
How to notify a Discord channel in Python
How to create a shortcut command for LINUX
I read "How to make a hacking lab"
[Python] How to draw a histogram in Matplotlib
Tips for using ElasticSearch in a good way
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
Let's make a module for Python using SWIG
How to upload to a shared drive using pydrive