1.Predict with pre-trained SSD models
Load a pretrained model
from gluoncv import model_zoo, data, utils
net = model_zoo.get_model('ssd_512_resnet50_v1_voc', pretrained=True)
- gluoncv / model_zoo / model_zoo.py
...
from .ssd import *
...
def get_model(name, **kwargs):
name = name.lower()
if name not in _models:
err_str = '"%s" is not among the following model list:\n\t' % (name)
err_str += '%s' % ('\n\t'.join(sorted(_models.keys())))
raise ValueError(err_str)
net = _models[name](**kwargs)
return net
_models = {
'resnet18_v1': resnet18_v1,
'resnet34_v1': resnet34_v1,
'resnet50_v1': resnet50_v1,
...
}
- gluoncv / model_zoo / ssd / init.py
from __future__ import absolute_import
from .ssd import *
from .presets import *
from .vgg_atrous import *
models 是定义在model_zoo.py中的一个字典,字典内的每个value都导向一个取模型函数。导向的过程比较有意思,ssd是一个内部有__init_.py的目录,import过程中会取执行该脚本,脚本如下所示。从而可以指定导入某些特定的脚本。在脚本中使用__all__同时可以指定该脚本中在import *时被导出的函数或类。
- gluoncv / model_zoo / ssd / preset.py
def ssd_512_resnet50_v1_voc(pretrained=False, pretrained_base=True, **kwargs):
classes = VOCDetection.CLASSES
return get_ssd('resnet50_v1', 512,
features=['stage3_activation5', 'stage4_activation2'],
filters=[512, 512, 256, 256],
sizes=[51.2, 102.4, 189.4, 276.4, 363.52, 450.6, 492],
ratios=[[1, 2, 0.5]] + [[1, 2, 0.5, 3, 1.0/3]] * 3 + [[1, 2, 0.5]] * 2,
steps=[16, 32, 64, 128, 256, 512],
classes=classes, dataset='voc', pretrained=pretrained,
pretrained_base=pretrained_base, **kwargs)
get_ssd函数的一个分类接口。pretrained表示是否使用预训练的网络,pretrained_base表示是否使用仅初始化的网络。
- gluoncv / model_zoo / ssd / ssd.py
def get_ssd(name, base_size, features, filters, sizes, ratios, steps, classes,
dataset, pretrained=False, pretrained_base=True, ctx=mx.cpu(),
root=os.path.join('~', '.mxnet', 'models'),
anchor_generator=SSDAnchorGenerator, **kwargs):
pretrained_base = False if pretrained else pretrained_base
base_name = None if callable(features) else name
net = SSD(base_name, base_size, features, filters, sizes, ratios, steps,
pretrained=pretrained_base, classes=classes, ctx=ctx, root=root,
minimal_opset=pretrained, anchor_generator=anchor_generator, **kwargs)
if pretrained:
from ..model_store import get_model_file
full_name = '_'.join(('ssd', str(base_size), name, dataset))
net.load_parameters(get_model_file(full_name, tag=pretrained, root=root), ctx=ctx)
return net
name:网络名;base_size:训练图的size;features:内部输出的名字列表; filters:卷积层的通道数;sizes:锚框的size;ratios:锚框的ratios; steps:?classes:最终分类的类别名;dataset:使用的数据集;
|