1

I am trying to use yahoo nsfw model with OpenCV. Here is what I have tried. I just copied deploy.prototxt and resnet_50_1by2_nsfw.caffemodel from the repository.

import cv2 as cv
cvNet = cv.dnn.readNetFromCaffe('deploy.prototxt','resnet_50_1by2_nsfw.caffemodel')
image_path = 'adult.jpg'
image = cv.imread(image_path)
blob = cv.dnn.blobFromImage(image, 1, (224,224), (0,0,0), True, crop=False)
cvNet.setInput(blob)
detections = cvNet.forward()
print(detections)

Whatever image I have as an input I get an output like this [[0.9855554 0.01444463]], the first value is always larger. I chose the image size (224, 224) because in deploy.prototxt, I found the following:

name: "ResNet_50_1by2_nsfw"
layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 1 dim: 3 dim: 224 dim: 224 } }
}

What did I do wrong?

1 Answer 1

1

I am not sure if you have managed to solve this problem already and I am not an expert on this, but I find very strange that you don't perform mean subtraction when creating the blob (the means are set to (0,0,0) in your code). Looking at the Github repository that you linked the authors do perform mean subtraction in https://github.com/yahoo/open_nsfw/blob/master/classify_nsfw.py line 114:

caffe_transformer.set_mean('data', np.array([104, 117, 123]))

This could have an impact if the classification of the network. I suggest using their parameters in your OpenCV implementation.

Sign up to request clarification or add additional context in comments.

2 Comments

I suggest quoting directly the line 114 in the answer so it becomes more accessible to all SO users.
Yes, you are absolutely right. I have edited the answer so that it includes that line. Thank you for calling it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.