Demo 1 [3] based on the detection of pedestrians object_detection API: model training and testing

Prepare training

Model selection

Select ssd_mobilenet_v2_coco model Download (https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md), to extract ./Pedestrian_Detection/ssd_mobilenet_v2_coco_2018_03_29.

Object_detection modify configuration files

Enter the directory ./Pedestrian_Detection/models/research/object_detection/samples/configs find the corresponding model profiles ssd_mobilenet_v2_coco.config modify the configuration file.

Follow the prompts:

1, line 9, the detection class to 90 to 1, because we only detect pedestrians, there is only one category.

2, modify all tips except outside

  2.1, the first (156 line) is the path we need models, namely Previous downloaded: ./Pedestrian_Detection/ssd_mobilenet_v2_coco_2018_03_29/model.ckpt

  2.2, second (175 lines) is the path train.record files, last article we prepared record file: . /Pedestrian_Detection/project/pedestrian_train/data/pascal_train.record

  2.3, third (177 lines) is ready label_map.pbtxt on a path: . /Pedestrian_Detection/project/pedestrian_train/data/label_map.pbtxt

  2.4, fourth (189 lines) is the path eval.record files, last article we prepared record file: . /Pedestrian_Detection/project/pedestrian_train/data/pascal_eval.record

  2.5, fifth (191 OK) with 2.3

This config file is modified done. Then put it: ./Pedestrian_Detection/project/pedestrian_train/models directory. Finally, create two folders in the directory: train and eval, training and validation for storage of records.

Start training

Open a command line window

In research directory, enter:

(dl) D:\Study\dl\Pedestrian_Detection\models\research>python object_detection/legacy/train.py --train_dir=D:\Study\dl\Pedestrian_Detection\project\pedestrian_train\models\train --pipeline_config_path=D:\Study\dl\Pedestrian_Detection\project\pedestrian_train\models\ssd_mobilenet_v2_coco.config --logtostderr

You can start training.

After here we choose 2000 times, press ctrl + c end of the training. Details of the training can be done by tensorboard view (no further explanation).

Check out our training record:

Export Model

Here we select training data of 2391 times to generate a model.

The following figure four files into: ./Pedestrian_Detection/pedestrian_data/model   directory

 

 

 

 

Enter the command at the command line window:

(dl) D:\Study\dl\Pedestrian_Detection\models\research>python object_detection/export_inference_graph.py --input_type=image_tensor --pipeline_config_path=D:\Study\dl\Pedestrian_Detection\project\pedestrian_train\models\ssd_mobilenet_v2_coco.config --trained_checkpoint_prefix=D:\Study\dl\Pedestrian_Detection\pedestrian_data\model\model.ckpt-2391 --output_directory=D:\Study\dl\Pedestrian_Detection\pedestrian_data\test

View found under the corresponding catalog has generated a series of model documents:

 

Test Model

Test code:

 1 import os
 2 import sys
 3 
 4 import cv2
 5 import numpy as np
 6 import tensorflow as tf
 7 
 8 sys.path.append("..")
 9 from object_detection.utils import label_map_util
10 from object_detection.utils import visualization_utils as vis_util
11 
12 ##################################################
13 
14 ##################################################
15 
16 # Path to frozen detection graph
17 PATH_TO_CKPT = 'D:/Study/dl/Pedestrian_Detection/pedestrian_data/test/frozen_inference_graph.pb'
18 
19 # List of the strings that is used to add correct label for each box.
20 PATH_TO_LABELS = os.path.join('D:/Study/dl/Pedestrian_Detection/project/pedestrian_train/data', 'label_map.pbtxt')
21 
22 NUM_CLASSES = 1
23 detection_graph = tf.Graph()
24 with detection_graph.as_default():
25     od_graph_def = tf.GraphDef()
26     with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
27         serialized_graph = fid.read()
28         od_graph_def.ParseFromString(serialized_graph)
29         tf.import_graph_def(od_graph_def, name='')
30 
31 label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
32 categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
33 category_index = label_map_util.create_category_index(categories)
34 
35 
36 def load_image_into_numpy_array(image):
37     (im_width, im_height) = image.size
38     return np.array(image.getdata()).reshape(
39       (im_height, im_width, 3)).astype(np.uint8)
40 
41 
42 with detection_graph.as_default():
43     with tf.Session(graph=detection_graph) as sess:
44         image_np = cv2.imread("D:/Study/dl/Pedestrian_Detection/project/test_images/3600.jpg")
45         # image_np = cv2.imread("D:/images/pedestrain.png")
46         cv2.imshow("input", image_np)
47         print(image_np.shape)
48         # image_np == [1, None, None, 3]
49         image_np_expanded = np.expand_dims(image_np, axis=0)
50         image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
51         boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
52         scores = detection_graph.get_tensor_by_name('detection_scores:0')
53         classes = detection_graph.get_tensor_by_name('detection_classes:0')
54         num_detections = detection_graph.get_tensor_by_name('num_detections:0')
55         # Actual detection.
56         (boxes, scores, classes, num_detections) = sess.run(
57             [boxes, scores, classes, num_detections],
58             feed_dict={image_tensor: image_np_expanded})
59         # Visualization of the results of a detection.
60         vis_util.visualize_boxes_and_labels_on_image_array(
61               image_np,
62               np.squeeze(boxes),
63               np.squeeze(classes).astype(np.int32),
64               np.squeeze(scores),
65               category_index,
66               use_normalized_coordinates=True,
67               min_score_thresh=0.5,
68               line_thickness=1)
69         cv2.imshow('object detection', image_np)
70         cv2.imwrite("D:/run_result.png", image_np)
71         cv2.waitKey(0)
72         cv2.destroyAllWindows()

测试效果:

 

Guess you like

Origin www.cnblogs.com/mxiaoy/p/11258582.html