DNN forwards only first image

asked Dec 26 '19

evgevd gravatar image

updated Dec 27 '19

I make input for neural network with cv.dnn.blobFromImages, and then run forward function. Number of input images = 20. But I always get only one output, for first blob. I saw documentation of blobFromImages and forward fucntions but it didn't help. I got the same result on Python and C++

imgs = [cv.imread(e) for e in os.listdir()] #20 images
cv_input = cv.dnn.blobFromImages(imgs, 1, (64,64))
cvNet.setInput(cv_input)
pred = cvNet.forward()
pred # array([[1.]], dtype=float32)

.............

vector<vector<cv::Mat>> pred;
// images = vector of cv::Mat
cv::Mat inp = cv::dnn::blobFromImages(images, 1. / 255, cv::Size(64, 64), cv::Scalar(), true);
cnn_cl.setInput(inp);
cnn_cl.forward(pred);
Preview: (hide)

Comments

opencv version ? what kind of network do you load ?

berak gravatar imageberak (Dec 27 '19)edit

OpenCV 4.1. I use CNN with 64x64x3 on input and sigmoid on output layer. I got it by converting keras→tensorflow→openCV DNN. It works correctly on single image.

evgevd gravatar imageevgevd (Dec 27 '19)edit

how should the output look like ? what is expected ? (is it just a classification network ?)

berak gravatar imageberak (Dec 27 '19)edit
1

It is a binary classificator. When I run it on single images i got correct outputs.

cv_pred = []
for img in imgs:
    cv_input = cv.dnn.blobFromImage(img)#, size = img.shape, swapRB=True, crop=False)
    cvNet.setInput(cv_input)
    cvOut = cvNet.forward()
    cv_pred.append(cvOut[0][0])

Out:

[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0]

But with running in multiple images I got only one prediction: array([[1.]], dtype=float32)

evgevd gravatar imageevgevd (Dec 27 '19)edit

seems to work as expected with 4.2.0 or master. can you try to update ?

berak gravatar imageberak (Dec 27 '19)edit

The same result on python with cv__version__ = 4.2.0

Did I correctly understand? You got multiple output after blobFromImages and one forward command?

evgevd gravatar imageevgevd (Dec 27 '19)edit

unfortunately, yes, it works for me

berak gravatar imageberak (Dec 27 '19)edit

btw, vector<Mat> pred; not vector vector (and that works, too ;()

berak gravatar imageberak (Dec 27 '19)edit
1

try :

nom_modele ="bvlc_googlenet.caffemodel"
nom_proto = "bvlc_googlenet.prototxt"
nom_classe = "classification_classes_ILSVRC2012.txt"
caffe = cv.dnn.readNet(nom_modele, nom_proto)
try:
    with open(nom_classe, 'rt') as f:
        classes = f.read().split('\n')
except:
    classes = None

img_original1 = cv.imread(r"C:/tmp/neuralstyle/elephant.jpg")
img_original2 = cv.imread(r"C:/tmp/neuralstyle/zebra.jpg")
if img_original1 is not None and img_original2 is not None:
    moy_image = cv.mean(img_original1) 
    img_all = np.array([img_original1,img_original2])
    blob_all = cv.dnn.blobFromImages(img_all, 1.0, (227, 227), moy_image, swapRB=False)
    caffe.setInput(blob_all)
    val_sorties = caffe.forward()
LBerger gravatar imageLBerger (Dec 27 '19)edit

I tried. But it doesnt work.

I see that my original keras model uses «channels last» so input array has shape (20, 64, 64, 3) But opencv works with (20, 3, 64, 64). Could this fact be causing the problem? I guess no but ..

evgevd gravatar imageevgevd (Dec 28 '19)edit