生活随笔
收集整理的這篇文章主要介紹了
faster rcnn源码解读(五)之layer(网络里的input-data)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
轉(zhuǎn)載自:faster rcnn源碼解讀(五)之layer(網(wǎng)絡(luò)里的input-data) - 野孩子的專欄 - 博客頻道 - CSDN.NET
http://blog.csdn.net/u010668907/article/details/51945844
faster rcnn用python版本的https://github.com/rbgirshick/py-faster-rcnn
layer源碼地址:https://github.com/rbgirshick/py-faster-rcnn/blob/master/lib/roi_data_layer/layer.py
源碼:
[python]?view plaincopy print?
?? ?? ?? ?? ?? ?? ?? ? ? ? ?? ?? import?caffe?? from?fast_rcnn.config?import?cfg?? from?roi_data_layer.minibatch?import?get_minibatch?? import?numpy?as?np?? import?yaml?? from?multiprocessing?import?Process,?Queue?? ?? class?RoIDataLayer(caffe.Layer):?? ?????? ?? ????def?_shuffle_roidb_inds(self):?? ?????????? ????????if?cfg.TRAIN.ASPECT_GROUPING:?? ????????????widths?=?np.array([r['width']?for?r?in?self._roidb])?? ????????????heights?=?np.array([r['height']?for?r?in?self._roidb])?? ????????????horz?=?(widths?>=?heights)?? ????????????vert?=?np.logical_not(horz)?? ????????????horz_inds?=?np.where(horz)[0]?? ????????????vert_inds?=?np.where(vert)[0]?? ????????????inds?=?np.hstack((?? ????????????????np.random.permutation(horz_inds),?? ????????????????np.random.permutation(vert_inds)))?? ????????????inds?=?np.reshape(inds,?(-1,?2))?? ????????????row_perm?=?np.random.permutation(np.arange(inds.shape[0]))?? ????????????inds?=?np.reshape(inds[row_perm,?:],?(-1,))?? ????????????self._perm?=?inds?? ????????else:?? ????????????self._perm?=?np.random.permutation(np.arange(len(self._roidb)))?? ????????self._cur?=?0?? ?? ????def?_get_next_minibatch_inds(self):?? ?????????? ????????if?self._cur?+?cfg.TRAIN.IMS_PER_BATCH?>=?len(self._roidb):?? ????????????self._shuffle_roidb_inds()?? ?? ????????db_inds?=?self._perm[self._cur:self._cur?+?cfg.TRAIN.IMS_PER_BATCH]?? ????????self._cur?+=?cfg.TRAIN.IMS_PER_BATCH?? ????????return?db_inds?? ?? ????def?_get_next_minibatch(self):?? ????????? ? ? ? ?? ????????if?cfg.TRAIN.USE_PREFETCH:?? ????????????return?self._blob_queue.get()?? ????????else:?? ????????????db_inds?=?self._get_next_minibatch_inds()?? ????????????minibatch_db?=?[self._roidb[i]?for?i?in?db_inds]?? ????????????return?get_minibatch(minibatch_db,?self._num_classes)?? ?? ????def?set_roidb(self,?roidb):?? ?????????? ????????self._roidb?=?roidb?? ????????self._shuffle_roidb_inds()?? ????????if?cfg.TRAIN.USE_PREFETCH:?? ????????????self._blob_queue?=?Queue(10)?? ????????????self._prefetch_process?=?BlobFetcher(self._blob_queue,?? ?????????????????????????????????????????????????self._roidb,?? ?????????????????????????????????????????????????self._num_classes)?? ????????????self._prefetch_process.start()?? ?????????????? ????????????def?cleanup():?? ????????????????print?'Terminating?BlobFetcher'?? ????????????????self._prefetch_process.terminate()?? ????????????????self._prefetch_process.join()?? ????????????import?atexit?? ????????????atexit.register(cleanup)?? ?? ????def?setup(self,?bottom,?top):?? ?????????? ?? ?????????? ????????layer_params?=?yaml.load(self.param_str_)?? ?? ????????self._num_classes?=?layer_params['num_classes']?? ?? ????????self._name_to_top_map?=?{}?? ?? ?????????? ????????idx?=?0?? ????????top[idx].reshape(cfg.TRAIN.IMS_PER_BATCH,?3,?? ????????????max(cfg.TRAIN.SCALES),?cfg.TRAIN.MAX_SIZE)?? ????????self._name_to_top_map['data']?=?idx?? ????????idx?+=?1?? ?? ????????if?cfg.TRAIN.HAS_RPN:?? ????????????top[idx].reshape(1,?3)?? ????????????self._name_to_top_map['im_info']?=?idx?? ????????????idx?+=?1?? ?? ????????????top[idx].reshape(1,?4)?? ????????????self._name_to_top_map['gt_boxes']?=?idx?? ????????????idx?+=?1?? ????????else:??? ?????????????? ?????????????? ?????????????? ????????????top[idx].reshape(1,?5)?? ????????????self._name_to_top_map['rois']?=?idx?? ????????????idx?+=?1?? ?? ?????????????? ?????????????? ????????????top[idx].reshape(1)?? ????????????self._name_to_top_map['labels']?=?idx?? ????????????idx?+=?1?? ?? ????????????if?cfg.TRAIN.BBOX_REG:?? ?????????????????? ?????????????????? ????????????????top[idx].reshape(1,?self._num_classes?*?4)?? ????????????????self._name_to_top_map['bbox_targets']?=?idx?? ????????????????idx?+=?1?? ?? ?????????????????? ?????????????????? ????????????????top[idx].reshape(1,?self._num_classes?*?4)?? ????????????????self._name_to_top_map['bbox_inside_weights']?=?idx?? ????????????????idx?+=?1?? ?? ????????????????top[idx].reshape(1,?self._num_classes?*?4)?? ????????????????self._name_to_top_map['bbox_outside_weights']?=?idx?? ????????????????idx?+=?1?? ?? ????????print?'RoiDataLayer:?name_to_top:',?self._name_to_top_map?? ????????assert?len(top)?==?len(self._name_to_top_map)?? ?? ????def?forward(self,?bottom,?top):?? ?????????? ????????blobs?=?self._get_next_minibatch()?? ?? ????????for?blob_name,?blob?in?blobs.iteritems():?? ????????????top_ind?=?self._name_to_top_map[blob_name]?? ?????????????? ????????????top[top_ind].reshape(*(blob.shape))?? ?????????????? ????????????top[top_ind].data[...]?=?blob.astype(np.float32,?copy=False)?? ?? ????def?backward(self,?top,?propagate_down,?bottom):?? ?????????? ????????pass?? ?? ????def?reshape(self,?bottom,?top):?? ?????????? ????????pass?? ?? class?BlobFetcher(Process):?? ?????? ????def?__init__(self,?queue,?roidb,?num_classes):?? ????????super(BlobFetcher,?self).__init__()?? ????????self._queue?=?queue?? ????????self._roidb?=?roidb?? ????????self._num_classes?=?num_classes?? ????????self._perm?=?None?? ????????self._cur?=?0?? ????????self._shuffle_roidb_inds()?? ?????????? ????????np.random.seed(cfg.RNG_SEED)?? ?? ????def?_shuffle_roidb_inds(self):?? ?????????? ?????????? ????????self._perm?=?np.random.permutation(np.arange(len(self._roidb)))?? ????????self._cur?=?0?? ?? ????def?_get_next_minibatch_inds(self):?? ?????????? ?????????? ????????if?self._cur?+?cfg.TRAIN.IMS_PER_BATCH?>=?len(self._roidb):?? ????????????self._shuffle_roidb_inds()?? ?? ????????db_inds?=?self._perm[self._cur:self._cur?+?cfg.TRAIN.IMS_PER_BATCH]?? ????????self._cur?+=?cfg.TRAIN.IMS_PER_BATCH?? ????????return?db_inds?? ?? ????def?run(self):?? ????????print?'BlobFetcher?started'?? ????????while?True:?? ????????????db_inds?=?self._get_next_minibatch_inds()?? ????????????minibatch_db?=?[self._roidb[i]?for?i?in?db_inds]?? ????????????blobs?=?get_minibatch(minibatch_db,?self._num_classes)?? ????????????self._queue.put(blobs)??
下面的roidb都只是一次batch的
3.1 setup在caffe.SGDSolver時(shí)調(diào)用;setup的top(list猜測(cè)是c++的vector)的每個(gè)項(xiàng)是caffe._caffe.Blob
(猜測(cè),輸出的Top shape就是上面的top,在setup中被shape;top[0],1 3 [600] 1000;top[1],1 3;top[2], 1 4)(疑問,在forward中blob的數(shù)據(jù)shape被重置,有時(shí)大小甚至?xí)欢?#xff09;
??3.2 name_to_top: {'gt_boxes': 2, 'data': 0, 'im_info': 1}字典的value值是top的對(duì)應(yīng)索引
??3.3 solver.step(1)會(huì)調(diào)用layer的reshape、forward
??3.4 self._perm: 把roidb的索引打亂,造成圖片的shuffle,打亂的索引存儲(chǔ)的地方
??3.5 cfg.TRAIN.IMS_PER_BATCH: (猜測(cè),每次取圖片的數(shù)量)
??3.6 self._cur: 相當(dāng)于一個(gè)指向_perm的指針,每次取走圖片后,他會(huì)跟著變化
??3.7 db_inds: 本次取得圖片的索引
??3.8 def _get_next_minibatch_inds(self): 取得本次圖片的索引,即db_inds
??3.9 minibatch_db: 本次的roidb
??3.10 _num_classes: 網(wǎng)絡(luò)里的類別數(shù)值21
??3.11 forward(): 得到blob并處理放進(jìn)top
solver.step(1)-》reshape-》forward-》_get_next_minbatch-》_get_next_minbatch_inds-》(前面在layers里,現(xiàn)在進(jìn)入minibatch組建真正的blob)get_minibatch
總結(jié)
以上是生活随笔為你收集整理的faster rcnn源码解读(五)之layer(网络里的input-data)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。