Train your own model with TensorFlow object detection under win

1. Environment

1.1 create a virtual environment Python 3 7. Install tensorflow GPU = = 1.13 1. Install PIL (PIP install pilot).
1.2 download labelimg, use labelimg to label, save and generate xml files (use these three shortcut keys: ctrl+s to save, d next, w brush tool, and preferably label in string form).
1.3 establish 4 folders (train training pictures, train_xml training pictures, XML files marked by labelimg, and test files, the same as above).
1.4 cloning of tensorflow models File is to use the model and configuration file here to train your own data.
1.5. 1. Download the corresponding version protoc , unzip the [protocol. Exe] in the bin folder to C:\Windows,
1.5. 2 open the command line window under the directory of models\research \ and enter

protoc object_detection/protos/*.proto --python_out=.

1.5. 3 create a new variable named PYTHONPATH in this computer - properties - advanced system settings - environment variables - system variables
Add the complete directories of the models/research / and models/research/slim folders, separated by semicolons.
1.5. 4 copy and paste the nets folder under the slim folder into research / object_ Under the detection folder
1.5. 5 delete the bulid file in the slim folder, then open the terminal at this location and enter

python setup.py build
python setup.py install

1.5. 6 test API, input

python object_detection/builders/model_builder_test.py

If no error is reported, the operation is successful.

2.xml generates csv file, and then generates record file

2.1 for training files and test files, use the following two files to generate their own csv files respectively

'''
xml File generation csv file,Generated to their respective xml In the folder
 Change three places
'''
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET

os.chdir(r'F:\bomb\test_xml')   ## 1. Change the path to the training (or test) xml folder
path = r'F:\bomb\test_xml'    ## 2. Ibid

def xml_to_csv(path):
    xml_list = []
    for xml_file in glob.glob(path + '\\*.xml'):
        tree = ET.parse(xml_file)
        #print(tree)
        root = tree.getroot()
        #hh=root.findall('object')
        #print(hh[0][0].text)
        #print(root.find('size')[0])
        for member in root.findall('object'):
            #print(member,member[0].text)
            try:
                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)
                     )
            except:
                pass
            #print(value)
            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)    #3. Output file name and generate train CSV or test csv
    print('Successfully converted xml to csv.')


main()

2.1 generate their own record files for the generated two csv files

Create a new generate_tfrecord.py file, modify your own path inside

# -*- coding: utf-8 -*-
"""
csv File generation record file,Generated to their respective xml In the folder
"""
"""
Usage:
  # From tensorflow/models/
  # Create train data:
  python C:\\Users\\YFZX\\Desktop\\image_augment\\image_augment\\generate_tfrecord.py --csv_input=train.csv  --output_path=train.record
  python generate_tfrecord.py --csv_input=F:\\bomb\\test.csv  --output_path=F:\\bomb\\test.record
"""
#Change
import sys
sys.path.append(r'C:\\models-r1.13.0\research\object_detection\utils')#1. Change to research \ object in the model folder of tensorflow downloaded by yourself_ Detection \ utils folder path
import dataset_util

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(r'C:\Users\YFZX\Desktop\dz')
# print(sys.argv[0])
flags = tf.app.flags
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')#Here is the input file to run the py file called csv_input
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
FLAGS = flags.FLAGS


# TO-DO replace this with label map
#2. Pay attention to changing the corresponding label to its own category!!!!!!!!!!
def class_text_to_int(row_label):
    if row_label == 'class3':
        return 3
    elif row_label == 'class4':
        return 4
    elif row_label == 'class5':
        return 5
    elif row_label == 'class6':
        return 6
    elif row_label == 'class7':
        return 7
    else:
        return 0


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(_):
    writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
    path = os.path.join(os.getcwd(), r'F:\bomb\train')    #3. Change the storage path of your own train (or test) image
    examples = pd.read_csv(FLAGS.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(), FLAGS.output_path)
    print('Successfully created the TFRecords: {}'.format(output_path))


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

Save the py file, and then run the py file in the terminal's virtual environment (activate tf1.13) (install shift here, right-click and select here to open the command window), as follows:
csv_input = your own CSV file address. output_path = address and name of the generated record file. (if there is an error, you can try to write the address as an absolute path)

python F:\bomb\generate_tfrecord.py --csv_input=F:\bomb\train_xml\\train.csv  --output_path=F:\bomb\train_xml\\train.record

3. Modify the configuration file

3.1 in object_ Create your own project directory folder (Bob) under the detection folder.
3.2 in object_ Create your own pbtxt file (Bob. Pbtxt) in the data folder under the detection folder.

Keywords: Python TensorFlow Deep Learning job

Added by Incessant-Logic on Thu, 23 Dec 2021 04:43:41 +0200