欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在pycharm中简单调试Faster RCNN

程序员文章站 2022-05-10 15:12:55
...

        Faster RCNN写的确实很复杂,看源码有些头疼,而且前面是用sublime写程序,看程序。后面改用pycharm,这个编辑器可以很方便调试代码,因此想在此sdk下调试一下Faster RCNN,最开始想着,直接写个启动faster_rcnn_end2end.sh脚本的python程序启动它,奈何命令行样式在pycharm中好像不能调试,我们看程序知道faster_rcnn_end2end.sh实际上去调用了tools文件夹下的train_net.py,如此我直接把命令行中需要传给那个faster_rcnn_end2end.sh脚本的参数直接传递给train_net.py不就搞定了,但为了保留train_net.py这个文件,我把tools下的train_net.py 复制一份放置在本文件夹下,并改名为train_netForDebug.py,同时修改一下这个文件下的一些部分就可以了,实现后果然能够调试了,因此记录于此。由于改动和原始代码不长,下面直接把train_netForDebug.py完整放置在此。

#!/usr/bin/env python

# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

"""Train a Fast R-CNN network on a region of interest database."""

import _init_paths
from fast_rcnn.train import get_training_roidb, train_net
from fast_rcnn.config import cfg, cfg_from_file, cfg_from_list, get_output_dir
from datasets.factory import get_imdb
import datasets.imdb
import caffe
import argparse
import pprint
import numpy as np
import sys

###############################增添的部分#####################################
import os
PyFasterRCNNPath = r'/home/py-faster'
sys.path.insert(0, PyFasterRCNNPath)
os.chdir(PyFasterRCNNPath)

##配置参数在定义的类中修改吧
class TrainInputParms:
	def __init__(self):
            self.cfg_file='experiments/cfgs/faster_rcnn_end2end.yml'
            self.gpu_id=0
            self.imdb_name='voc_2007_trainval'
            self.max_iters=10000
            self.pretrained_model='data/imagenet_models/VGG16.v2.caffemodel'
            self.randomize=False
            self.set_cfgs=None
            self.solver='models/pascal_voc/VGG16/faster_rcnn_end2end/solver.prototxt'
###############################增添的部分#####################################

def combined_roidb(imdb_names):
    def get_roidb(imdb_name):
        imdb = get_imdb(imdb_name)
        print 'Loaded dataset `{:s}` for training'.format(imdb.name)
        imdb.set_proposal_method(cfg.TRAIN.PROPOSAL_METHOD)
        print 'Set proposal method: {:s}'.format(cfg.TRAIN.PROPOSAL_METHOD)
        roidb = get_training_roidb(imdb)
        return roidb

    roidbs = [get_roidb(s) for s in imdb_names.split('+')]
    roidb = roidbs[0]
    if len(roidbs) > 1:
        for r in roidbs[1:]:
            roidb.extend(r)
        imdb = datasets.imdb.imdb(imdb_names)
    else:
        imdb = get_imdb(imdb_names)
    return imdb, roidb


if __name__ == '__main__':
    args=TrainInputParms()#命令行参数解析换为一个类
    print('Called with args:')
    print(args)

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    cfg.GPU_ID = args.gpu_id

    print('Using config:')
    pprint.pprint(cfg)

    if not args.randomize:
        # fix the random seeds (numpy and caffe) for reproducibility
        np.random.seed(cfg.RNG_SEED)
        caffe.set_random_seed(cfg.RNG_SEED)

    # set up caffe
    caffe.set_mode_gpu()
    caffe.set_device(args.gpu_id)

    imdb, roidb = combined_roidb(args.imdb_name)
    print '{:d} roidb entries'.format(len(roidb))
    roidb_1=roidb[1]
    roidbKeys=roidb_1.keys()
    print 'roidb_1.keys=',str(roidbKeys)

    output_dir = get_output_dir(imdb)
    print 'Output will be saved to `{:s}`'.format(output_dir)

    train_net(args.solver, roidb, output_dir,
              pretrained_model=args.pretrained_model,
              max_iters=args.max_iters)

这个改动是很小的,也方便调试