From fd3d293103370e98cd8492679cc20f76cfd1a60b Mon Sep 17 00:00:00 2001 From: chenyuntc Date: Fri, 29 Dec 2017 14:22:37 +0800 Subject: [PATCH] remove chainer dependencies --- data/util.py | 2 +- data/voc_dataset.py | 22 +------------- model/faster_rcnn.py | 14 +++++---- model/faster_rcnn_vgg16.py | 49 ++------------------------------ model/region_proposal_network.py | 8 +++--- model/utils/creator_tool.py | 7 +++-- 6 files changed, 22 insertions(+), 80 deletions(-) diff --git a/data/util.py b/data/util.py index a7a2b92..01c3169 100644 --- a/data/util.py +++ b/data/util.py @@ -121,7 +121,7 @@ def crop_bbox( This method is mainly used together with image cropping. This method translates the coordinates of bounding boxes like - :func:`~chainercv.transforms.translate_bbox`. In addition, + :func:`data.util.translate_bbox`. In addition, this function truncates the bounding boxes to fit within the cropped area. If a bounding box does not overlap with the cropped area, this bounding box will be removed. diff --git a/data/voc_dataset.py b/data/voc_dataset.py index 0ae80b6..a8a10ee 100644 --- a/data/voc_dataset.py +++ b/data/voc_dataset.py @@ -1,23 +1,3 @@ -# Mofidied work: -# Original works by: -# -------------------------------------------------------- -# Faster R-CNN implementation In ChainerCV -# Copyright (c) 2017 Preferred Networks, Inc. -# Licensed under The MIT License [see LICENSE for details] -# https://github.com/chainer/chainercv -# -------------------------------------------------------- -# Faster R-CNN implementation by Chainer -# Copyright (c) 2016 Shunta Saito -# Licensed under The MIT License [see LICENSE for details] -# https://github.com/mitmul/chainer-faster-rcnn -# -------------------------------------------------------- -# Faster R-CNN -# Copyright (c) 2015 Microsoft -# Licensed under The MIT License [see LICENSE for details] -# Written by Ross Girshick and Sean Bell -# https://github.com/rbgirshick/py-faster-rcnn -# ---------------------------------------------------- - import os import xml.etree.ElementTree as ET @@ -51,7 +31,7 @@ class VOCBboxDataset: The labels are packed into a one dimensional tensor of shape :math:`(R,)`. :math:`R` is the number of bounding boxes in the image. The class name of the label :math:`l` is :math:`l` th element of - :obj:`chainercv.datasets.voc_bbox_label_names`. + :obj:`VOC_BBOX_LABEL_NAMES`. The array :obj:`difficult` is a one dimensional boolean array of shape :math:`(R,)`. :math:`R` is the number of bounding boxes in the image. diff --git a/model/faster_rcnn.py b/model/faster_rcnn.py index b9c225a..7aefafb 100644 --- a/model/faster_rcnn.py +++ b/model/faster_rcnn.py @@ -27,7 +27,7 @@ class FasterRCNN(nn.Module): in the RoIs and improve localizations. Each stage is carried out by one of the callable - :class:`chainer.Chain` objects :obj:`feature`, :obj:`rpn` and :obj:`head`. + :class:`torch.nn.Module` objects :obj:`feature`, :obj:`rpn` and :obj:`head`. There are two functions :meth:`predict` and :meth:`__call__` to conduct object detection. @@ -49,10 +49,10 @@ class FasterRCNN(nn.Module): extractor (nn.Module): A module that takes a BCHW image array and returns feature maps. rpn (nn.Module): A module that has the same interface as - :class:`~chainercv.links.model.faster_rcnn.RegionProposalNetwork`. + :class:`model.region_proposal_network.RegionProposalNetwork`. Please refer to the documentation found there. - head (nn.Module): A callable that takes - a BCHW array, RoIs and batch indices for RoIs. This returns class + head (nn.Module): A module that takes + a BCHW variable, RoIs and batch indices for RoIs. This returns class dependent localization paramters and class scores. loc_normalize_mean (tuple of four floats): Mean values of localization estimates. @@ -99,7 +99,7 @@ class FasterRCNN(nn.Module): the :math:`L` th class. Args: - x (~chainer.Variable): 4D image variable. + x (autograd.Variable): 4D image variable. scale (float): Amount of scaling applied to the raw image during preprocessing. @@ -261,6 +261,10 @@ class FasterRCNN(nn.Module): return bboxes, labels, scores def get_optimizer(self): + """ + return optimizer, It could be overwriten if you want to specify + special optimizer + """ lr = opt.lr params = [] for key, value in dict(self.named_parameters()).items(): diff --git a/model/faster_rcnn_vgg16.py b/model/faster_rcnn_vgg16.py index 3790292..de1eda2 100644 --- a/model/faster_rcnn_vgg16.py +++ b/model/faster_rcnn_vgg16.py @@ -37,58 +37,17 @@ def decom_vgg16(): class FasterRCNNVGG16(FasterRCNN): """Faster R-CNN based on VGG-16. - - When you specify the path of a pre-trained chainer model serialized as - a :obj:`.npz` file in the constructor, this chain model automatically - initializes all the parameters with it. - When a string in prespecified set is provided, a pretrained model is - loaded from weights distributed on the Internet. - The list of pretrained models supported are as follows: - - * :obj:`voc07`: Loads weights trained with the trainval split of \ - PASCAL VOC2007 Detection Dataset. - * :obj:`imagenet`: Loads weights trained with ImageNet Classfication \ - task for the feature extractor and the head modules. \ - Weights that do not have a corresponding layer in VGG-16 \ - will be randomly initialized. - For descriptions on the interface of this model, please refer to - :class:`~chainercv.links.model.faster_rcnn.FasterRCNN`. - - :class:`~chainercv.links.model.faster_rcnn.FasterRCNNVGG16` - supports finer control on random initializations of weights by arguments - :obj:`vgg_initialW`, :obj:`rpn_initialW`, :obj:`loc_initialW` and - :obj:`score_initialW`. - It accepts a callable that takes an array and edits its values. - If :obj:`None` is passed as an initializer, the default initializer is - used. + :class:`model.faster_rcnn.FasterRCNN`. Args: n_fg_class (int): The number of classes excluding the background. - pretrained_model (str): The destination of the pre-trained - chainer model serialized as a :obj:`.npz` file. - If this is one of the strings described - above, it automatically loads weights stored under a directory - :obj:`$CHAINER_DATASET_ROOT/pfnet/chainercv/models/`, - where :obj:`$CHAINER_DATASET_ROOT` is set as - :obj:`$HOME/.chainer/dataset` unless you specify another value - by modifying the environment variable. - min_size (int): A preprocessing paramter for :meth:`prepare`. - max_size (int): A preprocessing paramter for :meth:`prepare`. ratios (list of floats): This is ratios of width to height of the anchors. anchor_scales (list of numbers): This is areas of anchors. Those areas will be the product of the square of an element in :obj:`anchor_scales` and the original area of the reference window. - vgg_initialW (callable): Initializer for the layers corresponding to - the VGG-16 layers. - rpn_initialW (callable): Initializer for Region Proposal Network - layers. - loc_initialW (callable): Initializer for the localization head. - score_initialW (callable): Initializer for the score head. - proposal_creator_params (dict): Key valued paramters for - :class:`~chainercv.links.model.faster_rcnn.ProposalCreator`. """ @@ -128,14 +87,12 @@ class VGG16RoIHead(nn.Module): This class is used as a head for Faster R-CNN. This outputs class-wise localizations and classification based on feature maps in the given RoIs. + Args: n_class (int): The number of classes possibly including the background. roi_size (int): Height and width of the feature maps after RoI-pooling. spatial_scale (float): Scale of the roi is resized. - vgg_initialW (callable): Initializer for the layers corresponding to - the VGG-16 layers. - loc_initialW (callable): Initializer for the localization head. - score_initialW (callable): Initializer for the score head. + classifier (nn.Module): Two layer Linear ported from vgg16 """ diff --git a/model/region_proposal_network.py b/model/region_proposal_network.py index 83c2422..d395117 100644 --- a/model/region_proposal_network.py +++ b/model/region_proposal_network.py @@ -34,10 +34,10 @@ class RegionProposalNetwork(nn.Module): initialize weight. May also be a callable that takes an array and edits its values. proposal_creator_params (dict): Key valued paramters for - :class:`~chainercv.links.model.faster_rcnn.ProposalCreator`. + :class:`model.utils.creator_tools.ProposalCreator`. .. seealso:: - :class:`~chainercv.links.model.faster_rcnn.ProposalCreator` + :class:`~model.utils.creator_tools.ProposalCreator` """ @@ -70,7 +70,7 @@ class RegionProposalNetwork(nn.Module): * :math:`A` is number of anchors assigned to each pixel. Args: - x (~chainer.Variable): The Features extracted from images. + x (~torch.autograd.Variable): The Features extracted from images. Its shape is :math:`(N, C, H, W)`. img_size (tuple of ints): A tuple :obj:`height, width`, which contains image size after scaling. @@ -78,7 +78,7 @@ class RegionProposalNetwork(nn.Module): reading them from files. Returns: - (~chainer.Variable, ~chainer.Variable, array, array, array): + (~torch.autograd.Variable, ~torch.autograd.Variable, array, array, array): This is a tuple of five following values. diff --git a/model/utils/creator_tool.py b/model/utils/creator_tool.py index 4cec973..c631eca 100644 --- a/model/utils/creator_tool.py +++ b/model/utils/creator_tool.py @@ -1,5 +1,5 @@ import numpy as np -from chainer import cuda +import cupy from model.utils.bbox_tools import bbox2loc, bbox_iou, loc2bbox from model.utils.nms import non_maximum_suppression @@ -419,9 +419,10 @@ class ProposalCreator: # Apply nms (e.g. threshold = 0.7). # Take after_nms_topN (e.g. 300). - # NOTE: somthing is wrong here! + # unNOTE: somthing is wrong here! + # TODO: remove cuda.to_gpu keep = non_maximum_suppression( - cuda.to_gpu(roi), + cp.ascontiguousarray(cp.asarray(a)), thresh=self.nms_thresh) if n_post_nms > 0: keep = keep[:n_post_nms] -- GitLab