Skip to content

How To Convert Models

ProGamerGov edited this page Feb 28, 2020 · 3 revisions

Using MMdnn and any applicable libraries like PyTorch, TensorFlow, etc...

Convert the Caffe model to Mmdnn's IR format:

mmtoir -f caffe -o bvlc_googlenet.h5 -w bvlc_googlenet.caffemodel --inputShape 3,224,224

Generate the required files for PyTorch using the IR model:

mmtocode -f pytorch -n bvlc_googlenet.h5.pb --IRWeightPath bvlc_googlenet.h5.npy --dstModelPath bvlc_googlenet.py -dw bvlc_googlenet.h5.npy

You will have to find your Mmdnn pip install and allow_pickle = True to a one or more lines to make it work properly.

Like with the Mmdnn code, you will have to add allow_pickle = True to the bvlc_googlenet.py file before running the next command. You can make a copy of bvlc_googlenet.py called bvlc_googlenet_class.py to use as your class file.

Convert the IR model to PyTorch:

mmtomodel -f pytorch -in bvlc_googlenet.py -iw bvlc_googlenet.h5.npy -o bvlc_googlenet.pth

Create a copy of bvlc_googlenet.py and replace all mentions of self.__conv(2, name='...', with nn.Conv2d( and all mentions of self.__dense(name = '...' with nn.Linear(. Change "KitModel" to something else, and remove the weights parameter. Comment out any secondary loss outputs in the forward function as those seem to cause errors.

With NotePad++:

Using the class model script:

Find: name='.*?' Replace with: `` (blank)

Find: self.__conv(2, , Replace with: nn.Conv2d(

Find: name = '.*?' Replace with: `` (blank)

Find: self.__dense(, Replace with: nn.Linear(

Copy the initialization function from the non class script and do this to extract the layer names:

Find: =.*$ Replace with: ',

Find: self. Replace with: '

Setup and run the final conversion/setup script: https://gist.github.com/ProGamerGov/4fe325efe31b5a650c24a56151dd1952

  • The model should now be usable, provided that you load it with the class that you setup.

Clone this wiki locally