Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
remove VGG16FeatureExtractor
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyu2172 committed Jun 15, 2017
1 parent 84a905d commit a83d548
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 80 deletions.
1 change: 0 additions & 1 deletion chainercv/links/model/faster_rcnn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from chainercv.links.model.faster_rcnn.faster_rcnn import FasterRCNN # NOQA
from chainercv.links.model.faster_rcnn.faster_rcnn_train_chain import FasterRCNNTrainChain # NOQA
from chainercv.links.model.faster_rcnn.faster_rcnn_vgg import FasterRCNNVGG16 # NOQA
from chainercv.links.model.faster_rcnn.faster_rcnn_vgg import VGG16FeatureExtractor # NOQA
from chainercv.links.model.faster_rcnn.faster_rcnn_vgg import VGG16RoIHead # NOQA
from chainercv.links.model.faster_rcnn.region_proposal_network import RegionProposalNetwork # NOQA
from chainercv.links.model.faster_rcnn.utils.anchor_target_creator import AnchorTargetCreator # NOQA
Expand Down
78 changes: 3 additions & 75 deletions chainercv/links/model/faster_rcnn/faster_rcnn_vgg.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import collections
import numpy as np

import chainer
import chainer.functions as F
import chainer.links as L
from chainer.links import VGG16Layers

from chainercv.links.model.faster_rcnn.faster_rcnn import FasterRCNN
from chainercv.links.model.faster_rcnn.region_proposal_network import \
RegionProposalNetwork
from chainercv.links.model.vgg.vgg16 import VGG16Layers
from chainercv.utils import download_model


Expand Down Expand Up @@ -103,7 +102,7 @@ def __init__(self,
if vgg_initialW is None and pretrained_model:
vgg_initialW = chainer.initializers.constant.Zero()

extractor = VGG16FeatureExtractor(initialW=vgg_initialW)
extractor = VGG16Layers(feature='conv5_3', initialW=vgg_initialW)
rpn = RegionProposalNetwork(
512, 512,
ratios=ratios,
Expand Down Expand Up @@ -139,12 +138,8 @@ def __init__(self,
chainer.serializers.load_npz(pretrained_model, self)

def _copy_imagenet_pretrained_vgg16(self):
pretrained_model = VGG16Layers()
pretrained_model = VGG16Layers(pretrained_model='imagenet')
self.extractor.conv1_1.copyparams(pretrained_model.conv1_1)
# The pretrained weights are trained to accept BGR images.
# Convert weights so that they accept RGB images.
self.extractor.conv1_1.W.data[:] =\
self.extractor.conv1_1.W.data[:, ::-1]
self.extractor.conv1_2.copyparams(pretrained_model.conv1_2)
self.extractor.conv2_1.copyparams(pretrained_model.conv2_1)
self.extractor.conv2_2.copyparams(pretrained_model.conv2_2)
Expand Down Expand Up @@ -225,75 +220,8 @@ def __call__(self, x, rois, roi_indices):
return roi_cls_locs, roi_scores


class VGG16FeatureExtractor(chainer.Chain):
"""Truncated VGG-16 that extracts a conv5_3 feature map.
Args:
initialW (callable): Initializer for the weights.
"""

def __init__(self, initialW=None):
super(VGG16FeatureExtractor, self).__init__()
with self.init_scope():
self.conv1_1 = L.Convolution2D(3, 64, 3, 1, 1, initialW=initialW)
self.conv1_2 = L.Convolution2D(64, 64, 3, 1, 1, initialW=initialW)
self.conv2_1 = L.Convolution2D(64, 128, 3, 1, 1, initialW=initialW)
self.conv2_2 = L.Convolution2D(
128, 128, 3, 1, 1, initialW=initialW)
self.conv3_1 = L.Convolution2D(
128, 256, 3, 1, 1, initialW=initialW)
self.conv3_2 = L.Convolution2D(
256, 256, 3, 1, 1, initialW=initialW)
self.conv3_3 = L.Convolution2D(
256, 256, 3, 1, 1, initialW=initialW)
self.conv4_1 = L.Convolution2D(
256, 512, 3, 1, 1, initialW=initialW)
self.conv4_2 = L.Convolution2D(
512, 512, 3, 1, 1, initialW=initialW)
self.conv4_3 = L.Convolution2D(
512, 512, 3, 1, 1, initialW=initialW)
self.conv5_1 = L.Convolution2D(
512, 512, 3, 1, 1, initialW=initialW)
self.conv5_2 = L.Convolution2D(
512, 512, 3, 1, 1, initialW=initialW)
self.conv5_3 = L.Convolution2D(
512, 512, 3, 1, 1, initialW=initialW)

self.functions = collections.OrderedDict([
('conv1_1', [self.conv1_1, F.relu]),
('conv1_2', [self.conv1_2, F.relu]),
('pool1', [_max_pooling_2d]),
('conv2_1', [self.conv2_1, F.relu]),
('conv2_2', [self.conv2_2, F.relu]),
('pool2', [_max_pooling_2d]),
('conv3_1', [self.conv3_1, F.relu]),
('conv3_2', [self.conv3_2, F.relu]),
('conv3_3', [self.conv3_3, F.relu]),
('pool3', [_max_pooling_2d]),
('conv4_1', [self.conv4_1, F.relu]),
('conv4_2', [self.conv4_2, F.relu]),
('conv4_3', [self.conv4_3, F.relu]),
('pool4', [_max_pooling_2d]),
('conv5_1', [self.conv5_1, F.relu]),
('conv5_2', [self.conv5_2, F.relu]),
('conv5_3', [self.conv5_3, F.relu]),
])

def __call__(self, x):
h = x
for key, funcs in self.functions.items():
for func in funcs:
h = func(h)
return h


def _roi_pooling_2d_yx(x, indices_and_rois, outh, outw, spatial_scale):
xy_indices_and_rois = indices_and_rois[:, [0, 2, 1, 4, 3]]
pool = F.roi_pooling_2d(
x, xy_indices_and_rois, outh, outw, spatial_scale)
return pool


def _max_pooling_2d(x):
return F.max_pooling_2d(x, ksize=2)
4 changes: 0 additions & 4 deletions docs/source/reference/links/faster_rcnn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ RegionProposalNetwork
:members:
:special-members: __call__

VGG16FeatureExtractor
~~~~~~~~~~~~~~~~~~~~~
.. autoclass:: VGG16FeatureExtractor

VGG16RoIHead
~~~~~~~~~~~~
.. autoclass:: VGG16RoIHead
Expand Down

0 comments on commit a83d548

Please sign in to comment.