当前位置:网站首页>Object detection: object_ Detection API +ssd target detection model
Object detection: object_ Detection API +ssd target detection model
2022-07-29 04:10:00 【Arvin L】
Environmental Science
window10, anaconda Python 3.5.6, tensorflow 1.8.0
tensorflow1.4later model download :https://download.csdn.net/download/weixin_42748604/12647262
install object_detection API
Refer to the official installation documentation
This document uses WIN10 System , Some changes have been made in some places , Please refer to .
Install dependency packages
pip install --user Cython
pip install --user contextlib2
pip install --user pillow
pip install --user lxml
pip install --user jupyter
pip install --user matplotlib
pip install --user tf_sliminstall COCO API
git This installation COCO
This method requires the computer to install git
pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPIpython install COCO
git clone https://github.com/pdollar/coco.git
cd coco/PythonAPI
python setup.py build_ext --inplace
python setup.py build_ext install
test COCO API Installation
Get into python,import pycocotools See if there's a mistake .
compile Protobuf
Google official use proto, It needs to be compiled as .py file
stay models-master/research/ Execute under directory :
for /f %i in ('dir /b object_detection\protos\*.proto') do protoc object_detection\protos\%i --python_out=.Add environment variables
stay models-master\research Execute under directory :
SET PYTHONPATH=%cd%;%cd%\slimRun setup.py
stay models\research Run under folder :
python setup.py build
python setup.py installtest API
Use Jupyter notebook open models-master/research/object_detection Under the object_detection_tutorial.ipynb file ,
It means that API Environment installed successfully .( notes :Jupyter notebook To run in the same environment )

Test picture in research\object_detection\test_images Under the table of contents , You can add your own pictures , With image3... name .
Establish the target detection model
Data preparation
Use labellmage Label pictures , Generate xml file . Then from xml File generation csv file , Again by csv File generation tfrecord file , Google tensorflow Use this format file for training .
labellmage File acquisition and use
Collected relatively new versions , Please click on Download link .
Usage method :

When the label is finished , stay object_detection New under folder images Folder , stay images New under file train,test Folder , Put the picture files into them respectively .
from xml File generation csv file
stay research\object_detection Execute the xml2csv Program .
train,test The two folders need to be generated separately
# xml2csv.py
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
# Change to store train,test The address of
os.chdir('C:/Users/lishu/Desktop/tensorflow/tf1/models-1.4later/models-adfd5a3aca41638aa9fb297c5095f33d64446d8f/research/object_detection/images/test')
path = 'C:/Users/lishu/Desktop/tensorflow/tf1/models-1.4later/models-adfd5a3aca41638aa9fb297c5095f33d64446d8f/research/object_detection/images/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)
# Modify to generate csv File address
xml_df.to_csv('C:/Users/lishu/Desktop/tensorflow/tf1/models-1.4later/models-adfd5a3aca41638aa9fb297c5095f33d64446d8f/research/object_detection/data/test.csv', index=None)
print('Successfully converted xml to csv.')
main()from csv File generation tfrecord file
stay research\object_detection Execute the following code :
# Create train data:
python generate_tfrecord.py --csv_input=data/train.csv --output_path=data/train.record
# Create test data:
python generate_tfrecord.py --csv_input=data/test.csv --output_path=data/test.record
# generate_tfrecord.py
# -*- coding: utf-8 -*-
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
# Modify the path
os.chdir('C:/Users/lishu/Desktop/tensorflow/tf1/models-1.4later/models-adfd5a3aca41638aa9fb297c5095f33d64446d8f/research/object_detection')
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 == 'mlj': # Need to change
return 1
else:
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(_):
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
path = os.path.join(os.getcwd(), 'C:/Users/lishu/Desktop/tensorflow/tf1/models-1.4later/models-adfd5a3aca41638aa9fb297c5095f33d64446d8f/research/object_detection/images/test') # Need to change
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()Model and profile settings
stay research\object_detection New under the directory training Folder , take research\object_detection\samples\configs Under folder
ssd_mobilenet_v1_pets.config File copy to training Under the folder , And modify some code :
1、 take num_classes Change according to the actual situation , In this paper 23;
2、batch_size Change it to 1 or 2, If the computer has good performance , It can be changed to 2 The power of ;
3、input_path Change it to your own train.tfrecord route , if necessary eval Test words , You also need to modify the path .
4、 stay object_detection/data Copy one randomly under the folder .pbtxt The file copy is named hg.pbtxt, Change the code inside as follows :
item {
name: "hb"
id: 1
}
...
representative label The name of and the corresponding number , take label_map_path Set to the corresponding hg.pbtxt route , I am here data/hg.pbtxt.
5、 Setup of pre training model fine_tune_checkpoint: "C:/Users/lishu/Desktop/tensorflow/tf1/models/research/object_detection/ssd_mobilenet_v1_coco_2017_11_17/model.ckpt"
from_detection_checkpoint: true
The pre training model needs to be downloaded online according to the model you choose , This article USES ssd_mobilenet_v1_coco_2017_11_17.
Training models
stay research\object_detection Execute under directory :
python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v1_coco.configUse tensorboard Check the training status
Enter the same training environment and directory research\object_detection Next , perform :
tensorboard --logdir=trainingSave the model
Use research\object_detection Under the export_inference_graph.py File save model file .
In the code
--trained_checkpoint_prefix training/model.ckpt-10317 Save training to 10317 Step saved model
--output_directory mlj Represents the folder where the model is saved
python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/ssd_mobilenet_v1_coco.config --trained_checkpoint_prefix training/model.ckpt-10317 --output_directory hgAfter execution mlj A series of files will be formed under the folder ,frozen_inference_graph.pb Files required for testing the model .
test model
Use eval.py test
stay research\object_detection Under the table of contents , Enter this command :
python eval.py --logtostderr --checkpoint_dir=training --eval_dir=eval --pipeline_config_path=training/ssd_mobilenet_v1_coco.configUse Jupyter notebook test
Use Jupyter notebook open research\object_detection In the catalog object_detection_tutorial.ipynb file ,
modify Variables The content below is :
MODEL_NAME = 'hg'
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
PATH_TO_LABELS = os.path.join('data', 'hg.pbtxt')
NUM_CLASSES = 23Delete Download Model All codes under the module ,
The test picture is on research\object_detection\test_images Under the folder , Picture with image1... name ,
Run the ipynb file , Display test results .
Welcome to exchange and study .
边栏推荐
- [kvm] create virtual machine from kickstart file
- Press the missing number of interview question 17.04 | | 260. the number that appears only once (including bit operation knowledge points)
- 大佬们flink的JDBC SQL Connector现在不支持所有的数据库吗,例如vertica?
- OA项目之会议通知(查询&是否参会&反馈详情)
- Is the browser multi process or single process?
- rman不标记过期备份
- How to solve the problem of store ranking?
- Is there any way for Youxuan database to check the log volume that the primary cluster transmits to the standby cluster every day?
- 数据集成这个地方的过滤条件该咋写,用的啥语法?sql语法处理bizdate可以不
- 优炫数据库有办法查到主集群每天传给备集群的日志量吗?
猜你喜欢

Meeting notice of OA project (Query & whether to attend the meeting & feedback details)

3. Solve pychart's error unresolved reference 'selenium' unresolved reference 'webdriver‘

Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes

数据挖掘——关联分析例题代码实现(下)

初识C语言(3)

Const char* and char*, string constants

Is the browser multi process or single process?

Problems encountered in vscode connection SSH

伏英娜:元宇宙就是新一代互联网!

Install the laser of ROS_ scan_ Problems encountered in match library (I)
随机推荐
First ALV program 2
索引的最左前缀原理
Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation
力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
[kvm] common commands
Design of environment detection system based on STM32 and Alibaba cloud
对一个元素使用多种变形的方法
I. creation and constraint of MySQL table
Deep understanding of browser caching mechanism (HTTP)
BIO、NIO、AIO的区别和原理
Arrow function of new features of ES6
The data source is SQL server. I want to configure the incremental data of the last two days of the date field updatedate to add
Opensql quick learning
如何查询版本的提交号
Mmdetection preliminary use
Data mining -- code implementation of association analysis example (Part 2)
How to execute insert into select from job in SQL client
Configmap configuration and secret encryption
C declaration and initialization and assignment
有一种密码学专用语言叫做ASN.1