背景
在外实习的时候接到了公司给的毕业设计题目,其中数据由我们组老大帮忙爬取并存入数据库中,我负责做聚类算法分析。
花了24h将所有图片下载完成后,我提取其中200份(现阶段先运行一遍后可能会增加一些)并使用labellmg为图片打上标签,作为训练集。
前置需求
1、首先安装配置好TensorFlow
2、TensorFlow模型源码
通过pip安装pillow,jupyter,matplotlib,lxml 如下:
pip install pillow
3、安装tensorboard:
注意 因为tensorboard所需要的pycocotools不支持Windows所以不能直接pip安装,不过git上的大牛提供了windows版本。安装命令如下:
pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
这里有可能报错:
我的解决方法:
直接下载https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
然后解压到本地F:\coco\cocoapi-master
用notebook++打开 修改F:\coco\cocoapi-master\PythonAPI\setup.py为如下
然后再anaconda Promat中 cd到这个目录,并执行
python setup.py install
这样 pycocotools就安装完成了。
4、编译Protobuf,生产py文件
需要先安装Google的protobuf,下载protoc-3.4.0-win32.zip
打开cmd窗口,cd到models/research/目录下(老版本没有research目录),执行如下:
protoc object_detection/protos/*.proto --python_out=.
然后在F:\Tensorflow\models\research\object_detection\protos中会生成一大堆python文件 如下图所示:
最后测试一下:
python object_detection/builders/model_builder_test.py
注意:如果出现No module named 'object_detection' 这是因为没有将工作目录添加到环境变量中
添加完成后就OK了。
开始训练自己的数据集
1.标注自己的样本图片
这里我们会使用到labelImg这个工具:
LabelImg是一个图形图像注释工具。
它用python编写的,用QT作为图形界面。
注释被按照ImageNet所使用的PASCAL VOC格式存成XML文件。
样例:
其标注后的xml文件内容
-
Train
ImageSets101.jpg
F:\Image\download\Train\ImageSets101.jpg-
Unknown
-
1000
1000
3
0-
glasses
Unspecified
0
0-
24
301
960
716
2.标注完成后,需要将xml转为csv文件,使用xml_to_csv.py,生成eval.csv验证集和train.csv训练集
代码如下:
importosimportglobimportpandas as pdimportxml.etree.ElementTree as ETdefxml_to_csv(path):
xml_list=[]#读取注释文件
for xml_file in glob.glob(path + '/*.xml'):
tree=ET.parse(xml_file)
root=tree.getroot()for member in root.findall('object'):
value= (root.find('filename').text,
int(root.find('size')[0].text),
int(root.find('size')[1].text),
member[0].text,
int(member[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text)
)