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 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_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 """ 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, ignore_extra=True, allow_missing=True) 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
def get_mobilenet_v2(multiplier, pretrained=False, ctx=cpu(), root=os.path.join('models', 'definitions', 'mobilenet', 'weights'), 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 gluoncv.model_zoo.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 gluoncv.data import ImageNet1kAttr attrib = ImageNet1kAttr() net.synset = attrib.synset net.classes = attrib.classes net.classes_long = attrib.classes_long return net
def resnet101(pretrained=False, root='~/.mxnet/models', ctx=cpu(0), **kwargs): model = ResNet(Bottleneck, [3, 4, 23, 3], name_prefix='resnet_', **kwargs) if pretrained: model.load_parameters(os.path.join(root, "resnet101.params"), ctx=ctx) 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', num_sync_bn_devices=-1, **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. num_sync_bn_devices : int, default is -1 Number of devices for training. If `num_sync_bn_devices < 2`, SyncBatchNorm is disabled. """ net = MobileNet(multiplier, num_sync_bn_devices=num_sync_bn_devices, **kwargs) if pretrained: from gluoncv.model_zoo.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 gluoncv.data import ImageNet1kAttr attrib = ImageNet1kAttr() net.synset = attrib.synset net.classes = attrib.classes net.classes_long = attrib.classes_long 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
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
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
def main(): """ Main body of script. """ args = parse_args() # Load a testing image: image = cv2.imread(args.image, flags=cv2.IMREAD_COLOR) # cv2.imshow("image", image) # cv2.waitKey(0) # cv2.destroyAllWindows() image = cv2.cvtColor(image, code=cv2.COLOR_BGR2RGB) # Resize image with keeping aspect ratio: resize_value = int(math.ceil(float(args.input_size) / args.resize_inv_factor)) h, w = image.shape[:2] if not ((w == resize_value and w <= h) or (h == resize_value and h <= w)): resize_size = (resize_value, int(resize_value * h / w)) if w < h else (int(resize_value * w / h), resize_value) image = cv2.resize(image, dsize=resize_size, interpolation=cv2.INTER_LINEAR) # Center crop of the image: h, w = image.shape[:2] th, tw = args.input_size, args.input_size ih = int(round(0.5 * (h - th))) jw = int(round(0.5 * (w - tw))) image = image[ih:(ih + th), jw:(jw + tw), :] # cv2.imshow("image2", image) # cv2.waitKey(0) # cv2.destroyAllWindows() # Convert image to a float tensor and normalize it: x = image.astype(np.float32) x = x / 255.0 x = (x - np.array(args.mean_rgb)) / np.array(args.std_rgb) # Create `use_cuda` flag: use_cuda = (args.num_gpus > 0) # Convert the tensor to a Pytorch tensor: x = x.transpose(2, 0, 1) x = np.expand_dims(x, axis=0) x = torch.FloatTensor(x) if use_cuda: x = x.cuda() # Create model with loading pretrained weights: net = ptcv_get_model(args.model, pretrained=True) net.eval() if use_cuda: net = net.cuda() # Evaluate the network: y = net(x) probs = torch.nn.Softmax(dim=-1)(y) # Show results: top_k = 5 probs_np = probs.cpu().detach().numpy().squeeze(axis=0) top_k_inds = probs_np.argsort()[::-1][:top_k] classes = ImageNet1kAttr().classes print("The input picture is classified to be:") for k in range(top_k): print("{idx}: [{class_name}], with probability {prob:.3f}.".format( idx=(k + 1), class_name=classes[top_k_inds[k]], prob=probs_np[top_k_inds[k]]))
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
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
opt = get_args() # Takes in what directory the user wants the outputs saved too, if wanted directory = input('Choose a directory to save the file too (include final file, but no extension): ') \ if opt.save_to_file else None # Load Model model_name = opt.model pretrained = True if opt.saved_params == '' else False net = get_model(model_name, pretrained=pretrained) if not pretrained: net.load_parameters(opt.saved_params) attrib = ImageNet1kAttr() classes = attrib.classes else: classes = net.classes # Stores the saved picture directories pictures = [] # Recurses through folders def find_all_images(input_file=None): # Attempts scan the file provided, seeing if it is a folder for file in os.scandir( opt.input_fldr if input_file is None else input_file): file = os.path.abspath(file)
import flask import math import numpy as np import mxnet as mx from mxnet import nd from mxnet.gluon.data.vision import transforms import gluoncv as gcv from gluoncv.data import ImageNet1kAttr prefix = '/opt/ml/' model_path = os.path.join(prefix, 'model') artifact_file = os.path.join(model_path, 'artifact.json') mode_name = None preprocessor = None classes = ImageNet1kAttr().classes with open(artifact_file) as json_file: artifact = json.load(json_file) model_name = artifact['model_name'] if model_name.startswith('inception') or model_name.startswith( 'googlenet'): input_size = 299 elif model_name == 'resnest101': input_size = 256 elif model_name == 'resnest200': input_size = 320 elif model_name == 'resnest269': input_size = 416 else: input_size = 224 resize = int(math.ceil(input_size / 0.875))