caffe2 icon indicating copy to clipboard operation
caffe2 copied to clipboard

How to get the feature maps generated by the network?

Open lzx1413 opened this issue 9 years ago • 3 comments

When I use the workspace.Predictor to run a network, how can I get the feature maps generated by a special layer. I try to transform a caffe model to caffe2 but the result is not right, so I want to see where it is wrong. Could anyone help me, thanks.

lzx1413 avatar May 06 '17 12:05 lzx1413

The MNIST tutorial show 15th feature map of 'conv1' layer. https://github.com/caffe2/caffe2/blob/master/caffe2/python/tutorials/MNIST.ipynb

Convolutions for this mini-batch

pyplot.figure() conv = workspace.FetchBlob('conv1') shape = list(conv.shape) shape[1] = 1

We can look into any channel. This of it as a feature model learned

conv = conv[:,15,:,:].reshape(shape)

_ = visualize.NCHW.ShowMultiple(conv)

astroli avatar May 06 '17 16:05 astroli

@astroli thanks for your reply ,but I have some problems about it yet. my test code is like this

   with open(init_model_path) as f:
        init_net = f.read()
    with open(predict_model_path) as f:
        predict_net = f.read()
    net = workspace.Predictor(init_net, predict_net)

    init_img = cv2.imread('k.jpg')  # image read
    img = np.asarray(init_img, dtype=np.float32)
    img -= mean_value
    img = _img.transpose(2, 0, 1)
    img = _img.reshape((1,) + _img.shape)
    output_prob = net_0.run([_img])
    blobs = workplace.Blobs()#[]
    blob = workplace.FetchBlob('conv1')#error None

And in this way I can not get the blobs in the workspace, any ideas about it? thanks

lzx1413 avatar May 07 '17 02:05 lzx1413

I solve this problem, I should add another two line code first.

   with open(init_model_path) as f:
        init_net = f.read()
    with open(predict_model_path) as f:
        predict_net = f.read()
    #init the workplace
    workspace.RunNetOnce(init_net)
    workspace.CreateNet(predict_net)
    net = workspace.Predictor(init_net, predict_net)

    init_img = cv2.imread('k.jpg')  # image read
    img = np.asarray(init_img, dtype=np.float32)
    img -= mean_value
    img = _img.transpose(2, 0, 1)
    img = _img.reshape((1,) + _img.shape)
    output_prob = net_0.run([_img])
    blobs = workplace.Blobs()#[]
    blob = workplace.FetchBlob('conv1')#error None

thanks a lot

lzx1413 avatar May 07 '17 08:05 lzx1413