问题及解决方案
问题一
creat_list.py执行后,生成的trainval.txt和test.txt为空文件夹。
![6a79c75f9002b277434c44bf696bb265.png](https://i-blog.csdnimg.cn/blog_migrate/284bced4ab6bf93918d57fb6cf290213.png)
解决方法
标准的trainval.txt文件格式,有".jpg"+".xmi"的照片对应信息。
![b20de58004740d0e3db53022a5f4fe96.png](https://i-blog.csdnimg.cn/blog_migrate/290f16e5bee133dc4249c208b0ca8e6d.png)
目前不知道是什么原因creat_list生成的txt文件为空,但是我们可以自己写一个creat_txt.py文件代替creat_list.py文件,将其进行补全:
1.创建creat_txt.py:
import osimport reimport randomdevkit_dir = './'output_dir = './'def get_dir(devkit_dir, type): return os.path.join(devkit_dir, type)def walk_dir(devkit_dir): filelist_dir = get_dir(devkit_dir, 'ImageSets/Main') annotation_dir = get_dir(devkit_dir, 'Annotations') img_dir = get_dir(devkit_dir, 'JPEGImages') trainval_list = [] train_list = [] val_list = [] test_list = [] added = set() for _, _, files in os.walk(filelist_dir): for fname in files: print(fname) img_ann_list = [] if re.match('trainval.txt', fname): img_ann_list = trainval_list elif re.match('train.txt', fname): img_ann_list = train_list elif re.match('val.txt', fname): img_ann_list = val_list elif re.match('test.txt', fname): img_ann_list = test_list else: continue fpath = os.path.join(filelist_dir, fname) for line in open(fpath): name_prefix = line.strip().split()[0] print(name_prefix) added.add(name_prefix) #ann_path = os.path.join(annotation_dir, name_prefix + '.xml') ann_path = annotation_dir + '/' + name_prefix + '.xml' print(ann_path) #img_path = os.path.join(img_dir, name_prefix + '.jpg') img_path = img_dir + '/' + name_prefix + '.jpg' assert os.path.isfile(ann_path), 'file %s not found.' % ann_path assert os.path.isfile(img_path), 'file %s not found.' % img_path img_ann_list.append((img_path, ann_path)) print(img_ann_list) return trainval_list, train_list, val_list, test_listdef prepare_filelist(devkit_dir, output_dir): trainval_list = [] train_list = [] val_list = [] test_list = [] trainval, train, val, test = walk_dir(devkit_dir) trainval_list.extend(trainval) train_list.extend(train) val_list.extend(val) test_list.extend(test) #print(trainval) with open(os.path.join(output_dir, 'trainval.txt'), 'w') as ftrainval: for item in trainval_list: ftrainval.write(item[0] + ' ' + item[1] + '\n') with open(os.path.join(output_dir, 'train.txt'), 'w') as ftrain: for item in train_list: ftrain.write(item[0] + ' ' + item[1] + '\n') with open(os.path.join(output_dir, 'val.txt'), 'w') as fval: for item in val_list: fval.write(item[0] + ' ' + item[1] + '\n') with open(os.path.join(output_dir, 'test.txt'), 'w') as ftest: for item in test_list: ftest.write(item[0] + ' ' + item[1] + '\n')if __name__ == '__main__': prepare_filelist(devkit_dir, output_dir)
2.运行py程序:
!python dataset/voc/creat_txt.py
3.生成结果:
![97fa00c84b802f6aa698d9c7a77b8fae.png](https://i-blog.csdnimg.cn/blog_migrate/f48f26621a8d4cdd594cc2e2254ddf45.png)
问题二
执行train.py报错:
![90800bc514c72a524eb6e2e1846410ea.png](https://i-blog.csdnimg.cn/blog_migrate/4d5ead7487f2befd8c6396c9c640a6da.png)
该报错说明该函数本应该接收到2个参数,但只接收到一个参数,所以我们需要找到该参数,给该参数进行赋值即可。
查看报错函数:
![ca80407c27dde0b6f9a9ae7b65073fde.png](https://i-blog.csdnimg.cn/blog_migrate/68d8cde5f2cc312604de3443bf9820a3.png)
解决方法
在voc.py中找到该函数,将“None”改为自己对应的"img_path"(照片的路径)和"anno_path"(label的路径)。
![fcc0a593ab2e16714c9ded66ef820d64.png](https://i-blog.csdnimg.cn/blog_migrate/68e3b045ed4046bbdb68f7bfe7431914.png)
注意:需要将默认的label函数中的标签改为自己的label_list,如下图所示:
![cb8383fec5a41bebd09a3f0f5d44fd87.png](https://i-blog.csdnimg.cn/blog_migrate/35e3b2ab30f80b5e4c9366d2709759cf.png)
![132f4f92fb3f74b37d753c8a3e8ab945.png](https://i-blog.csdnimg.cn/blog_migrate/c3ee4bdde03a580dc321900344595b11.png)