<commit_before>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.convolution_2d_block import Convolution2DBlock
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.

    This model is a feature extraction link.
    The network can choose to output features from set of all
    intermediate and final features produced by the original architecture.
    The value of :obj:`VGG16.feature_names` selects the features 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.
        >>> prob = model(imgs)

        >>> model.feature_names = 'conv5_3'
        # This is feature conv5_3.
        >>> feat5_3 = model(imgs)

        >>> model.feature_names = ['conv5_3', 'fc6']
        >>> # These are features conv5_3 and fc6.
        >>> feat5_3, feat6 = model(imgs)

    .. 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 dimension of the output of fc8.
        mean (numpy.ndarray): A mean image. If :obj:`None` and
            a supported pretrained model is used,
            the mean image 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']
        self.mean = mean

        # Employ default initializers used in the original paper.
        if initialW is None:
            initialW = normal.Normal(0.01)
        if initial_bias is None:
            initial_bias = constant.Zero()
        kwargs = {'initialW': initialW, 'initial_bias': initial_bias}<commit_msg>use Zero initialization when pretrained model is used<commit_after>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.convolution_2d_block import Convolution2DBlock
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.

    This model is a feature extraction link.
    The network can choose to output features from set of all
    intermediate and final features produced by the original architecture.
    The value of :obj:`VGG16.feature_names` selects the features 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.
        >>> prob = model(imgs)

        >>> model.feature_names = 'conv5_3'
        # This is feature conv5_3.
        >>> feat5_3 = model(imgs)

        >>> model.feature_names = ['conv5_3', 'fc6']
        >>> # These are features conv5_3 and fc6.
        >>> feat5_3, feat6 = model(imgs)

    .. 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 dimension of the output of fc8.
        mean (numpy.ndarray): A mean image. If :obj:`None` and
            a supported pretrained model is used,
            the mean image 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']
        self.mean = mean

        # Employ default initializers used in the original paper.
        if initialW is None:
            initialW = normal.Normal(0.01)
        if initial_bias is None:
            initial_bias = constant.Zero()

        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}
{'fraction_non_alphanumeric': 0.07809768038232895, 'fraction_numerical': 0.017717682713602984, 'mean_word_length': 2.634053367217281, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7229089', 'n_tokens_mistral': 2497, 'n_tokens_neox': 2369, 'n_words': 796}