Search code examples
machine-learningneural-networkdeep-learningcaffepycaffe

How to get the output shape of a layer via Pycaffe


The title already contains the full question: How can I get the output shape of a given layer of a Caffe model using Pycaffe?

I have a caffe.Net() object and now I want the output shape of a specific layer in the model.


Solution

  • Given the layer name, you can get its index via:

    l_idx = list(net._layer_names).index(my_layer_name)
    

    Once you have l_idx, you can get its outputs (aka "top"s):

    tops = [(net._blob_names[bi], net.blobs[net._blob_names[bi]].data.shape) 
            for bi in list(net._top_ids(li))]
    

    for each "top" you can get the informtation

    for tn in tops:
      print "output name {} has shape {}".format(tn, net.blobs[tn].data.shape)
    

    A more detailed example on how to access net's structure via pycaffe interface can be found here.