Ask Your Question
0

How to use mean.binaryproto with blobFromImage(s) ?

asked 2017-11-18 21:01:18 -0600

fabito gravatar image

I’m using dnn::readNetFromCaffe to load this model published in the caffe model zoo:

https://github.com/BVLC/caffe/wiki/Mo...

Apart from the standard .prototxt and .caffemodel files, a third file is provided: mean.binaryproto.

I know I should use it to perform the mean subtraction. But I couldn't figure out whether its a cv::subtract I should apply before invoking blobFromImage or is it something I'd use to somehow infer the mean and scalefactor parameters.

Mat mean = readMeanFromBinary(); // How to read a binaryproto into a Mat ?
Mat img = imread(imgFile);
// How to use mean ?
Mat inputBlob = blobFromImage(img, 1.0f, Size(227, 227), Scalar(), false);
net.setInput(inputBlob, "data");
Mat prob = age.forward("prob");

So I have basically two questions:

  • How to read the mean.binaryproto file into a Mat ?
  • Once I have the mean, can I use it with blobFromImage ?

thanks Fábio

edit retag flag offensive close merge delete

Comments

1

can you take a look here ?

berak gravatar imageberak ( 2017-11-19 12:18:55 -0600 )edit

I know this question is old but now I am in the same situation: I want to load the files without having to install Caffe. Is it possible? Have you found a solution?

Francesco Boi gravatar imageFrancesco Boi ( 2018-10-23 06:58:33 -0600 )edit

@Francesco Boi :

my current solution is to install caffe on google's colab via !apt install -y caffe-cuda, and run the python script below ;)

berak gravatar imageberak ( 2018-10-23 07:50:35 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2017-11-20 08:24:29 -0600

dkurt gravatar image

@fabito, Besides .binaryprotocontains a blob with pixel-wise mean values, Caffe uses a global mean values for each channel (https://github.com/BVLC/caffe/blob/ma...). So you need to compute them once and use a regular way to subtract means by blobFromImage.

In case of Age and Gender classification model, they are (78.4263377603, 87.7689143744, 114.895847746) for blue, green and red channels correspondingly.

import caffe
import numpy as np

blob = caffe.proto.caffe_pb2.BlobProto()
with open('mean.binaryproto', 'rb') as f:
    blob.ParseFromString(f.read())
    data = np.array(blob.data).reshape([blob.channels, blob.height, blob.width])
    print np.mean(data[0]), np.mean(data[1]), np.mean(data[2])
edit flag offensive delete link more

Comments

@dkurt, do you know if this BlobProto protobuf class is available in OpenCV ? Just wondering if we could load and process the .binaryproto without having to install caffe and python.

fabito gravatar imagefabito ( 2017-11-21 04:49:14 -0600 )edit

@fabito, it isn't hard to implement but I don't know we really need it. You may try to follow @berak 's comment and check an experimental protobuf parser module.

dkurt gravatar imagedkurt ( 2017-11-21 06:13:26 -0600 )edit

@fabito Hi, so the correct command (in Python) would be cv2.dnn.blobFromImage(img, 1, (227, 227), (78.4263377603, 87.7689143744, 114.895847746), false). Is that correct?

lux28099 gravatar imagelux28099 ( 2018-01-31 03:30:51 -0600 )edit

@lux28099, cv2.dnn.blobFromImage(img, 1, (227, 227), (78.4263377603, 87.7689143744, 114.895847746), swapRB=False, crop=False) even better.

dkurt gravatar imagedkurt ( 2018-01-31 09:13:55 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-11-18 21:01:18 -0600

Seen: 2,797 times

Last updated: Nov 20 '17