github 上大神的代码 https://github.com/endernewton/tf-faster-rcnn.git

在自己跑的过程中的问题:

1. 数据集的问题:

作者实现了 voc,coco数据集接口。由于我要跑自己的数据,所以要重写数据接口。为了方便我将自己的数据格式改为voc的数据格式,使用原来voc的数据接口pascal_voc.py。

voc 数据格式中需要文件:

data

-----VOCdevkit2007  (自己可以改)

|

----VOC2007

|

-----Annotations (目标的标注文件.xml)

-----ImageSets

|

-----  trainval.txt  (用于训练的图像名)

----- test.txt     (用于测试的图像名)

-----JPEGImages  (jpg 图像)

具体  .xml 文件编写根据自己已有的数据

写xml 文件主要内容:

from  xml.dom.minidom import Document

doc=Document()
Annotation=doc.createElement('annotation') # 创建annotation 域
doc.appendChild(Annotation) # 写入annotation 域 object=doc.createElement('object')
Annotation.appendChild('object') # 写入name
object_name=doc.createElement('name')
object_name_text=doc.createTextNode('分类类别名')
object_name.appendChild(object_name_text)
object.appendChild(object_name) # 写入difficult,虽然不用,但是如果不加直接使用pascal_voc会出错
object_difficult=doc.createElement('difficult')
object_difficult_text=doc.createTextNode('0')
object_difficult.appendChild(object_difficult_text)
object.appendChild(object_difficult) # 写入box
bndbox=doc.createElement('bndbox')
object.appendChild(bndbox) object_box=doc.createElement('bndbox')
object_box_xmin=doc.createElement('xmin')
object_box_xmin_text=doc.createTextNode(str(image_box[0]))
object_box_xmin.appendChild(object_box_xmin_text)
bndbox.appendChild(object_box_xmin) object_box_ymin=doc.createElement('ymin')
object_box_ymin_text=doc.createTextNode(str(image_box[1]))
object_box_ymin.appendChild(object_box_ymin_text)
bndbox.appendChild(object_box_ymin) object_box_xmax=doc.createElement('xmax')
object_box_xmax_text=doc.createTextNode(str(image_box[2]))
object_box_xmax.appendChild(object_box_xmax_text)
bndbox.appendChild(object_box_xmax) object_box_ymax=doc.createElement('ymax')
object_box_ymax_text=doc.createTextNode(str(image_box[3]))
object_box_ymax.appendChild(object_box_ymax_text)
bndbox.appendChild(object_box_ymax) f=open(filename,"w")
f.write(doc.toprettyxml(indent=" "))
f.close()

  得到:

<annotation>
<object>
<name>abc</name>
<difficult>0</difficult>
<bndbox>
<xmin>107</xmin>
<ymin>155</ymin>
<xmax>193</xmax>
<ymax>214</ymax>
</bndbox>
</object>
</annotation>

改pascal_voc.py 文件,修改自己的classes,以及xml中对应域的名字等。

2. 数据完成之后,就可以用来训练了,此时出现问题:

Assign requires shapes of both tensors to match. lhs shape= [2048,124] rhs shape= [2048,84]

因为我现在变为30类,30+1 (背景),31*4=124 (4为box 的定位),而原来为84类。

怎么改最后的输出类别个数?在caffe中可以直接在prototxt 定义的网络结构中改,在tensorflow中怎么改呢?

  1. 我们执行train_faster_rcnn 传入了(gpuId, dataset, net) 调用tools/trainval_net.py
  2. 在trainval_net.py 中调用net=resnetv1, load 网络模型, 调用models/train_net
  3. 在train_net 中调用train_model 函数,定义计算图,在initialize 函数中对sess 进行初始化
  def initialize(self, sess):
# Initial file lists are empty
np_paths = []
ss_paths = []
# Fresh train directly from ImageNet weights
print('Loading initial model weights from {:s}'.format(self.pretrained_model))
variables = tf.global_variables()
# Initialize all variables first
sess.run(tf.variables_initializer(variables, name='init'))
var_keep_dic = self.get_variables_in_checkpoint_file(self.pretrained_model)
# Get the variables to restore, ignoring the variables to fix
variables_to_restore = self.net.get_variables_to_restore(variables, var_keep_dic)
# 要加载的变量
restorer = tf.train.Saver(variables_to_restore)
# 进行加载。。出错的地方就是这里
restorer.restore(sess, self.pretrained_model)
print('Loaded.')
# Need to fix the variables before loading, so that the RGB weights are changed to BGR
# For VGG16 it also changes the convolutional weights fc6 and fc7 to
# fully connected weights
self.net.fix_variables(sess, self.pretrained_model)
print('Fixed.')
last_snapshot_iter = 0
rate = cfg.TRAIN.LEARNING_RATE
stepsizes = list(cfg.TRAIN.STEPSIZE) return rate, last_snapshot_iter, stepsizes, np_paths, ss_paths

  要改正,就要不加载最后的 预测层和 box 回归层。

对要加载的文件进行选择,然后就可训练自己的数据了

tensorflow faster rann的更多相关文章

  1. tensorflow faster rcnn 代码分析一 demo.py

    os.environ["CUDA_VISIBLE_DEVICES"]=2 # 设置使用的GPU tfconfig=tf.ConfigProto(allow_soft_placeme ...

  2. Tensorflow faster rcnn系列一

    注意:本文主要是学习用,发现了一个在faster rcnn训练流程写的比较详细的博客. 大部分内容来自以下博客连接:https://blog.csdn.net/weixin_37203756/arti ...

  3. python3 + Tensorflow + Faster R-CNN训练自己的数据

    之前实现过faster rcnn, 但是因为各种原因,有需要实现一次,而且发现许多博客都不全面.现在发现了一个比较全面的博客.自己根据这篇博客实现的也比较顺利.在此记录一下(照搬). 原博客:http ...

  4. Faster_Rcnn在windows下运行踩坑总结

    Faster_Rcnn在windows下运行踩坑总结  20190524 今天又是元气满满的一天! 1.代码下载 2.编译 3.下载数据集 4.下载pre-train Model 5.运行train ...

  5. TensorFlow_Faster_RCNN中demo.py的运行(CPU Only)

    GitHub项目地址,https://github.com/endernewton/tf-faster-rcnnTensorflow Faster RCNN for Object Detection. ...

  6. Technology Document Guide of TensorRT

    Technology Document Guide of TensorRT Abstract 本示例支持指南概述了GitHub和产品包中包含的所有受支持的TensorRT 7.2.1示例.Tensor ...

  7. 新人如何运行Faster RCNN的tensorflow代码

    0.目的 刚刚学习faster rcnn目标检测算法,在尝试跑通github上面Xinlei Chen的tensorflow版本的faster rcnn代码时候遇到很多问题(我真是太菜),代码地址如下 ...

  8. Tensorflow版Faster RCNN源码解析(TFFRCNN) (2)推断(测试)过程不使用RPN时代码运行流程

    本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第二篇   推断(测试)过程不使用RPN时代码运行流程 作者:Jiang Wu  原文见:https://hom ...

  9. TensorFlow Object Detection API中的Faster R-CNN /SSD模型参数调整

    关于TensorFlow Object Detection API配置,可以参考之前的文章https://becominghuman.ai/tensorflow-object-detection-ap ...

随机推荐

  1. 20165223 学习基础和C语言基础调查

    一.学习基础 1. 我所擅长的技能 从小我就对新鲜事物抱有浓厚的兴趣,因此多年来培养了许多爱好,对感兴趣的诸如绘画方面的国画.油画.素描.漫画等:音乐方面的钢琴.吉他.架子鼓等:运动方面的滑板.溜冰. ...

  2. 洛谷 P2158 仪仗队

    欧拉函数入门题... 当然如果有兴趣也可以用反演做...类似这题 题意就是求,方阵从左下角出发能看到多少个点. 从0开始给坐标 发现一个点能被看到,那么横纵坐标互质. 然后求欧拉函数的前缀和,* 2 ...

  3. HDU/HDOJ 1867 A + B for you again

    仔细了解KMP之后再看这题就会发现是裸题. 因为kmp我们可以求出s的f数组,表示能与p的多少前缀匹配.那么我们只需取f[s.size() - 1]即可. #include <cstdio> ...

  4. 【洛谷P1052】过河 离散化+dp

    题目大意:给定一个长度为 N 的序列,有 M 个点对答案的贡献为 1,其余为 0,现从起点出发,每次只能走 [s,t] 个单位,求从起点走到终点时答案贡献最小是多少. 题解:由于 N 很大,无法直接记 ...

  5. 【CF1141E】Superhero Battle

    \[x*p\ge y\rightarrow x=\lfloor{{y-1}\over p}\rfloor+1\]

  6. react-native中的navigator

    第一步安装相关插件 添加一些依赖 package com.awesomeproject; import com.facebook.react.ReactActivity; import com.fac ...

  7. pytest 1.简单介绍一,安装和如何运行

    一.pytest是一个接口测试框架,试用版起来比较轻便灵活.首先来介绍他的安装: 直接使用命令 : pip install -U pytest 通过命令 :pytest --version  来查看版 ...

  8. Linux之LVS 20180708

    LVS负载均衡项目,是Linux开源项目中专门用于负载均衡的,主要应用在client访问server时,通过LVS来分配对应的server来响应client的请求.client访问时,都是访问的LVS ...

  9. day-02(css,js)

    本文档并非个人所写,只是便于参考:回顾: html: 作用:展示 文件标签: <html> <head> <title></title> </he ...

  10. 【强大知名的CAD绘图工具】AutoCAD 2019 for Mac

    以上图片来源于互联网分享,如涉及版权问题请联系作者删除. 文章素材来源:风云社区(www.scoee.com) 下载地址:风云社区(www.scoee.com)   [简介] AutoCAD 2019 ...