[Solved]Cannot export model of person re-id

I run the train.py of person re-id to train a model, so far so good, but can’t find away to export it.

The codes(export_my_model.py) I use to convert the model

import gluoncv as gcv
import mxnet as mx

from gluoncv.utils import export_block
from networks import resnet50

context = mx.cpu()
model = resnet50(ctx=context, pretrained=False)
model.load_parameters('params/resnet50.params', ctx=context, allow_missing=True, ignore_extra=True)
#help(model.load_parameters)
model.hybridize()
#print(model)
export_block('params/resnet50.params', model, data_shape=(128, 384, 3))

Error messages:

UserWarning: Parameter resnet0_dense0_weight is not used by any computation. Is this intended?
 out = self.forward(*args)
Traceback (most recent call last):
 File "convert_mxnet_model_to_json.py", line 14, in <module>
   export_block('params/resnet50.params', model, data_shape=(128, 384, 3))
 File "C:\Users\yyyy\Anaconda3\lib\site-packages\gluoncv\utils\export_helper.py", line 111, in export_block
   wrapper_block.export(path, epoch)
 File "C:\Users\yyyy\Anaconda3\lib\site-packages\mxnet\gluon\block.py", line 897, in export
   assert name in aux_names
AssertionError

How could I solve it?

Edit : I am able to export it in train.py, but don’t know why I can’t export it by export_my_model.py

The way to export is add one line after net.save_parameters(“params/resnet50.params”)

net.export("model", 0)

I guess I find a way to export the model

import gluoncv as gcv
import mxnet as mx

from gluoncv.utils import export_block
from mxnet.gluon import nn

from networks import resnet50

context = mx.cpu()
model = resnet50(ctx=context, pretrained=False)
model.load_parameters('params/resnet50.params', ctx=context, allow_missing=True, ignore_extra=True)
net = nn.HybridSequential()
net.add(model.base)
net.add(model.avgpool)
net.add(model.bn)
net.add(model.flatten)
export_block('resnet50.params', net, data_shape=(128, 384, 3))

I haven’t tried it with c++ api yet, not sure it can work or not, but at least I am able to export the network by this solution.

I guess it is the if condition prevent the network to export after I reload the weights

Source codes of resnet.py

def hybrid_forward(self, F, x):
        x = self.base(x)
        x = self.avgpool(x)
        x = self.bn(self.flatten(x))
        #these two lines are the culprit, I wonder
        if self.pretrained:
            x = self.classifier(x)
        return x

I think you are right that if self.pretrained is False, then the classifier branch wouldn’t run and you would get this error : Parameter resnet0_dense0_weight is not used by any computation. Is this intended? out = self.forward(*args)

To test if it is going to work in cpp, you can simply try to load the model in a symbol block using

net = gluon.nn.SymbolBlock.imports(...
net(mx.nd.ones((1, 128, 384, 3))) 

btw are you sure about the shape of your data? Shouldn’t it be (3,128,384) ?

1 Like

Thanks for your helps

Because the shape param of export_block is based on HWC

By the way, do you know a way to change the batch size of the c++ api(Executor) at run time without rebind?

Hi, just want to tell you the model works with c++ and thanks for you helps.
You can check my blog and codes if you are interesting