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.
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. 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! !!
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.
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
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
" pytorch-yolo-v3/util.py "
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.
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.
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.
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