[Python] I tried using YOLO v3

Introduction

In the previous article "Making a cat detector using OpenCV", there are some examples that worked and some that did not. When I was investigating whether I could improve the accuracy somehow, I found a method called YOLOv3 and tried it (in short, it was less accurate than I expected, so I was disappointed ...). The word YOLO was mentioned in the hackathon I experienced in the previous article, but I was assigned another task, so the hackathon ended without touching YOLO, so it was a good study, so I wrote it in the article. I will write it.

What is YOLO v3

YOLO is an abbreviation for You Only Look Once. it's interesting! Even if you don't build a model from scratch with NN (neural network), YOLO is okay, and if you download YOLO, you can build it, so you can just pass it through. Also, since it can be linked not only with images but also with Web cameras, it is possible to detect in real time, so I felt that it would be useful in various situations in images. YOLO official website

You can actually do this with YOLO. Screen_Shot_2018-03-24_at_10.48.42_PM.png In this way, it is possible to detect an object and label something. Let's try on the images that the cat detector couldn't actually do! !!

Use YOLO v3

I referred to this article for the introduction of YOLO. (Running Object Detection Algorithm YOLO V3 on Mac)

Use the terminal. I will explain with the premise that anaconda is installed. If anaconda is not installed, please install it first.

$ conda create -n yolo_v3 python=3.6 pip
$ source activate yolo_v3

You have now created a Python virtual environment. If you do so far

(yolo_v3) $

I think it will be. Next, install the required packages as they are.

(yolo_v3) $ conda install pandas opencv
(yolo_v3) $ conda install pytorch torchvision -c pytorch
(yolo_v3) $ pip install matplotlib cython

In the above reference article, it was stated that the error was that an error occurred when installing matplotlib, but the error is mac in my case, but I think that there was no particular error.

When you are done so far, use'cd'to go down to the folder you want to work on this time and use git clone to import the next file. ** Launch a new command and copy and paste below. ** **

$ git clone https://github.com/ayooshkathuria/pytorch-yolo-v3.git

Also, return to the terminal that is (yolo_v3) earlier

(yolo_v3) $ cd ~~/pytorch-yolo-v3
(yolo_v3) $ wget https://pjreddie.com/media/files/yolov3.weights

** ~~/pytorch-yolo-v3 should be done in the directory corresponding to each individual. ** ** When you do wget, the model will be built automatically. After waiting for a while, YOLO v3 is ready for use.

I actually tried

When you try it, use the command prompt. The execution itself can be done with the command shown below, but you need to put the images you want to try in a file called images. Don't forget to put it in before executing. When I cloned github, there were some sample images, so I think you can try them.

(yolo_v3) $ python detect.py --images imgs --det det

I got an error ...

In my case, I got the following error, so I searched again and found a solution. I was able to resolve the RuntimeError in pytorch-yolo-v3

Traceback (most recent call last):
File "detect.py", line 234, in
output = torch.cat((output,prediction))
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 8 and 86 in dimension 1 at d:\build\pytorch\pytorch-1.0.1\aten\src\th\generic\thtensormoremath.cpp:1307

Apparently it worked by deleting the file " pytorch-yolo-v3/util.py " and replacing it with util.py in the link below. This link

How to save this util.py on your PC

  1. Open the link
  2. Right-click on Raw
  3. Click `Save Link As As`` (I made it util.py without changing the name)
  4. Paste so that it becomes " pytorch-yolo-v3/util.py "

result

I first tried the result with the default image.

You can label the horse in the image! It's amazing lol

continue

It can be identified as a traffic light, car, truck, etc.

I tried it with an image that didn't really work

Last time I turned diagonally and didn't respond, but this time it labeled with cat! !!

Last time, only one of the three cats could be detected, but all three cats could be detected, and even the car could be detected, so I felt that the accuracy was very good.

An example that still didn't work

Consideration

Bad photos are misrecognized, but I thought the recognition was very accurate. Also, I often heard about YOLO, so I tried it and thought that the accuracy was good. In addition to cats, I tried it with photos of the very crowded news of Shibuya Halloween, and I got a surprisingly good impression of the accuracy even if the photos are far away or only part of them are shown. It's not a consideration, but I thought it was very good that I was able to absorb quite a bit by myself after researching a lot about YOLO that I couldn't absorb in the hackathon. Of the three models handled by Hackerson, face recognition, whole body recognition, and gait recognition (skeleton recognition), face recognition with OpenCV, which was dealt with in the previous article, and whole body recognition with YOLO v3, which was dealt with this time, have been cleared.

Next, I would like to investigate and implement OpenPose, HumanPose, etc. used for skeleton authentication and write an article! Also, somebody in the laboratory or someone who follows me requested an article about basic mathematics used in machine learning, so I will write about mathematics someday (maybe by the end of the year?)! What should I write about ...? I haven't decided yet, but w

Thank you for reading this far. If you like, please follow us and LGTM.

reference

Object detection with YOLO or OpenCV Running object detection algorithm YOLO V3 on Mac I was able to resolve the RuntimeError in pytorch-yolo-v3 pytorch-yolo-v3

Recommended Posts

[Python] I tried using YOLO v3
[Python] I tried using OpenPose
I tried using YOUTUBE Data API V3
I tried using Bayesian Optimization in Python
I tried using UnityCloudBuild API from Python
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried using Summpy
I tried Python> autopep8
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried Python> decorator
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
vprof --I tried using the profiler for Python
I tried web scraping using python and selenium
I tried using the Datetime module by Python
I tried scraping with Python
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using Random Forest
I tried using BigQuery ML
I tried Python C extension
I tried using magenta / TensorFlow
I tried gRPC with Python
I tried scraping with python
I tried using AWS Chalice
I tried using Slack emojinator
[Python] I immediately tried using Pylance's VS Code extension.
I tried using TradeWave (BitCoin system trading in Python)
[Python] I tried running a local server using flask
I tried drawing a pseudo fractal figure using Python
I tried to access Google Spread Sheets using Python
I tried using Rotrics Dex Arm # 2
I made a Line-bot using Python!
I tried to touch Python (installation)
I tried web scraping with python.
I tried using Rotrics Dex Arm
I tried using GrabCut of OpenCV
[Python scraping] I tried google search top10 using Beautifulsoup & selenium
I tried server-client communication using tmux
Miscellaneous notes that I tried using python for the matter
[Python] I tried collecting data using the API of wikipedia