Load params from Symbol models to Gluon

Hi

Assume I’ve a gluon model net created from Hybridblocks. I want to know if it’s possible to net.load_params from checkpoints of the same model but already created from Symbols and trained before.

Assuming you have the -0000.params and the -symbol.json files, for example for resnet18-0000.params and resnet18-symbol.json you can do the following:

With 1.2.1 and later you can simply do:

net = gluon.nn.SymbolBlock.imports('resnet18-symbol.json',
                                   ['data'], 
                                   param_file='resnet18-0000.params',
                                   ctx=mx.gpu())

With 1.2.0 and earlier you can do:

sym, arg_params, aux_params = mx.model.load_checkpoint('resnet18', 0)

net = gluon.nn.SymbolBlock(outputs=sym, inputs=mx.sym.var('data'))
# Set the params
net_params = net.collect_params()
for param in arg_params:
    if param in net_params:
        net_params[param]._load_init(arg_params[param], ctx=ctx)
for param in aux_params:
    if param in net_params:
        net_params[param]._load_init(aux_params[param], ctx=ctx)
5 Likes