Change the internal layer inside the pre-trained model


#1

Hi,

Does anybody know how can I change the particular internal layer of the loaded model? This is not the case when I have the code of the model, and not the case when I would want to change just the outputs. The architecture is quite big, so I do not want to reproduce it from the scratch.

I loaded the model structure .json and the parameters .params, and in fact I have the access to all layers. Now I want to build a new architecture on the basis of this one, while altering layers which will not be the last to the output. For instance, a layer called ‘conv1’ in squeezenet, etc.

Thank you in advance for any advice.

Regards

Alex


#2

If you have a symbol you can always traverse the graph back, starting from the top node, using get_children() method.

But replacing a symbol means that you need to rewire it, making sure that input and output shapes matches. I don’t know a way to do it, without recreating the next chain.

Imagine you have:

FullyConnected1 -> FullyConnected2 -> FullyConnected3

and you want to replace FullyConnected2 with a Dropout (so you don’t need to worry about shapes matching). You can traverse to the FullyConnected1 and create a dropout using: dropout = mx.sym.Dropout(data=fc1, p=0.2), but later you need to recreate the whole chain after fc2 (fc3 only in our case, but I guess this is not the case for squeezenet).

There is no easy way to do that at MXNet at this point. This thing would be easier once https://github.com/apache/incubator-mxnet/pull/10593 is done. But at this point, I imagine, you need to use reflection to get the type of the block and maybe sym.list_attr() to get the attributes of the block. Then you traverse all the way back to recreate the network block by block… And there is only one direction you may go, so it is not easy.


#3

Sergey, thank you.This is actually what I got too. I just thought that maybe there is a smarter way I am not familiar with. It is a bit disappointing that having ‘graph’ and ‘symbolic’ concepts in the MXNet, we are not able to amend the symbols without redrafting the whole subsequent structure.


#4

In fact the workaround is to edit the .json file directly. It is a bit odd that MXNet did not release the related simple option via API or the additional tool.


#5

Hi Alex,

Yes, editing json file directly is possible. MXNet doesn’t have this piece of functionality, because it doesn’t look like there were a push from the community to have.

You are more than welcomed to contribute to MXNet by submitting a pull request to the git repository.