当前位置:网站首页>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 .
边栏推荐
- SQL server当存储过程接收的参数是int类型时,如何做判断?
- "Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
- 为什么opengauss启动的时候这么多的unknown?
- 数据挖掘——关联分析例题代码实现(下)
- [principle] several ways of horizontal penetration
- Ssl== certificate related concepts
- Object array merges elements according to a field
- nacos注册中心
- Note: restframe work records many to one tables, how to serialize in that table (reverse query)
- 【BGP】小型实验
猜你喜欢

Three tier architecture of enterprise network

Zhihuijun, a genius of Huawei, made a modular mechanical keyboard, which caused an earthquake in the geek circle. Netizens: This is the real customization

When array is used as a function parameter, it is better to use the array size as a function parameter
![[deep learning CPU (part outside) - virtual memory]](/img/f7/4c72d583456f6f68c52424602ff5d9.png)
[deep learning CPU (part outside) - virtual memory]

Configmap configuration and secret encryption

从淘宝,天猫,1688,微店,京东,苏宁,淘特等其他平台一键复制商品到拼多多平台(批量上传宝贝详情接口教程)

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

Simple cases of inner connection and left connection
![[原理] 横向渗透的几种方式](/img/fc/2ef7dd6ebc5c0bd8f7d302d8b596d6.png)
[原理] 横向渗透的几种方式

Value transmission and address transmission of C language, pointer of pointer
随机推荐
数据集成这个地方的过滤条件该咋写,用的啥语法?sql语法处理bizdate可以不
STM32F103ZET6程序移植为C8T6+C8T6下载程序flash timeout的解决方案
"Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
面试必备!TCP协议经典十五连问!
有一种密码学专用语言叫做ASN.1
Taobao product details interface (product details page data interface)
Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)
数据挖掘——关联分析基础介绍(上)
Applet: Area scrolling, pull-down refresh, pull-up load more
Configmap配置与Secret加密
Asp. Net MVC, how can the controller in the folder jump to the controller in the root directory?
从淘宝,天猫,1688,微店,京东,苏宁,淘特等其他平台一键复制商品到拼多多平台(批量上传宝贝详情接口教程)
Data mining -- code implementation of association analysis example (Part 2)
MPU6050
谁能详细说下mysqlRC下的半一致读和怎么样减少死锁概率?
当我从数据库获取到了winfrom特定的控件ID之后我需要通过这个ID找到对应的控件,并对控件的TEXT文本进行赋值这该怎么做
淘宝商品详情接口(商品详情页面数据接口)
BGP的基础配置---建立对等体、路由宣告
[kvm] install KVM
数据源是SQL server ,我要配置日期字段 updateDate 最后两天日期的增量数据,做增