Converting FAI Torch models to Caffe

Has anyone tried this before? I’d like to reuse a model I trained using the fastai API but I need to convert the model and weights for Caffe.

Is this possible?

-aps

1 Like

I do consulting where I convert clients’s models to run on iOS, and this often includes converting weights from Caffe, Keras, PyTorch, and so on. So yes, it is definitely possible. :slight_smile:

The first step is to extract the weights from the model, then transpose them so they are in the correct order (different packages put the weights in a different order), and finally writing it out in the new file format.

There are some subtle details, such as how different packages store the batch normalization parameters, etc.

My suggestion would be to look on GitHub to see if someone already made a similar converter (from PyTorch to Caffe), and if so, use that as a starting point.

Thanks machinethink!

I have done exactly that. But they want a model file from Pytorch which I’m unsure how to create from the learner class.

Any chance you have a real example? I saved the weights to the “.h5” file so that’s done. Now I’m trying to get a model file out so I can pump it through some of these GitHub based command line tools (note that most of them I find to be completely broken).

I use

torch.save(learn.model, 'model_name.h5')

to save both the PyTorch model and its weights.

That doesn’t work for me as torch tries to pickle a lambda function which isn’t supported. I’m surprised it works for you.

I get stuff like this:

/usr/local/lib/python3.5/dist-packages/torch/serialization.py in <lambda>(f)
    132         pickle_protocol: can be specified to override the default protocol
    133     """
--> 134     return _with_file_like(f, "wb", lambda f: _save(obj, f, pickle_module, pickle_protocol))
    135 
    136 

/usr/local/lib/python3.5/dist-packages/torch/serialization.py in _save(obj, f, pickle_module, pickle_protocol)
    195     pickler = pickle_module.Pickler(f, protocol=pickle_protocol)
    196     pickler.persistent_id = persistent_id
--> 197     pickler.dump(obj)
    198 
    199     serialized_storage_keys = sorted(serialized_storages.keys())

AttributeError: Can't pickle local object 'resnext_50_32x4d.<locals>.<lambda>'

Strange. Sorry it didn’t help. For what it’s worth - I’m using Python 3.6 (not 3.5) and it worked for all the pretrained ResNet models I used (haven’t tried ResNext).

Eventually tried to torch.save a ResNext model and ran into the same error due to the pickling of a lambda function. To get it working, all I had to do was the following:

import dill
torch.save(learn.model, 'model_name.h5', pickle_module=dill)

Hope it helps you this time.

but while loading the model again. Its requires fastai. is there a way the not involve fastai in production.

Why do you say it requires fastai? I would use torch.load(model_path).

when I load using torch.
this is the error msg
ImportError: No module named ‘fastai’

the full error msg
ImportError Traceback (most recent call last)
in ()
----> 1 the_model = torch.load(’/Users/kobagapu.rao/Desktop/jupyter notebooks/save_project3_torch.h5’, map_location=lambda storage, loc: storage)

/anaconda/lib/python3.5/site-packages/torch/serialization.py in load(f, map_location, pickle_module)
259 f = open(f, ‘rb’)
260 try:
–> 261 return _load(f, map_location, pickle_module)
262 finally:
263 if new_fd:

/anaconda/lib/python3.5/site-packages/torch/serialization.py in _load(f, map_location, pickle_module)
407 unpickler = pickle_module.Unpickler(f)
408 unpickler.persistent_load = persistent_load
–> 409 result = unpickler.load()
410
411 deserialized_storage_keys = pickle_module.load(f)

ImportError: No module named ‘fastai’

Strange. How did you save the model you are trying to load here?

torch.save(learn.model, ‘save_project3_model_name.h5’)

tried this also
torch.save(learn.models.model, ‘save_project3_model_name.h5’)

but same error

I’m sorry, I haven’t come across something like this before and not experienced enough with fastai and PyTorch to solve your problem. Don’t understand why fastai is needed in torch.load().

we it torch.save() saved the whole object using pickel, in that object there might be so reference to fastai.

anyways thankyou for the help :slight_smile:

I ran into lots of these problems when converting to Caffe or back to Keras.
This lead me to prototype a simple web app: https://convertmodel-ai.carrd.co
Currently collecting feedback on the most annoying problems, let me know what you think.