# 毕业设计 **Repository Path**: dalerxli/graduation_project ## Basic Information - **Project Name**: 毕业设计 - **Description**: 基于迁移学习的毕业设计 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-03-03 - **Last Updated**: 2021-03-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 毕业设计 ## 项目总览 ![image-20200710095759941](ReadMe.assets/image-20200710095759941.png) Annotations-标记文件 Images--图像 ​ -- train 训练集图像 ​ -- test 测试集图像 Test--预测模型 Train-训练模型 Tool-工具 进行格式转化 ## 前期环境配置 ### · 安装Tensorflow models 源地址 ``` git clone https://github.com/tensorflow/models.git ``` 码云地址(快速) ``` git clone https://gitee.com/sakura674834119/models.git ``` 注意:现在GitHub上使用的是TensorFlow2.x,所以使用TensorFlow1.x的是不能正确运行的 如果你是TensorFlow1.x,使用下面的版本 ``` git clone -b r1.5 https://github.com/tensorflow/models.git ``` ### · Make sure you have `pycocotools` installed 方案一: 1. ~~Microsoft Visual C++ 14.0 is required.~~ ~~解决方案:安装Microsoft Visual C++ 14.0~~ ~~链接: https://pan.baidu.com/s/1VYMsVlPpMj2WoyQeCKuubQ 提取码: qc8q~~ 2. ~~安装所需要的包~~ ``` pip install PyHamcrest pip install pycocotools ``` 方案二: ```bash pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI ``` ### · Compile protobufs and install the object_detection package · 下载protobufs 地址:https://github.com/protocolbuffers/protobuf/releases · 把bin目录加入环境变量中 · 在./models/research/ 目录下运行 ``` protoc object_detection/protos/*.proto --python_out=. ``` 在./research/object_detection/protos目录下可以看见很多.proto后缀的文件,说明成功 · 完成后在./model/research 目录下执行 ``` python setup.py install ``` 运行pip list 可以看到 `object-detection 0.1`这个包已经安装成功了 ### · Install the TensorFlow Model Garden pip package ``` pip install tf-models-nightly ``` ### · install slim ``` pip install tf-models-nightly ``` 把slim里面的deployment文件夹复制到python安装目录下的Lib\site-packages中 ### · Test #### Test1 · 在/models/research/object_detection下打开jupyter notebook ```bash jupyter notebook --ip=127.0.0.1 --port=8000 pause ``` 把这两行保存为`openjupter.bat`文件,以后直接点击该文件就能在这个目录下打开jupyter了 · 一行一行运行./models/research/object_detection下的`object_detection_tutorial.ipynb` ![image-20200715213004605](ReadMe.assets/image-20200715213004605.png) #### Test2 ​ 切换到models\research\object_detection\legacy下 运行trainer_tf1_test.py ,不报错即可 ## 一、图片打标签 生成XML文件 图片打标签使用的是labelimg_windows_v1.5.2软件 使用方式很简单 [链接: https://pan.baidu.com/s/1vHJd1Gpx3GZ73iJ-VpQuXQ 提取码: 6qql] ![image-20200710100124507](ReadMe.assets/image-20200710100124507.png) 完成标注后的文件 ![image-20200710100157781](ReadMe.assets/image-20200710100157781.png) ## 二、把XML->CVS->TFRecord文件 ### 2.1 xml2csv.py ```python # xml2csv.py # xml2csv.py import os import glob import pandas as pd import xml.etree.ElementTree as ET os.chdir('C:/Users/yqx/Desktop/boat_detection/annotations/test') path = 'C:/Users/yqx/Desktop/boat_detection/annotations/test' def xml_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) ) xml_list.append(value) column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax'] xml_df = pd.DataFrame(xml_list, columns=column_name) return xml_df def main(): image_path = path xml_df = xml_to_csv(image_path) xml_df.to_csv('test.csv', index=None) print('Successfully converted xml to csv.') main() ``` ![image-20200710100321788](ReadMe.assets/image-20200710100321788.png) ### 2.2 generate_tfrecord ```python # generate_tfrecord.py # -*- coding: utf-8 -*- from __future__ import division from __future__ import print_function from __future__ import absolute_import import os import io import pandas as pd import tensorflow as tf from PIL import Image from object_detection.utils import dataset_util from collections import namedtuple, OrderedDict ##只需要修改这一部分路径##################################################### # csv文件位置 csv_input= "C:/Users/yqx/Desktop/boat_detection/annotations/test/test.csv" # tfrecord文件输出路径 output_path= "C:/Users/yqx/Desktop/boat_detection/annotations/test/test.record" # 原图像路径 image_dir= "C:/Users/yqx/Desktop/boat_detection/images/test" ########################################################################### # TO-DO replace this with label map def class_text_to_int(row_label): if row_label == 'boat': # 记得修改标签名称 return 1 else: return None def split(df, group): data = namedtuple('data', ['filename', 'object']) gb = df.groupby(group) return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)] def create_tf_example(group, path): with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid: encoded_jpg = fid.read() encoded_jpg_io = io.BytesIO(encoded_jpg) image = Image.open(encoded_jpg_io) width, height = image.size filename = group.filename.encode('utf8') image_format = b'jpg' xmins = [] xmaxs = [] ymins = [] ymaxs = [] classes_text = [] classes = [] for index, row in group.object.iterrows(): xmins.append(row['xmin'] / width) xmaxs.append(row['xmax'] / width) ymins.append(row['ymin'] / height) ymaxs.append(row['ymax'] / height) classes_text.append(row['class'].encode('utf8')) classes.append(class_text_to_int(row['class'])) tf_example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_example def main(): # python generate_tfrecord.py writer = tf.python_io.TFRecordWriter(output_path) path = os.path.join(image_dir) examples = pd.read_csv(csv_input) grouped = split(examples, 'filename') for group in grouped: tf_example = create_tf_example(group, path) writer.write(tf_example.SerializeToString()) writer.close() output_path = os.path.join(os.getcwd(), output_path) print('Successfully created the TFRecords: {}'.format(output_path)) main() ``` 分别对Test和Train进行转化 ## 三、创建标签文件 ![image-20200710100916792](ReadMe.assets/image-20200710100916792.png) 内容如下 name中为你的类别名称 ```txt item { id: 1 name: 'boat' } ``` ## 四、下载预训练模型 本例中使用下面这个预训练模型 [Faster R-CNN ResNet152 V1 800x1333](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet152_v1_800x1333_coco17_gpu-8.tar.gz) 其他模型也可以在models\research\object_detection\g3doc\tf2_detection_zoo.md中选择查看 ## 五、修改配置文件 复制object_detection/samples/configs下的faster_rcnn_resnet152_v1_800x1333_coco17_gpu-8.config 到 我们的文件夹下,并做如下修改: ```txt # SSD with Mobilenet v1 configuration for MSCOCO Dataset. # Users should configure the fine_tune_checkpoint field in the train config as # well as the label_map_path and input_path fields in the train_input_reader and # eval_input_reader. Search for "PATH_TO_BE_CONFIGURED" to find the fields that # should be configured. model { ssd { num_classes: 1 #根据自己类别数进行修改 box_coder { faster_rcnn_box_coder { y_scale: 10.0 x_scale: 10.0 height_scale: 5.0 width_scale: 5.0 } } …. train_config: { batch_size: 24 optimizer { rms_prop_optimizer: { learning_rate: { exponential_decay_learning_rate { initial_learning_rate: 0.004 decay_steps: 800720 decay_factor: 0.95 } } momentum_optimizer_value: 0.9 decay: 0.9 epsilon: 1.0 } } #检查点位置 自己下载的模型文件夹 fine_tune_checkpoint: "G:/machine_learning/git-Object_detection/models/research/object_detection/ssd_mobilenet_v1_coco_2017_11_17/model.ckpt" from_detection_checkpoint: true # Note: The below line limits the training process to 200K steps, which we # empirically found to be sufficient enough to train the pets dataset. This # effectively bypasses the learning rate schedule (the learning rate will # never decay). Remove the below line to train indefinitely. num_steps: 200000 data_augmentation_options { random_horizontal_flip { } } data_augmentation_options { ssd_random_crop { } } } ######################################################################### train_input_reader: { tf_record_input_reader { input_path: "C:/Users/yqx/Desktop/boat_detection/annotations/train/train.record" } label_map_path: "C:/Users/yqx/Desktop/boat_detection/label_map.pbtxt" } ######################################################################### eval_config: { num_examples: 14 # Note: The below line limits the evaluation process to 10 evaluations. # Remove the below line to evaluate indefinitely. max_evals: 10 } ######################################################################### eval_input_reader: { tf_record_input_reader { input_path: "C:/Users/yqx/Desktop/boat_detection/annotations/test/test.record" } label_map_path: "C:/Users/yqx/Desktop/boat_detection/label_map.pbtxt" shuffle: false num_readers: 1 } ######################################################################### ``` ## 六、开始训练 控制台进入到G:\machine_learning\git-Object_detection\models\research\object_detection\legacy 进行训练 python train.py --train_dir=C:\Users\yqx\Desktop\raccoon_dataset-\train --pipeline_config_path=C:\Users\yqx\Desktop\raccoon_dataset-\ssd_mobilenet_v1_coco.config > train_dir--训练文件夹地址 > > pipeline_config_path--配置信息地址 若是GPU,则需要在train.py文件的第55行,加入下面这句: \#GPU需要加上下面这句,0表示第1块GPU设备 os.environ['CUDA_VISIBLE_DEVICES'] = "0" ![image-20200710101629354](ReadMe.assets/image-20200710101629354.png) ## 七、进行预测 控制台进入到**G:\machine_learning\git-Object_detection\models\research\object_detection\legacy** 进行预测 python eval.py --logtostderr --pipeline_config_path=C:\Users\yqx\Desktop\raccoon_dataset-\ssd_mobilenet_v1_coco.config --checkpoint_dir=C:\Users\yqx\Desktop\raccoon_dataset-\train --eval_dir=C:\Users\yqx\Desktop\raccoon_dataset-\test > checkpoint_dir--训练文件夹地址(检查点) > > eval_dir--测试文件夹地址 ## 八、可视化 在训练一定的时间后,可以在TensorBoard里面进行查看训练的成果 控制台中输入 tensorboard --logdir=C:\Users\yqx\Desktop\boat_detection ## 九、保存训练的模型 导出模型 进入到在object_detection目录,目录下有一个export_inference_graph.py,用于冻结模型 python export_inference_graph.py --pipeline_config_path=C:\Users\yqx\Desktop\boat_detection\ssd_mobilenet_v1_coco.config --trained_checkpoint_prefix=C:\Users\yqx\Desktop\boat_detection\train\model.ckpt-**2987** --output_directory=C:\Users\yqx\Desktop\boat_detection\train -**2987** 这是检查点训练的次数,找到相应检查点记录文件 比如model.ckpt-1817就是训练了1817次。 ## 十、输出测试结果 见test.py ## 参考文献 1. 用TensorFlow训练一个物体检测器 https://blog.csdn.net/chenmaolin88/article/details/79357263 2. 下安装TensorFlow Object Detection API(对象检测API) https://blog.csdn.net/chenmaolin88/article/details/79371891