连载已经完结,
百度网盘测试APP下载地址:
链接:https://pan.baidu.com/s/1grwUcLkI9i3OABsLtB5h3Q
提取码:pkbl
先见效果图,另外我已经上传到了抖音视频,想看NB效果,可以点击链接直接观看:
http://v.douyin.com/roLnjL/
接下来的博客开始记录我研究过程,过程是:采集样本->标注->训练->测试。
上一期讲了如何进行标注,本期将开始训练。
本次使用的是谷歌云平台训练,实际上就是一个ubuntu 系统,不过现在有免费的300美金使用。部署好服务器后。将训练的文件上传到云服务器。需要的文件有这些。
然后执行训练
python object_detection/model_main.py --model_dir=/home/softboyes/digeai/majiang/training --pipeline_config_path=/home/softboyes/digeai/majiang/ssd_mobilenet_v1_pets.config
由于我使用的8cpu+20G内存 训练36小时训练了34k 步法。
通过tensorboard观察,loss效果不太理想打算训练到60k。
下载后导出模型:
python object_detection/export_inference_graph.py --input_type image_tensor --pipeline_config_path E:/AI_LAB/majiang/gitdata/ssd_mobilenet_v1_pets.config --trained_checkpoint_prefix E:/AI_LAB/majiang/gitdata/out/model.ckpt-24720 --output_directory E:/AI_LAB/majiang/gitdata/export2
导出后模型如下:并写下测试test.py
import os
import sys
import tarfile
import cv2
import numpy as np
import tensorflow as tf
sys.path.append("..")
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
# Path to frozen detection graph
CWD_PATH = os.getcwd()
PATH_TO_CKPT = os.path.join(CWD_PATH,'frozen_inference_graph.pb')
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join(CWD_PATH,'labelmap.pbtxt')
NUM_CLASSES = 28
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
label_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)
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
image_np = cv2.imread("test.jpg")
cv2.imshow("input", image_np)
print(image_np.shape)
# image_np == [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
min_score_thresh=0.4,
line_thickness=3)
cv2.imshow('object detection', image_np)
cv2.imwrite("run_result.png", image_np)
cv2.waitKey(0)
cv2.destroyAllWindows()
sess.close()
测试模型,发现效果还算理想。接下来往安卓设备上面迁移。
迁移需要的问题: frozen_inference_graph.pb labelmap.pbtxt
拷贝到TensorFlow demo里面assert 文件夹。修改 DetectorActivity
修改后:
private static final String TF_OD_API_MODEL_FILE =
"file:///android_asset/frozen_inference_graph.pb";
private static final String TF_OD_API_LABELS_FILE = "file:///android_asset/majiang.txt";
然后用Android studio编译生成APK,然后安装到安卓手机。
下一章讲安卓手机效果与训练心得以及样本要求。