gluoncv 目标检测,训练自己的数据集
https://gluon-cv.mxnet.io/build/examples_datasets/detection_custom.html
官方提供两种方案,一种是lst文件,一种是xml文件(voc的格式);
voc 格式的标注有标注工具,但是你如果是json文件标注的信息,或者其他格式的,你就要转成voc格式的。
于是就选择第一种数据格式lst序列文件格式,格式很简单。

根据你自己的json或者其他格式文件转换一下。
import json
import os
import cv2
import numpy as np def write_line(img_path, im_shape, boxes, ids, idx):
h, w, c = im_shape
# for header, we use minimal length 2, plus width and height
# with A: 4, B: 5, C: width, D: height
A = 4
B = 5
C = w
D = h
# concat id and bboxes
labels = np.hstack((ids.reshape(-1, 1), boxes)).astype('float')
# normalized bboxes (recommanded)
labels[:, (1, 3)] /= float(w)
labels[:, (2, 4)] /= float(h)
# flatten
labels = labels.flatten().tolist()
str_idx = [str(idx)]
str_header = [str(x) for x in [A, B, C, D]]
str_labels = [str(x) for x in labels]
str_path = [img_path]
line = '\t'.join(str_idx + str_header + str_labels + str_path) + '\n'
return line files = os.listdir('train_front')
json_url = []
cnt = 0
for file in files:
tmp = os.listdir('train_front/'+file)
for js in tmp:
if js.endswith('json'):
json_url.append('train_front/'+file+'/'+js)
cnt+=1
print(cnt) fwtrain = open("train.lst","w")
fwval = open("val.lst","w") first_flag = []
flag = True cnt = 0
cnt1 = 0
cnt2 = 0
for json_url_index in json_url:
file = open(json_url_index,'r')
for line in file:
js = json.loads(line) if 'person' in js:
boxes = []
ids = []
for i in range(len(js['person'])):
if js['person'][i]['attrs']['ignore'] == 'yes' or js['person'][i]['attrs']['occlusion']== 'heavily_occluded' or js['person'][i]['attrs']['occlusion']== 'invisible':
continue bbox = js['person'][i]['data']
url = '/mnt/hdfs-data-4/data/jian.yin/'+json_url_index[:-5]+'/'+js['image_key']
width = js['width']
height = js['height']
boxes.append(bbox)
ids.append(0) print(url)
print(bbox) if len(boxes) > 0:
if flag:
flag = False
first_flag = boxes
ids = np.array(ids) if cnt < 27853//2: line = write_line(url,(height,width,3),boxes,ids,cnt1)
fwtrain.write(line)
cnt1+=1 if cnt >= 27853//2:
line = write_line(url, (height, width, 3), boxes, ids, cnt2)
fwval.write(line)
cnt2+=1 cnt += 1 fwtrain.close()
fwval.close()
print(first_flag)
lst文件就转换好了。
然后添加自己的数据集:
https://github.com/dmlc/gluon-cv/blob/master/scripts/detection/faster_rcnn/train_faster_rcnn.py#L73
这里不能直接套用前面的导入数据的过程。
按照教程给出的方式添加。投机取巧的验证方式,直接引用前面的。
或者不验证:https://github.com/dmlc/gluon-cv/blob/master/scripts/detection/faster_rcnn/train_faster_rcnn.py#L393 部分注释掉。
elif dataset.lower() == 'pedestrian':
lst_dataset = LstDetection('train_val.lst',root=os.path.expanduser('.'))
print(len(lst_dataset))
first_img = lst_dataset[0][0] print(first_img.shape)
print(lst_dataset[0][1]) train_dataset = LstDetection('train.lst',root=os.path.expanduser('.'))
val_dataset = LstDetection('val.lst',root=os.path.expanduser('.'))
classs = ('pedestrian',)
val_metric = VOC07MApMetric(iou_thresh=0.5,class_names=classs)
训练参数:
https://github.com/dmlc/gluon-cv/blob/master/scripts/detection/faster_rcnn/train_faster_rcnn.py#L73
添加自己的训练参数或者直接套用。
if args.dataset == 'voc' or args.dataset == 'pedestrian':
args.epochs = int(args.epochs) if args.epochs else 20
args.lr_decay_epoch = args.lr_decay_epoch if args.lr_decay_epoch else '14,20'
args.lr = float(args.lr) if args.lr else 0.001
args.lr_warmup = args.lr_warmup if args.lr_warmup else -1
args.wd = float(args.wd) if args.wd else 5e-4
model_zoo.py添加自己的数据集映射方案。这里如果是pip install gluoncv ,就要到site-package里面改。
https://github.com/dmlc/gluon-cv/blob/master/gluoncv/model_zoo/model_zoo.py#L32
'faster_rcnn_resnet50_v1b_pedestrian': faster_rcnn_resnet50_v1b_voc,
gluoncv 目标检测,训练自己的数据集的更多相关文章
- 目标检测网络之 YOLOv2
YOLOv1基本思想 YOLO将输入图像分成SxS个格子,若某个物体 Ground truth 的中心位置的坐标落入到某个格子,那么这个格子就负责检测出这个物体. 每个格子预测B个bounding b ...
- 目标检测之YOLO V2 V3
YOLO V2 YOLO V2是在YOLO的基础上,融合了其他一些网络结构的特性(比如:Faster R-CNN的Anchor,GooLeNet的\(1\times1\)卷积核等),进行的升级.其目的 ...
- 目标检测网络之 YOLOv3
本文逐步介绍YOLO v1~v3的设计历程. YOLOv1基本思想 YOLO将输入图像分成SxS个格子,若某个物体 Ground truth 的中心位置的坐标落入到某个格子,那么这个格子就负责检测出这 ...
- Faster-rcnn实现目标检测
Faster-rcnn实现目标检测 前言:本文浅谈目标检测的概念,发展过程以及RCNN系列的发展.为了实现基于Faster-RCNN算法的目标检测,初步了解了RCNN和Fast-RCNN实现目标检 ...
- 可变卷积Deforable ConvNet 迁移训练自己的数据集 MXNet框架 GPU版
[引言] 最近在用可变卷积的rfcn 模型迁移训练自己的数据集, MSRA官方使用的MXNet框架 环境搭建及配置:http://www.cnblogs.com/andre-ma/p/8867031. ...
- 【转】目标检测之YOLO系列详解
本文逐步介绍YOLO v1~v3的设计历程. YOLOv1基本思想 YOLO将输入图像分成SxS个格子,若某个物体 Ground truth 的中心位置的坐标落入到某个格子,那么这个格子就负责检测出这 ...
- CenterNet算法笔记(目标检测论文)
论文名称:CenterNet: Keypoint Triplets for Object Detectiontection 论文链接:https://arxiv.org/abs/1904.08189 ...
- 动手创建 SSD 目标检测框架
参考:单发多框检测(SSD) 本文代码被我放置在 Github:https://github.com/XinetAI/CVX/blob/master/app/gluoncvx/ssd.py 关于 SS ...
- 第三十二节,使用谷歌Object Detection API进行目标检测、训练新的模型(使用VOC 2012数据集)
前面已经介绍了几种经典的目标检测算法,光学习理论不实践的效果并不大,这里我们使用谷歌的开源框架来实现目标检测.至于为什么不去自己实现呢?主要是因为自己实现比较麻烦,而且调参比较麻烦,我们直接利用别人的 ...
随机推荐
- golang学习之struct
结构体定义的一般方式如下: type identifier struct { field1 type1 field2 type2 ... } type T struct {a, b int} 也是合法 ...
- window.open()被浏览器拦截问题汇总
一.问题描述 最近在做项目的时候碰到了使用window.open被浏览器拦截的情况,虽然在自己的环境可以对页面进行放行,但是对用户来说,不能要求用户都来通过拦截.何况当出现拦截时,很多用户根本不知道发 ...
- 二:Vim常用命令
一般模式下的命令: -- 插入命令 i 光标前插入 I 当前行开始 o 下一行 O 上一行插入新行 a 光标后插入 A 当前行末尾 -- 定位命令 :set nu 显示行号 :set nonu 取消行 ...
- How WindowLeaked exception occured?
If a Activity performDestroy, and there is window not closed whose window token is the Activity's mW ...
- 使用PowerShell批量解除锁定下载的文件
使用PowerShell批量解除锁定下载的文件 3.在需要解锁的文件所在的文件夹中空白处,按住Shift然后单击右键,在弹出的右键菜单中,选择“在此处打开PowerShell窗口”, 输入Get-Ch ...
- JS之捕获冒泡和事件委托
一.事件流(捕获,冒泡) 事件流:指从页面中接收事件的顺序,有冒泡流和捕获流. 当页面中发生某种事件(比如鼠标点击,鼠标滑过等)时,毫无疑问子元素和父元素都会接收到该事件,可具体顺序是怎样的呢?冒 ...
- 如何检测页面是否有重复的id属性值
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta nam ...
- php编程--二叉树遍历算法实现
今天使用php来实现二叉树的遍历 创建的二叉树如下图所示 php代码如下所示: <?php class Node { public $value; public $child_l ...
- 工作中常用的sql语句以及知识整理
一.常用的sql语句 1.建表语句 create table tabname(colname1 type1 [not null][primary key], colname2 type2,...) 根 ...
- 在vue中安装使用vux
最近因为的工作的原因在弄vue,从后端弄到前端之前一直用js,现在第一次接触vue感觉还挺有意思的,就是自己太菜了,这个脑子呀....不太够用.....页面设计用了一个叫vux的东西,vux可以提供一 ...