轉載自:faster rcnn源碼解讀(三)train_faster_rcnn_alt_opt.py - 野孩子的專欄 - 博客頻道 - CSDN.NET
http://blog.csdn.net/u010668907/article/details/51945320
faster用python版本的https://github.com/rbgirshick/py-faster-rcnn
train_faster_rcnn_alt_opt.py源碼在https://github.com/rbgirshick/py-faster-rcnn/blob/master/tools/train_faster_rcnn_alt_opt.py
faster rcnn訓練的開始是:faster_rcnn_alt_opt.sh。下面命令是訓練的,還有它的參數說明。
1.調用最初腳本的說明
cd $FRCN_ROOT
# ./experiments/scripts/faster_rcnn_alt_opt.sh ?GPU ?NET ?DATASET [options args to {train,test}_net.py]
# GPU_ID is the GPU you want to train on
# NET in {ZF, VGG_CNN_M_1024, VGG16} is the network arch to use
# DATASET is only pascal_voc for now
train_faster_rcnn_alt_opt.py的源碼:
[python]?view plaincopy print?
?? ?? ?? ?? ?? ?? ?? ?? ?? ? ? ? ? ?? ?? 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?? from?rpn.generate?import?imdb_proposals?? import?argparse?? import?pprint?? import?numpy?as?np?? import?sys,?os?? import?multiprocessing?as?mp?? import?cPickle?? import?shutil?? ?? def?parse_args():?? ????? ? ?? ????parser?=?argparse.ArgumentParser(description='Train?a?Faster?R-CNN?network')?? ????parser.add_argument('--gpu',?dest='gpu_id',?? ????????????????????????help='GPU?device?id?to?use?[0]',?? ????????????????????????default=0,?type=int)?? ????parser.add_argument('--net_name',?dest='net_name',?? ????????????????????????help='network?name?(e.g.,?"ZF")',?? ????????????????????????default=None,?type=str)?? ????parser.add_argument('--weights',?dest='pretrained_model',?? ????????????????????????help='initialize?with?pretrained?model?weights',?? ????????????????????????default=None,?type=str)?? ????parser.add_argument('--cfg',?dest='cfg_file',?? ????????????????????????help='optional?config?file',?? ????????????????????????default=None,?type=str)?? ????parser.add_argument('--imdb',?dest='imdb_name',?? ????????????????????????help='dataset?to?train?on',?? ????????????????????????default='voc_2007_trainval',?type=str)?? ????parser.add_argument('--set',?dest='set_cfgs',?? ????????????????????????help='set?config?keys',?default=None,?? ????????????????????????nargs=argparse.REMAINDER)?? ?? ????if?len(sys.argv)?==?1:?? ????????parser.print_help()?? ????????sys.exit(1)?? ?? ????args?=?parser.parse_args()?? ????return?args?? ?? def?get_roidb(imdb_name,?rpn_file=None):?? ????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)?? ????if?rpn_file?is?not?None:?? ????????imdb.config['rpn_file']?=?rpn_file?? ????roidb?=?get_training_roidb(imdb)?? ????return?roidb,?imdb?? ?? def?get_solvers(net_name):?? ?????? ????n?=?'faster_rcnn_alt_opt'?? ?????? ????solvers?=?[[net_name,?n,?'stage1_rpn_solver60k80k.pt'],?? ???????????????[net_name,?n,?'stage1_fast_rcnn_solver30k40k.pt'],?? ???????????????[net_name,?n,?'stage2_rpn_solver60k80k.pt'],?? ???????????????[net_name,?n,?'stage2_fast_rcnn_solver30k40k.pt']]?? ????solvers?=?[os.path.join(cfg.MODELS_DIR,?*s)?for?s?in?solvers]?? ?????? ????max_iters?=?[80000,?40000,?80000,?40000]?? ?????? ?????? ????rpn_test_prototxt?=?os.path.join(?? ????????cfg.MODELS_DIR,?net_name,?n,?'rpn_test.pt')?? ????return?solvers,?max_iters,?rpn_test_prototxt?? ?? ?? ?? ?? ?? ?? ?? def?_init_caffe(cfg):?? ????? ?? ?? ????import?caffe?? ?????? ????np.random.seed(cfg.RNG_SEED)?? ????caffe.set_random_seed(cfg.RNG_SEED)?? ?????? ????caffe.set_mode_gpu()?? ????caffe.set_device(cfg.GPU_ID)?? ?? def?train_rpn(queue=None,?imdb_name=None,?init_model=None,?solver=None,?? ??????????????max_iters=None,?cfg=None):?? ????? ?? ?? ?????? ????cfg.TRAIN.HAS_RPN?=?True?? ????cfg.TRAIN.BBOX_REG?=?False???? ????cfg.TRAIN.PROPOSAL_METHOD?=?'gt'?? ????cfg.TRAIN.IMS_PER_BATCH?=?1?? ????print?'Init?model:?{}'.format(init_model)?? ????print('Using?config:')?? ????pprint.pprint(cfg)?? ?? ????import?caffe?? ????_init_caffe(cfg)?? ?? ????roidb,?imdb?=?get_roidb(imdb_name)?? ????print?'roidb?len:?{}'.format(len(roidb))?? ????output_dir?=?get_output_dir(imdb)?? ????print?'Output?will?be?saved?to?`{:s}`'.format(output_dir)?? ?? ????model_paths?=?train_net(solver,?roidb,?output_dir,?? ????????????????????????????pretrained_model=init_model,?? ????????????????????????????max_iters=max_iters)?? ?????? ????for?i?in?model_paths[:-1]:?? ????????os.remove(i)?? ????rpn_model_path?=?model_paths[-1]?? ?????? ????queue.put({'model_path':?rpn_model_path})?? ?? def?rpn_generate(queue=None,?imdb_name=None,?rpn_model_path=None,?cfg=None,?? ?????????????????rpn_test_prototxt=None):?? ????? ?? ?? ????cfg.TEST.RPN_PRE_NMS_TOP_N?=?-1??????? ????cfg.TEST.RPN_POST_NMS_TOP_N?=?2000???? ????print?'RPN?model:?{}'.format(rpn_model_path)?? ????print('Using?config:')?? ????pprint.pprint(cfg)?? ?? ????import?caffe?? ????_init_caffe(cfg)?? ?? ?????? ?????? ?????? ????imdb?=?get_imdb(imdb_name)?? ????print?'Loaded?dataset?`{:s}`?for?proposal?generation'.format(imdb.name)?? ?? ?????? ????rpn_net?=?caffe.Net(rpn_test_prototxt,?rpn_model_path,?caffe.TEST)?? ????output_dir?=?get_output_dir(imdb)?? ????print?'Output?will?be?saved?to?`{:s}`'.format(output_dir)?? ?????? ????rpn_proposals?=?imdb_proposals(rpn_net,?imdb)?? ?????? ?????? ????rpn_net_name?=?os.path.splitext(os.path.basename(rpn_model_path))[0]?? ????rpn_proposals_path?=?os.path.join(?? ????????output_dir,?rpn_net_name?+?'_proposals.pkl')?? ????with?open(rpn_proposals_path,?'wb')?as?f:?? ????????cPickle.dump(rpn_proposals,?f,?cPickle.HIGHEST_PROTOCOL)?? ????print?'Wrote?RPN?proposals?to?{}'.format(rpn_proposals_path)?? ????queue.put({'proposal_path':?rpn_proposals_path})?? ?? def?train_fast_rcnn(queue=None,?imdb_name=None,?init_model=None,?solver=None,?? ????????????????????max_iters=None,?cfg=None,?rpn_file=None):?? ????? ?? ?? ????cfg.TRAIN.HAS_RPN?=?False????????????? ????cfg.TRAIN.PROPOSAL_METHOD?=?'rpn'????? ????cfg.TRAIN.IMS_PER_BATCH?=?2?? ????print?'Init?model:?{}'.format(init_model)?? ????print?'RPN?proposals:?{}'.format(rpn_file)?? ????print('Using?config:')?? ????pprint.pprint(cfg)?? ?? ????import?caffe?? ????_init_caffe(cfg)?? ?? ????roidb,?imdb?=?get_roidb(imdb_name,?rpn_file=rpn_file)?? ????output_dir?=?get_output_dir(imdb)?? ????print?'Output?will?be?saved?to?`{:s}`'.format(output_dir)?? ?????? ????model_paths?=?train_net(solver,?roidb,?output_dir,?? ????????????????????????????pretrained_model=init_model,?? ????????????????????????????max_iters=max_iters)?? ?????? ????for?i?in?model_paths[:-1]:?? ????????os.remove(i)?? ????fast_rcnn_model_path?=?model_paths[-1]?? ?????? ????queue.put({'model_path':?fast_rcnn_model_path})?? ?? if?__name__?==?'__main__':?? ????args?=?parse_args()?? ?? ????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?? ?? ?????? ?????? ?????? ?????? ?????? ?????? ?? ?????? ????mp_queue?=?mp.Queue()?? ?????? ????solvers,?max_iters,?rpn_test_prototxt?=?get_solvers(args.net_name)?? ?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ????print?'Stage?1?RPN,?init?from?ImageNet?model'?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ?? ????cfg.TRAIN.SNAPSHOT_INFIX?=?'stage1'?? ????mp_kwargs?=?dict(?? ????????????queue=mp_queue,?? ????????????imdb_name=args.imdb_name,?? ????????????init_model=args.pretrained_model,?? ????????????solver=solvers[0],?? ????????????max_iters=max_iters[0],?? ????????????cfg=cfg)?? ????p?=?mp.Process(target=train_rpn,?kwargs=mp_kwargs)?? ????p.start()?? ????rpn_stage1_out?=?mp_queue.get()?? ????p.join()?? ?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ????print?'Stage?1?RPN,?generate?proposals'?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ?? ????mp_kwargs?=?dict(?? ????????????queue=mp_queue,?? ????????????imdb_name=args.imdb_name,?? ????????????rpn_model_path=str(rpn_stage1_out['model_path']),?? ????????????cfg=cfg,?? ????????????rpn_test_prototxt=rpn_test_prototxt)?? ????p?=?mp.Process(target=rpn_generate,?kwargs=mp_kwargs)?? ????p.start()?? ????rpn_stage1_out['proposal_path']?=?mp_queue.get()['proposal_path']?? ????p.join()?? ?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ????print?'Stage?1?Fast?R-CNN?using?RPN?proposals,?init?from?ImageNet?model'?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ?? ????cfg.TRAIN.SNAPSHOT_INFIX?=?'stage1'?? ????mp_kwargs?=?dict(?? ????????????queue=mp_queue,?? ????????????imdb_name=args.imdb_name,?? ????????????init_model=args.pretrained_model,?? ????????????solver=solvers[1],?? ????????????max_iters=max_iters[1],?? ????????????cfg=cfg,?? ????????????rpn_file=rpn_stage1_out['proposal_path'])?? ????p?=?mp.Process(target=train_fast_rcnn,?kwargs=mp_kwargs)?? ????p.start()?? ????fast_rcnn_stage1_out?=?mp_queue.get()?? ????p.join()?? ?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ????print?'Stage?2?RPN,?init?from?stage?1?Fast?R-CNN?model'?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ?? ????cfg.TRAIN.SNAPSHOT_INFIX?=?'stage2'?? ????mp_kwargs?=?dict(?? ????????????queue=mp_queue,?? ????????????imdb_name=args.imdb_name,?? ????????????init_model=str(fast_rcnn_stage1_out['model_path']),?? ????????????solver=solvers[2],?? ????????????max_iters=max_iters[2],?? ????????????cfg=cfg)?? ????p?=?mp.Process(target=train_rpn,?kwargs=mp_kwargs)?? ????p.start()?? ????rpn_stage2_out?=?mp_queue.get()?? ????p.join()?? ?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ????print?'Stage?2?RPN,?generate?proposals'?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ?? ????mp_kwargs?=?dict(?? ????????????queue=mp_queue,?? ????????????imdb_name=args.imdb_name,?? ????????????rpn_model_path=str(rpn_stage2_out['model_path']),?? ????????????cfg=cfg,?? ????????????rpn_test_prototxt=rpn_test_prototxt)?? ????p?=?mp.Process(target=rpn_generate,?kwargs=mp_kwargs)?? ????p.start()?? ????rpn_stage2_out['proposal_path']?=?mp_queue.get()['proposal_path']?? ????p.join()?? ?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ????print?'Stage?2?Fast?R-CNN,?init?from?stage?2?RPN?R-CNN?model'?? ????print?'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'?? ?? ????cfg.TRAIN.SNAPSHOT_INFIX?=?'stage2'?? ????mp_kwargs?=?dict(?? ????????????queue=mp_queue,?? ????????????imdb_name=args.imdb_name,?? ????????????init_model=str(rpn_stage2_out['model_path']),?? ????????????solver=solvers[3],?? ????????????max_iters=max_iters[3],?? ????????????cfg=cfg,?? ????????????rpn_file=rpn_stage2_out['proposal_path'])?? ????p?=?mp.Process(target=train_fast_rcnn,?kwargs=mp_kwargs)?? ????p.start()?? ????fast_rcnn_stage2_out?=?mp_queue.get()?? ????p.join()?? ?? ?????? ????final_path?=?os.path.join(?? ????????????os.path.dirname(fast_rcnn_stage2_out['model_path']),?? ????????????args.net_name?+?'_faster_rcnn_final.caffemodel')?? ????print?'cp?{}?->?{}'.format(?? ????????????fast_rcnn_stage2_out['model_path'],?final_path)?? ????shutil.copy(fast_rcnn_stage2_out['model_path'],?final_path)?? ????print?'Final?model:?{}'.format(final_path)??
2. train_faster_rcnn_alt_opt.py的部分參數說明
net_name: ? ? ?{ZF, VGG_CNN_M_1024, VGG16}
pretrained_model: ? ? ?data/imagenet_models/${net_name}.v2.caffemodel
cfg_file: ? ? experiments/cfgs/faster_rcnn_alt_opt.yml
imdb_name: ? ? "voc_2007_trainval" or "voc_2007_test"
?
cfg.TRAIN.HAS_RPN = True表示用xml提供的propoal
cfg是配置文件,它的默認值放在上面的cfg_file里,其他還可以自己寫配置文件之后與默認配置文件融合。
??2.1 net_name是用get_solvers()找到網絡。還要用到cfg的參數MODELS_DIR,
????例子是join(MODELS_DIR,?net_name,?'faster_rcnn_alt_opt', 'stage1_rpn_solver60k80k.pt')
??2.2 imdb_name在factory中被拆成‘2007’(year)和‘trainval’/‘test’(split)到類pascal_voc中產生相應的imdb
??2.3?整個step的大致流程:
(ImageNet model)->stage1_rpn_train->rpn_test
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|(proposal_path)
? ? ? ? ? ? ? ? ? ? (ImageNetmodel)->stage1_fast_rcnn_train-> stage2_rpn_train-> rpn_test-> stage2_fast_rcnn_train
??2.4?數據imdb和roidb
??roidb原本是imdb的一個屬性,但imdb其實是為了計算roidb存在的,他所有的其他屬性和方法都是為了計算roidb
總結
以上是生活随笔為你收集整理的faster rcnn源码解读(三)train_faster_rcnn_alt_opt.py的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。