【深度学习】嘿马深度学习笔记第13篇:YOLO与SSD,4.3 案例:SSD进行物体检测【附代码文档】

🏆🏆🏆教程全知识点简介:1.深度学习概述包括深度学习与机器学习区别、深度学习应用场景、深度学习框架介绍、项目演示、开发环境搭建(pycharm安装)。2. TensorFlow基础涵盖TF数据流图、TensorFlow实现加法运算、图与TensorBoard(图结构、图相关操作、默认图、创建图、OP)、张量(张量概念、张量的阶、张量数学运算)、变量OP(创建变量)、增加其他功能(命名空间、模型保存与加载、命令行参数使用)、逻辑回归案例。3. 神经网络基础包括playground使用、多个神经元效果演示、深层神经网络。4. 神经网络与tf.keras。5. 梯度下降算法改进涵盖指数加权平均、动量梯度下降法、RMSProp算法、Adam算法、TensorFlow Adam算法API、学习率衰减、标准化输入、神经网络调优、批标准化。6. 卷积神经网络包括CNN原理、CIFAR类别分类(API使用、步骤分析代码实现缩减版LeNet)、卷积神经网络学习特征可视化。7. 经典分类网络结构涵盖LeNet解析、AlexNet、卷积网络结构优化、Inception结构、pre_trained模型VGG预测(VGG模型使用、步骤代码)。8. CNN网络实战技巧。9. 迁移学习案例包括基于VGG的五种图片类别识别迁移学习(数据集迁移需求、思路步骤、训练时读取本地图片类别、特征图平均值输出替代全连接层)。10. 目标检测包括目标检测任务描述、目标定位实现思路、产品物体检测项目介绍、R-CNN(Overfeat模型、SPPNet)、Faster R-CNN(RPN原理)、YOLO(单元格grid cell、非最大抑制NMS、训练)、SSD。11. 产品检测数据集训练涵盖标注数据读取存储(xml读取本地文件存储pkl、解析结构、one_hot编码函数)、训练(案例训练结果、多GPU训练代码修改)、本地预测测试(预测代码)、模型导出(keras模型TensorFlow导出)。12. 模型部署包括Web与模型服务对接逻辑、Docker部署环境、TF Serving与Web开启服务(安装Tensorflow Serving、commodity模型服务运行)、TensorFlow Client对接模型服务、Web Server开启。

📚📚👉👉👉本站这篇博客: https://bbs.huaweicloud.com/blogs/455848 中查看
📚📚👉👉👉本站这篇博客: https://bbs.huaweicloud.com/blogs/455848 中查看
📚📚👉👉👉本站这篇博客: https://bbs.huaweicloud.com/blogs/458269 中查看
✨ 本教程项目亮点
🧠 知识体系完整:覆盖从基础原理、核心方法到高阶应用的全流程内容
💻 全技术链覆盖:完整前后端技术栈,涵盖开发必备技能
🚀 从零到实战:适合 0 基础入门到提升,循序渐进掌握核心能力
📚 丰富文档与代码示例:涵盖多种场景,可运行、可复用
🛠 工作与学习双参考:不仅适合系统化学习,更可作为日常开发中的查阅手册
🧩 模块化知识结构:按知识点分章节,便于快速定位和复习
📈 长期可用的技术积累:不止一次学习,而是能伴随工作与项目长期参考
🎯🎯🎯全教程总章节
🚀🚀🚀本篇主要内容
YOLO与SSD
4.3 案例:SSD进行物体检测
学习目标
-
目标
-
无
-
应用
-
应用keras SSD进行物体检测案例
4.3.1 案例效果
使用已经训练过的模型进行加载之后,总共基础训练时有动物、载具等等共20个物体类别的训练集。一下是对没有训练过的图像的检测结果
4.3.2 案例需求
使用开源的SSD网络结构进行检测的是的代码编写,由于开源代码使用 keras 编写,没有tf.keras版本,需要下载 keras-1.2.2 包
pip install keras==1.2.2
- 使用SSD网络模型,输入图片数据,处理图片数据
- 得到预测的类别和预测的位置
- 在图片中显示出来
4.3.3 步骤分析以及代码
-
代码结构:
-
ckpt:模型参数保存目录
- image:测试图片
- nets:模型网络路径
- utils:公共组件(模型工具,BBox处理)
- 定义好类别数量以及输出
class SSDTrain(object):
def __init__(self):
self.classes_name = ['Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle',
'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Diningtable',
'Dog', 'Horse', 'Motorbike', 'Person', 'Pottedplant',
'Sheep', 'Sofa', 'Train', 'Tvmonitor']
self.classes_nums = len(self.classes_name) + 1
self.input_shape = (300, 300, 3)
模型预测流程
- SSD300模型输入以及加载参数
- 读取多个本地路径测试图片,preprocess_input以及保存图像像素值(显示需要)
- 模型预测结果,得到priorbox
-
进行非最大抑制算法处理
-
SSD300模型输入以及加载参数
-
by_name:按照每一层名字进行填充参数
[FastAPI 文档]
``python If
by_name` is True, weights are loaded into layers only if they share the same name. This is useful for fine-tuning or transfer-learning models where some of the layers have changed.
```python
[re 正则表达式]
model = SSD300(self.input_shape, num_classes=self.classes_nums)
model.load_weights('./ckpt/weights_SSD300.hdf5', by_name=True)
- 读取多个本地路径测试图片,preprocess_input以及保存图像像素值(显示需要)
需要使用
from keras.applications.imagenet_utils import preprocess_input
from keras.preprocessing.image import load_img, img_to_array
from scipy.misc import imread
import os
from nets.ssd_net import SSD300
from utils.ssd_utils import BBoxUtility
代码:
# 循环读取图片进行多个图片输出检测
feature = []
images = []
for pic_name in os.listdir("./image/"):
img_path = os.path.join("./image/", pic_name)
print(img_path)
# 读取图片
# 转换成数组
# 模型输入
img = load_img(img_path, target_size=(self.input_shape[0], self.input_shape[1]))
img = img_to_array(img)
feature.append(img)
images.append(imread(img_path))
# 处理图片数据,ndarray数组输入
inputs = preprocess_input(np.array(feature))
- 模型预测结果,得到priorbox
# 预测
preds = model.predict(inputs, batch_size=1, verbose=1)
- 进行非最大抑制算法处理
# 定义BBox工具
bbox_util = BBoxUtility(self.classes_nums)
# 使用非最大抑制算法过滤
results = bbox_util.detection_out(preds)
print(results[0].shape, results[1].shape)
图片的检测结果显示
需要下载图像显示库
pip install matplotlib
- 对结果进行标记
def tag_picture(self, images, results):
"""
对图片预测结果画图显示
:param images:
:param results:
:return:
"""
for i, img in enumerate(images):
# 解析输出结果,每张图片的标签,置信度和位置
pre_label = results[i][:, 0]
pre_conf = results[i][:, 1]
pre_xmin = results[i][:, 2]
pre_ymin = results[i][:, 3]
pre_xmax = results[i][:, 4]
pre_ymax = results[i][:, 5]
print("label:{}, probability:{}, xmin:{}, ymin:{}, xmax:{}, ymax:{}".
format(pre_label, pre_conf, pre_xmin, pre_ymin, pre_xmax, pre_ymax))
# 过滤置信度低的结果
top_indices = [i for i, conf in enumerate(pre_conf) if conf >= 0.6]
top_conf = pre_conf[top_indices]
top_label_indices = pre_label[top_indices].tolist()
top_xmin = pre_xmin[top_indices]
top_ymin = pre_ymin[top_indices]
top_xmax = pre_xmax[top_indices]
top_ymax = pre_ymax[top_indices]
# 定义21中颜色,显示图片
# currentAxis增加图中文本显示和标记显示
colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
plt.imshow(img / 255.)
currentAxis = plt.gca()
for i in range(top_conf.shape[0]):
xmin = int(round(top_xmin[i] * img.shape[1]))
ymin = int(round(top_ymin[i] * img.shape[0]))
xmax = int(round(top_xmax[i] * img.shape[1]))
ymax = int(round(top_ymax[i] * img.shape[0]))
# 获取该图片预测概率,名称,定义显示颜色
score = top_conf[i]
label = int(top_label_indices[i])
label_name = self.classes_name[label - 1]
display_txt = '{:0.2f}, {}'.format(score, label_name)
coords = (xmin, ymin), xmax - xmin + 1, ymax - ymin + 1
color = colors[label]
# 显示方框
currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
# 左上角显示概率以及名称
currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor': color, 'alpha': 0.5})
[httpx 文档]
plt.show()
总结
每日
YOLO与SSD
2.1 目标检测数据集
学习目标
-
目标
-
了解常用目标检测数据集
-
了解数据集构成
-
应用
-
无
2.1.1 常用目标检测数据集
- pascal Visual Object Classes
VOC数据集是目标检测经常用的一个数据集,从05年到12年都会举办比赛(比赛有task: Classification、Detection、Segmentation、PersonLayout),主要由VOC2007和VOC2012两个数据集
注:
官网地址:[
下载地址:[
- Open Images Dataset V4
