[python] The tragedy caused by os.listdir

Recently, I am running some target detection networks on my own data set, so I need to process the data set into a format that conforms to the network input. One of the networks is to write the true value labels of the pictures into the json file in the order of the pictures. The picture names of my dataset are 0.jpg, 1.jpg, 2.jpg..., and the label files are 0.txt, 1. txt, 2.txt..., and then use os.listdir to read all the files under the label folder. I directly added 1 to the corresponding picture serial number at that time in the for loop (this caused the label and the picture to not correspond). The correspondence between labels and pictures in the final generated json file becomes:

Label picture
0.txt 0.txt
1.txt 1.txt
10.txt 2.txt
100.txt 3.txt
1000.txt 4.txt

When I tested it on the test set after training the network with such labels, the detected targets were very confusing, and the places where there were no targets at all were judged to be targets, and then I found the problem when I went back to check the data set. A line of code for sorting is added after the original os.listdir, so that the labels and pictures can be matched.

    ann_list = os.listdir(ann_root)
    ann_list.sort(key=lambda x: int(x[:-4]))  ### 新加的,否则写入json的文件顺序为0、1、10、100、1000.。。

A bloody lesson, be careful when using os.listdir in the future! ! !

Guess you like

Origin blog.csdn.net/zylooooooooong/article/details/121262963