本文仅做记录,供本人私下查阅。
调试Faster RCNN的初衷是进行光学镜片表面疵病质检项目,找到了一个Faster RCNN开源代码,github地址:https://github.com/jwyang/faster-rcnn.pytorch,master分支使用的是pythorc0.4.0,与GPU服务器上的环境不匹配导致CUDA编译失败。最后选择的是pytorch-1.0分支,在GPU服务器上调试成功,项目位于根目录下,项目名称为faster-rcnn.pytorch-pytorch-1.0,使用的是anaconda虚拟环境lvcai(位于~/anaconda2/envs)。
在环境匹配之后,根据readme教程调试Faster RCNN的过程比较顺利,后面换成自己的数据集训练的过程中,存在一些问题,此处简要记录以便后续查阅。
问题1: 前期使用master分支时,因为torch版本较高,导致CUDA编译不通过,另外还会导致torch.cuda.is_available() 是False,导致代码跑不起来。
记录制作自己数据集的过程
此处使用的是 [飞粤云端2018]广东工业智造大数据创新大赛—智能算法赛 的数据,因为比赛已经结束,数据无法下载,在自己的百度网盘中存有这份数据‘lvcaidata’, 百度云链接: https://pan.baidu.com/s/17ZqPss30HnktdEKdYF7gkg 提取码: ji7g 。
使用的数据为赛制第二阶段的数据,数据格式为:
每种瑕疵中均包含图片和其对应的json文件。其中单瑕疵图片中是分类别的,多瑕疵图片没有分类别(上面的截图中没有图片)。
第一步: 新建一个voc2007文件夹,并有这样的结构。
第二步: 将自己数据集中的图片都移动到VOC2007/JPEGImages/目录下,将所有json汇总到一起,汇总到一个临时文件夹中
import os
import shutil
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# 将图片都移动到 VOC2007/JPEGImages/目录下
path = "/guangdong_round2_train_20181011/单瑕疵图片/"
dst_path = "voc2007/JPEGImages/"
files = os.listdir(path)
for f in files:
print(f.decode('utf-8'))
path2 = path + f.decode('utf-8') + '/'
print(path2)
if os.path.isdir(path2):
all_files = os.listdir(path2)
for af in all_files:
if af.endswith('.jpg'):
print(af.decode('utf-8'))
print(path2+af.decode('utf-8'))
shutil.move(path2+af.decode('utf-8'), dst_path)
path = "lvcaidata/guangdong_round2_train_20181011/多瑕疵图片/"
dst_path = "/voc2007/JPEGImages/"
files = os.listdir(path)
for f in files:
print(f.decode('utf-8'))
path2 = path + f.decode('utf-8')
print(path2)
if path2.endswith('.jpg'):
shutil.move(path2, dst_path)
# 将所有json汇总到一起,汇总到一个临时文件夹中
path = "guangdong_round2_train_20181011/单瑕疵图片/"
dst_path = "guangdong_round2_train_20181011/json/"
files = os.listdir(path)
for f in files:
print(f.decode('utf-8'))
path2 = path + f.decode('utf-8') + '/'
print(path2)
if os.path.isdir(path2):
all_files = os.listdir(path2)
for af in all_files:
if af.endswith('.json'):
print(af.decode('utf-8'))
print(path2+af.decode('utf-8'))
shutil.move(path2+af.decode('utf-8'), dst_path)
path = "guangdong_round2_train_20181011/多瑕疵图片/"
dst_path = "guangdong_round2_train_20181011/json"
files = os.listdir(path)
for f in files:
print(f.decode('utf-8'))
path2 = path + f.decode('utf-8')
print(path2)
if path2.endswith('.json'):
shutil.move(path2, dst_path)
第三步: 处理json文件,生成xml文件,并生成训练集、验证集和测试集
# -*- coding=utf-8-*-
import os
import numpy as np
import codecs
import json
from glob import glob
import cv2
import shutil
from sklearn.model_selection import train_test_split
label_warp = <