【Tensorflow】SSD_Mobilenet_v2实现目标检测(一):环境配置+训练

Tensorflow(官方介绍使用)

要求

Tensorflow官方模型库
升级到最新的Tensorflow2

pip install tf-nightly

安装

方法一:安装Tensorflow模型pip包

pip 自动安装所有的模型和依赖项

pip install tf-models-official

若要安装最新的更改则:

pip install tf-models-nightly

方法二:克隆源码文件

1. 克隆 GitHub存储库
git clone https://github.com/tensorflow/models.git

git下载时出现

error: RPC failed; curl 56 GnuTLS recv error (-54): Error in the pull function.
fatal: The remote end hung up unexpectedly
fatal: 过早的文件结束符(EOF)
fatal: index-pack failed
解决方法:

先输入:

 git init

然后再

git config http.postBuffer 524288000
2. 将./models文件夹添加Python路径
export PYTHONPATH=$PYTHONPATH:/path/to/models
3. 安装其他的依赖库
pip install --user -r official/requirements.txt

也可以一个一个安装
其他依赖库参考:requirement.txt
six
google-api-python-client>=1.6.7
google-cloud-bigquery>=0.31.0
kaggle>=1.3.9
numpy>=1.15.4
oauth2client
pandas>=0.22.0
psutil>=5.4.3
py-cpuinfo>=3.3.0
scipy>=0.19.1
tensorflow-hub>=0.6.0
tensorflow-model-optimization>=0.4.1
tensorflow-datasets
tensorflow-addons
dataclasses
gin-config
tf_slim>=1.1.0
Cython
matplotlib
pyyaml>=5.1
#CV related dependencies
opencv-python-headless
Pillow
pycocotools
#NLP related dependencies
seqeval
sentencepiece

安装tensorflow object detection API (笔者的使用方法)

笔者在Ubuntu16.04的服务器进行环境配置

1. 创建anaconda虚拟环境

conda create -n tfssd python=3.6
conda activate tfssd

2. 安装tensorflow-gpu

conda install tensorflow-gpu=1.15.0

3. 安装依赖项

pip install lxml, Cython, contextlib2 , jupyter, matplotlib,  pillow

4. 下载tensorflow models源码

git clone https://github.com/tensorflow/models.git

或者直接下载安装包,进行解压

5. 安装COCO API installation

git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
make install
python setup.py install
cp -r ./cocoapi/PythonAPI/pycocotools ./models-master/research/  # 复制pycocotools到models-master/research/

6. 安装+编译protobuf

方法一:

protobuf下载地址:https://github.com/google/protobuf/releases
笔者下载的是protoc-3.13.0-linux-x86_64.zip,将压缩包进行解压。
在这里插入图片描述

cd /models-master/protoc-3.13.0-linux-x86_64/bin  # 进入bin文件夹
chmod +x configure  # configure没有执行权限,通过chmod给其加上x权限
export PATH=/opt/protoc-3.6.1-linux-x86_64/bin:$PATH  # 增加环境变量
source ~/.bashrc

测试是否安装成功:

cd ~/models-master/research  # 进入tensorflow的项目文件
protoc object_detection/protos/*proto --python_out=.
protoc --version
输出 libprotoc 3.13.0

准备数据集

1. 制作数据集

1.1 VOC数据集

下载VOC数据集,放在 model-master/research/object_detection/images/(images是笔者新建的文件夹用于储存原始数据集)
VOC数据集有五个文件夹
├── Annotations # 存放xml文件,主要是记录标记框位置信息
├── ImageSets # 存放的都是txt文件,txt文件中每一行包含一个图片的名称,末尾会加上+1或者-1表示正负样本
  ├── Action
  ├── Layout
  ├── Main
  └── Segmentation
├── JPEGImages # 存放源图片
├── SegmentationClass
└── SegmentationObject
制作自己的数据集时只需要用到Annotations、ImageSets、JPEGImages三个文件夹
具体制作过程见【VOC数据集】制作

1.2 自己标注的数据集

├──hengfeng # 笔者的目标名
  ├── Annotations # 存放xml文件,主要是记录标记框位置信息
  ├── JPEGImages # 存放源图片

2. 将样本数据转换为TFRecord格式

2.1 数据集划分

新建train_test_split.py把xml文件数据集分为了train、test、validation三部分,并存储在Annotations文件夹中,训练验证集占80%,测试集占20%。训练集占训练验证集的80%。代码如下:

import os  
import random  
import time  
import shutil

xmlfilepath=r'./Annotations'  
saveBasePath=r"./Annotations"

trainval_percent=0.8  
train_percent=0.8  
total_xml = os.listdir(xmlfilepath)  
num=len(total_xml)  
list=range(num)  
tv=int(num*trainval_percent)  
tr=int(tv*train_percent)  
trainval= random.sample(list,tv)  
train=random.sample(trainval,tr)  
print("train and val size",tv)  
print("train size",tr) 

start = time.time()

test_num=0  
val_num=0  
train_num=0  

for i in list:  
    name=total_xml[i]
    if i in trainval:  #train and val set 
        if i in train: 
            directory="train"  
            train_num += 1  
            xml_path = os.path.join(os.getcwd(), 'Annotations/{}'.format(directory))  
            if(not os.path.exists(xml_path)):  
                os.mkdir(xml_path)  
            filePath=os.path.join(xmlfilepath,name)  
            newfile=os.path.join(saveBasePath,os.path.join(directory,name))  
            shutil.copyfile(filePath, newfile)
        else:
            directory="validation"  
            xml_path = os.path.join(os.getcwd(), 'Annotations/{}'.format(directory))  
            if(not os.path.exists(xml_path)):  
                os.mkdir(xml_path)  
            val_num += 1  
            filePath=os.path.join(xmlfilepath,name)   
            newfile=os.path.join(saveBasePath,os.path.join(directory,name))  
            shutil.copyfile(filePath, newfile)

    else:
        directory="test"  
        xml_path = os.path.join(os.getcwd(), 'Annotations/{}'.format(directory))  
        if(not os.path.exists(xml_path)):  
                os.mkdir(xml_path)  
        test_num += 1  
        filePath=os.path.join(xmlfilepath,name)  
        newfile=os.path.join(saveBasePath,os.path.join(directory,name))  
        shutil.copyfile(filePath, newfile)

end = time.time()  
seconds=end-start  
print("train total : "+str(train_num))  
print("validation total : "+str(val_num))  
print("test total : "+str(test_num))  
total_num=train_num+val_num+test_num  
print("total number : "+str(total_num))  
print( "Time taken : {0} seconds".format(seconds))
2.2 把xml转换成csv文件

xml_to_csv.py将生成的csv文件放在 model-master/research/object_detection/data/hengfeng/代码如下:

import os  
import glob  
import pandas as pd  
import xml.etree.ElementTree as ET 

def xml_to_csv(path):  
    xml_list = []  
    for xml_file in glob.glob(path + '/*.xml'):  
        tree = ET.parse(xml_file)  
        root = tree.getroot()
        
        print(root.find('filename').text)  
        for member in root.findall('object'): 
            value = (root.find('filename').text,  
                int(root.find('size')[0].text),   #width  
                int(root.find('size')[1].text),   #height  
                member[0].text,  
                int(member[4][0].text),  
                int(float(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():  
    for directory in ['train','test','validation']:  
        xml_path = os.path.join(os.getcwd(), 'Annotations/{}'.format(directory))  

        xml_df = xml_to_csv(xml_path)  
        # xml_df.to_csv('whsyxt.csv', index=None)  
        xml_df.to_csv('/home/z/work/models-master/research/object_detection/data/hengfeng/hengfeng_{}_labels.csv'.format(directory), index=None)  
        print('Successfully converted xml to csv.')

main()

2.3 生成tfrecord文件

generate_tfrecord.py代码如下

# -*- coding: utf-8 -*-
"""
2020.09.23:alian, 最终可使用版本*********
生成tfrecord文件
"""

"""
Usage:
# Create train data:
python generate_tfrecord.py --csv_input=object_detection/data/hengfeng_train_labels.csv --output_path=object_detection/data/hengfeng_train.tfrecord
# Create val data:
python generate_tfrecord.py --csv_input=object_detection/data/hengfeng_validation_labels.csv --output_path=object_detection/data/hengfeng_validation.tfrecord
# Create test data:
python generate_tfrecord.py --csv_input=object_detection/data/hengfeng_test_labels.csv --output_path=object_detection/data/hengfeng_test.tfrecord
"""

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

os.chdir('/home/lianlirong/models-master/research/')  # 改变当前目录到指定的目录

flags = tf.app.flags
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
FLAGS = flags.FLAGS


# TO-DO replace this with label map
def class_text_to_int(row_label):
        # 你的所有类别——————————————————————————————————————————————————————————
    if row_label == 'Hengfeng':
            return 1

    elif row_label == 'Beijing':
            return 2
    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.io.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:  #  tf.gfile.GFile 替换成 tf.io.gfile.GFile
        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(_):
    writer = tf.io.TFRecordWriter(FLAGS.output_path)
    path = os.path.join(os.getcwd(), './models-master/research/object_detection/images/VOC2007-hengfeng/JPEGImages/')  # 原图所在的路径
    examples = pd.read_csv(FLAGS.csv_input)
    grouped = split(examples, 'filename')
    num = 0
    for group in grouped:
        num += 1
        tf_example = create_tf_example(group, path)
        writer.write(tf_example.SerializeToString())
        if (num % 100 == 0):    # 每完成100个转换,打印一次
            print(num)

    writer.close()
    output_path = os.path.join(os.getcwd(), FLAGS.output_path)
    print('Successfully created the TFRecords: {}'.format(output_path))

if __name__ == '__main__':
    tf.app.run()
    # tf.compat.v1.app.run

主要是在 row_label 这里要添加上你标注的类别,字符串 row_label 应于labelImg中标注的名称相同;同样 path 为图片的路径。

cd  /models-master/research
python generate_tfrecord.py --csv_input=object_detection/data/hengfeng/hengfeng_train_labels.csv --output_path=object_detection/data/hengfeng/hengfeng_train.tfrecord

generate_tfrecord.py需要在research目录下,也就是object_detection的上级目录,因为在脚本中使用了 object_detection.utils,如果在 object_detection 下执行命令会报错(No module named object_detection)。

其实这句命令很好理解,其实就是根据脚本中提供的图片路径,找到图片所在。至于是哪些图片?由csv文件来决定。csv文件主要就是记录图片的名称、类别、以及标记框的坐标。如下图所示:
【Tensorflow】SSD_Mobilenet_v2实现目标检测(一):环境配置+训练_第1张图片

类似的,我们可以输入如下命令,将验证集和测试集也转换为tfrecord格式。

python generate_tfrecord.py --csv_input=object_detection/data/hengfeng/hengfeng_validation_labels.csv --output_path=object_detection/data/hengfeng/hengfeng_validation.tfrecord
python generate_tfrecord.py --csv_input=object_detection/data/hengfeng/hengfeng_test_labels.csv --output_path=object_detection/data/hengfeng/hengfeng_test.tfrecord

训练

1. 在object_detection/data文件夹下创建标签分类的配置文件(object(目标名)_label_map.pbtxt),需要检测几种目标,将创建几个id,代码如下:
 item {
  id: 1    # id 从1开始编号
  name: 'Hengfeng'
}

item {
  id: 2
  name: 'Beijing'
}
2. 配置管道配置文件,找到object_detection/samples/config/ssd_mobilenet_v2_coco.config,复制到data/object/文件夹下。修改后的代码如下:
# SSD with Mobilenet v2 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: 2   ######
    box_coder {
      faster_rcnn_box_coder {
        y_scale: 10.0
        x_scale: 10.0
        height_scale: 5.0
        width_scale: 5.0
      }
    }
    matcher {
      argmax_matcher {
        matched_threshold: 0.5
        unmatched_threshold: 0.5
        ignore_thresholds: false
        negatives_lower_than_unmatched: true
        force_match_for_each_row: true
      }
    }
    similarity_calculator {
      iou_similarity {
      }
    }
    anchor_generator {
      ssd_anchor_generator {
        num_layers: 6
        min_scale: 0.2
        max_scale: 0.95
        aspect_ratios: 1.0
        aspect_ratios: 2.0
        aspect_ratios: 0.5
        aspect_ratios: 3.0
        aspect_ratios: 0.3333
      }
    }
    image_resizer {
      fixed_shape_resizer {
        height: 300
        width: 300
      }
    }
    box_predictor {
      convolutional_box_predictor {
        min_depth: 0
        max_depth: 0
        num_layers_before_predictor: 0
        use_dropout: false
        dropout_keep_probability: 0.8
        kernel_size: 1
        box_code_size: 4
        apply_sigmoid_to_scores: false
        conv_hyperparams {
          activation: RELU_6,
          regularizer {
            l2_regularizer {
              weight: 0.00004
            }
          }
          initializer {
            truncated_normal_initializer {
              stddev: 0.03
              mean: 0.0
            }
          }
          batch_norm {
            train: true,
            scale: true,
            center: true,
            decay: 0.9997,
            epsilon: 0.001,
          }
        }
      }
    }
    feature_extractor {
      type: 'ssd_mobilenet_v2'
      min_depth: 16
      depth_multiplier: 1.0
      conv_hyperparams {
        activation: RELU_6,
        regularizer {
          l2_regularizer {
            weight: 0.00004
          }
        }
        initializer {
          truncated_normal_initializer {
            stddev: 0.03
            mean: 0.0
          }
        }
        batch_norm {
          train: true,
          scale: true,
          center: true,
          decay: 0.9997,
          epsilon: 0.001,
        }
      }
    }
    loss {
      classification_loss {
        weighted_sigmoid {
        }
      }
      localization_loss {
        weighted_smooth_l1 {
        }
      }
      hard_example_miner {
        num_hard_examples: 3000
        iou_threshold: 0.99
        loss_type: CLASSIFICATION
        max_negatives_per_positive: 3
        min_negatives_per_image: 3
      }
      classification_weight: 1.0
      localization_weight: 1.0
    }
    normalize_loss_by_num_matches: true
    post_processing {
      batch_non_max_suppression {
        score_threshold: 1e-8
        iou_threshold: 0.6
        max_detections_per_class: 100
        max_total_detections: 100
      }
      score_converter: SIGMOID
    }
  }
}

train_config: {
  batch_size: 2  ######
  optimizer {
    rms_prop_optimizer: {
      learning_rate: {
        exponential_decay_learning_rate {
          initial_learning_rate: 0.00001   ######
          decay_steps: 800720
          decay_factor: 0.95  ######
        }
      }
      momentum_optimizer_value: 0.9
      decay: 0.9
      epsilon: 1.0
    }
  }
  fine_tune_checkpoint: "/models-master/research/object_detection/ssd_mobilenet_v2_coco_2018_03_29/model.ckpt"
  fine_tune_checkpoint_type:  "detection"
  # 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: 6000  #######
  data_augmentation_options {
    random_horizontal_flip {
    }
  }
  data_augmentation_options {
    ssd_random_crop {
    }
  }
}

train_input_reader: {
  tf_record_input_reader {
    input_path: "/models-master/research/object_detection/data/hengfeng/hengfeng_train.tfrecord"  ###### 建议使用绝对路径
  }
  label_map_path: "/models-master/research/object_detection/data/hengfeng/hengfeng_label_map.pbtxt"  ###### 建议使用绝对路径
}

eval_config: {
  num_examples: 8000
  # 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: "/models-master/research/object_detection/data/hengfeng/hengfeng_validation.tfrecord"  ###### 建议使用绝对路径
  }
  label_map_path: "/models-master/research/object_detection/data/hengfeng/hengfeng_label_map.pbtxt"  ###### 建议使用绝对路径
  shuffle: false
  num_readers: 1
}

修改的部分主要包括:9:标注的类别数目、141:batch_size(建议设置小一点)、146:学习率和退化率、156:预训练权重的路径,162:训练的总步数、175177:训练集和189191:验证集的tfrecord的路径、label_map的路径.

3. 开始训练

输入训练指令:

python object_detection/model_main.py --logtostderr --model_dir=/models-master/logs/ --pipeline_config_path=/models-master/research/object_detection/data/hengfeng/ssd_mobilenet_v2_coco.config

训练时终端显示如下:
【Tensorflow】SSD_Mobilenet_v2实现目标检测(一):环境配置+训练_第2张图片
训练过程就到此结束,后续的测试将在下一篇博客进行说明。
下面整理下以上提到文件的放置位置:(以下的hengfeng为笔者的训练目标)
├──models-master (tensorflow项目文件)
    ├── logs # 笔者存放训练模型的目录
        ├──hengfeng
    ├── research
        ├──generate_tfrecord.py # 生成tfrecord文件的代码
        ├──object_detection
            ├──ssd_mobilenet_v2_coco_2018_03_29
                ├──model.ckpt # 预训练模型文件
            ├──model_main.py # 训练代码文件
            ├──images
                ├──hengfeng # 目标数据集
                    ├──Annotations # xml文件
                    ├──JPEGImages # 原图片
                    ├──train_test_split.py # 划分训练测试集的代码
                    ├──xml_to_csv.py # 生成csv的代码文件
                    ├──others_object …# 其他目标的数据集
            ├──data
                ├──hengfeng # 目标训练的必要文件
                    ├──hengfeng_label_map.pbtxt # 目标标签文件
                    ├──hengfeng_train.tfrecord #生成的tfrecord文件
                    ├──hengfeng_val.tfrecord
                    ├──hengfeng_test.tfrecord
                    ├──hengfeng_train_labels.csv # 生成的csv文件
                    ├──hengfeng_val_labels.csv
                    ├──hengfeng_test_labels.csv
                    ├──ssd_mobilenet_v2_coco.config # 训练的配置文件
                ├──others_object #其他目标

你可能感兴趣的:(Tensorflow,ubuntu,深度学习,tensorflow,python)