2023年7月3日发(作者:)
tensorflow版本的fasterrcnn在windows系统下⽤cpu训练代码运⾏路径运⾏在没有中⽂的路径中,环境要求:win10+vs2015+python3.4+tensorflowpython所需第三⽅包:cython, opencv-python, easydict(版本最好为1.6)、scipy、pillow⼀、训练模型⾸先在cpu下完成训练就要把程序中⽤gpu的部分注释掉,具体需要注释的部分如下:(1)tf-faster-rcnn-master/lib/model/路径下USE_GPU_NMS 改为false:(2)lib/model/nms_路径下,注释掉以下部分:(3)lib/整个⽂件重新写⼀份,其中相应的gpu部分已经注释:# --------------------------------------------------------# Fast R-CNN# Copyright (c) 2015 Microsoft# Licensed under The MIT License [see LICENSE for details]# Written by Ross Girshick# --------------------------------------------------------import osfrom import join as pjoinimport numpy as npfrom import setupfrom ion import Extensionfrom ils import build_extdef find_in_path(name, path): "Find a file in a search path" #adapted fom /recipes/52224-find-a-file-given-a-search-path/ for dir in (p): binpath = pjoin(dir, name) if (binpath): return h(binpath) return Nonedef locate_cuda(): """Locate the CUDA environment on the system Returns 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, everything is based on finding 'nvcc' in the PATH. is based on finding 'nvcc' in the PATH. """ # first check if the CUDAHOME env variable is in use if 'CUDAHOME' in n: home = n['CUDAHOME'] nvcc = pjoin(home, 'bin', 'nvcc') else: # otherwise, search the PATH for NVCC nvcc = find_in_path('', n['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') return None home = e(e(nvcc)) cudaconfig = {'home':home, 'nvcc':nvcc, 'include': pjoin(home, 'include'), 'lib64': pjoin(home, 'lib', 'x64')} for k, v in iter(()): if not (v): # raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v)) return None return cudaconfigCUDA = locate_cuda()# Obtain the numpy include directory. This logic works across numpy : numpy_include = _include()except AttributeError: numpy_include = _numpy_include()def customize_compiler_for_nvcc(self): # _ imports: import os import shutil import stat import subprocess import winreg from import DistutilsExecError, DistutilsPlatformError, CompileError, LibError, LinkError from ler import CCompiler, gen_lib_options from distutils import log from import get_platform from itertools import count super = e _('.cu') # find python include import sys py_dir = e('', '/').split('/')[:-1] py_include = pjoin('/'.join(py_dir), 'include') # override method in _, starts from line 340 def compile(sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None): if not lized: lize() compile_info = self._setup_compile(output_dir, macros, include_dirs, sources, depends, extra_postargs) macros, objects, extra_postargs, pp_opts, build = compile_info macros, objects, extra_postargs, pp_opts, build = compile_info compile_opts = extra_preargs or [] compile_('/c') if debug: compile_(e_options_debug) else: compile_(e_options) add_cpp_opts = False for obj in objects: try: src, ext = build[obj] except KeyError: continue if debug: # pass the full pathname to MSVC in debug mode, # this allows the debugger to find the source file # without asking the user to browse for it src = h(src) if ext in self._c_extensions: input_opt = "/Tc" + src elif ext in self._cpp_extensions: input_opt = "/Tp" + src add_cpp_opts = True elif ext in self._rc_extensions: # compile .RC to .RES file input_opt = src output_opt = "/fo" + obj try: ([] + pp_opts + [output_opt, input_opt]) except DistutilsExecError as msg: raise CompileError(msg) continue elif ext in self._mc_extensions: # Compile .MC to .RC file to .RES file. # * '-h dir' specifies the directory for the # generated include file # * '-r dir' specifies the target directory of the # generated RC file and the binary message resource # it includes # # For now (since there are no options to change this), # we use the source-directory for the include file and # the build directory for the RC file and message # resources. This works at least for win32all. h_dir = e(src) rc_dir = e(obj) try: # first compile .MC to .RC and .H file ([, '-h', h_dir, '-r', rc_dir, src]) base, _ = xt(me(src)) rc_file = (rc_dir, base + '.rc') # then compile .RC to .RES file ([, "/fo" + obj, rc_file]) except DistutilsExecError as msg: raise CompileError(msg) continue elif ext == '.cu': # a trigger for cu compile try: # use the cuda for .cu files # _executable('compiler_so', CUDA['nvcc']) # _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 class postargs = extra_postargs['nvcc'] arg = [CUDA['nvcc']] + sources + ['-odir', pjoin(output_dir, 'nms')] for include_dir in include_dirs: ('-I') (include_dir) arg += ['-I', py_include] # arg += ['-lib', CUDA['lib64']] arg += ['-Xcompiler', '/EHsc,/W3,/nologo,/Ox,/MD'] arg += postargs (arg) continue except DistutilsExecError as msg: # raise CompileError(msg) continue else: # how to handle this file? raise CompileError("Don't know how to compile {} to {}" .format(src, obj)) args = [] + compile_opts + pp_opts if add_cpp_opts: ('/EHsc') (input_opt) ("/Fo" + obj) (extra_postargs) try: (args) except DistutilsExecError as msg: raise CompileError(msg) return objects e = compile# run the customize_compilerclass custom_build_ext(build_ext): def build_extensions(self): customize_compiler_for_nvcc(er) build__extensions(self)ext_modules = [ Extension( "_bbox", ["utils/"], extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]}, include_dirs = [numpy_include] ), Extension( "_nms", ["nms/cpu_"], include_dirs = [numpy_include] ), # Extension('_nms', # ['nms/nms_', 'nms/gpu_'], # library_dirs=[CUDA['lib64']], # libraries=['cudart'], # language='c++', # # this syntax is specific to this build system # # we're only going to use certain compiler args with nvcc and not with gcc # # the implementation of this trick is in customize_compiler() below # extra_compile_args={'gcc': ["-Wno-unused-function"], # 'nvcc': ['-arch=sm_52', # 'nvcc': ['-arch=sm_52', # '--ptxas-options=-v', # '-c']}, # include_dirs = [numpy_include, CUDA['include']] # )]setup( name='tf_faster_rcnn', ext_modules=ext_modules, # inject our custom trigger cmdclass={'build_ext': custom_build_ext},)(4)(a)⽂件lib/nms/cpu_:改变第25⾏:cdef y[_t, ndim=1] order = t()[::-1]为:cdef y[64_t, ndim=1] order = t()[::-1](b)⽂件lib/nms/gpu_:改变25⾏: cdef y[_t, ndim=1] 为:cdef y[64_t, ndim=1] (c)⽂件lib/datasets/pascal_:改变226⾏:'{:s}.xml')为: '{0:s}.xml')(d)⽂件 lib/datasets/voc_:改变121⾏:with open(cachefile, 'w') as f:为:with open(cachefile, 'wb') as f:(5)转到 lib ⽬录下, 终端运⾏ python build_ext.
这⼀步如果遇到⼀些错误,建议删除所有的.c和.cpp⽂件,(lib/nms/cpu_nms.c, lib/nms/gpu_, lib/utils/bbox.c)注:如果⽤gpu训练的话,可能会出现错误:这种情况下,就前往nms/gpu_⽂件的2152⾏下,将:_nms((&(*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int32_t *, __pyx_pybuffernd_er->......中的__pyx_t_5numpy_int32_t *”转换成 int *,即:_nms((&(*__Pyx_BufPtrStrided1d(int *, __pyx_pybuffernd_er->修改之后,在运⾏⼀次 python build_ext. 就OK了(6)训练模型:python build_ext命令执⾏完后会在lib/build⽬录下⽣成如下⽂件,将nms⽂件夹⾥⾯的两个⽂件移动到lib/nms⽂件夹中,utils⽂件夹中的⼀个⽂件移动到lib/utils⽂件夹下⾄此,就可以正式开始训练模型。因为GitHub原作者是在ubuntu系统下进⾏训练的,所以⽤./experiments/scripts/train_faster_ 0 pascal_voc vgg16就可以进⾏,⽽.sh⽂件不能⽤在windows平台上(本⼈对ubuntu⼀概不知,所以也不会弄),但是训练的过程它是调⽤的trainval_开始训练的,所以我就把trainval_进⾏修改,代码如下:from __future__ import absolute_importfrom __future__ import divisionfrom __future__ import print_functionimport _init_pathsfrom _val import get_training_roidb, train_netfrom import cfg, cfg_from_file, cfg_from_list, get_output_dir, get_output_tb_dirfrom y import get_imdbimport port argparseimport pprintimport numpy as npimport sysimport tensorflow as tffrom 16 import vgg16from _v1 import resnetv1class args: """ Parse input arguments """ cfg_file = '../experiments/cfgs/' weight = '../data/imagenet_weights/' imdb_name = 'voc_2007_trainval' imdbval_name = 'voc_2007_test' max_iters = 100000 tag = None net = 'vgg16' set_cfgs = ['ANCHOR_SCALES', '[8,16,32]', 'ANCHOR_RATIOS', '[0.5,1,2]', 'ZE', '[50000]']def combined_roidb(imdb_names): """ Combine multiple roidbs """ def get_roidb(imdb_name): imdb = get_imdb(imdb_name) print('Loaded dataset `{:s}` for training'.format()) _proposal_method(AL_METHOD) print('Set proposal method: {:s}'.format(AL_METHOD)) roidb = get_training_roidb(imdb) return roidb roidbs = [get_roidb(s) for s in imdb_('+')] roidb = roidbs[0] if len(roidbs) > 1: for r in roidbs[1:]: (r) tmp = get_imdb(imdb_('+')[1]) imdb = (imdb_names, s) else: imdb = get_imdb(imdb_names) return imdb, roidbif __name__ == '__main__': # args = parse_args() print('Called with args:') if _file is not None: cfg_from_file(_file) if _cfgs is not None: cfg_from_list(_cfgs) print('Using config:') (cfg) (_SEED) # train set imdb, roidb = combined_roidb(_name) print('{:d} roidb entries'.format(len(roidb))) # output directory where the models are saved output_dir = get_output_dir(imdb, ) print('Output will be saved to `{:s}`'.format(output_dir)) # tensorboard directory where the summaries are saved during training tb_dir = get_output_tb_dir(imdb, ) print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir)) # also add the validation set, but with no flipping images orgflip = _FLIPPED _FLIPPED = False _, valroidb = combined_roidb(l_name) print('{:d} validation roidb entries'.format(len(valroidb))) _FLIPPED = orgflip # load network if == 'vgg16': net = vgg16() elif == 'res50': net = resnetv1(num_layers=50) elif == 'res101': net = resnetv1(num_layers=101) elif == 'res152': net = resnetv1(num_layers=152) elif == 'mobile': net = mobilenetv1() else: raise NotImplementedError train_net(net, imdb, roidb, valroidb, output_dir, tb_dir, pretrained_model=, max_iters=_iters)原来的trainval_⽂件中的args是通过命令⾏来传参的,这⾥我设置了⼀个args类对所需的变量进⾏赋值。之后运⾏trainval_就可以训练⽹络模型~~~⼆、测试模型对./tools/test_进⾏修改,类似于在trainval_⽂件中的修改,将原本需要通过命令⾏传递的参数写⼀个args类对所需的变量进⾏赋值class args: """ Parse input arguments """ cfg_file = '../experiments/cfgs/ //模型存放的路径 model = 'E:workspacepythonzj/tf-faster-rcnn-master(endernewton)__gpu_v6output(64,128)/vgg16/vgg16_faster_rcnn_iter_' # net = 'res101' net = 'vgg16' imdb_name = 'voc_2007_test' comp_mode =True TRAIN_IMDB="voc_2007_trainval" TEST_IMDB="voc_2007_test" ITERS=7000 max_per_image = 100 tag = None set_cfgs = ['ANCHOR_SCALES', '[8,16,32]', 'ANCHOR_RATIOS', '[0.5,1,2]']修改完之后,直接运⾏test_对训练好的模型进⾏测试三、demo测试同样写⼀个args类进⾏传参,class args: cfg_file = '../experiments/cfgs/' imdb_name = 'voc_2007_test' comp_mode =True TRAIN_IMDB="voc_2007_trainval" TEST_IMDB="voc_2007_test" ITERS=7000 max_per_image = 100 tag = None # net = 'res101' net = 'vgg16' set_cfgs = ['ANCHOR_SCALES', '[8,16,32]', 'ANCHOR_RATIOS', '[0.5,1,2]'] model = 'E:workspacepythonzj/tf-faster-rcnn-master(endernewton)__gpu_v6tf-faster-rcnn-masteroutputvgg16voc_2007_trainvaldefaultvgg16_默认从data/demo路径下读取图⽚进⾏检测,并将检测的结果可视化出来
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688384292a129984.html
评论列表(0条)