DNN forwards only first image

asked 2019-12-26 16:16:55 -0600

evgevd gravatar image

updated 2019-12-27 01:57:05 -0600

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);
edit retag flag offensive close merge delete

Comments

opencv version ? what kind of network do you load ?

berak gravatar imageberak ( 2019-12-27 02:04:57 -0600 )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 ( 2019-12-27 02:11:59 -0600 )edit

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

berak gravatar imageberak ( 2019-12-27 02:15:46 -0600 )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 ( 2019-12-27 02:27:09 -0600 )edit

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

berak gravatar imageberak ( 2019-12-27 05:21:29 -0600 )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 ( 2019-12-27 06:28:22 -0600 )edit

unfortunately, yes, it works for me

berak gravatar imageberak ( 2019-12-27 07:10:54 -0600 )edit

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

berak gravatar imageberak ( 2019-12-27 07:21:03 -0600 )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 ( 2019-12-27 07:32:36 -0600 )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 ( 2019-12-27 18:03:24 -0600 )edit