目录

  • 1、模型准备工作
    • 1-1 模型运行环境
    • 1-2 模型地址
    • 1-3 数据集及resnet网络地址
    • 1-4 修改setup.py
  • 2、Demo测试模型
    • 2-1 放置resnet网络及修改cfgs.py
    • 2-2 下载权重
    • 2-3 数据集设置
    • 2-4 编译setup.py
    • 2-5 运行inference.py演示文件
  • 3、训练VOC数据集
    • 3-1 划分数据集
    • 3-2 修改参数
    • 3-3 read_tfrecord.py的修改
    • 3-4 运行read_tfrecord.py
    • 3-5 train.py

1、模型准备工作

1-1 模型运行环境

python3.6
TensorFlow1.15-GPU
windows10
Ananonda
Pycharm
CUDA9 + cuDNN7.6
opence-python
tensorflow-plot
:由于模型其中的一些模块需要用到C++编译,所以还需要下载一个VS2019

1-2 模型地址

https://github.com/DetectionTeamUCAS/FPN_Tensorflow

1-3 数据集及resnet网络地址

VOC2007:https://uinedu-my.sharepoint.com/:f:/g/personal/19604_myoffice_site/EiLTzAbNirROrQQF20eupMQB-KpIfZOa7w2YS5MB2ARvSA
resnet50_v1:http://download.tensorflow.org/models/resnet_v1_50_2016_08_28.tar.gz
resnet101_v1:http://download.tensorflow.org/models/resnet_v1_101_2016_08_28.tar.gz

1-4 修改setup.py

由于源码模型是适用于ubuntu16.04的编译,我们修改的地方主要是帮助其找到win10下的nvcc.exe以及相应的lib文件。并且没有用到gcc,要注释掉。
(本次修改参考博文:配置FPN_tensorflow)
setup.py文件路径:

PATH\FPN_Tensorflow-master\libs\box_utils\cython_utils
PATH是你自己保存模型的路径

修改后代码如下:

# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------import os
from os.path import join as pjoin
import numpy as np
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_extlib_dir = 'lib/x64'def find_in_path(name, path):"Find a file in a search path"#adapted fom http://code.activestate.com/recipes/52224-find-a-file-given-a-search-path/for dir in path.split(os.pathsep):binpath = pjoin(dir, name)if os.path.exists(binpath):return os.path.abspath(binpath)return Nonedef locate_cuda():"""Locate the CUDA environment on the systemReturns a dict with keys 'home', 'nvcc', 'include', and 'lib64'and values giving the absolute path to each directory.Starts by looking for the CUDAHOME env variable. If not found, everythingis based on finding 'nvcc' in the PATH."""# first check if the CUDAHOME env variable is in useif 'CUDA_PATH' in os.environ:home = os.environ['CUDA_PATH']print("home = %s\n" % home)nvcc = pjoin(home, 'bin', 'nvcc.exe')else:# otherwise, search the PATH for NVCCdefault_path = pjoin(os.sep, 'usr', 'local', 'cuda', 'bin')nvcc = find_in_path('nvcc', os.environ['PATH'] + os.pathsep + default_path)if nvcc is None:raise EnvironmentError('The nvcc binary could not be ''located in your $PATH. Either add it to your path, or set $CUDAHOME')home = os.path.dirname(os.path.dirname(nvcc))cudaconfig = {'home':home, 'nvcc':nvcc,'include': pjoin(home, 'include'),'lib64': pjoin(home, lib_dir)}for k, v in cudaconfig.items():if not os.path.exists(v):raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))return cudaconfig
CUDA = locate_cuda()# Obtain the numpy include directory.  This logic works across numpy versions.
try:numpy_include = np.get_include()
except AttributeError:numpy_include = np.get_numpy_include()def customize_compiler_for_nvcc(self):"""inject deep into distutils to customize how the dispatchto cl/nvcc works.If you subclass UnixCCompiler, it's not trivial to get your subclassinjected in, and still have the right customizations (i.e.distutils.sysconfig.customize_compiler) run on it. So instead of goingthe OO route, I have this. Note, it's kindof like a wierd functionalsubclassing going on."""# tell the compiler it can processes .cu#self.src_extensions.append('.cu')# save references to the default compiler_so and _comple methods#default_compiler_so = self.spawn #default_compiler_so = self.rcsuper = self.compile# now redefine the _compile method. This gets executed for each# object but distutils doesn't have the ability to change compilers# based on source extension: we add it.def compile(sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None):postfix=os.path.splitext(sources[0])[1]if postfix == '.cu':# use the cuda for .cu files#self.set_executable('compiler_so', CUDA['nvcc'])# use only a subset of the extra_postargs, which are 1-1 translated# from the extra_compile_args in the Extension classpostargs = extra_postargs['nvcc']else:postargs = extra_postargs['cl']return super(sources, output_dir, macros, include_dirs, debug, extra_preargs, postargs, depends)# reset the default compiler_so, which we might have changed for cuda#self.rc = default_compiler_so# inject our redefined _compile method into the classself.compile = compile# run the customize_compiler
class custom_build_ext(build_ext):def build_extensions(self):customize_compiler_for_nvcc(self.compiler)build_ext.build_extensions(self)ext_modules = [Extension("cython_bbox",["bbox.pyx"],#extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},extra_compile_args={'cl': []},include_dirs = [numpy_include]),Extension("cython_nms",["nms.pyx"],#extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},extra_compile_args={'cl': []},include_dirs = [numpy_include])# Extension(#     "cpu_nms",#     ["cpu_nms.pyx"],#     extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},#     include_dirs = [numpy_include]# )
]setup(name='tf_faster_rcnn',ext_modules=ext_modules,# inject our custom triggercmdclass={'build_ext': custom_build_ext},
)

2、Demo测试模型

2-1 放置resnet网络及修改cfgs.py

在下载你所需的网络后,将网络解压到下面这个路径

D:\pyprojects\FPN_Tensorflow-master\data\pretrained_weights

接下来修改cfgs.py文件,路径如下

D:\pyprojects\FPN_Tensorflow-master\libs\configs

打开configs文件夹,我们可以发现有如下文件

本人使用的网络为resnet101,而默认的cfgs.py中使用的也是resnet101,所以这部分不需要修改,如果你使用resnet50,则用第三个py文件的内容替代cfgs.py中的内容即可。
另外,在cfgs.py中我们还需要修改其中的默认路径,将其改为我们存放模型的路径。
修改之处如下

这部分也需要修改

2-2 下载权重

在选定cfgs.py所用的resnet网络后,我们去下载对应的网络的权重文件,权重文件地址如下:

https://github.com/DetectionTeamUCAS/Models/tree/master/FPN_Tensorflow

将下载好的权重文件解压在文件夹(D:\pyprojects\FPN_Tensorflow-master\output\trained_weights)中。

2-3 数据集设置

格式如下

├── data
│ ├── train
│—— ├──——Annotation
│—— ├──—— JPEGImages
│ ├── test
│—— ├──—— Annotation
│—— ├──—— JPEGImages

2-4 编译setup.py

打开Anaconda的命令提示符窗口
激活模型虚拟环境
进入模型文件的路径

cd D:\pyprojects\FPN_Tensorflow-master


再进入setup.py文件所在的路径

cd D:\pyprojects\FPN_Tensorflow-master\libs\box_utils\cython_utils

进入后在其中输入如下命令并运行

python setup.py build_ext --inplace


运行成功后如上图所示
其中cython_utils文件夹下会多出以下三个文件

2-5 运行inference.py演示文件

首先进入tools文件夹

cd D:\pyprojects\FPN_Tensorflow-master\tools

在tools文件夹下新建一个名为results的文件夹,用来保存回归框架
输入命令

python inference.py --data_dir="D:/pyprojects/FPN_Tensorflow-master/tools/demos/"  --save_dir="D:/pyprojects/FPN_Tensorflow-master/tools/results/"  --GPU='0'

3、训练VOC数据集

3-1 划分数据集

将VOC数据集放在data文件夹下

D:\pyprojects\FPN_Tensorflow-master\data\VOC\VOC_test\VOC2007

然后用以下代码划分训练集和测试集

# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import
import syssys.path.append('../../')
import shutil
import os
import random
import mathdef mkdir(path):if not os.path.exists(path):os.makedirs(path)divide_rate = 0.8#root_path = '/mnt/ExtraDisk/yangxue/data_ship_clean'
root_path="D:\pyprojects\FPN_Tensorflow-master"  ##注释 修改成我们自己的主路径#image_path = root_path + '/VOCdevkit/JPEGImages'
image_path = root_path + "/data/VOC/VOC_test/VOC2007/JPEGImages/"  ##注释 修改成图像存放的主路径
xml_path = root_path + "/data/VOC/VOC_test/VOC2007/Annotations/"  ##注释 修改成图像标注的存放的主路径image_list = os.listdir(image_path)image_name = [n.split('.')[0] for n in image_list]random.shuffle(image_name)train_image = image_name[:int(math.ceil(len(image_name)) * divide_rate)]
test_image = image_name[int(math.ceil(len(image_name)) * divide_rate):]image_output_train = os.path.join(root_path, 'data/train/JPEGImages') ##注释 输出的train影像的路径
mkdir(image_output_train)
image_output_test = os.path.join(root_path, 'data/test/JPEGImages')#注释 输出的test影像的路径
mkdir(image_output_test)xml_train = os.path.join(root_path, 'data/train/Annotations')##注释 输出的train影像的标注路径
mkdir(xml_train)
xml_test = os.path.join(root_path, 'data/test/Annotations')##注释 输出的test影像的标注路径
mkdir(xml_test)count = 0
for i in train_image:shutil.copy(os.path.join(image_path, i + '.jpg'), image_output_train) ##影像数据格式.jpgshutil.copy(os.path.join(xml_path, i + '.xml'), xml_train)if count % 1000 == 0:print("process step {}".format(count))count += 1for i in test_image:shutil.copy(os.path.join(image_path, i + '.jpg'), image_output_test)shutil.copy(os.path.join(xml_path, i + '.xml'), xml_test)if count % 1000 == 0:print("process step {}".format(count))count += 1

3-2 修改参数

首先修改label_dict.py,位于:

D:\pyprojects\FPN_Tensorflow-master\libs\label_name_dict

class_names = ['back_ground', 'aeroplane', 'bicycle', 'bird', 'boat','bottle', 'bus', 'car', 'cat', 'chair','cow', 'dining table', 'dog', 'horse', 'motorbike','person', 'potted plant', 'sheep', 'sofa', 'train', 'tvmonitor']classes_originID = {'aeroplane': 1, 'bicycle': 2, 'bird': 3, 'boat': 4,'bottle': 5, 'bus': 6, 'car': 7, 'cat': 8, 'chair': 9,'cow': 10, 'dining table': 11, 'dog': 12, 'horse': 13,'motorbike': 14, 'person': 15, 'potted plant': 16, 'sheep': 17,'sofa': 18, 'train': 19, 'tvmonitor': 20}

要与这部分匹配

elif cfgs.DATASET_NAME == 'pascal':NAME_LABEL_MAP = {'back_ground': 0,'aeroplane': 1,'bicycle': 2,'bird': 3,'boat': 4,'bottle': 5,'bus': 6,'car': 7,'cat': 8,'chair': 9,'cow': 10,'diningtable': 11,'dog': 12,'horse': 13,'motorbike': 14,'person': 15,'pottedplant': 16,'sheep': 17,'sofa': 18,'train': 19,'tvmonitor': 20}

3-3 read_tfrecord.py的修改

在模型文件夹的

D:\pyprojects\FPN_Tensorflow-master\data\io

在这里需要修改数据集的名字,但是由于使用的就是PASCAL_VOC的数据集,并且代码中已经有了pascal的数据名称字典,因此这里不需要修改。

3-4 运行read_tfrecord.py

通过命令

cd D:\pyprojects\FPN_Tensorflow-master\data\io

进入read_tfrecord.py所在的文件夹
运行命令

python convert_data_to_tfrecord.py --VOC_dir="D:\pyprojects/FPN_Tensorflow-master/data\train/"  --save_name="train_TF"
python convert_data_to_tfrecord.py --VOC_dir="D:\pyprojects/FPN_Tensorflow-master/data\test/"  --save_name="test_TF"


路径

D:\pyprojects\FPN_Tensorflow-master\data\tfrecord

生成两个对应文件

3-5 train.py

运行以下路径中的train.py即可开始训练

D:\pyprojects\FPN_Tensorflow-master\tools

Windows10下的FPN_TensorFlow复现相关推荐

  1. Windows10下利用Visual Studio Code搭建C语言开发环境

    Windows10下利用Visual Studio Code搭建C语言开发环境 1. 前言 2. 下载安装VSCode以及MinGW 2.1 下载安装VSCode 2.2 下载安装MinGW-w64 ...

  2. Windows10下python-pcl的安装步骤说明,亲测ok

    Windows10下python-pcl的安装步骤说明 1. 环境依赖 2. 安装步骤 2.1 安装visual studio 2017 2.2 安装pcl并配置环境变量 2.3 编译python-p ...

  3. Windows10下如何安装配置 perl 环境

    Perl 最重要的特性是Perl内部集成了正则表达式的功能,以及巨大的第三方代码库CPAN.这篇文章主要介绍了Windows10下安装配置 perl 环境的详细教程,需要的朋友可以参考下 Perl 是 ...

  4. windows10下mysql-8.0.21的安装和使用

    windows10下mysql的安装和使用 文章目录 windows10下mysql的安装和使用 下载 解压文件 在解压后的文件中创建my.ini my.ini中写入如下内容 配置环境变量 cmd 以 ...

  5. windows10下postgreSQL 下载、安装、启动、关闭

    windows10下postgreSQL 下载.安装.启动.关闭 下载 安装 l设置密码和端口,一直next直到安装完成 环境变量

  6. Windows10下安装unbuntu双系统 以及花屏解决办法

    文章目录 Windows10下安装unbuntu双系统 1 创建未分配卷,为ubuntu系统提供空间 2 制作ubuntu启动u盘 下载ubuntu 下载Universal-USB-Installer ...

  7. windows10下使用wget命令(安装失败,请大家提意见)

    windows10下使用wget命令(安装失败,请大家提意见) 下载 下载地址 安装 配置环境变量 计算机–属性–高级系统设置–环境变量 可以在用户变量或者系统变量里新建变量,俩个选一个就行(我在个人 ...

  8. windows10下 tensorflow2.0 gpu 安装

    windows10下 tensorflow2.0 gpu 安装 安装显卡驱动. 确定python.cuda和cudnn对应的版本 安装cuda 安装cudnn 查看tensorflow的版本 impo ...

  9. windows10下pip安装速度慢的解决办法

    windows10下pip安装速度慢的解决办法 pip修改源 我们这里使用的镜像源是清华大学的镜像源,使用方法有两种,第一种是临时使用: pip install -i https://pypi.tun ...

最新文章

  1. 【转】Python机器学习库
  2. HTML的标签描述9
  3. Unix/Linux环境C编程新手教程(22) C/C++怎样获取程序的执行时间
  4. TurboMail邮件系统通过涉密信息系统产品认定
  5. form表单的一个页面多个上传按钮实例
  6. redis 系列26 Cluster高可用 (1)
  7. 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(一)-- 起步
  8. element-UI响应式(布局原理)讲解 - 贴文篇
  9. Java高并发编程详解-代码在本地
  10. 前端纯css 图片的模糊处理
  11. MyApps接口引擎,打破跨系统间的壁垒
  12. 一直想当5G老大的美国,现在进展怎样了?
  13. TMS320C6748_ECAP_APWM
  14. 学习笔记之-51单片机定时计数器
  15. 笨笨图片批量下载器[C# | WinForm | 正则表达式 | HttpWebRequest]
  16. 【研一小白论文精读】《Big Self-Supervised Models are Strong Semi-Supervised Learners》
  17. 【总结】浏览器 User-Agent 大全
  18. poj 1187 陨石的秘密
  19. mysql grant all on_mysql 赋给用户权限 grant all privileges on
  20. 作用域-函数包围代码

热门文章

  1. 格力手机2 官方固件_ROG游戏手机2备货不足,官方发表致歉信
  2. java入门篇(21)File类
  3. 信息系统安全等级保护一些实施难点
  4. threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index]
  5. Golang之Shadowed Variables(幽灵变量)
  6. 分布式锁1 Java常用技术方案
  7. 基于有道翻译的英翻中微信小程序
  8. 51单片机系列--闪烁灯,呼吸灯与流水灯
  9. 《微观经济学》第八章 博弈论与寡头市场初步笔记
  10. 后台如何清理软Raid