Error when trying to import a trained net: multiple outputs with name


#1

Hi!
I’m trying to finetune a gluoncv pre-trained resnet50:

class modified_resnet50(nn.HybridBlock):
def init(self, classes, pretrained, ctxs,base_model=“ResNet50_v1c”,**kwargs):
super(modified_resnet50, self).init(**kwargs)
base_model = get_model(base_model, pretrained=pretrained)

    new_conv2d = nn.Conv2D(channels=32, kernel_size=(3,3), padding=(1,1), in_channels=4, use_bias=False)
    #set in_channels=4
    new_weight_data = mx.nd.zeros(shape=(32,4,3,3))
    new_weight_data[:,:3,:,:] = base_model.conv1[0].weight.data()
    new_weight_data[:,3,:,:] = new_weight_data[:,0,:,:].copy()
    new_conv2d.weight.initialize()
    new_conv2d.weight.set_data(new_weight_data)

    conv1 = nn.HybridSequential()
    conv1.add(new_conv2d)
    for i in range(6): #add the last 6 layers of finetune_net.conv1
        conv1.add(base_model.conv1[i+1])
    
    self.conv1 = conv1
    
    self.bn1 = base_model.bn1
    self.relu1 = base_model.relu
    self.maxpool = base_model.maxpool
    self.layer1 = base_model.layer1
    self.layer2 = base_model.layer2
    self.layer3 = base_model.layer3
    self.layer4 = base_model.layer4
    self.avgpool = base_model.avgpool
    self.flat = base_model.flat
    self.fc2 = nn.Dense(classes)
    self.fc2.initialize(init.Xavier(), ctx = ctxs)
    
    self.collect_params().reset_ctx(ctxs)
    
def hybrid_forward(self, F, x):
    
    x = self.conv1(x)
    x = self.bn1(x)
    x = self.relu1(x)
    x = self.maxpool(x)
    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
    x = self.layer4(x)
    x = self.avgpool(x)
    x = self.flat(x)
    x = self.fc2(x)
    
    return x

I export the net after it has benn trained. But when I try to imports it, I get following error:


ValueError Traceback (most recent call last)
in ()
----> 1 finetune_net = gluon.nn.SymbolBlock.imports(“resnet50_fc_rgby_focalloss_alpha_epoch-symbol.json”, [‘data’], “resnet50_fc_rgby_focalloss_alpha_epoch-0016.params”, ctx=ctxs)

~/anaconda3/lib/python3.7/site-packages/mxnet/gluon/block.py in imports(symbol_file, input_names, param_file, ctx)
1021 input_names = [input_names]
1022 inputs = [symbol.var(i) for i in input_names]
-> 1023 ret = SymbolBlock(sym, inputs)
1024 if param_file is not None:
1025 ret.collect_params().load(param_file, ctx=ctx)

~/anaconda3/lib/python3.7/site-packages/mxnet/gluon/block.py in init(self, outputs, inputs, params)
1049 row_sparse_storage = ndarray.ndarray._STORAGE_TYPE_STR_TO_ID[‘row_sparse’]
1050 for i in out:
-> 1051 for j in i.get_internals():
1052 assert(j.attr(“storage_type”) != str(row_sparse_storage)),
1053 "SymbolBlock doesn’t support Parameter ‘%s’ because its storage " \

~/anaconda3/lib/python3.7/site-packages/mxnet/symbol/symbol.py in (.0)
91
92 “”"
—> 93 return (self[i] for i in self.list_outputs())
94
95 def add(self, other):

~/anaconda3/lib/python3.7/site-packages/mxnet/symbol/symbol.py in getitem(self, index)
515 if name == index:
516 if idx is not None:
–> 517 raise ValueError(‘There are multiple outputs with name “%s”’ % index)
518 idx = i
519 if idx is None:

ValueError: There are multiple outputs with name “resnetv1b0_layers1_relu0_fwd_output”

I looked up in the .json file. there are indeed 3 “resnetv1b0_layers1_relu0_fwd_output”.
Could someone please tell me how to fix it? Thanks!

By the way, I’m using MXNet 1.3.0 and gluoncv 0.3.0


#2

It seems that it’s an gluoncv problem. I try to export the pre-trained model and imports it

x = nd.zeros(shape=(32,3,256,256))
model = get_model("ResNet50_v1c", pretrained=True)
model.hybridize()
model(x)
model.export("net",epoch=0)
model2 = gluon.nn.SymbolBlock.imports("net-symbol.json", ['data'], "net-0000.params")

And I get the same error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-912e14f249ce> in <module>()
----> 1 model2 = gluon.nn.SymbolBlock.imports("fff-symbol.json", ['data'], "fff-0000.params")

~/anaconda3/lib/python3.7/site-packages/mxnet/gluon/block.py in imports(symbol_file, input_names, param_file, ctx)
   1021             input_names = [input_names]
   1022         inputs = [symbol.var(i) for i in input_names]
-> 1023         ret = SymbolBlock(sym, inputs)
   1024         if param_file is not None:
   1025             ret.collect_params().load(param_file, ctx=ctx)

~/anaconda3/lib/python3.7/site-packages/mxnet/gluon/block.py in __init__(self, outputs, inputs, params)
   1049         row_sparse_storage = ndarray.ndarray._STORAGE_TYPE_STR_TO_ID['row_sparse']
   1050         for i in out:
-> 1051             for j in i.get_internals():
   1052                 assert(j.attr("__storage_type__") != str(row_sparse_storage)), \
   1053                     "SymbolBlock doesn't support Parameter '%s' because its storage " \

~/anaconda3/lib/python3.7/site-packages/mxnet/symbol/symbol.py in <genexpr>(.0)
     91         <Symbol _plus0>
     92         """
---> 93         return (self[i] for i in self.list_outputs())
     94 
     95     def __add__(self, other):

~/anaconda3/lib/python3.7/site-packages/mxnet/symbol/symbol.py in __getitem__(self, index)
    515                 if name == index:
    516                     if idx is not None:
--> 517                         raise ValueError('There are multiple outputs with name \"%s\"' % index)
    518                     idx = i
    519             if idx is None:

ValueError: There are multiple outputs with name "resnetv1b3_layers1_relu0_fwd_output"

#3

You’ve stumbled upon the same bug as a few others, but luckily it’s been fixed already (see here).

You can get the fix by installing the latest pre-release version of Gluon CV with:

pip install gluoncv --pre --upgrade