Transfer weights from caffe to tensorflew

222 views Asked by At

The convnet filter shape in caffe is [128 64 3 3] or [out_dim, in_dim, filter_height, filter_weight]. How do I convert it to the tensorflow shape [filter_height, filter_width, in_dim, out_dim] or [3 3 64 128], to get the same results with a tensorflow model as with a caffe model.

1

There are 1 answers

0
kmario23 On BEST ANSWER

I think you can simply use numpy to resize the array to the dimension that you wish to transform it to. And then just pass it to tensorflow computation graph as usual.

For your case, you can use:

tf_array = caffe_array.reshape([3, 3, 64, 128])

Example:

In [1]: zer = np.array([[[ [1, 2, 2], [3, 4, 4]], [[5, 6, 6], 
                             [7, 8, 8]], [[0, 1, 1], [2, 1, 1] ] ]])

In [2]: zer.shape
Out[2]: (1, 3, 2, 3)

In [3]: zer
Out[3]: 
array([[[[1, 2, 2],
         [3, 4, 4]],

        [[5, 6, 6],
         [7, 8, 8]],

        [[0, 1, 1],
         [2, 1, 1]]]])


In [4]: rez = zer.reshape([3, 2, 3, 1])

In [5]: rez.shape
Out[5]: (3, 2, 3, 1)

In [6]: rez
array([[[[1],
         [2],
         [2]],

        [[3],
         [4],
         [4]]],

       [[[5],
         [6],
         [6]],

        [[7],
         [8],
         [8]]],

       [[[0],
         [1],
         [1]],

        [[2],
         [1],
         [1]]]])