예제 #1
0
def get_center_net(name, dataset, pretrained=False, ctx=mx.cpu(),
                   root=os.path.join('~', '.mxnet', 'models'), **kwargs):
    """Get a center net instance.

    Parameters
    ----------
    name : str or None
        Model name, if `None` is used, you must specify `features` to be a `HybridBlock`.
    dataset : str
        Name of dataset. This is used to identify model name because models trained on
        different datasets are going to be very different.
    pretrained : bool or str
        Boolean value controls whether to load the default pretrained weights for model.
        String value represents the hashtag for a certain version of pretrained weights.
    ctx : mxnet.Context
        Context such as mx.cpu(), mx.gpu(0).
    root : str
        Model weights storing path.

    Returns
    -------
    HybridBlock
        A CenterNet detection network.

    """
    # pylint: disable=unused-variable
    net = CenterNet(**kwargs)
    if pretrained:
        from gluoncv.model_zoo.model_store import get_model_file
        full_name = '_'.join(('center_net', name, dataset))
        net.load_parameters(get_model_file(full_name, tag=pretrained, root=root), ctx=ctx)
    else:
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            net.initialize()
        for v in net.collect_params().values():
            try:
                v.reset_ctx(ctx)
            except ValueError:
                pass
    return net
def get_mobilenet_v2(multiplier,
                     pretrained=False,
                     ctx=cpu(),
                     root='~/.mxnet/models',
                     **kwargs):
    r"""MobileNetV2 model from the
    `"Inverted Residuals and Linear Bottlenecks:
      Mobile Networks for Classification, Detection and Segmentation"
    <https://arxiv.org/abs/1801.04381>`_ paper.

    Parameters
    ----------
    multiplier : float
        The width multiplier for controling the model size. Only multipliers that are no
        less than 0.25 are supported. The actual number of channels is equal to the original
        channel size multiplied by this multiplier.
    pretrained : bool or str
        Boolean value controls whether to load the default pretrained weights for model.
        String value represents the hashtag for a certain version of pretrained weights.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default $MXNET_HOME/models
        Location for keeping the model parameters.
    """
    net = MobileNetV2(multiplier, **kwargs)

    if pretrained:
        from .model_store import get_model_file
        version_suffix = '{0:.2f}'.format(multiplier)
        if version_suffix in ('1.00', '0.50'):
            version_suffix = version_suffix[:-1]
        net.load_parameters(get_model_file('mobilenetv2_%s' % version_suffix,
                                           tag=pretrained,
                                           root=root),
                            ctx=ctx)
        from ..data import ImageNet1kAttr
        attrib = ImageNet1kAttr()
        net.synset = attrib.synset
        net.classes = attrib.classes
        net.classes_long = attrib.classes_long
    return net
예제 #3
0
def resnet34_v1b(pretrained=False,
                 root='~/.mxnet/models',
                 ctx=cpu(0),
                 **kwargs):
    """Constructs a ResNetV1b-34 model.

    Parameters
    ----------
    pretrained : bool or str
        Boolean value controls whether to load the default pretrained weights for model.
        String value represents the hashtag for a certain version of pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    dilated: bool, default False
        Whether to apply dilation strategy to ResNetV1b, yielding a stride 8 model.
    norm_layer : object
        Normalization layer used (default: :class:`mxnet.gluon.nn.BatchNorm`)
        Can be :class:`mxnet.gluon.nn.BatchNorm` or :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
    last_gamma : bool, default False
        Whether to initialize the gamma of the last BatchNorm layer in each bottleneck to zero.
    use_global_stats : bool, default False
        Whether forcing BatchNorm to use global statistics instead of minibatch statistics;
        optionally set to True if finetuning using ImageNet classification pretrained models.
    """
    model = ResNetV1b(BasicBlockV1b, [3, 4, 6, 3],
                      name_prefix='resnetv1b_',
                      **kwargs)
    if pretrained:
        from .model_store import get_model_file
        model.load_parameters(get_model_file('resnet%d_v%db' % (34, 1),
                                             tag=pretrained,
                                             root=root),
                              ctx=ctx)
        from ..data import ImageNet1kAttr
        attrib = ImageNet1kAttr()
        model.synset = attrib.synset
        model.classes = attrib.classes
        model.classes_long = attrib.classes_long
    return model
예제 #4
0
def get_efficientnet(model_name,
                     pretrained=False,
                     ctx=mx.cpu(),
                     root='~/.mxnet/models',
                     norm_layer=nn.BatchNorm,
                     norm_kwargs=None,
                     **kwargs):
    width, depth, res, dropout = efficientnet_params(model_name)
    net = EfficientNet(width,
                       depth,
                       dropout=dropout,
                       norm_layer=norm_layer,
                       norm_kwargs=norm_kwargs,
                       **kwargs)
    if pretrained:
        from gluoncv.model_zoo.model_store import get_model_file
        net.load_parameters(get_model_file(model_name,
                                           tag=pretrained,
                                           root=root),
                            ctx=ctx)
    return net
예제 #5
0
def get_vgg_extractor(num_layers,
                      pretrained=False,
                      ctx=mx.cpu(),
                      root='/home/kevin/vgg_params',
                      **kwargs):
    """Get VGG feature extractor networks
    
    Parameters
    ----------
    num_layers : int
        VGG types,can be 11,13,16,19
    pretrained : bool or str
        Boolean value controls whether to load the default pretrained weights for model.
        String value represents the hashtag for a certain version of pretrained weights.
    ctx : mx.Context
        Context such as mx.cpu(), mx.gpu(0).
    root : str
        Model weights storing path.
        
    Returns
    -------
    mxnet.gluon.HybridBlock
        The returned network.    

    """
    layers, filters = vgg_spec[num_layers]
    net = VGGExtractor(layers, filters, extra_spec, **kwargs)
    if pretrained:
        from gluoncv.model_zoo.model_store import get_model_file
        batch_norm_suffix = '_bn' if kwargs.get('batch_norm') else ''
        net.initialize(ctx=ctx)
        assert num_layers >= 16, "current import_params only support vgg 16 or 19, but got {}".format(
            num_layers)
        net.import_params(get_model_file('vgg%d%s' %
                                         (num_layers, batch_norm_suffix),
                                         tag=pretrained,
                                         root=root),
                          ctx=ctx)

    return net
예제 #6
0
def resnet101_v1e(pretrained=False,
                  root='~/.mxnet/models',
                  ctx=cpu(0),
                  **kwargs):
    """Constructs a ResNetV1e-50 model.

    Parameters
    ----------
    pretrained : bool or str
        Boolean value controls whether to load the default pretrained weights for model.
        String value represents the hashtag for a certain version of pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    dilated: bool, default False
        Whether to apply dilation strategy to ResNetV1b, yielding a stride 8 model.
    norm_layer : object
        Normalization layer used (default: :class:`mxnet.gluon.nn.BatchNorm`).
        Can be :class:`mxnet.gluon.nn.BatchNorm` or :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
    """
    model = ResNetV1b(BottleneckV1b, [3, 4, 23, 3],
                      deep_stem=True,
                      avg_down=True,
                      stem_width=64,
                      name_prefix='resnetv1e_',
                      **kwargs)
    if pretrained:
        from .model_store import get_model_file
        model.load_parameters(get_model_file('resnet%d_v%dd' % (101, 1),
                                             tag=pretrained,
                                             root=root),
                              ctx=ctx)
        from ..data import ImageNet1kAttr
        attrib = ImageNet1kAttr()
        model.synset = attrib.synset
        model.classes = attrib.classes
        model.classes_long = attrib.classes_long
    return model
예제 #7
0
파일: vgg.py 프로젝트: sunbc0120/gluon-ocr
def get_vgg(num_layers,
            strides=[(2, 2), (2, 2), (2, 2), (2, 2), (2, 2)],
            pretrained=False,
            ctx=cpu(),
            root='~/.mxnet/models',
            **kwargs):
    r"""VGG model from the `"Very Deep Convolutional Networks for Large-Scale Image Recognition"
    <https://arxiv.org/abs/1409.1556>`_ paper.

    Parameters
    ----------
    num_layers : int
        Number of layers for the variant of densenet. Options are 11, 13, 16, 19.
    pretrained : bool or str
        Boolean value controls whether to load the default pretrained weights for model.
        String value represents the hashtag for a certain version of pretrained weights.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default $MXNET_HOME/models
        Location for keeping the model parameters.
    """

    layers, filters = vgg_spec[num_layers]
    net = VGG(layers, filters, strides, **kwargs)
    if pretrained:
        from gluoncv.model_zoo.model_store import get_model_file
        batch_norm_suffix = '_bn' if kwargs.get('batch_norm') else ''
        net.load_parameters(get_model_file('vgg%d%s' %
                                           (num_layers, batch_norm_suffix),
                                           tag=pretrained,
                                           root=root),
                            ctx=ctx)
        from gluoncv.data import ImageNet1kAttr
        attrib = ImageNet1kAttr()
        net.synset = attrib.synset
        net.classes = attrib.classes
        net.classes_long = attrib.classes_long

    return net
예제 #8
0
def get_resnet(version,
               num_layers,
               pretrained=False,
               ctx=cpu(),
               root='~/.mxnet/models',
               use_se=False,
               **kwargs):
    r"""ResNet V1 model from `"Deep Residual Learning for Image Recognition"
    <http://arxiv.org/abs/1512.03385>`_ paper.
    ResNet V2 model from `"Identity Mappings in Deep Residual Networks"
    <https://arxiv.org/abs/1603.05027>`_ paper.

    Parameters
    ----------
    version : int
        Version of ResNet. Options are 1, 2.
    num_layers : int
        Numbers of layers. Options are 18, 34, 50, 101, 152.
    pretrained : bool or str
        Boolean value controls whether to load the default pretrained weights for model.
        String value represents the hashtag for a certain version of pretrained weights.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default $MXNET_HOME/models
        Location for keeping the model parameters.
    use_se : bool, default False
        Whether to use Squeeze-and-Excitation module
    norm_layer : object
        Normalization layer used (default: :class:`ssd.group_batch_norm.GroupBatchNorm`)
    norm_kwargs : dict
        Additional `batch_norm_layer` arguments, for example `bn_group=4`
        for :class:`ssd.group_batch_norm.GroupBatchNorm`.
    """
    assert num_layers in resnet_spec, \
        "Invalid number of layers: %d. Options are %s"%(
            num_layers, str(resnet_spec.keys()))
    block_type, layers, channels = resnet_spec[num_layers]
    assert 1 <= version <= 2, \
        "Invalid resnet version: %d. Options are 1 and 2."%version
    resnet_class = resnet_net_versions[version - 1]
    block_class = resnet_block_versions[version - 1][block_type]
    net = resnet_class(block_class, layers, channels, **kwargs)
    if pretrained:
        from gluoncv.model_zoo.model_store import get_model_file
        if not use_se:
            net.load_parameters(get_model_file('resnet%d_v%d' %
                                               (num_layers, version),
                                               tag=pretrained,
                                               root=root),
                                ctx=ctx)
        else:
            net.load_parameters(get_model_file('se_resnet%d_v%d' %
                                               (num_layers, version),
                                               tag=pretrained,
                                               root=root),
                                ctx=ctx)
        from gluoncv.data import ImageNet1kAttr
        attrib = ImageNet1kAttr()
        net.synset = attrib.synset
        net.classes = attrib.classes
        net.classes_long = attrib.classes_long
    return net
예제 #9
0
def get_resnext(num_layers,
                strides=[(2, 2), (1, 1), (2, 2), (2, 2), (2, 2)],
                cardinality=32,
                bottleneck_width=4,
                use_se=False,
                deep_stem=False,
                avg_down=False,
                pretrained=False,
                ctx=cpu(),
                root=os.path.join('~', '.mxnet', 'models'),
                **kwargs):
    r"""ResNext model from `"Aggregated Residual Transformations for Deep Neural Network"
    <http://arxiv.org/abs/1611.05431>`_ paper.

    Parameters
    ----------
    num_layers : int
        Numbers of layers. Options are 50, 101.
    cardinality: int
        Number of groups
    bottleneck_width: int
        Width of bottleneck block
    pretrained : bool or str
        Boolean value controls whether to load the default pretrained weights for model.
        String value represents the hashtag for a certain version of pretrained weights.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    norm_layer : object
        Normalization layer used (default: :class:`mxnet.gluon.nn.BatchNorm`)
        Can be :class:`mxnet.gluon.nn.BatchNorm` or :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
    norm_kwargs : dict
        Additional `norm_layer` arguments, for example `num_devices=4`
        for :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
    """
    assert num_layers in resnext_spec, \
        "Invalid number of layers: %d. Options are %s" % (
            num_layers, str(resnext_spec.keys()))

    layers = resnext_spec[num_layers]
    net = ResNext(layers,
                  cardinality,
                  bottleneck_width,
                  strides,
                  use_se=use_se,
                  deep_stem=deep_stem,
                  avg_down=avg_down,
                  **kwargs)
    if pretrained:
        from gluoncv.model_zoo.model_store import get_model_file
        if not use_se:
            net.load_parameters(
                get_model_file('resnext%d_%dx%dd' %
                               (num_layers, cardinality, bottleneck_width),
                               tag=pretrained,
                               root=root),
                ctx=ctx)
        else:
            net.load_parameters(
                get_model_file('se_resnext%d_%dx%dd' %
                               (num_layers, cardinality, bottleneck_width),
                               tag=pretrained,
                               root=root),
                ctx=ctx)
        from gluoncv.data import ImageNet1kAttr
        attrib = ImageNet1kAttr()
        net.synset = attrib.synset
        net.classes = attrib.classes
        net.classes_long = attrib.classes_long

    return net
예제 #10
0
from gluoncv.data.transforms.presets.imagenet import transform_eval
from mxnet import nd, image
from gluoncv.model_zoo.model_store import get_model_file
from numpy.testing import assert_array_almost_equal

if __name__ == "__main__":
    ctx = mx.gpu(0)
    input_pic = '../gluon-cv/street.jpg'
    img = image.imread(input_pic)
    img = transform_eval(img).as_in_context(ctx)
    print(img.shape)

    for num_layers in [16, 19]:
        for bn in [False, True]:
            _bn = '_bn' if bn else ''
            name = 'vgg{}{}'.format(num_layers, _bn)
            model = get_model_file(name, tag=True, root='models')
            print('--------------- {} -------------'.format(model))
            layers, filters = vgg_spec[num_layers]
            vgg = VGG(layers, filters, batch_norm=bn)
            vgg.initialize(ctx=ctx)
            vgg.load_parameters(model, ctx, ignore_extra=True)
            vggbase = VGGBase(layers, filters, batch_norm=bn)
            vggbase.initialize(ctx=ctx)
            vggbase.import_params(model, ctx)
            pred1 = vgg(img)
            pred2 = vggbase(img)
            print('pred1 shape:', pred1.shape)
            print('pred2 shape:', pred2.shape)
            assert_array_almost_equal(pred1.asnumpy(), pred2.asnumpy())
예제 #11
0
def get_hrnet(model_name,
              stage_interp_type='nearest',
              purpose='cls',
              pretrained=False,
              ctx=cpu(),
              root='~/.mxnet/models',
              norm_layer=BatchNorm,
              norm_kwargs=None,
              num_classes=1000,
              **kwargs):
    r"""HRNet model from the
    `"Deep High-Resolution Representation Learning for Visual Recognition"
    <https://arxiv.org/pdf/1908.07919>`_ paper.

    Parameters
    ----------
    model_name : string
        The name of hrnet models: w18_small_v1/w18_small_v2/w30/w32/w40/w42/w48.
    stage_interp_type : string
        The interpolation type for upsample in each stage, nearest, bilinear and
        bilinear_like are supported.
    purpose: string
        The purpose of model, cls and seg are supported.
    pretrained : bool or str
        Boolean value controls whether to load the default pretrained weights for model.
        String value represents the hashtag for a certain version of pretrained weights.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default $MXNET_HOME/models
        Location for keeping the model parameters.
    norm_layer : object
        Normalization layer used (default: :class:`mxnet.gluon.nn.BatchNorm`)
        Can be :class:`mxnet.gluon.nn.BatchNorm` or :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
    norm_kwargs : dict
        Additional `norm_layer` arguments, for example `num_devices=4`
        for :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
    """
    if model_name not in HRNET_SPEC.keys():
        raise NotImplementedError

    spec = HRNET_SPEC[model_name]

    if purpose == 'cls':
        net = HighResolutionClsNet(spec, stage_interp_type, norm_layer,
                                   norm_kwargs, num_classes, **kwargs)
        from gluoncv.data import ImageNet1kAttr
        attrib = ImageNet1kAttr()
        net.synset = attrib.synset
        net.classes = attrib.classes
        net.classes_long = attrib.classes_long
    elif purpose == 'seg':
        net = HighResolutionSegNet(spec, stage_interp_type, norm_layer,
                                   norm_kwargs, num_classes, **kwargs)
    else:
        raise NotImplementedError

    if pretrained:
        from gluoncv.model_zoo.model_store import get_model_file
        net.load_parameters(get_model_file('hrnet_%s_%s' %
                                           (model_name, purpose),
                                           tag=pretrained,
                                           root=root),
                            ctx=ctx,
                            ignore_extra=True)
    return net
예제 #12
0
def get_mobilenet_v3(model_name,
                     strides=[(2, 2), (2, 2), (2, 2), (2, 2)],
                     multiplier=1.,
                     pretrained=False,
                     ctx=cpu(),
                     root='~/.mxnet/models',
                     norm_layer=BatchNorm,
                     norm_kwargs=None,
                     **kwargs):
    r"""MobileNet model from the
    `"Searching for MobileNetV3"
    <https://arxiv.org/abs/1905.02244>`_ paper.

    Parameters
    ----------
    model_name : string
        The name of mobilenetv3 models, large and small are supported.
    multiplier : float
        The width multiplier for controlling the model size. Only multipliers that are no
        less than 0.25 are supported. The actual number of channels is equal to the original
        channel size multiplied by this multiplier.
    pretrained : bool or str
        Boolean value controls whether to load the default pretrained weights for model.
        String value represents the hashtag for a certain version of pretrained weights.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default $MXNET_HOME/models
        Location for keeping the model parameters.
    norm_layer : object
        Normalization layer used (default: :class:`mxnet.gluon.nn.BatchNorm`)
        Can be :class:`mxnet.gluon.nn.BatchNorm` or :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
    norm_kwargs : dict
        Additional `norm_layer` arguments, for example `num_devices=4`
        for :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`.
    """

    if model_name == "large":
        cfg = [
            # k, exp, c,  se,     nl,  s,
            [3, 16, 16, False, 'relu', (1, 1)],
            [3, 64, 24, False, 'relu', strides[0]],
            [3, 72, 24, False, 'relu', (1, 1)],
            [5, 72, 40, True, 'relu', strides[1]],
            [5, 120, 40, True, 'relu', (1, 1)],
            [5, 120, 40, True, 'relu', (1, 1)],
            [3, 240, 80, False, 'hard_swish', strides[2]],
            [3, 200, 80, False, 'hard_swish', (1, 1)],
            [3, 184, 80, False, 'hard_swish', (1, 1)],
            [3, 184, 80, False, 'hard_swish', (1, 1)],
            [3, 480, 112, True, 'hard_swish', (1, 1)],
            [3, 672, 112, True, 'hard_swish', (1, 1)],
            [5, 672, 160, True, 'hard_swish', strides[3]],
            [5, 960, 160, True, 'hard_swish', (1, 1)],
            [5, 960, 160, True, 'hard_swish', (1, 1)],
        ]
        cls_ch_squeeze = 960
        cls_ch_expand = 1280
    elif model_name == "small":
        cfg = [
            # k, exp, c,  se,     nl,  s,
            [3, 16, 16, True, 'relu', strides[0]],
            [3, 72, 24, False, 'relu', strides[1]],
            [3, 88, 24, False, 'relu', (1, 1)],
            [5, 96, 40, True, 'hard_swish', strides[2]],
            [5, 240, 40, True, 'hard_swish', (1, 1)],
            [5, 240, 40, True, 'hard_swish', (1, 1)],
            [5, 120, 48, True, 'hard_swish', (1, 1)],
            [5, 144, 48, True, 'hard_swish', (1, 1)],
            [5, 288, 96, True, 'hard_swish', strides[3]],
            [5, 576, 96, True, 'hard_swish', (1, 1)],
            [5, 576, 96, True, 'hard_swish', (1, 1)],
        ]
        cls_ch_squeeze = 576
        cls_ch_expand = 1280
    else:
        raise NotImplementedError
    net = _MobileNetV3(cfg, cls_ch_squeeze, \
                        cls_ch_expand, multiplier=multiplier, \
                        final_drop=0.2, norm_layer=norm_layer, **kwargs)
    if pretrained:
        from gluoncv.model_zoo.model_store import get_model_file
        net.load_parameters(get_model_file('mobilenetv3_%s' % model_name,
                                           tag=pretrained,
                                           root=root),
                            ctx=ctx)
        from gluoncv.data import ImageNet1kAttr
        attrib = ImageNet1kAttr()
        net.synset = attrib.synset
        net.classes = attrib.classes
        net.classes_long = attrib.classes_long

    return net
예제 #13
0
def get_darknet(pretrained=False,
                ctx=mx.cpu(),
                root=os.path.join('models', 'definitions', 'darknet',
                                  'weights'),
                conv_types=[2, 2, 2, 2, 2, 2],
                return_features=False,
                channels_factor=1,
                **kwargs):
    """
    get a 2D or 2+1D or 3D darknet model with correct transfer of imagenet pretrained weights

    Args:
        pretrained: boolean - use imagenet weights
        ctx: cpu or gpu context?
        root: the root to store / load the pretrained weights
        conv_types: a list of len=6 with either 2, 21, or 3 for the conv layer type for layers upto and not including:
                    [2, 5, 10, 27, 44, 53-1]
        **kwargs:

    Returns:
        net: the network

    """

    layers = [1, 2, 8, 8, 4]
    assert channels_factor in [1, 2, 4, 8, 16]
    if channels_factor > 1:
        pretrained = False

    channels = [
        int(n / channels_factor) for n in [32, 64, 128, 256, 512, 1024]
    ]
    net = Darknet3D(layers,
                    channels,
                    conv_types,
                    return_features=return_features,
                    **kwargs)
    net.initialize()
    if pretrained:
        if 3 not in conv_types and 21 not in conv_types:
            net.load_parameters(
                get_model_file('darknet53', tag=pretrained, root=root),
                ctx=ctx,
                ignore_extra=return_features)  # we won't have the dense layers
            return net

        # transfer weights from 2D
        base_net = Darknet3D(layers,
                             channels, [2, 2, 2, 2, 2, 2],
                             return_features=return_features,
                             **kwargs)
        base_net.load_parameters(
            get_model_file('darknet53', tag=pretrained, root=root),
            ctx=ctx,
            ignore_extra=return_features)  # we won't have the dense layers

        base_params = base_net.collect_params()
        net_params = net.collect_params()

        if 3 in conv_types:
            assert 21 not in conv_types

            for layer_name in net_params:
                if len(net_params[layer_name].shape) == 5:
                    repetitions = net_params[layer_name].shape[-3]
                    base_layer_name = layer_name.replace(
                        net_params.prefix, base_net.prefix)
                    base_weight = base_params[base_layer_name].data()
                    base_weight = base_weight / repetitions
                    new_weight = mx.nd.repeat(mx.nd.expand_dims(base_weight,
                                                                axis=-3),
                                              repetitions,
                                              axis=-3)
                    net.collect_params()[layer_name].set_data(new_weight)
                else:
                    base_layer_name = layer_name.replace(
                        net_params.prefix, base_net.prefix)
                    new_weight = base_params[base_layer_name].data()
                    net.collect_params()[layer_name].set_data(new_weight)
        elif 21 in conv_types:
            assert 3 not in conv_types

            conv_layer_num = -1
            for layer_name in net_params:
                if len(net_params[layer_name].shape) == 5:
                    if net_params[layer_name].shape[-3] == 3 and net_params[
                            layer_name].shape[-1] == 1:  # the temp conv
                        repetitions = net_params[layer_name].shape[-3]
                        out_channels = net_params[layer_name].shape[0]
                        new_weight = mx.nd.ones(shape=(
                            out_channels, 1, repetitions, 1, 1)) / repetitions
                        net.collect_params()[layer_name].set_data(new_weight)
                    else:  # the spatial conv
                        conv_layer_num += 1
                        repetitions = net_params[layer_name].shape[-3]
                        base_layer_name = layer_name.replace(
                            net_params.prefix, base_net.prefix)
                        base_layer_name = base_layer_name.split(
                            '_')[0] + '_conv' + str(conv_layer_num) + '_weight'
                        base_weight = base_params[base_layer_name].data()
                        base_weight = base_weight / repetitions
                        new_weight = mx.nd.expand_dims(base_weight, axis=-3)
                        net.collect_params()[layer_name].set_data(new_weight)
                else:
                    if 'conv' in layer_name:
                        conv_layer_num += 1
                        base_layer_name = layer_name.replace(
                            net_params.prefix, base_net.prefix)
                        base_layer_name = base_layer_name.split(
                            '_')[0] + '_conv' + str(conv_layer_num) + '_weight'
                    else:
                        base_layer_name = layer_name.replace(
                            net_params.prefix, base_net.prefix)

                    new_weight = base_params[base_layer_name].data()
                    net.collect_params()[layer_name].set_data(new_weight)

    net.collect_params().reset_ctx(ctx)
    return net
예제 #14
0
def generate_models(model_name,
                    nclass=400,
                    pretrained=False,
                    pretrained_base=True,
                    use_tsn=False,
                    partial_bn=False,
                    num_segments=1,
                    num_crop=1,
                    root='~/.mxnet/models',
                    ctx=mx.cpu(),
                    **kwargs):
    r"""ResNet18 model trained on Kinetics400 dataset.
    Parameters
    ----------
    nclass : int.
        Number of categories in the dataset.
    pretrained : bool or str.
        Boolean value controls whether to load the default pretrained weights for model.
        String value represents the hashtag for a certain version of pretrained weights.
    pretrained_base : bool or str, optional, default is True.
        Load pretrained base network, the extra layers are randomized. Note that
        if pretrained is `True`, this has no effect.
    ctx : Context, default CPU.
        The context in which to load the pretrained weights.
    root : str, default $MXNET_HOME/models
        Location for keeping the model parameters.
    num_segments : int, default is 1.
        Number of segments used to evenly divide a video.
    num_crop : int, default is 1.
        Number of crops used during evaluation, choices are 1, 3 or 10.
    partial_bn : bool, default False.
        Freeze all batch normalization layers during training except the first layer.
    """
    modality = kwargs['modality']

    in_channels = 3 if modality == 'rgb' else 20
    model = ActionRecResNetV1bCustom(depth=18,
                                     nclass=nclass,
                                     partial_bn=partial_bn,
                                     num_segments=num_segments,
                                     num_crop=num_crop,
                                     dropout_ratio=0.5,
                                     init_std=0.01,
                                     modality=modality,
                                     in_channels=in_channels)

    if pretrained:

        from gluoncv.model_zoo.model_store import get_model_file
        params_dict = mx.nd.load(
            get_model_file('resnet18_v1b_kinetics400',
                           tag=pretrained,
                           root=root))
        if modality == 'tvl1_flow':
            params_dict = change_key_names(params_dict, in_channels=20)

        model.load_dict(params_dict, ctx=ctx)
        from gluoncv.data import Kinetics400Attr
        attrib = Kinetics400Attr()
        model.classes = attrib.classes
    model.collect_params().reset_ctx(ctx)
    return model
예제 #15
0
import os
import mxnet as mx
from gluoncv.model_zoo.model_store import get_model_file

mxnet_gluon_repo = os.environ.get("MXNET_GLUON_REPO", None)
if not mxnet_gluon_repo:
    os.environ["MXNET_GLUON_REPO"] = "https://apache-mxnet.s3.cn-north-1.amazonaws.com.cn"

num_layers = 50
version = 1
pretrained = True
root = "./pretrained"
file_path = get_model_file('resnet%d_v%d' % (num_layers, version), tag=pretrained, root=root)
print("models is saved in {}".format(file_path))

gcv_params = mx.nd.load(file_path)

cvt_params = dict()
for k in gcv_params:
    if not k.startswith("features"):
        continue
    k_list = k.split(".")
    if k_list[1] == "0":
        cvt_k = "arg:conv0_" + k_list[2]
        cvt_params[cvt_k] = gcv_params[k]
    elif k_list[1] == "1":
        if k_list[-1].endswith("running_mean"):
            cvt_k = "aux:bn0_moving_mean"
        elif k_list[-1].endswith("running_var"):
            cvt_k = "aux:bn0_moving_var"
        else: