def inception_v3(pretrained=False, ctx=cpu(), root='~/.mxnet/models', **kwargs): r"""Inception v3 model from `"Rethinking the Inception Architecture for Computer Vision" <http://arxiv.org/abs/1512.00567>`_ paper. 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. 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 = Inception3(**kwargs) if pretrained: from .model_store import get_model_file net.load_parameters(get_model_file('inceptionv3', 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
def __init__(self, symbol, data_names, label_names, logger=logging, context=ctx.cpu(), work_load_list=None, max_data_shapes=None, max_label_shapes=None, fixed_param_prefix=None): super(MutableModule, self).__init__(logger=logger) self._symbol = symbol self._data_names = data_names self._label_names = label_names self._context = context self._work_load_list = work_load_list self._curr_module = None self._max_data_shapes = max_data_shapes self._max_label_shapes = max_label_shapes self._fixed_param_prefix = fixed_param_prefix if self._max_data_shapes is None: self._max_data_shapes = [] if self._max_label_shapes is None: self._max_label_shapes = [] if self._fixed_param_prefix is None: self._fixed_param_prefix = [] fixed_param_names = list() for name in self._symbol.list_arguments(): for prefix in self._fixed_param_prefix: if prefix in name: fixed_param_names.append(name) self._fixed_param_names = fixed_param_names
def resnet18_v1b_89(pretrained=False, root='~/.mxnet/models', ctx=cpu(0), **kwargs): """Constructs a ResNetV1b-18_2.6x model. Uses resnet18_v1b construction from resnetv1b.py 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. """ model = ResNetV1b(BasicBlockV1b, [2, 2, 2, 2], name_prefix='resnetv1b_', **kwargs) dirname = os.path.dirname(__file__) json_filename = os.path.join(dirname, 'resnet%d_v%db_%.1fx' % (18, 1, 2.6) + ".json") with open(json_filename, "r") as jsonFile: params_shapes = json.load(jsonFile) if pretrained: from ..model_store import get_model_file params_file = get_model_file('resnet%d_v%db_%.1fx' % (18, 1, 2.6), tag=pretrained, root=root) prune_gluon_block(model, model.name, params_shapes, params=ndarray.load(params_file), pretrained=True, ctx=ctx) else: prune_gluon_block(model, model.name, params_shapes, params=None, pretrained=False, ctx=ctx) if pretrained: from ...data import ImageNet1kAttr attrib = ImageNet1kAttr() model.synset = attrib.synset model.classes = attrib.classes model.classes_long = attrib.classes_long return model
def get_mobilenet(multiplier, pretrained=False, ctx=cpu(), root='~/.mxnet/models', **kwargs): r"""MobileNet model from the `"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" <https://arxiv.org/abs/1704.04861>`_ 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 = MobileNet(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('mobilenet%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
def resnet152_v1b(pretrained=False, root='~/.mxnet/models', ctx=cpu(0), **kwargs): """Constructs a ResNetV1b-152 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(BottleneckV1b, [3, 8, 36, 3], name_prefix='resnetv1b_', **kwargs) if pretrained: from .model_store import get_model_file model.load_parameters(get_model_file('resnet%d_v%db'%(152, 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
def inception_v3(pretrained=False, ctx=cpu(), root='~/.mxnet/models', **kwargs): r"""Inception v3 model from `"Rethinking the Inception Architecture for Computer Vision" <http://arxiv.org/abs/1512.00567>`_ paper. 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. 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`. """ net = Inception3(**kwargs) if pretrained: from .model_store import get_model_file net.load_parameters(get_model_file('inceptionv3', 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
def get_deeplab(dataset='pascal_voc', backbone='resnet50', pretrained=False, root='~/.mxnet/models', ctx=cpu(0), **kwargs): r"""DeepLabV3 Parameters ---------- dataset : str, default pascal_voc The dataset that model pretrained on. (pascal_voc, ade20k) 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. Examples -------- >>> model = get_fcn(dataset='pascal_voc', backbone='resnet50', pretrained=False) >>> print(model) """ acronyms = { 'pascal_voc': 'voc', 'pascal_aug': 'voc', 'ade20k': 'ade', 'coco': 'coco', } from ..data import datasets # infer number of classes model = DeepLabV3(datasets[dataset].NUM_CLASS, backbone=backbone, ctx=ctx, **kwargs) if pretrained: from .model_store import get_model_file model.load_parameters(get_model_file('deeplab_%s_%s'%(backbone, acronyms[dataset]), tag=pretrained, root=root), ctx=ctx) return model
def resnet152_v1s(pretrained=False, root='~/.mxnet/models', ctx=cpu(0), **kwargs): """Constructs a ResNetV1s-152 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, 8, 36, 3], deep_stem=True, stem_width=64, name_prefix='resnetv1s_', **kwargs) if pretrained: from .model_store import get_model_file model.load_parameters(get_model_file('resnet%d_v%ds'%(152, 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
def get_vgg(num_layers, 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, **kwargs) if pretrained: from .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 ..data import ImageNet1kAttr attrib = ImageNet1kAttr() net.synset = attrib.synset net.classes = attrib.classes net.classes_long = attrib.classes_long return net
def get_densenet(num_layers, pretrained=False, ctx=cpu(), root='~/.mxnet/models', **kwargs): r"""Densenet-BC model from the `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_ paper. Parameters ---------- num_layers : int Number of layers for the variant of densenet. Options are 121, 161, 169, 201. 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`. """ num_init_features, growth_rate, block_config = densenet_spec[num_layers] net = DenseNet(num_init_features, growth_rate, block_config, **kwargs) if pretrained: from .model_store import get_model_file net.load_parameters(get_model_file('densenet%d'%(num_layers), 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
def get_params(self, arg_params, aux_params): """ Copy data from each executor to `arg_params` and `aux_params`. Parameters ---------- arg_params : list of NDArray target parameter arrays aux_params : list of NDArray target aux arrays Notes ----- - This function will inplace update the NDArrays in arg_params and aux_params. """ for name, block in zip(self.param_names, self.param_arrays): weight = sum(w.copyto(ctx.cpu()) for w in block) / len(block) weight.astype(arg_params[name].dtype).copyto(arg_params[name]) for name, block in zip(self.aux_names, self.aux_arrays): weight = sum(w.copyto(ctx.cpu()) for w in block) / len(block) weight.astype(aux_params[name].dtype).copyto(aux_params[name])
def get_simple_pose_resnet(base_name, pretrained=False, ctx=cpu(), root='~/.mxnet/models', **kwargs): net = SimplePoseResNet(base_name, **kwargs) if pretrained: from ..model_store import get_model_file net.load_parameters(get_model_file('simple_pose_%s'%(base_name), tag=pretrained, root=root), ctx=ctx) return net
def fetcher_loop(data_queue, data_buffer, pin_memory=False): """Fetcher loop for fetching data from queue and put in reorder dict.""" while True: idx, batch = data_queue.get() if idx is None: break if pin_memory: batch = _as_in_context(batch, context.cpu_pinned()) else: batch = _as_in_context(batch, context.cpu()) data_buffer[idx] = batch
def __init__(self, nclass, backbone='resnet50', aux=True, ctx=cpu(), pretrained_base=True, base_size=520, crop_size=480, **kwargs): super(FCN, self).__init__(nclass, aux, backbone, ctx=ctx, base_size=base_size, crop_size=crop_size, pretrained_base=pretrained_base, **kwargs) with self.name_scope(): self.head = _FCNHead(2048, nclass, **kwargs) self.head.initialize(ctx=ctx) self.head.collect_params().setattr('lr_mult', 10) if self.aux: self.auxlayer = _FCNHead(1024, nclass, **kwargs) self.auxlayer.initialize(ctx=ctx) self.auxlayer.collect_params().setattr('lr_mult', 10)
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:`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 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 .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 ..data import ImageNet1kAttr attrib = ImageNet1kAttr() net.synset = attrib.synset net.classes = attrib.classes net.classes_long = attrib.classes_long return net
def __init__(self, nclass, backbone='resnet50', aux=True, ctx=cpu(), pretrained_base=True, base_size=520, crop_size=480, **kwargs): super(DeepLabV3, self).__init__(nclass, aux, backbone, ctx=ctx, base_size=base_size, crop_size=crop_size, pretrained_base=pretrained_base, **kwargs) with self.name_scope(): self.head = _DeepLabHead(nclass, height=self._up_kwargs['height']//8, width=self._up_kwargs['width']//8, **kwargs) self.head.initialize(ctx=ctx) self.head.collect_params().setattr('lr_mult', 10) if self.aux: self.auxlayer = _FCNHead(1024, nclass, **kwargs) self.auxlayer.initialize(ctx=ctx) self.auxlayer.collect_params().setattr('lr_mult', 10)
def test_model_save_load(gluon_model, model_data, model_path): _, _, test_data = model_data expected = nd.argmax(gluon_model(test_data), axis=1) mlflow.gluon.save_model(gluon_model, model_path) # Loading Gluon model model_loaded = mlflow.gluon.load_model(model_path, ctx.cpu()) actual = nd.argmax(model_loaded(test_data), axis=1) assert all(expected == actual) # Loading pyfunc model pyfunc_loaded = mlflow.pyfunc.load_model(model_path) test_pyfunc_data = pd.DataFrame(test_data.asnumpy()) pyfunc_preds = pyfunc_loaded.predict(test_pyfunc_data) assert all(np.argmax(pyfunc_preds.values, axis=1) == expected.asnumpy())
def __init__(self, base_name='resnet50_v1b', pretrained_base=False, pretrained_ctx=cpu(), num_joints=17, num_deconv_layers=3, num_deconv_filters=(256, 256, 256), num_deconv_kernels=(4, 4, 4), final_conv_kernel=1, deconv_with_bias=False, in_channels=3, in_size=(256, 192), **kwargs): super(SimplePoseResNet, self).__init__(**kwargs) assert (in_channels == 3) self.in_size = in_size from gluoncv.model_zoo import get_model base_network = get_model(base_name, pretrained=pretrained_base, ctx=pretrained_ctx, norm_layer=gcv.nn.BatchNormCudnnOff) self.resnet = nn.HybridSequential() if base_name.endswith('v1'): for layer in ['features']: self.resnet.add(getattr(base_network, layer)) else: for layer in [ 'conv1', 'bn1', 'relu', 'maxpool', 'layer1', 'layer2', 'layer3', 'layer4' ]: self.resnet.add(getattr(base_network, layer)) self.deconv_with_bias = deconv_with_bias # used for deconv layers self.deconv_layers = self._make_deconv_layer( num_deconv_layers, num_deconv_filters, num_deconv_kernels, ) self.final_layer = nn.Conv2D( channels=num_joints, kernel_size=final_conv_kernel, strides=1, padding=1 if final_conv_kernel == 3 else 0, weight_initializer=initializer.Normal(0.001), bias_initializer=initializer.Zero())
def get_psp(dataset='pascal_voc', backbone='resnet50', pretrained=False, root='~/.mxnet/models', ctx=cpu(0), pretrained_base=True, **kwargs): r"""Pyramid Scene Parsing Network Parameters ---------- dataset : str, default pascal_voc The dataset that model pretrained on. (pascal_voc, ade20k) 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. pretrained_base : bool or str, default True This will load pretrained backbone network, that was trained on ImageNet. Examples -------- >>> model = get_fcn(dataset='pascal_voc', backbone='resnet50', pretrained=False) >>> print(model) """ acronyms = { 'pascal_voc': 'voc', 'pascal_aug': 'voc', 'ade20k': 'ade', 'coco': 'coco', 'citys': 'citys', } from ..data import datasets # infer number of classes model = PSPNet(datasets[dataset].NUM_CLASS, backbone=backbone, pretrained_base=pretrained_base, ctx=ctx, **kwargs) model.classes = datasets[dataset].CLASSES if pretrained: from .model_store import get_model_file model.load_parameters(get_model_file('psp_%s_%s' % (backbone, acronyms[dataset]), tag=pretrained, root=root), ctx=ctx) return model
def i3d_inceptionv3_kinetics400(nclass=400, pretrained=False, pretrained_base=True, ctx=cpu(), root='~/.mxnet/models', use_tsn=False, num_segments=1, num_crop=1, partial_bn=False, **kwargs): r"""Inception v3 model from `"Rethinking the Inception Architecture for Computer Vision" <http://arxiv.org/abs/1512.00567>`_ paper. Inflated 3D model (I3D) from `"Quo Vadis, Action Recognition? A New Model and the Kinetics Dataset" <https://arxiv.org/abs/1705.07750>`_ paper. 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. 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. partial_bn : bool, default False Freeze all batch normalization layers during training except the first layer. 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`. """ model = I3D_InceptionV3(nclass=nclass, partial_bn=partial_bn, pretrained_base=pretrained_base, num_segments=num_segments, num_crop=num_crop, dropout_ratio=0.5, init_std=0.01, ctx=ctx, **kwargs) if pretrained: from ..model_store import get_model_file model.load_parameters(get_model_file('i3d_inceptionv3_kinetics400', tag=pretrained, root=root), ctx=ctx) from ...data import Kinetics400Attr attrib = Kinetics400Attr() model.classes = attrib.classes model.collect_params().reset_ctx(ctx) return model
def func(pretrained=False, tag=None, root='~/.mxnet/models', ctx=cpu(0), **kwargs): r"""Quantized 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. tag : str, default is None Optional length-8 sha1sum of parameter file. If `None`, best parameter file will be used. 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. """ from ..model_zoo import get_model from ..model_store import get_model_file curr_dir = os.path.abspath(os.path.dirname(__file__)) model_name = name.replace('mobilenet1_', 'mobilenet1.') model_name = model_name.replace('mobilenet0_', 'mobilenet0.') json_file = os.path.join(curr_dir, '{}-symbol.json'.format(model_name)) base_name = '_'.join(model_name.split('_')[:-1]) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") param_file = get_model_file(base_name, tag=tag, root=root) if pretrained else None net = get_model('_'.join(model_name.split('_')[:-1]), prefix=sym_prefix) classes = getattr(net, 'classes', []) sym_net = SymbolBlock.imports(json_file, ['data'], None, ctx=ctx) if param_file: # directly imports weights saved by save_parameters is not applicable # so we hack it by load and export once to a temporary params file import tempfile net.load_params(param_file) net.hybridize() if '512' in base_name: net(mx.nd.zeros((1, 3, 512, 512))) elif '300' in base_name: net(mx.nd.zeros((1, 3, 300, 300))) else: net(mx.nd.zeros((1, 3, 224, 224))) with tempfile.TemporaryDirectory() as tmpdirname: prefix = os.path.join(tmpdirname, 'tmp') net.export(prefix, epoch=0) param_prefix = prefix + '-0000.params' sym_net.collect_params().load(param_prefix) sym_net.classes = classes sym_net.reset_class = _not_impl sym_net.set_nms = _not_impl return sym_net
def get_fcn(dataset='pascal_voc', backbone='resnet50', pretrained=False, root='~/.mxnet/models', ctx=cpu(0), pretrained_base=True, **kwargs): r"""FCN model from the paper `"Fully Convolutional Network for semantic segmentation" <https://people.eecs.berkeley.edu/~jonlong/long_shelhamer_fcn.pdf>`_ Parameters ---------- dataset : str, default pascal_voc The dataset that model pretrained on. (pascal_voc, ade20k) 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. pretrained_base : bool or str, default True This will load pretrained backbone network, that was trained on ImageNet. Examples -------- >>> model = get_fcn(dataset='pascal_voc', backbone='resnet50', pretrained=False) >>> print(model) """ acronyms = { 'pascal_voc': 'voc', 'pascal_aug': 'voc', 'ade20k': 'ade', 'coco': 'coco', } from ..data import datasets # infer number of classes model = FCN(datasets[dataset].NUM_CLASS, backbone=backbone, pretrained_base=pretrained_base, ctx=ctx, **kwargs) if pretrained: from .model_store import get_model_file model.load_parameters(get_model_file('fcn_%s_%s' % (backbone, acronyms[dataset]), tag=pretrained, root=root), ctx=ctx) return model
def get_fcn(dataset='pascal_voc', backbone='resnet50', pretrained=False, root='~/.mxnet/models', ctx=cpu(0), **kwargs): r"""FCN model from the paper `"Fully Convolutional Network for semantic segmentation" <https://people.eecs.berkeley.edu/~jonlong/long_shelhamer_fcn.pdf>`_ Parameters ---------- dataset : str, default pascal_voc The dataset that model pretrained on. (pascal_voc, ade20k) pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default '~/.mxnet/models' Location for keeping the model parameters. Examples -------- >>> model = get_fcn(dataset='pascal_voc', backbone='resnet50', pretrained=False) >>> print(model) """ from ..data.pascal_voc.segmentation import VOCSegmentation from ..data.pascal_aug.segmentation import VOCAugSegmentation from ..data.ade20k.segmentation import ADE20KSegmentation acronyms = { 'pascal_voc': 'voc', 'pascal_aug': 'voc', 'ade20k': 'ade', } datasets = { 'pascal_voc': VOCSegmentation, 'pascal_aug': VOCAugSegmentation, 'ade20k': ADE20KSegmentation, } # infer number of classes model = FCN(datasets[dataset].NUM_CLASS, backbone=backbone, ctx=ctx, **kwargs) if pretrained: from .model_store import get_model_file model.load_params(get_model_file('fcn_%s_%s' % (backbone, acronyms[dataset]), root=root), ctx=ctx) return model
def __init__(self, nclass, backbone='resnet50', aux=False, ctx=cpu(), pretrained_base=True, height=None, width=None, base_size=520, crop_size=480, lr_mult=10, **kwargs): super(ICNet, self).__init__(nclass, aux=aux, backbone=backbone, ctx=ctx, base_size=base_size, crop_size=crop_size, pretrained_base=pretrained_base, **kwargs) height = height if height is not None else crop_size width = width if width is not None else crop_size self._up_kwargs = {'height': height, 'width': width} self.base_size = base_size self.crop_size = crop_size with self.name_scope(): # large resolution branch self.conv_sub1 = nn.HybridSequential() with self.conv_sub1.name_scope(): self.conv_sub1.add(ConvBnRelu(3, 32, 3, 2, 1, **kwargs), ConvBnRelu(32, 32, 3, 2, 1, **kwargs), ConvBnRelu(32, 64, 3, 2, 1, **kwargs)) self.conv_sub1.initialize(ctx=ctx) self.conv_sub1.collect_params().setattr('lr_mult', lr_mult) # small and medium resolution branches, backbone comes from segbase.py base_psp_head = _PSPHead(nclass, feature_map_height=self._up_kwargs['height'] // 32, feature_map_width=self._up_kwargs['width'] // 32, **kwargs) self.psp_head = nn.HybridSequential() with self.psp_head.name_scope(): self.psp_head.add(base_psp_head.psp, base_psp_head.block[:-1]) self.psp_head.initialize(ctx=ctx) self.psp_head.collect_params().setattr('lr_mult', lr_mult) # ICNet head self.head = _ICHead(nclass=nclass, height=self._up_kwargs['height'], width=self._up_kwargs['width'], **kwargs) self.head.initialize(ctx=ctx) self.head.collect_params().setattr('lr_mult', lr_mult) # reduce conv self.conv_sub4 = ConvBnRelu(512, 256, 1, **kwargs) self.conv_sub4.initialize(ctx=ctx) self.conv_sub4.collect_params().setattr('lr_mult', lr_mult) self.conv_sub2 = ConvBnRelu(512, 256, 1, **kwargs) self.conv_sub2.initialize(ctx=ctx) self.conv_sub2.collect_params().setattr('lr_mult', lr_mult)
def resnet50_v2b(pretrained=False, root='~/.mxnet/models', ctx=cpu(0), ratio=0., **kwargs): model = _ResNetV2(_BottleneckV2, [3, 4, 6, 3], ratio=[ratio, ratio, ratio, 0.], name_prefix='R50_', down_pos=1, **kwargs) if pretrained: raise NotImplementedError( 'Please manually load the pretrained params.') return model
def __init__(self, symbol, data_names, label_names, logger=logging, context=ctx.cpu(), work_load_list=None, max_data_shapes=None, max_label_shapes=None, fixed_param_names=None): super(MutableModule, self).__init__(logger=logger) self._symbol = symbol self._data_names = data_names self._label_names = label_names self._context = context self._work_load_list = work_load_list self._curr_module = None self._max_data_shapes = {} if max_data_shapes is None else dict(max_data_shapes) self._max_label_shapes = {} if max_label_shapes is None else dict(max_label_shapes) self._fixed_param_names = fixed_param_names
def get_mobilenet(multiplier, pretrained=False, ctx=cpu(), root='~/.mxnet/models', norm_layer=BatchNorm, norm_kwargs=None, **kwargs): r"""MobileNet model from the `"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" <https://arxiv.org/abs/1704.04861>`_ paper. Parameters ---------- 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`. """ net = MobileNet(multiplier, norm_layer=norm_layer, norm_kwargs=norm_kwargs, **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('mobilenet%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
def mobilenet_v1_100(pretrained=False, root='~/.mxnet/models', ctx=cpu(0), ratio=0., **kwargs): net = _MobileNetV1(multiplier=1.0, ratio=ratio, name_prefix='M100_', norm_kwargs=None, **kwargs) if pretrained: raise NotImplementedError( 'Please manually load the pretrained params.') return net
def r2plus1d_resnet34_kinetics400_custom(nclass=400, pretrained=False, pretrained_base=True,use_kinetics_pretrain=True, root='~/.mxnet/models', num_segments=1, num_crop=1, feat_ext=False, ctx=cpu(), **kwargs): from .model_zoo import get_model #model = get_model('r2plus1d_resnet34_kinetics400', nclass=nclass,num_crop=num_crop, # feat_ext=feat_ext,num_segments=num_segments,ctx=ctx,pretrained=False) model = R2Plus1D(nclass=nclass, block=BasicBlock, layers=[3, 4, 6, 3], num_segments=num_segments, num_crop=num_crop, feat_ext=feat_ext, ctx=ctx, **kwargs) model.initialize(init.MSRAPrelu(), ctx=ctx) if use_kinetics_pretrain and not pretrained: #from .model_store import get_model_file kinetics_model = get_model('r2plus1d_resnet34_kinetics400', nclass=400, pretrained=True) source_params = kinetics_model.collect_params() target_params = model.collect_params() assert len(source_params.keys()) == len(target_params.keys()) pretrained_weights = [] for layer_name in source_params.keys(): pretrained_weights.append(source_params[layer_name].data()) for i, layer_name in enumerate(target_params.keys()): #print(i,',',layer_name) if i + 2 == len(source_params.keys()): # skip the last dense layer break target_params[layer_name].set_data(pretrained_weights[i]) #from ...data import Kinetics400Attr #attrib = Kinetics400Attr() #model.classes = attrib.classes elif pretrained: #model.load_parameters(get_model_file('r2plus1d_resnet18_kinetics400',tag=pretrained, root=root), ctx=ctx) pass else: print('use_kinetics_pretrain == False') #model.initialize(init.MSRAPrelu(), ctx=ctx) model.collect_params().reset_ctx(ctx) return model
def resnet18_v1b_89(pretrained=False, root='~/.mxnet/models', ctx=cpu(0), **kwargs): """Constructs a ResNetV1b-18_2.6x model. Uses resnet18_v1b construction from resnetv1b.py 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. """ model = ResNetV1b(BasicBlockV1b, [2, 2, 2, 2], name_prefix='resnetv1b_', **kwargs) dirname = os.path.dirname(__file__) json_filename = os.path.join( dirname, 'resnet%d_v%db_%.1fx' % (18, 1, 2.6) + ".json") with open(json_filename, "r") as jsonFile: params_shapes = json.load(jsonFile) if pretrained: from ..model_store import get_model_file params_file = get_model_file('resnet%d_v%db_%.1fx' % (18, 1, 2.6), tag=pretrained, root=root) prune_gluon_block(model, model.name, params_shapes, params=ndarray.load(params_file), pretrained=True, ctx=ctx) else: prune_gluon_block(model, model.name, params_shapes, params=None, pretrained=False, ctx=ctx) if pretrained: from ...data import ImageNet1kAttr attrib = ImageNet1kAttr() model.synset = attrib.synset model.classes = attrib.classes model.classes_long = attrib.classes_long return model
def get_deeplab(dataset='pascal_voc', backbone='resnet50', pretrained=False, root='~/.mxnet/models', ctx=cpu(0), **kwargs): r"""DeepLabV3 Parameters ---------- dataset : str, default pascal_voc The dataset that model pretrained on. (pascal_voc, pascal_aug, ade20k, coco, citys) 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. Examples -------- >>> model = get_fcn(dataset='pascal_voc', backbone='resnet50', pretrained=False) >>> print(model) """ acronyms = { 'pascal_voc': 'voc', 'pascal_aug': 'voc', 'ade20k': 'ade', 'coco': 'coco', 'citys': 'citys', } from ..data import datasets # infer number of classes if pretrained: kwargs['pretrained_base'] = False kwargs['root'] = root model = DeepLabV3(datasets[dataset].NUM_CLASS, backbone=backbone, ctx=ctx, **kwargs) model.classes = datasets[dataset].CLASSES if pretrained: from .model_store import get_model_file model.load_parameters(get_model_file('deeplab_%s_%s' % (backbone, acronyms[dataset]), tag=pretrained, root=root), ctx=ctx) return model
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, default False Whether to load the pretrained weights for model. 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 """ 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 .model_store import get_model_file if not use_se: net.load_parameters(get_model_file('resnet%d_v%d' % (num_layers, version), root=root), ctx=ctx) else: net.load_parameters(get_model_file('se_resnet%d_v%d' % (num_layers, version), root=root), ctx=ctx) return net
def __init__(self, symbol, data_names=('data',), label_names=('softmax_label',), logger=logging, context=ctx.cpu(), work_load_list=None, fixed_param_names=None, state_names=None): super(Module, self).__init__(logger=logger) if isinstance(context, ctx.Context): context = [context] self._context = context if work_load_list is None: work_load_list = [1] * len(self._context) assert len(work_load_list) == len(self._context) self._work_load_list = work_load_list self._symbol = symbol data_names = list(data_names) if data_names is not None else [] label_names = list(label_names) if label_names is not None else [] state_names = list(state_names) if state_names is not None else [] fixed_param_names = list(fixed_param_names) if fixed_param_names is not None else [] _check_input_names(symbol, data_names, "data", True) _check_input_names(symbol, label_names, "label", False) _check_input_names(symbol, state_names, "state", True) _check_input_names(symbol, fixed_param_names, "fixed_param", True) arg_names = symbol.list_arguments() input_names = data_names + label_names + state_names self._param_names = [x for x in arg_names if x not in input_names] self._fixed_param_names = fixed_param_names self._aux_names = symbol.list_auxiliary_states() self._data_names = data_names self._label_names = label_names self._state_names = state_names self._output_names = symbol.list_outputs() self._arg_params = None self._aux_params = None self._params_dirty = False self._optimizer = None self._kvstore = None self._update_on_kvstore = None self._updater = None self._preload_opt_states = None self._grad_req = None self._exec_group = None self._data_shapes = None self._label_shapes = None
def r2plus1d_resnet34_tranconv_lateral_tanhbn(nelength=16, pretrained=False,ctx=cpu(),**kwargs): model = R2Plus1D_TranConv_lateral_tanhbn() if pretrained: pass #modelfile = '0.9315-ucf101-r2plus1d_resnet34_tranconv_lateral-079-best.params'#'0.8567-ucf101-r2plus1d_resnet34_tranconv_lateral-079-best.params' #root = '/home/hp/lcx/Action-Recognition/logs/param_rgb_r2plus1d_resnet18_kinetics400_custom_ucf101_nlength16_lateral_1' #filepath = os.path.join(root,modelfile) #filepath = os.path.expanduser(filepath) #model.load_parameters(modelfile,ctx=ctx,allow_missing=True) #print(filepath) else: model.initialize(init.MSRAPrelu(), ctx=ctx) #model.collect_params().reset_ctx(ctx) return model
def get_mobile_pose(base_name, ctx=cpu(), pretrained=False, root='~/.mxnet/models', **kwargs): net = MobilePose(base_name, **kwargs) if pretrained: from gluoncv.model_zoo.model_store import get_model_file net.load_parameters(get_model_file('mobile_pose_%s' % (base_name), tag=pretrained, root=root), ctx=ctx) return net
def get_shufflenet_v2(pretrained=False, root='~/.mxnet/models', ctx=cpu(), norm_layer=BatchNorm, norm_kwargs=None, **kwargs): net = ShuffleNetV2(norm_layer=norm_layer, norm_kwargs=norm_kwargs, **kwargs) from ..data import ImageNet1kAttr attrib = ImageNet1kAttr() net.synset = attrib.synset net.classes = attrib.classes net.classes_long = attrib.classes_long return net
def fetcher_loop(data_queue, data_buffer, pin_memory=False, data_buffer_lock=None): """Fetcher loop for fetching data from queue and put in reorder dict.""" while True: idx, batch = data_queue.get() if idx is None: break if pin_memory: batch = _as_in_context(batch, context.cpu_pinned()) else: batch = _as_in_context(batch, context.cpu()) if data_buffer_lock is not None: with data_buffer_lock: data_buffer[idx] = batch else: data_buffer[idx] = batch
def get_densenet_y(num_layers, pretrained=False, ctx=cpu(), bits=1, bits_a=1, opt_init_features=None, opt_growth_rate=None, opt_reduction=None, root=os.path.join(base.data_dir(), 'models'), **kwargs): r"""Densenet-BC model from the `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_ paper. Parameters ---------- num_layers : int Number of layers for the variant of densenet_y. Options are 121, 161, 169, 201. pretrained : bool, default False Whether to load the pretrained weights for model. 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. """ init_features, growth_rate, bn_size, reduction, block_config = densenet_y_spec[ num_layers] num_transition_blocks = len(block_config) - 1 if opt_init_features is not None: init_features = opt_init_features if opt_growth_rate is not None: growth_rate = opt_growth_rate if opt_reduction is not None: split = [float(x) for x in opt_reduction.split(",")] if len(split) == 1: split *= num_transition_blocks reduction = split assert len( reduction ) == num_transition_blocks, "need one or three values for --reduction" else: reduction = [reduction] * num_transition_blocks net = DenseNetY(bits, bits_a, init_features, growth_rate, block_config, reduction, bn_size, **kwargs) if pretrained: raise ValueError("No pretrained model exists, yet.") # from ..model_store import get_model_file # net.load_parameters(get_model_file('densenet_y%d'%(num_layers), root=root), ctx=ctx) return net
def __init__(self, nclass, aux=True, ctx=cpu(), pretrained_base=False, height=None, width=None, base_size=2048, crop_size=1024, **kwargs): super(FastSCNN, self).__init__() height = height if height is not None else crop_size width = width if width is not None else crop_size self._up_kwargs = {'height': height, 'width': width} self.base_size = base_size self.crop_size = crop_size self.aux = aux with self.name_scope(): self.learning_to_downsample = LearningToDownsample( 32, 48, 64, **kwargs) self.learning_to_downsample.initialize(ctx=ctx) self.global_feature_extractor = GlobalFeatureExtractor( 64, [64, 96, 128], 128, 6, [3, 3, 3], height=height // 32, width=width // 32, **kwargs) self.global_feature_extractor.initialize(ctx=ctx) self.feature_fusion = FeatureFusionModule(64, 128, 128, height=height // 8, width=width // 8, **kwargs) self.feature_fusion.initialize(ctx=ctx) self.classifier = Classifer(128, nclass, **kwargs) self.classifier.initialize(ctx=ctx) if self.aux: self.auxlayer = _auxHead(in_channels=64, channels=64, nclass=nclass, **kwargs) self.auxlayer.initialize(ctx=ctx) self.auxlayer.collect_params().setattr('lr_mult', 10)
def prune_gluon_block(net, prefix, params_shapes, params=None, pretrained=False, ctx=cpu(0)): """ :param params_shapes: dictionary of shapes of convolutional weights :param prefix: prefix of the original resnet50_v1d :param pretrained: Boolean specifying if the pretrained model parameters needs to be loaded :param net: original network that is required to be pruned :param params: dictionary of parameters for the pruned network. Size of the parameters in this dictionary tells what should be the size of channels of each convolution layer. :param ctx: cpu(0) :return: "net" """ for _, layer in net._children.items(): if pretrained: if isinstance(layer, nn.BatchNorm): params_layer = layer._collect_params_with_prefix() for param_name in ['beta', 'gamma', 'running_mean', 'running_var']: param_val = params[layer.name.replace(prefix, "resnetv1d") + "_" + param_name] layer.params.get(param_name)._shape = param_val.shape params_layer[param_name]._load_init(param_val, ctx=ctx) if isinstance(layer, nn.Conv2D): param_shape = params_shapes[layer.name.replace(prefix, "resnetv1d") + "_weight"] layer._channels = param_shape[0] layer._kwargs['num_filter'] = param_shape[0] params_layer = layer._collect_params_with_prefix() for param_name in ['weight']: param_shape = params_shapes[ layer.name.replace(prefix, "resnetv1d") + "_" + param_name] layer.params.get(param_name)._shape = param_shape if pretrained: param_val = params[layer.name.replace(prefix, "resnetv1d") + "_" + param_name] params_layer[param_name]._load_init(param_val, ctx=ctx) if isinstance(layer, nn.Dense): layer._in_units = params_shapes[layer.name.replace(prefix, "resnetv1d") + "_weight"][1] params_layer = layer._collect_params_with_prefix() for param_name in ['weight', 'bias']: param_shape = params_shapes[ layer.name.replace(prefix, "resnetv1d") + "_" + param_name] layer.params.get(param_name)._shape = param_shape if pretrained: param_val = params[layer.name.replace(prefix, "resnetv1d") + "_" + param_name] params_layer[param_name]._load_init(param_val, ctx=ctx) else: prune_gluon_block(layer, prefix, params_shapes, params, pretrained, ctx)
def r2plus1d_resnet152_kinetics400(nclass=400, pretrained=False, pretrained_base=True, root='~/.mxnet/models', num_segments=1, num_crop=1, feat_ext=False, ctx=cpu(), **kwargs): r"""R2Plus1D with ResNet152 backbone 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. feat_ext : bool. Whether to extract features before dense classification layer or do a complete forward pass. """ model = R2Plus1D(nclass=nclass, block=Bottleneck, layers=[3, 8, 36, 3], num_segments=num_segments, num_crop=num_crop, feat_ext=feat_ext, ctx=ctx, **kwargs) model.initialize(init.MSRAPrelu(), ctx=ctx) if pretrained: from .model_store import get_model_file model.load_parameters(get_model_file('r2plus1d_resnet152_kinetics400', tag=pretrained, root=root), ctx=ctx) #from ...data import Kinetics400Attr #attrib = Kinetics400Attr() #model.classes = attrib.classes model.collect_params().reset_ctx(ctx) return model
def get_pose_resnet(base_name, pretrained=False, ctx=cpu(), root='~/.mxnet/models', **kwargs): net = PoseResNet(base_name, **kwargs) if pretrained: from .model_store import get_model_file net.load_parameters(get_model_file('pose_resnet%d' % (num_layers), tag=pretrained, root=root), ctx=ctx) return net
def get_simple_pose_resnet(base_name, pretrained=False, ctx=cpu(), root='~/.mxnet/models', **kwargs): net = SimplePoseResNet(base_name, **kwargs) if pretrained: from ..model_store import get_model_file net.load_parameters(get_model_file('simple_pose_%s' % (base_name), tag=pretrained, root=root), ctx=ctx) return net
def resnext50_32x4d_v1b(pretrained=False, root='~/.mxnet/models', ctx=cpu(0), ratio=0., **kwargs): model = _ResNetV1(_BottleneckV1, [3, 4, 6, 3], ratio=[ratio, ratio, ratio, 0.], num_mid=(128, 256, 512, 1024), groups=32, name_prefix='RX50_', down_pos=1, **kwargs) if pretrained: raise NotImplementedError( 'Please manually load the pretrained params.') return model
def __init__(self, nclass, backbone='resnet50', aux=True, ctx=cpu(), pretrained_base=True, height=None, width=None, base_size=520, crop_size=480, **kwargs): super(DeepLabV3, self).__init__(nclass, aux, backbone, ctx=ctx, base_size=base_size, crop_size=crop_size, pretrained_base=pretrained_base, **kwargs) height = height if height is not None else crop_size width = width if width is not None else crop_size with self.name_scope(): self.head = _DeepLabHead(nclass, height=height//8, width=width//8, **kwargs) self.head.initialize(ctx=ctx) self.head.collect_params().setattr('lr_mult', 10) if self.aux: self.auxlayer = _FCNHead(1024, nclass, **kwargs) self.auxlayer.initialize(ctx=ctx) self.auxlayer.collect_params().setattr('lr_mult', 10) self._up_kwargs = {'height': height, 'width': width}
def __init__(self, nclass, backbone='resnet50', aux=False, ctx=cpu(), pretrained_base=True, height=None, width=None, base_size=520, crop_size=480, dilated=True, in_channels=3, in_size=(480, 480), **kwargs): super(DANet, self).__init__(nclass, aux, backbone, ctx=ctx, base_size=base_size, crop_size=crop_size, pretrained_base=pretrained_base, **kwargs) self.in_size = in_size self.classes = nclass self.aux = aux height = height if height is not None else crop_size width = width if width is not None else crop_size with self.name_scope(): self.head = DANetHead(2048, nclass, **kwargs) self.head.initialize(ctx=ctx) self._up_kwargs = {'height': height, 'width': width}
def get_mobilenet_v2(multiplier, pretrained=False, ctx=cpu(), root='~/.mxnet/models', norm_layer=BatchNorm, norm_kwargs=None, **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 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`. """ net = MobileNetV2(multiplier, norm_layer=norm_layer, norm_kwargs=norm_kwargs, **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) return net
def c3d_kinetics400(nclass=400, pretrained=False, ctx=cpu(), root='~/.mxnet/models', num_segments=1, num_crop=1, feat_ext=False, **kwargs): r"""The Convolutional 3D network (C3D) trained on Kinetics400 dataset. Learning Spatiotemporal Features with 3D Convolutional Networks. ICCV, 2015. https://arxiv.org/abs/1412.0767 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. 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. feat_ext : bool. Whether to extract features before dense classification layer or do a complete forward pass. """ model = C3D(nclass=nclass, ctx=ctx, **kwargs) model.initialize(init.MSRAPrelu(), ctx=ctx) if pretrained: from ..model_store import get_model_file model.load_parameters(get_model_file('c3d_kinetics400', tag=pretrained, root=root), ctx=ctx) from ...data import Kinetics400Attr attrib = Kinetics400Attr() model.classes = attrib.classes model.collect_params().reset_ctx(ctx) return model
def __init__(self, symbol, data_names, label_names, logger=logging, context=ctx.cpu(), work_load_list=None, max_data_shapes=None, max_label_shapes=None): super(MutableModule, self).__init__(logger=logger) self._symbol = symbol self._data_names = data_names self._label_names = label_names self._context = context self._work_load_list = work_load_list self._curr_module = None self._max_data_shapes = max_data_shapes self._max_label_shapes = max_label_shapes if self._max_data_shapes is None: self._max_data_shapes = [] if self._max_label_shapes is None: self._max_label_shapes = []
def get_mobilenet_v2(multiplier, pretrained=False, ctx=cpu(), root='~/.mxnet/models', norm_layer=BatchNorm, norm_kwargs=None, **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 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`. """ net = MobileNetV2(multiplier, norm_layer=norm_layer, norm_kwargs=norm_kwargs, **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
def get_fcn(dataset='pascal_voc', backbone='resnet50', pretrained=False, root='~/.mxnet/models', ctx=cpu(0), pretrained_base=True, **kwargs): r"""FCN model from the paper `"Fully Convolutional Network for semantic segmentation" <https://people.eecs.berkeley.edu/~jonlong/long_shelhamer_fcn.pdf>`_ Parameters ---------- dataset : str, default pascal_voc The dataset that model pretrained on. (pascal_voc, ade20k) 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. pretrained_base : bool or str, default True This will load pretrained backbone network, that was trained on ImageNet. Examples -------- >>> model = get_fcn(dataset='pascal_voc', backbone='resnet50', pretrained=False) >>> print(model) """ acronyms = { 'pascal_voc': 'voc', 'pascal_aug': 'voc', 'ade20k': 'ade', 'coco': 'coco', } from ..data import datasets # infer number of classes model = FCN(datasets[dataset].NUM_CLASS, backbone=backbone, pretrained_base=pretrained_base, ctx=ctx, **kwargs) if pretrained: from .model_store import get_model_file model.load_parameters(get_model_file( 'fcn_%s_%s'%(backbone, acronyms[dataset]), tag=pretrained, root=root), ctx=ctx) return model
def __init__(self, base_name='resnet50_v1b', pretrained_base=False, pretrained_ctx=cpu(), num_joints=17, num_deconv_layers=3, num_deconv_filters=(256, 256, 256), num_deconv_kernels=(4, 4, 4), final_conv_kernel=1, deconv_with_bias=False, **kwargs): super(SimplePoseResNet, self).__init__(**kwargs) from ..model_zoo import get_model base_network = get_model(base_name, pretrained=pretrained_base, ctx=pretrained_ctx, norm_layer=gcv.nn.BatchNormCudnnOff) self.resnet = nn.HybridSequential() if base_name.endswith('v1'): for layer in ['features']: self.resnet.add(getattr(base_network, layer)) else: for layer in ['conv1', 'bn1', 'relu', 'maxpool', 'layer1', 'layer2', 'layer3', 'layer4']: self.resnet.add(getattr(base_network, layer)) self.deconv_with_bias = deconv_with_bias # used for deconv layers self.deconv_layers = self._make_deconv_layer( num_deconv_layers, num_deconv_filters, num_deconv_kernels, ) self.final_layer = nn.Conv2D( channels=num_joints, kernel_size=final_conv_kernel, strides=1, padding=1 if final_conv_kernel == 3 else 0, weight_initializer=initializer.Normal(0.001), bias_initializer=initializer.Zero() )
def alexnet(pretrained=False, ctx=cpu(), root='~/.mxnet/models', **kwargs): r"""AlexNet model from the `"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper. 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. 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 = AlexNet(**kwargs) if pretrained: from .model_store import get_model_file net.load_parameters(get_model_file('alexnet', 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