哈哈,我又来了!!! 再次立下flag,开学之后还是要保持更新频率!!!

本次用efficientdet来对抽烟检测,检测出是否抽烟。那么,老规矩,先上结果图!!!

那么,接下来,还是原先一套流程。走起!!!

一、环境配置

  • python==3.7.4
  • tensorflow-gpu==1.14.0
  • keras==2.2.4
  • numpy==1.17.4

本次,在租的gpu的机器上的。没办法,efficientnet这个网络占据显存太大了。本次机器带不动呀。

二、抽烟数据集

本次数据集是用labelme标注的,提供的json格式的数据集,但本次我们的voc格式的xml数据集,所以需要对json格式的数据进行转换。

图片:

标注的json格式数据:

转换后的xml格式

本次json转xml的源码如下:

# -*- coding: utf-8 -*-
"""
Created on Sun May 31 10:19:23 2020
@author: ywx
"""
import os
from typing import List, Any
import numpy as np
import codecs
import json
from glob import glob
import cv2
import shutil
from sklearn.model_selection import train_test_split# 1.标签路径
labelme_path = "annotations/"
#原始labelme标注数据路径
saved_path = "VOC2007/"
# 保存路径
isUseTest=True#是否创建test集
# 2.创建要求文件夹
if not os.path.exists(saved_path + "Annotations"):os.makedirs(saved_path + "Annotations")
if not os.path.exists(saved_path + "JPEGImages/"):os.makedirs(saved_path + "JPEGImages/")
if not os.path.exists(saved_path + "ImageSets/Main/"):os.makedirs(saved_path + "ImageSets/Main/")
# 3.获取待处理文件
files = glob(labelme_path + "*.json")
files = [i.replace("\\","/").split("/")[-1].split(".json")[0] for i in files]
print(files)
# 4.读取标注信息并写入 xml
for json_file_ in files:json_filename = labelme_path + json_file_ + ".json"json_file = json.load(open(json_filename, "r", encoding="utf-8"))height, width, channels = cv2.imread('jpeg/' + json_file_ + ".jpg").shapewith codecs.open(saved_path + "Annotations/" + json_file_ + ".xml", "w", "utf-8") as xml:xml.write('<annotation>\n')xml.write('\t<folder>' + 'WH_data' + '</folder>\n')xml.write('\t<filename>' + json_file_ + ".jpg" + '</filename>\n')xml.write('\t<source>\n')xml.write('\t\t<database>WH Data</database>\n')xml.write('\t\t<annotation>WH</annotation>\n')xml.write('\t\t<image>flickr</image>\n')xml.write('\t\t<flickrid>NULL</flickrid>\n')xml.write('\t</source>\n')xml.write('\t<owner>\n')xml.write('\t\t<flickrid>NULL</flickrid>\n')xml.write('\t\t<name>WH</name>\n')xml.write('\t</owner>\n')xml.write('\t<size>\n')xml.write('\t\t<width>' + str(width) + '</width>\n')xml.write('\t\t<height>' + str(height) + '</height>\n')xml.write('\t\t<depth>' + str(channels) + '</depth>\n')xml.write('\t</size>\n')xml.write('\t\t<segmented>0</segmented>\n')for multi in json_file["shapes"]:points = np.array(multi["points"])labelName=multi["label"]xmin = min(points[:, 0])xmax = max(points[:, 0])ymin = min(points[:, 1])ymax = max(points[:, 1])label = multi["label"]if xmax <= xmin:passelif ymax <= ymin:passelse:xml.write('\t<object>\n')xml.write('\t\t<name>' + labelName+ '</name>\n')xml.write('\t\t<pose>Unspecified</pose>\n')xml.write('\t\t<truncated>1</truncated>\n')xml.write('\t\t<difficult>0</difficult>\n')xml.write('\t\t<bndbox>\n')xml.write('\t\t\t<xmin>' + str(int(xmin)) + '</xmin>\n')xml.write('\t\t\t<ymin>' + str(int(ymin)) + '</ymin>\n')xml.write('\t\t\t<xmax>' + str(int(xmax)) + '</xmax>\n')xml.write('\t\t\t<ymax>' + str(int(ymax)) + '</ymax>\n')xml.write('\t\t</bndbox>\n')xml.write('\t</object>\n')print(json_filename, xmin, ymin, xmax, ymax, label)xml.write('</annotation>')
# 5.复制图片到 VOC2007/JPEGImages/下
image_files = glob("jpeg/" + "*.jpg")
print("copy image files to VOC007/JPEGImages/")
for image in image_files:shutil.copy(image, saved_path + "JPEGImages/")
# 6.split files for txt
txtsavepath = saved_path + "ImageSets/Main/"
ftrainval = open(txtsavepath + '/trainval.txt', 'w')
ftest = open(txtsavepath + '/test.txt', 'w')
ftrain = open(txtsavepath + '/train.txt', 'w')
fval = open(txtsavepath + '/val.txt', 'w')
total_files = glob("./VOC2007/Annotations/*.xml")
total_files = [i.replace("\\","/").split("/")[-1].split(".xml")[0] for i in total_files]
trainval_files=[]
test_files=[]
if isUseTest:trainval_files, test_files = train_test_split(total_files, test_size=0.15, random_state=55)
else: trainval_files=total_files
for file in trainval_files: ftrainval.write(file + "\n")
# split
train_files, val_files = train_test_split(trainval_files, test_size=0.15, random_state=55)
# train
for file in train_files: ftrain.write(file + "\n")
# val
for file in val_files: fval.write(file + "\n")
for file in test_files:print(file)ftest.write(file + "\n")
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()

三、EfficientDet理论介绍

EfficientDet是基于Efficientnet的目标检测网络,所以需要先读懂Efficientnet,这里可以先去看我之前写的卷积神经网络发展史中有关于Efficientnet的介绍。

简短来说,EfficientNet是将图片的分辨率,网络的宽度,网络的深度这三者结合起来,通过α实现缩放模型,不同的α有不同的模型精度。

总的来说,efficientdet目标检测网络,是以efficientnet为主干网络,之后经过bifpn特征特征网络,之后再输出检测结果。

1.EfficientNet

EfficientNet主要由Efficient Blocks构成,在其中小残差边以及大残差边构成,并在其中添加了注意力模块。

def mb_conv_block(inputs, block_args, activation, drop_rate=None, prefix='', ):"""Mobile Inverted Residual Bottleneck."""has_se = (block_args.se_ratio is not None) and (0 < block_args.se_ratio <= 1)bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1# workaround over non working dropout with None in noise_shape in tf.kerasDropout = get_dropout(backend=backend,layers=layers,models=models,utils=keras_utils)# Expansion phasefilters = block_args.input_filters * block_args.expand_ratioif block_args.expand_ratio != 1:x = layers.Conv2D(filters, 1,padding='same',use_bias=False,kernel_initializer=CONV_KERNEL_INITIALIZER,name=prefix + 'expand_conv')(inputs)x = layers.BatchNormalization(axis=bn_axis, name=prefix + 'expand_bn')(x)x = layers.Activation(activation, name=prefix + 'expand_activation')(x)else:x = inputs# Depthwise Convolutionx = layers.DepthwiseConv2D(block_args.kernel_size,strides=block_args.strides,padding='same',use_bias=False,depthwise_initializer=CONV_KERNEL_INITIALIZER,name=prefix + 'dwconv')(x)x = layers.BatchNormalization(axis=bn_axis, name=prefix + 'bn')(x)x = layers.Activation(activation, name=prefix + 'activation')(x)# Squeeze and Excitation phaseif has_se:num_reduced_filters = max(1, int(block_args.input_filters * block_args.se_ratio))se_tensor = layers.GlobalAveragePooling2D(name=prefix + 'se_squeeze')(x)target_shape = (1, 1, filters) if backend.image_data_format() == 'channels_last' else (filters, 1, 1)se_tensor = layers.Reshape(target_shape, name=prefix + 'se_reshape')(se_tensor)se_tensor = layers.Conv2D(num_reduced_filters, 1,activation=activation,padding='same',use_bias=True,kernel_initializer=CONV_KERNEL_INITIALIZER,name=prefix + 'se_reduce')(se_tensor)se_tensor = layers.Conv2D(filters, 1,activation='sigmoid',padding='same',use_bias=True,kernel_initializer=CONV_KERNEL_INITIALIZER,name=prefix + 'se_expand')(se_tensor)if backend.backend() == 'theano':# For the Theano backend, we have to explicitly make# the excitation weights broadcastable.pattern = ([True, True, True, False] if backend.image_data_format() == 'channels_last'else [True, False, True, True])se_tensor = layers.Lambda(lambda x: backend.pattern_broadcast(x, pattern),name=prefix + 'se_broadcast')(se_tensor)x = layers.multiply([x, se_tensor], name=prefix + 'se_excite')# Output phasex = layers.Conv2D(block_args.output_filters, 1,padding='same',use_bias=False,kernel_initializer=CONV_KERNEL_INITIALIZER,name=prefix + 'project_conv')(x)x = layers.BatchNormalization(axis=bn_axis, name=prefix + 'project_bn')(x)if block_args.id_skip and all(s == 1 for s in block_args.strides) and block_args.input_filters == block_args.output_filters:if drop_rate and (drop_rate > 0):x = Dropout(drop_rate,noise_shape=(None, 1, 1, 1),name=prefix + 'drop')(x)x = layers.add([x, inputs], name=prefix + 'add')return x

2.BiFPN

改进了FPN中的多尺度特征融合方式,提出了加权双向特征金字塔网络BiFPN。BiFPN 引入了一种自顶向下的路径,融合P3~P7的多尺度特征

BiFPN模块类似于FPN网络(特征金字塔网络),不过比FPN更复杂些。其主要是为了增强特征,提取更有代表性的特征。

下图展示一下FPN网络:

而这是BiFPN的网络图:

其中的一个BiFPN模块为:

四、训练过程

1.准备数据集

准备抽烟数据,使用VOC格式的数据进行训练

  • 训练前将标签文件放在VOCdevkit文件夹下的VOC2007文件夹下的Annotation中。
  • 训练前将图片文件放在VOCdevkit文件夹下的VOC2007文件夹下的JPEGImages中。
  • 在训练前利用voc2efficientdet.py文件生成对应的txt。
VOCdevkit-VOC2007├─ImageSets    # 存放数据集列表文件,由voc2yolo3.py文件生成├─Annotations  # 存放数据集中图片文件├─JPEGImages   # 存放图片标签,xml 格式└─voc2yolo4.py # 用来生成数据集列表文件

2.运行生成EfficientDet所需的数据

再运行根目录voc_annotation.py,运行前需要将voc_annotation文件中classes改成你自己的classes。
每一行对应其图片位置及其真实框的位置

3.修改voc_classes.txt

在训练前需要修改model_data里面的voc_classes.txt文件,需要将classes改成你自己的classes。

4.修改yolo_anchors.txt

运行kmeans_for_anchors.py生成yolo_anchors.txt

5.运行

运行train.py

  • 在main函数下:phi来控制efficient的版本
  • 在main函数下:model_path参数用于控制预训练权重

6.测试图片

需修改efficientdet.py文件中模型的位置,替换成你训练好的模型并修改phi为efficientdet的版本。然后在根目录下,运行python predict.py进行测试。

好了,本次就到此结束了!!!

哦,突然想起,我还保存了logs,在展示一波的训练过程吧!!!

好了,下次看情况,更新点别的东西,更新点有关bert文本的吧!!!

EfficientDet训练自己的数据集实现抽烟检测相关推荐

  1. YOLOv7训练自己的数据集(口罩检测)

    YOLOv7训练自己的数据集(口罩检测) 前言 前提条件 实验环境 项目结构 制作自己的数据集 数据集目录结构 训练自己的数据集 VOC格式数据集转换成YOLO格式数据集 修改cfg配置 新建一个my ...

  2. TOLOv5训练自己的数据集--漫画人物检测

    TOLOv5训练自己的数据集--漫画人物检测 TOLOv5训练自己的数据集 YOLOv5模型下载 使用LabelImage标注图片 生成所需数据集 更改配置文件 训练 测试 TOLOv5训练自己的数据 ...

  3. 【google版efficientdet】官方版efficientdet训练自己的数据集,终于训练成功了

    看全网还没有一篇攻略,本文是第一个,有心人当点赞下,有问题可以下方留言,互相交流.如转载请注明出处,不枉解决各种各样的bug 环境: v100,cuda10.1,tensorflow2.1.0 ,py ...

  4. yolov5训练自己的数据集(缺陷检测分类——看了就懂系列)

    这里记录一下将yolov5用于训练自己的数据集,这里v5的github地址 Environment 本文是直接 pip install -r requirement.txt直接安装成功,如果不容易成功 ...

  5. 【Detectron2】使用 Detectron2 训练基于 coco 数据集的目标检测网络

    文章目录 一.安装 Detectron2 二.软连接 coco 数据集 三.训练 四.数据集相关参数 五.输出结果路径 六.COCO 数据集简介 七.模型相关参数 八.可视化结果 一.安装 Detec ...

  6. YOLOF训练自己的数据集(目标检测,cvpods版本)

    训练准备: github repo地址:https://github.com/megvii-model/YOLOF github上有两个版本,一个是cvpods,一个是detectron2,第二个我每 ...

  7. yolov3 tiny训练自己的数据集进行物体检测 (只检测行人)

    说明:我的电脑是个16年3500元买的笔记本(勇气可嘉:) 环境:额外的包之类的,我用的anaconda,可以非常容易安装各类包,如果运行显示缺包,那就去environment去安装对应的包. 我的版 ...

  8. EfficientDet训练自己的物体检测数据集

    向AI转型的程序员都关注了这个号???????????? 机器学习AI算法工程   公众号:datayx 目标检测能够既准确又快速吗? 我们常常有一个概念,SSD 等单阶段目标检测器很快,但准确性比不 ...

  9. yolov4-keras实现抽烟检测(源码+数据集)

    目录 前言 效果 运行环境 前言 上个礼拜一直在把结课答辩的小项目结题,做的就是一个基于yolov4的抽烟检测,虽然在答辩上没有啥特别亮点,并且有些地方可以改进,比如再加入烟雾检测,但算是拿来练手吧. ...

最新文章

  1. YML(2)yml 语法
  2. Servlet--生命周期
  3. java.text.dateformat_使用java.text.SimpleDateFormat类进行文本日期和Date日期的转换
  4. C#GDI+编程基础
  5. winform防止sqlserver注入_c# 防止sql注入对拼接sql脚本的各个参数处理
  6. 2019招行信用卡中心春招笔试题
  7. ezcad旋转轴标刻参数_激光打标机软件ezcad中菜单下的地球仪标刻功能介绍及其操作设置...
  8. 集成电路CAD习题及答案
  9. centos7 关闭自动yum更新
  10. 2020 HDU Multi-University Training Contest 6(部分)
  11. mediasoup安装使用
  12. htc android 9,点赞!HTC宣布这4款手机可升级至安卓9.0:均为U系列
  13. 前端大屏适配几种方案
  14. 比尔盖茨的15个预言,如今全已成真
  15. H5和Html5的关系
  16. 串行通信与并行通信的区别
  17. 吃货联盟系统 C++实现
  18. 如何查找本机IP地址?这个方法,简单易懂
  19. 烟花仓库智能管理-RFID仓库管理解决方案-智能RFID仓库管理系统-新导智能
  20. python使用清华源镜像安装包

热门文章

  1. DOM--操作元素案例:分时显示不同的图片以及问候语;仿淘宝关闭标签案例;隐藏显示文本框内容;仿新浪注册页面中密码长度的判定
  2. 登录工程:现代 Web 应用的典型身份验证需求
  3. C# 静态内部类单例模式-静态变量何时初始化
  4. 基于STM32F1的孤立词语语音识别
  5. boost::thread编程实战(2)——获取线程id并转化为DWORD类型
  6. 天猫精灵连接蓝牙摸索3 如何让天猫精灵根据语音命令播报设备上发的数据
  7. python内置函数之float() 函数
  8. 基于stm32mp157 linux开发板ARM裸机开发教程4:Cortex-A7 内核存储系统与流水线(连载中)
  9. 安卓包在真机上安装时的“风险提示”问题
  10. python新闻关键词次数_使用python抓取百度搜索、百度新闻搜索的关键词个数