import os import glob import pandas as pd import argparse import xml.etree.ElementTree as ET
defxml_to_csv(path): """Iterates through all .xml files (generated by labelImg) in a given directory and combines them in a single Pandas datagrame. Parameters: ---------- path : {str} The path containing the .xml files Returns ------- Pandas DataFrame The produced dataframe """
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
defmain(): # Initiate argument parser parser = argparse.ArgumentParser( description="Sample TensorFlow XML-to-CSV converter") parser.add_argument("-i", "--inputDir", help="Path to the folder where the input .xml files are stored", type=str) parser.add_argument("-o", "--outputFile", help="Name of output .csv file (including path)", type=str) args = parser.parse_args()
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 import sys sys.path.append("../../models/research")
from PIL import Image from object_detection.utils import dataset_util from collections import namedtuple, OrderedDict
flags = tf.app.flags flags.DEFINE_string('csv_input', '', 'Path to the CSV input') flags.DEFINE_string('output_path', '', 'Path to output TFRecord') flags.DEFINE_string('label', '', 'Name of class label') # if your image has more labels input them as ,若一張圖片內注釋了多個標籤,請使用以下代碼 # flags.DEFINE_string('label0', '', 'Name of class[0] label') # flags.DEFINE_string('label1', '', 'Name of class[1] label') # and so on. flags.DEFINE_string('img_path', '', 'Path to images') FLAGS = flags.FLAGS
# TO-DO replace this with label map # for multiple labels add more else if statements defclass_text_to_int(row_label): if row_label == 'Garen': # '標籤物件1': return1 elif row_label == 'Darius': # '標籤物件2': ***若只有一個標籤物件則刪除33-34行 return2 # comment upper if statement and uncomment these statements for multiple labelling # if row_label == FLAGS.label0: # return 1 # elif row_label == FLAGS.label1: # return 0 else: return0
defsplit(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)]
下一步,我們將會使用TensorFlow提供的原模型訓練自訂模型,這裡使用的是ssd_inception_v2_coco模型 因為它在準確度和回饋速度中取得一個較好的平衡,當然也有其他選擇,詳情請參考Detection Model Zoo 來到Detection Model Zoo尋找ssd_inception_v2_coco並點擊進行下載,下載好解壓放到 training_demo\pre-trained-model 目錄下
# SSD with Inception 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.
train_config: { batch_size: 12 # Increase/Decrease this value depending on the available memory (Higher values require more memory and vice-versa) 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: "pre-trained-model/model.ckpt" # Path to extracted files of pre-trained model 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: "annotations/train.record" # Path to training TFRecord file } label_map_path: "annotations/label_map.pbtxt" # Path to label map file }
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 }
# What model to download. MODEL_NAME = 'new_model_graph'
# Path to frozen detection graph. This is the actual model that is used for the object detection. PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'
# List of the strings that is used to add correct label for each box. PATH_TO_LABELS = os.path.join('data', 'label_map.pbtxt')
train_config: { batch_size: 1 # Increase/Decrease this value depending on the available memory (Higher values require more memory and vice-versa) optimizer { ... ...