-
Notifications
You must be signed in to change notification settings - Fork 304
Add VGG16 #265
Add VGG16 #265
Changes from 145 commits
46a07fb
4a32cce
8b35001
1cd87b6
af88d89
2bbece2
17f6333
2d4c45e
55ecaca
f570a92
f479f21
e23e5f5
b45fe22
1f3a999
1f0353a
d184963
c6d62de
d1a4e46
df66a0a
84a905d
a83d548
b696389
47326fb
30035bb
2ffae2b
8b3337c
fab7a7c
1ac56a1
86f351d
fdf763e
d37d08d
7f04616
ce77737
2206fc8
64efbc0
ab2cfee
3caec05
7b2d214
96e285c
b0b070c
a1d6927
338819a
700c5f4
b1983cd
5b08bd7
6a2b43c
724dcb5
49bee22
5af7506
aa18ad7
6c5f6f9
b703084
75db194
a36323e
a889d47
a91d29d
e0d9147
0972d02
d7f3f52
e794055
3309415
4c8b3e6
e51762f
10815f7
3e22012
c8fbda6
7ef4623
427f724
1c49938
467c500
a1f43dd
c3af69e
011b320
ae5fd2d
c8cdc36
268e781
ebe9340
8f65c02
80df6a9
320174d
55c50c9
57ab2cc
ec8ddf7
a232abc
348ae85
f97cd49
2940e0d
3b24821
8b408c6
578b3aa
05b478c
826c183
a5f0be7
23a42d9
775ab31
f553948
7602cc7
b9c3f5c
a982c8e
48db171
f78cec3
459e3c6
b8e890d
f04cb4b
d005cb9
3820df9
3323ce7
9d25473
f615940
3bc3b09
4233f8e
7608809
6b8c0da
b85a3ce
20783d6
29f8979
20b6eb9
a59194a
9a64c9b
799b48b
5c83e38
aafaecc
fdd6f48
4bbdd5a
465f1df
62edb90
895ef8a
141bc39
6d4cf29
b7df511
fd0dde5
5b2a257
86029ef
93d8e67
53bb7c6
3a13b35
8ea45df
c7a14ad
9a18739
26231db
d29172e
a3a20b2
10d2e09
3f0da8e
59f53e1
4cf289c
e45bc5a
49249ed
0dfddfe
0900011
4de11f7
9d1e8ee
70bf30b
743055f
7bd02b0
9ca4070
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from chainercv.links.model.vgg.vgg16 import VGG16 # NOQA |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
from __future__ import division | ||
|
||
import numpy as np | ||
|
||
import chainer | ||
from chainer.functions import dropout | ||
from chainer.functions import max_pooling_2d | ||
from chainer.functions import relu | ||
from chainer.functions import softmax | ||
from chainer.initializers import constant | ||
from chainer.initializers import normal | ||
|
||
from chainer.links import Linear | ||
|
||
from chainercv.utils import download_model | ||
|
||
from chainercv.links.connection.conv_2d_activ import Conv2DActiv | ||
from chainercv.links.model.sequential_feature_extractor import \ | ||
SequentialFeatureExtractor | ||
|
||
|
||
# RGB order | ||
_imagenet_mean = np.array( | ||
[123.68, 116.779, 103.939], dtype=np.float32)[:, np.newaxis, np.newaxis] | ||
|
||
|
||
class VGG16(SequentialFeatureExtractor): | ||
|
||
"""VGG16 Network for classification and feature extraction. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
This is a feature extraction model. | ||
The network can choose to output features from set of all | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
intermediate features. | ||
The value of :obj:`VGG16.feature_names` selects the features that are going | ||
to be collected by :meth:`__call__`. | ||
:obj:`self.all_feature_names` is the list of the names of features | ||
that can be collected. | ||
|
||
Examples: | ||
|
||
>>> model = VGG16() | ||
# By default, VGG16.__call__ returns a probability score. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about |
||
>>> prob = model(imgs) | ||
|
||
>>> model.feature_names = 'conv5_3' | ||
# This is feature conv5_3. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about |
||
>>> feat5_3 = model(imgs) | ||
|
||
>>> model.feature_names = ['conv5_3', 'fc6'] | ||
>>> # These are features conv5_3 and fc6. | ||
>>> feat5_3, feat6 = model(imgs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
.. seealso:: | ||
:class:`chainercv.links.model.SequentialFeatureExtractor` | ||
|
||
When :obj:`pretrained_model` is 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 the 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:`imagenet`: Loads weights trained with ImageNet and distributed \ | ||
at `Model Zoo \ | ||
<https://github.com/BVLC/caffe/wiki/Model-Zoo>`_. | ||
|
||
Args: | ||
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. | ||
n_class (int): The number of classes. | ||
mean (numpy.ndarray): A mean value. If :obj:`None` and | ||
a supported pretrained model is used, | ||
the mean value used to train the pretrained model will be used. | ||
initialW (callable): Initializer for the weights. | ||
initial_bias (callable): Initializer for the biases. | ||
|
||
""" | ||
|
||
_models = { | ||
'imagenet': { | ||
'n_class': 1000, | ||
'url': 'https://github.com/yuyu2172/share-weights/releases/' | ||
'download/0.0.4/vgg16_imagenet_convert_2017_07_18.npz', | ||
'mean': _imagenet_mean | ||
} | ||
} | ||
|
||
def __init__(self, | ||
pretrained_model=None, n_class=None, mean=None, | ||
initialW=None, initial_bias=None): | ||
if n_class is None: | ||
if pretrained_model in self._models: | ||
n_class = self._models[pretrained_model]['n_class'] | ||
else: | ||
n_class = 1000 | ||
|
||
if mean is None: | ||
if pretrained_model in self._models: | ||
mean = self._models[pretrained_model]['mean'] | ||
else: | ||
mean = _imagenet_mean | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about adding a note about these behaviours? The default values of |
||
self.mean = mean | ||
|
||
if initialW is None: | ||
# Employ default initializers used in the original paper. | ||
initialW = normal.Normal(0.01) | ||
if pretrained_model: | ||
# As a sampling process is time-consuming, | ||
# we employ a zero initializer for faster computation. | ||
initialW = constant.Zero() | ||
kwargs = {'initialW': initialW, 'initial_bias': initial_bias} | ||
|
||
super(VGG16, self).__init__() | ||
with self.init_scope(): | ||
self.conv1_1 = Conv2DActiv(None, 64, 3, 1, 1, **kwargs) | ||
self.conv1_2 = Conv2DActiv(None, 64, 3, 1, 1, **kwargs) | ||
self.pool1 = _max_pooling_2d | ||
self.conv2_1 = Conv2DActiv(None, 128, 3, 1, 1, **kwargs) | ||
self.conv2_2 = Conv2DActiv(None, 128, 3, 1, 1, **kwargs) | ||
self.pool2 = _max_pooling_2d | ||
self.conv3_1 = Conv2DActiv(None, 256, 3, 1, 1, **kwargs) | ||
self.conv3_2 = Conv2DActiv(None, 256, 3, 1, 1, **kwargs) | ||
self.conv3_3 = Conv2DActiv(None, 256, 3, 1, 1, **kwargs) | ||
self.pool3 = _max_pooling_2d | ||
self.conv4_1 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) | ||
self.conv4_2 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) | ||
self.conv4_3 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) | ||
self.pool4 = _max_pooling_2d | ||
self.conv5_1 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) | ||
self.conv5_2 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) | ||
self.conv5_3 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) | ||
self.pool5 = _max_pooling_2d | ||
self.fc6 = Linear(None, 4096, **kwargs) | ||
self.fc6_relu = relu | ||
self.fc6_dropout = dropout | ||
self.fc7 = Linear(None, 4096, **kwargs) | ||
self.fc7_relu = relu | ||
self.fc7_dropout = dropout | ||
self.fc8 = Linear(None, n_class, **kwargs) | ||
self.prob = softmax | ||
|
||
if pretrained_model in self._models: | ||
path = download_model(self._models[pretrained_model]['url']) | ||
chainer.serializers.load_npz(path, self) | ||
elif pretrained_model: | ||
chainer.serializers.load_npz(pretrained_model, self) | ||
|
||
|
||
def _max_pooling_2d(x): | ||
return max_pooling_2d(x, ksize=2) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,28 @@ | ||
Links | ||
===== | ||
|
||
.. module:: chainercv.links.model.faster_rcnn | ||
|
||
Model | ||
----- | ||
|
||
|
||
Feature Extraction | ||
~~~~~~~~~~~~~~~~~~ | ||
Feature extraction models can be used to extract feature(s) given images. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
.. toctree:: | ||
|
||
links/vgg | ||
|
||
|
||
.. autoclass:: chainercv.links.SequentialFeatureExtractor | ||
:members: | ||
|
||
.. autoclass:: chainercv.links.FeaturePredictor | ||
|
||
|
||
Detection | ||
--------- | ||
~~~~~~~~~ | ||
|
||
Detection links share a common method :meth:`predict` to detect objects in images. | ||
For more details, please read :func:`FasterRCNN.predict`. | ||
|
@@ -16,7 +34,7 @@ For more details, please read :func:`FasterRCNN.predict`. | |
|
||
|
||
Semantic Segmentation | ||
--------------------- | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. module:: chainercv.links.model.segnet | ||
|
||
|
@@ -29,7 +47,7 @@ For more details, please read :func:`SegNetBasic.predict`. | |
|
||
|
||
Classifiers | ||
----------- | ||
~~~~~~~~~~~ | ||
|
||
.. toctree:: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
VGG | ||
=== | ||
|
||
.. module:: chainercv.links.model.vgg | ||
|
||
|
||
VGG16 | ||
----- | ||
|
||
.. autoclass:: VGG16 | ||
:members: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you use
trial_4
? Is this the best model?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is the model that is converted from
faster_rcnn_vgg16_voc07_2017_06_06.npz
.It performs the same with the previously distributed model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps,
faster_rcnn_vgg16_voc07_2017_08_06.npz
is better. User will think "what is trial_4?" like me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Thanks for you feedback.