object detection训练自己数据
1、用labelImg標自己數據集。
并將圖片存放在JPEGImages中,xml存放在Annotations中
2、分離訓練和測試數據
import os import randomtrainval_percent = 0.66 train_percent = 0.5 xmlfilepath = 'Annotations' txtsavepath = 'ImageSets\Main' total_xml = os.listdir(xmlfilepath) print(total_xml) 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)ftrainval = open('ImageSets/Main/trainval.txt', 'w') ftest = open('ImageSets/Main/test.txt', 'w') ftrain = open('ImageSets/Main/train.txt', 'w') fval = open('ImageSets/Main/val.txt', 'w')for i in list:name=total_xml[i][:-4]+'\n'if i in trainval:ftrainval.write(name)if i in train:ftrain.write(name)else:fval.write(name)else:ftest.write(name)ftrainval.close() ftrain.close() fval.close() ftest .close()此時ImageSets/Main/目錄下生成這四個文件
3、根據train.txt?和 test.txt中內容分別建立train和test文件(存放xml文件)
import os import shutil files = os.listdir('E:\gitcode\\tensorflow-model\\VOCPolice\\VOC2007\\Annotations') file= open('E:\\gitcode\\tensorflow-model\\VOCPolice\\VOC2007\\ImageSets\\Main\\test.txt','r') newpath='E:\\gitcode\\tensorflow-model\\VOCPolice\\VOC2007\\test' num1=0 for line in file.readlines():num1=num1+1message=line.split('\n')num=message[0]xmlName=num+'.xml'for i in files:if i==xmlName:oldpath='E:\gitcode\\tensorflow-model\\VOCPolice\\VOC2007\\Annotations'+"\\"+ishutil.copy(oldpath,newpath)break print(num1)此時train和test文件中存放的就是train.txt? test.txt中所列的xml文件
4、將xml轉換為tfrecord文件
方法一:?xml轉為csv,csv轉為tfrecord
方法二:直接采用object_detection文件中的create_pascal_tf_record.py?
本人采用方法二失敗了。。方法一成功了。所以就列出來方法一的配置過程:
(1)將xml轉為csv
''' function:xml2csv ''' import os import glob import pandas as pd import xml.etree.ElementTree as ETdef 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_dfdef main():for directory in ['train', 'test']:project_path = 'E:\\gitcode\\tensorflow-model\\VOCPolice\\VOC2007'image_path = os.path.join(project_path, directory)xml_df = xml_to_csv(image_path)xml_df.to_csv('E:/gitcode/tensorflow-model/VOCPolice/VOC2007/{}_labels.csv'.format(directory), index=None)print('Successfully converted xml to csv.')main()運行完后VOC2007下有兩個文件:train_labels.csv test_labels.csv。如下圖所示:
這里運行的前提是之前產生的存放xml的train和test文件夾在project_path下。
(2)將csv文件轉換為tfrecord文件
# generate_tfrecord.py# -*- coding: utf-8 -*-""" Usage:# From tensorflow/models/# Create train data:python generate_tfrecord.py --csv_input=data/tv_vehicle_labels.csv --output_path=train.record# Create test data:python generate_tfrecord.py --csv_input=data/test_labels.csv --output_path=test.record """import os import io import pandas as pd import tensorflow as tffrom PIL import Image from object_detection.utils import dataset_util from collections import namedtuple, OrderedDictos.chdir('E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection')flags = tf.app.flags flags.DEFINE_string('csv_input', 'E:/gitcode/tensorflow-model/VOCPolice/VOC2007/train_labels.csv', 'Path to the CSV input') flags.DEFINE_string('output_path', 'train.record', 'Path to output TFRecord') FLAGS = flags.FLAGS# TO-DO replace this with label map def class_text_to_int(row_label):if row_label == 'police': # 需改動return 1else:Nonedef 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.sizefilename = 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_exampledef main(_):writer = tf.python_io.TFRecordWriter(FLAGS.output_path)path = os.path.join(os.getcwd(), 'images/test') # 需改動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()修改上述代碼中flags.DEFINE_string的csv_input路徑為上述步驟產生的train和test的csv路徑。然后將output_path分別命名為train.record和test.record文件.運行兩次,即可產生train.record?和test.record(注意你自己選擇的output路徑)
5、準備訓練
(1)下載模型ssd_mobilenet_v1_coco_2017_11_17并解壓在object_detection目錄下
(2)修改pascal_label_map.pbtxt文件。也可新建自己的pbtxt文件。
因為我這里只有一類,所以修改如上。
(3)修改模型的配置文件
修改位置如下:
num_classes: 1 #你的類別數 batch_size: 2 #你的電腦能承受的數量 fine_tune_checkpoint: "E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection/ssd_mobilenet_v1_coco_2017_11_17/model.ckpt"#改為你的模型所在的位置 我這里全部使用絕對路徑 num_steps: 50000 #改為你所需要訓練次數train_input_reader {label_map_path: "E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection/data/pascal_label_map.pbtxt"tf_record_input_reader {input_path: "E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection/train.record"} } #分別將label_map_path 和train input_path 改為你的文件所在的位置eval_input_reader {label_map_path: "E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection/data/pascal_label_map.pbtxt"shuffle: falsenum_readers: 1tf_record_input_reader {input_path: "E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection/test.record"} } #分別將label_map_path 和test input_path 改為你的文件所在的位置6、開始訓練
在object_detection/legacy/train.py?文件中
修改
flags.DEFINE_string中train_dir為你的結果存放path flags.DEFINE_string中'pipeline_config_path為剛才下載模型的.config所在路徑然后運行即可。
7、查看訓練曲線
在object_detection目錄下:? logdir目錄就是train_dir目錄
tensorboard --logdir=E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/ssdmodel1231
8、?導出模型
修改export_inference_graph.py?文件中
flags.DEFINE_string中pipeline_config_path為你的ssd模型.config所在路徑 flags.DEFINE_string中trained_checkpoint_prefix為你訓練結果的model.ckpt-訓練次數 flags.DEFINE_string中output_directory為你的導出路徑然后運行即可保存
9、測試模型
在object_detection下新建myTest.py?文件
# coding: utf-8# # Object Detection Demo # Welcome to the object detection inference walkthrough! This notebook will walk you step by step through the process of using a pre-trained model to detect objects in an image. Make sure to follow the [installation instructions](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md) before you start.from distutils.version import StrictVersion import numpy as np import os import six.moves.urllib as urllib import sys import tarfile import tensorflow as tf import zipfilefrom collections import defaultdict from io import StringIO from matplotlib import pyplot as plt from PIL import Image# This is needed since the notebook is stored in the object_detection folder. sys.path.append("..") from object_detection.utils import ops as utils_ops# if StrictVersion(tf.__version__) < StrictVersion('1.9.0'): # raise ImportError('Please upgrade your TensorFlow installation to v1.9.* or later!')# ## Env setup# In[2]:# This is needed to display the images. # get_ipython().magic(u'matplotlib inline')# ## Object detection imports # Here are the imports from the object detection module.from object_detection.utils import label_map_utilfrom object_detection.utils import visualization_utils as vis_util# # Model preparation# ## Variables # # Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_FROZEN_GRAPH` to point to a new .pb file. # # By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.# In[4]:# What model to download. MODEL_NAME = './Police_detection1231/' # MODEL_FILE = MODEL_NAME + '.tar.gz' # DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'# 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', 'pascal_label_map.pbtxt')NUM_CLASSES = 1# ## Download Model# opener = urllib.request.URLopener() # opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE) ''' tar_file = tarfile.open(MODEL_FILE) for file in tar_file.getmembers():file_name = os.path.basename(file.name)if 'frozen_inference_graph.pb' in file_name:tar_file.extract(file, os.getcwd()) '''# ## Load a (frozen) Tensorflow model into memory.detection_graph = tf.Graph() with detection_graph.as_default():od_graph_def = tf.GraphDef()with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:serialized_graph = fid.read()od_graph_def.ParseFromString(serialized_graph)tf.import_graph_def(od_graph_def, name='')# ## Loading label map # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be finelabel_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories)# ## Helper code# In[8]:def load_image_into_numpy_array(image):(im_width, im_height) = image.sizereturn np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)# # Detection# For the sake of simplicity we will use only 2 images: # image1.jpg # image2.jpg # If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS. PATH_TO_TEST_IMAGES_DIR = 'test_police' TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 22) ]# Size, in inches, of the output images. IMAGE_SIZE = (12, 8)# In[10]:config = tf.ConfigProto() config.gpu_options.allow_growth = True def run_inference_for_single_image(image, graph):with graph.as_default():with tf.Session(config=config) as sess:# Get handles to input and output tensorsops = tf.get_default_graph().get_operations()all_tensor_names = {output.name for op in ops for output in op.outputs}tensor_dict = {}for key in ['num_detections', 'detection_boxes', 'detection_scores','detection_classes', 'detection_masks']:tensor_name = key + ':0'if tensor_name in all_tensor_names:tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(tensor_name)if 'detection_masks' in tensor_dict:# The following processing is only for single imagedetection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(detection_masks, detection_boxes, image.shape[0], image.shape[1])detection_masks_reframed = tf.cast(tf.greater(detection_masks_reframed, 0.5), tf.uint8)# Follow the convention by adding back the batch dimensiontensor_dict['detection_masks'] = tf.expand_dims(detection_masks_reframed, 0)image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')# Run inferenceoutput_dict = sess.run(tensor_dict,feed_dict={image_tensor: np.expand_dims(image, 0)})# all outputs are float32 numpy arrays, so convert types as appropriateoutput_dict['num_detections'] = int(output_dict['num_detections'][0])output_dict['detection_classes'] = output_dict['detection_classes'][0].astype(np.uint8)output_dict['detection_boxes'] = output_dict['detection_boxes'][0]output_dict['detection_scores'] = output_dict['detection_scores'][0]if 'detection_masks' in output_dict:output_dict['detection_masks'] = output_dict['detection_masks'][0]return output_dict# In[ ]:for image_path in TEST_IMAGE_PATHS:image = Image.open(image_path)# the array based representation of the image will be used later in order to prepare the# result image with boxes and labels on it.image_np = load_image_into_numpy_array(image)# Expand dimensions since the model expects images to have shape: [1, None, None, 3]image_np_expanded = np.expand_dims(image_np, axis=0)# Actual detection.output_dict = run_inference_for_single_image(image_np, detection_graph)# Visualization of the results of a detection.vis_util.visualize_boxes_and_labels_on_image_array(image_np,output_dict['detection_boxes'],output_dict['detection_classes'],output_dict['detection_scores'],category_index,instance_masks=output_dict.get('detection_masks'),use_normalized_coordinates=True,line_thickness=8)# plt.subplot(1, 2, 1)# plt.imshow(image)# plt.subplot(1, 2, 2)# plt.imshow(image_np)plt.savefig(image_path + '_labeled.jpg')# plt.show()# plt.legend() print("finished")修改MODEL_NAME為你導出模型的路徑
修改 PATH_TO_LABELS為你的.pbtxt文件所在路徑
修改NUM_CLASSES為你的類別數
修改PATH_TO_TEST_IMAGES_DIR為你的測試圖片所在路徑。同時命名形式為image1.jpg、image2.jpg。
在TEST_IMAGE_PATHS中range輸入你的圖片范圍
運行即可。
10?評估模型
修改legacy/eval.py中以下參數。并運行如下
flags.DEFINE_string('checkpoint_dir', 'your trained model path','Directory containing checkpoints to evaluate, typically ''set to `train_dir` used in the training job.') flags.DEFINE_string('eval_dir', 'your eval model save path', 'Directory to write eval summaries to.') flags.DEFINE_string('pipeline_config_path', 'your .config path','Path to a pipeline_pb2.TrainEvalPipelineConfig config ''file. If provided, other configs are ignored')大功告成
參考自
https://blog.csdn.net/Arvin_liang/article/details/84752427
https://chtseng.wordpress.com/2019/02/16/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8google-object-detection-api%E8%A8%93%E7%B7%B4%E8%87%AA%E5%B7%B1%E7%9A%84%E6%A8%A1%E5%9E%8B/
https://www.cnblogs.com/zongfa/p/9663649.html
https://blog.csdn.net/linolzhang/article/details/87121875
https://blog.csdn.net/Arvin_liang/article/details/84752427
總結
以上是生活随笔為你收集整理的object detection训练自己数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像分类 数据准备(将文件夹中所有图片路
- 下一篇: C语言按位取反原理