Ask Your Question
0

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

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

AbhiTronix gravatar image

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

berak gravatar image

Hi,

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
0

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

berak gravatar image

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

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)
net.setInput(blob)

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

Comments

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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 2,252 times

Last updated: Jul 11 '18