YOLO_v3 code analysis and related precautions

[TOC]

1. Project introduction

$~~~~~~~$ This YOLO_v3 project comes from the project translated by the Heart of the Machine --- PyTorch project from scratch: YOLO v3 target detection implementation and PyTorch project from scratch: YOLO v3 target detection implementation (below ) consists of two parts, the original blog is here Series: YOLO object detector in PyTorch , the GitHub address of the original blog is: ayooshkathuria/pytorch-yolo-v3 , and the address of the paper is attached at the end: YOLOv3: An Incremental Improvement

2. Explanation of project requirements and related documents

$~~~~~~~$YOLO_v3's official original code is written in C language, I really admire the author's ability to code by hand, but since the source code does not have any relevant comments, it is very troublesome to read, so I refer to the online about YOLO_v3 migrated to the code of $Tensorflow$ and $Pytorch$ for reading, so as to have a deeper understanding and understanding of the code.

$~~~~~~~$ Regarding the $Pytorch$ code of this YOLO_v3, the corresponding requirements mentioned in the GitHub of the $Pytorch$ author are as follows:

  • Python3.5
  • OpenCV
  • Pytorch 0.4 ( where the author mentions: $Using$ $PyTorch$ $0.3$ $will$ $break$ $the$ $detector.$ )

$~~~~~~~$ After preparing the corresponding things, download the corresponding project $Clone$ $or$ $downoad$, in addition, you first need to download $weights$ $file$ (weight file), The address is as follows: a weightsfile only for COCO , the size is 237M, the downloaded weight file only needs to be placed in the downloaded $project$ root directory, the weight file name is: yolov3.weights

$~~~~~~~$ When everything is ready, you can perform image detection. If you want to detect images, you can find $Terminal$ in the bottom bar of $Pycharm$, which is $CMD$, and set the address $cd$ to The address of the project you downloaded, such as: D:\Software installation\pycharm\PyCharm Community Edition 2017.3.3\workplace\yolo_tensorflow-master> , then enter in $Terminal$: python detect.py --images imgs -- det det command, where --images represents the folder where images are stored or loaded, and --det represents the folder where the detected images will be saved after detecting the images. Of course, if you need to check the meaning of other tags (for example: - -bs ), you can check the parameters defined under the arg_parse() function by typing: python detect.py -h on the command line or by directly opening the detect.py file in $project$ . Even running the detect.py file directly can perform image detection.

$~~~~~~~$ If you want to detect video, you can run video.py to detect it, to run this detector on a video or webcam, the code can basically remain the same, just we won't iterate over the batch , but iterates over the frames of the video.

$~~~~~~~$ The code to run the detector on the video can be found in the video.py file in our GitHub. This code is very similar to the code in detect.py, with a few differences. It should be noted here that if you want to detect the video, you need to put the video file into the root directory of $projiect$, and the default parameters defined for the video in the video.py file are: default = "video.avi", So, you need to change the name of your video file or change the default settings of the parameters.

3. Detailed analysis of source code

$~~~~~~~$PASS

4. Problems in the source code

$~~~~~~~$ After downloading the project, I found that the source code has some pits, so that some codes cannot be run. Here are the pits encountered temporarily and the solutions.

4.1 Problems encountered in video detection:

$~~~~~~~$ This is not a project: The problem encountered in video detection in YOLO_v3_tutorial_from_scratch -master , but the problem encountered in video detection in project yolo_tensorflow-master , just here all the problems related to YOLO code They are all listed. In the test.py of the project yolo_tensorflow-master , the code in lines 201-203 is the code for camera detection: detect from camera In the code: cap = cv2.VideoCapture(0) , the original GitHub: hizhangp/yolo_tensorflow code The middle is: cap = cv2.VideoCapture(-1) , when the camera is turned on, the image displayed by the camera is full of snow noise on the screen, and nothing can be detected. You need to change the parameter -1 in parentheses to 0 to start normally. camera to detect. For the interpretation of this project yolo_tensorflow-master , please refer to Zhihu related columns: YOLO source code analysis Author: Goutoushan Renqi

4.2 Problems encountered in image detection:

$~~~~~$ The problems encountered here are very many and complicated. It took me a lot of energy to modify the code correctly to run normally. I will list them all here in case I encounter similar situations in the future.
$
~~~~~$Alt text

4.2.1 The problem of absolute path and relative path

$~~~~~~~$ When I first started running the image detection code (project: YOLO_v3_tutorial_from_scratch-master ), an error was reported directly, and the reported error was:

$~~~~~$AttributeError: 'NoneType' object has no attribute 'shape'
$
~~~$ ! ! Don't know the wrong kind of mistakes at all! !
$
~~~~~$Alt text

$~~~~~~~$ After a bunch of Baidu, Google checked and found that it reported an error that an empty object did not have the attribute shape . It may be because the path setting is incorrect, so the returned type is None. Later, it was found through bug exclusion that it was line 126 of the code : imlist = [osp.join(osp.realpath('.'), images, img) for img in os.listdir(images)] , this line of code is mainly used to generate The absolute address where the image is located:

D:\Software Installation\pycharm\PyCharm Community Edition 2017.3.3\workplace\pytorch-yolo-v3-master\imgs\dog.jpg
D:\Software Installation\pycharm\PyCharm Community Edition 2017.3.3\workplace\pytorch-yolo -v3-master\imgs\eagle.jpg
D:\Software Installation\pycharm\PyCharm Community Edition 2017.3.3\workplace\pytorch-yolo-v3-master\imgs\giraffe.jpg
D:\Software Installation\pycharm\PyCharm Community Edition 2017.3.3\workplace\pytorch-yolo-v3-master\imgs\herd_of_horses.jpg
D:\software installation\pycharm\PyCharm Community Edition 2017.3.3\workplace\pytorch-yolo-v3-master\imgs\img1.jpg
D:\Software Installation\pycharm\PyCharm Community Edition 2017.3.3\workplace\pytorch-yolo-v3-master\imgs\img2.jpg
D:\Software Installation\pycharm\PyCharm Community Edition 2017.3.3\workplace\pytorch-yolo -v3-master\imgs\img3.jpg
D:\Software Installation\pycharm\PyCharm Community Edition 2017.3.3\workplace\pytorch-yolo-v3-master\imgs\img4.jpg
D:\Software Installation\pycharm\PyCharm Community Edition 2017.3.3\workplace\pytorch-yolo -v3-master\imgs\lisa.jpg
$~~~~~~~$Alt text

$~~~~~$Here I realize that the error here may not be the error of the author's source code, but that the author obtains the absolute path when using this sentence osp.join(osp.realpath('.') , and my absolute path contains Chinese!!!!
$
~~~~~$Alt text

$~~~~~~~$ So in order to solve this problem, I need to change the absolute path to a relative path to avoid the headache of Chinese (in other words, a lot of bug -related problems are that the path is in Chinese!) , the modification method is to modify the above code to: osp.join(osp.relpath('.') , where rel refers to relative .


4.2.2 The problem of slash '/' and backslash '\'

$~~~~~~~~$ After the above problem is solved, run the code and find that the code is running normally, but the folder where the detected image is saved at the end --det does not have any images stored in it, and later locates the code for storing the image:

det_names = pd.Series(imlist).apply(lambda x: "{}/det_{}".format(args.det,x.split("/")[-1]))
list(map(cv2.imwrite, det_names, orig_ims))

$~~~~~~~$ Each image is saved as "det_" followed by the image name. We create a list of addresses, which is where we save our detection result images. Finally, write the image with the detection result to the address in det_names .

$~~~~~~~$ where det_name prints out the result as follows:

0 det / det _. \ Imgs \ dog.jpg
1 det / det _. \ Imgs \ eagle.jpg
2 det / det _. \ Imgs \ giraffe.jpg
3 det / det _. \ Imgs \ herd_of_horses.jpg
4 det / det _. \ imgs \ img1.jpg
5 det / det _. \ imgs \ img2.jpg
6 det / det _. \ imgs \ img3.jpg
7 det / det _. \ imgs \ img4.jpg
8 det / det _. \ imgs \ lisa.jpg
9 det / det _. \ imgs \ messi.jpg
10 det / det _. \ imgs \ person.jpg
11 det / det _. \ imgs \ scream.jpg
12 det / det _. \ imgs \ vagaa.jpg

$~~~~~~~$ and the result printed by imlist is:

['.\imgs\dog.jpg','.\imgs\eagle.jpg', '.\imgs\giraffe.jpg','.\imgs\herd_of_horses.jpg', '.\imgs\img1.jpg','.\imgs\img2.jpg', '.\imgs\img3.jpg',

$~~~~~~~$ Next code: list(map(cv2.imwrite, det_names, orig_ims)) :

$~~~~~$1. cv2.imwrite() function: The function to save the image, there are two parameters, the first is the name of the saved file, and the second is the read-in image.
$
~~~$2. orig_ims : The array matrix wrapped by the list, the type is unsigned 8-bit dtype=uint8 , so it is the encoding matrix of the picture.
$
~~~~~$3. map() function: The specified sequence will be mapped according to the provided function. The first argument function calls the function function with each element in the argument sequence, returning a new list containing the return value of each function function. The syntax is: map(function, iterable, ...) , where function -- a function with two parameters; iterable -- one or more sequences, for example: map(square, [1,2,3,4,5 ]) , prints: [1, 4, 9, 16, 25] , where square(x) is a function, return x$^2$ .

$~~~~~~~$ So it can be seen from the above that the map function creates a mapping between cv2.imwrite, det_names, orig_ims , where det_names, orig_ims are used as parameters of cv2.imwrite() , one of which is used as the save file name, A picture as read in.

$~~~~~~~$ Running this code shows:

[False, False, False, False, False, False, False, False, False, False, False, False, False]

$~~~~~~~$ indicates that the mapping failed and the image storage was unsuccessful. The display printed by det_name shows that the failure is due to the existence of a backslash '' in the file name. The backslash has an actual escape meaning in python , so it is necessary to replace the backslash with a slash, otherwise the code when running will run backslashes together.

$~~~~~~~$ The modification method is as follows:

    for i in range(len(imlist)):
    imlist[i] = imlist[i].replace('\\', '/')
    

$~~~~~~~$ It should also be noted here that imlist[i].replace('\', '/') will generate a list to store the modified value, not in the original list imlist[] to modify it.

$~~~~~~~$ The modified det_name is:

0 det / det_dog.jpg
1 det / det_eagle.jpg
2 det / det_giraffe.jpg
3 det / det_herd_of_horses.jpg
4 det / det_img1.jpg
5 det / det_img2.jpg
6 det / det_img3.jpg
7 det / det_img4.jpg
8 det /det_lisa.jpg
9 det / det_messi.jpg
10 det / det_person.jpg
11 det / det_scream.jpg
12 det / det_vagaa.jpg

$~~~~~~~$ At this point, the backslashes have been changed to slashes, the code runs normally, and the det folder also stores the detected images normally, as follows:
Alt text

5. About the author of YOLO_v3

$~~~~~~~$ There is a question in Zhihu about how to evaluate the latest YOLOv3? , very interesting, here are some interesting points:

$~~~~~~~$1. First of all, YOLO_v3's paper is not a paper at all, but more of a technical report, which is quite a joy to read. The author first honestly stated that he did not do any research last year, but mainly spent it on Twitter and playing with GAN.
Alt text
Alt text


$~~~~~~~$2. Secondly, the author of yolo is really strong in coding. Darknet hardly needs any library dependencies. Moreover, the homework may be a fan of My Little Pony, hahaha, very interesting, I feel like a little cute girl. Take a look at his resume, personal homepage, and git commit message to feel the cuteness.
Alt text
Alt text
Alt text

$~~~~~~~$ And he himself looks like this:
Alt text


$~~~~~~~$3. Directly diss a number of algorithms, such as those appearing in his paper:
Alt text

This picture puts itself in the second quadrant, expressing nakedly that you are all spicy chickens. . . .
This picture should be cut by the author from other papers, and then forcibly drawn; the
strength is mocking, I took the picture in Retina, and then diss it.


$~~~~~$4. The end of the paper is full of highlights. I admire that I can write my paper so casually. I envy the big guy:
Alt text
$
~~~~~$ The effect is so good that the paper can be written at will. Anyway, my effect is here. No matter how random the paper is written, you still have to cite it.


$~~~~~$5. And some careful netizens found that when the first model of the model published a research paper on arXiv, the brain circuit clearly cites and uses this paper.
Alt text
$
~~~~~$ And not long after the little brother self-quoted and used it, the arXiv official account announced that the server was down for unknown reasons...
Alt text

6. Completion plan

$~~~~~~~~$ The third part of the article is analyzed in detail, and I will continue to complete it when I have time, to be continued. . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326108581&siteId=291194637