Ask Your Question
0

OpenCV dnn classification result is not match to Caffe result

asked 2019-03-12 04:06:00 -0500

updated 2019-03-12 04:44:59 -0500

berak gravatar image

OpenCV Code:

image = cv2.imread("mysite/27.png")
blob = cv2.dnn.blobFromImage(image, 1, (224, 224), (123, 117, 104), swapRB=False, crop=False)
net = cv2.dnn.readNetFromCaffe("deploy.prototxt", "model.caffemodel")
net.setInput(blob)
preds = net.forward()
idxs = np.argsort(preds[0])[::-1][:5]
print(idxs)

Caffe code:

net = caffe.Classifier(MODEL_FILE, PRETRAINED, mean=np.array([104, 117, 123]), channel_swap=(2,1,0), input_scale=1.0, raw_scale=255, image_dims(224, 224))
input_image = caffe.io.load_image('27.png')
pred = net.predict([input_image])
idxs = np.argsort(pred[0])[::-1][:5]
print(idxs)

Even though I set swapRB=True/False, crop=True/False, all cases return different result. Result on Caffe is my expectation, but opencv isn't. I'm using OpenCV 3.4.2, Caffe 1.0.

Anyone can help. Thanks in advance.

edit retag flag offensive close merge delete

Comments

what kind of model is it, exactly ?

berak gravatar imageberak ( 2019-03-13 09:22:24 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2019-03-18 04:17:19 -0500

I understood why it's difference. Since caffe.Classifier() do different algorithm from openCV. It uses oversampling for prediction. If I use caffe.Net() and call forward(), it will return same result with openCV.

edit flag offensive delete link more

Comments

Can you updgraded OpenCV 4.0.1? But latest one will be 4.1.0 but not available yet.

supra56 gravatar imagesupra56 ( 2019-03-18 08:40:42 -0500 )edit
-1

answered 2019-03-13 09:07:46 -0500

supra56 gravatar image

updated 2019-03-13 09:13:43 -0500

Change this:

net = cv2.dnn.readNetFromCaffe("deploy.prototxt", "model.caffemodel")

to:

net = cv2.dnn.readNetFromCaffe(deploy, model)

This is what I used:

proto = 'mobilenet_deploy.prototxt'
weights = 'mobilenet.caffemodel'
net = cv2.dnn.readNetFromCaffe(proto, weights)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-03-12 04:06:00 -0500

Seen: 52 times

Last updated: Mar 18