Ask Your Question

OpenCV cv2.dnn.readNetFromCaffe : get output from specific layer only ?

asked 2018-07-11 10:20:27 -0500

AbhiTronix gravatar image

updated 2018-07-11 11:17:44 -0500

berak gravatar image


If I want to get the output of specific layer ('fc7' for example) of a caffe model , what should I do?

I have implemented this in caffe (Python) as: net.blobs["data"].data[...] = transformer.preprocess("data", image_block_rgb_227) out = net.forward() fVector = net.blobs['fc7'].data[0].copy() where 'fVector' is the desire output.

but how to implement the same in case of OpenCV ? Please help ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-07-11 10:27:22 -0500

berak gravatar image

updated 2018-07-11 10:51:05 -0500

using cv2.dnn, you can specify the desired output layer in the net's forward() function, like:

# assuming, that an fc layer at the end denotes a "classification" model
net = cv.dnn.readNet(caffemodel, prototxt)
img = ...
blob = cv.dnn.blobFromImage(img, scale, (227, 227), mean, isrgb, cropFromCenter)

output = net.forward("fc7") # <--your final layer here !
print np.argmax(output), np.max(output)

for a more detailled (python) sample, please have a look here !

edit flag offensive delete link more


Yeah that seems to work. Don't know if it is giving out true values or not. Cannot cross-check it with real values as i lack caffe and GPU system.

AbhiTronix gravatar imageAbhiTronix ( 2018-07-11 23:11:16 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-07-11 10:20:27 -0500

Seen: 22 times

Last updated: Jul 11