in Article, Blog Posts, Computer Vision (机器视觉)

Convert the saved checkpoint of cuda-convenet to the proto of Caffe

*Credits of the scripts go to jamt9000 on github, thanks for the helpful work!

You are a deep learning consumer, you have a saved check point you trained with the cuda-convenet. You want to use Caffe to evaluate the trained neural networks on new data possiblely due to the absent of GPU. Hence you need to convert the saved checkpoint to the format compatible to Caffe.

1. check out this version of caffe: jamt9000/caffe:convert-cuda-convnet or my copy.

$git clone convert-cuda-convnet

2. compile it following the Caffe installation instruction

## Misc. Setup 
$make all
$make pycaffe

3. convert the check-point with the following codesnippet

import sys

from convert_net import *

if (len(sys.argv) < 2):
    print "Usage:- path-to-saved-convnet"

arg_convnet = sys.argv[1]
output_prototxt = arg_convnet + "-prototxt"
output_proto = arg_convnet + "-proto"

netpt = cudaconv_to_prototxt(arg_convnet)
fh=open(output_prototxt, 'w')
print "Wrote {0}".format(output_prototxt)

netpb = cudaconv_to_proto(arg_convnet)
fh=open(output_proto, 'wb')
print "Wrote {0}".format(output_proto)
$python saved-convnet-checkpoint

4. Now you should find two files: saved-convnet-checkpoint-proto and saved-convnet-checkpoint-prototxt, they are in an older format, you need some tools provided by Caffe to upgrade them.

5. checkout and compile the up-to-date (or any more recent stable) version of Caffe

6. Under build/tools/ you will find tools named upgrade_net_proto_text.bin and upgrade_net_proto_binary.bin, use them to upgrade the saved-convnet-checkpoint-proto and saved-convnet-checkpoint-prototxt accordingly.

7. Almost done, you may need to manually fix the prototext file a little bit for example, add the input layer following any example proto text files provided by the Caffe.

Good luck!

Write a Comment