Skip to content

DNN problem with variable batch size #15054

@rbenes

Description

@rbenes

Hello,

I have problem with dnn package.
My network has two inputs (similar network with one input do not have such problems).
The problem occurs, when I use it with batch input and the batch size changes.

Steps to reproduce
import cv2
import numpy as np


def init_net():
    """ network has two inputs. With one input it works fine!!! """
    print("initialization of net")
    net = cv2.dnn.readNet("predict.prototxt", "_iter_10000.caffemodel")
    net.setInputsNames(["1", "2"])
    return net


def test_batches():
    net = init_net()

    for batch_size in range(1, 3):
        print(f"running with batch size: {batch_size}")
        # net = init_net()    # IMPORTANT if the init is done also here, it works as expected

        images = []
        hm = []
        img = np.zeros(shape=(112, 112), dtype=np.float32)
        for _ in range(batch_size):
            images.append(img)
            hm.append(img)

        blob_im = cv2.dnn.blobFromImages(images)
        blob_hm = cv2.dnn.blobFromImages(hm)

        net.setInput(blob_im, f"1")
        net.setInput(blob_hm, f"2")

        output = net.forward()

        print(output.shape)
        print(output)


if __name__ == "__main__":
    test_batches()

Output:

initialization of net
running with batch size: 1
(1, 46)
[[-0.26091588 -0.71490073 -0.0474911  -1.028769    0.46995926 -1.024709
   0.79288584 -1.1920797   1.2365346  -1.2698926   1.8807425  -0.80999535
   0.6938905  -0.675884   -0.38262892 -0.5509771  -0.06392805 -0.9718448
   1.3061959  -0.7347802   0.1999376  -0.9299036  -0.9454945   1.1263789
   0.07076232  0.7544833   1.0545751   0.95105386  0.40316564  0.1967495
   0.10929835  0.55842555 -0.48845428 -0.28978053  0.1788773  -1.091119
   1.0194228  -1.0266495   3.3518863  -0.19585449 -0.6665389  -0.28525275
  -1.3955094   1.8038036   3.4963293  -0.60448146]]
running with batch size: 2
(2, 46)
[[ 0.08830757 -0.9255449   0.19535485 -1.1427423   0.6046585  -1.1955231
   0.7312245  -1.3258915   1.1537099  -1.2734762   1.614879   -0.8369938
   0.7821025  -1.0009924  -0.05474987 -0.55121446  0.08144715 -1.1966816
   0.9157872  -0.73964655  0.26882052 -0.94621813 -0.6524792   0.9742072
   0.21209313  0.68126976  0.60695624  0.9665917   0.53314215  0.25114083
   0.2647437   0.22432345 -0.27708143 -0.4265472   0.87187344 -1.1792935
   0.8304731  -1.1644017   3.1948476   0.13450515 -0.6541345  -0.79727423
  -1.4468615   1.9239253   3.033583   -0.66370124]
 [ 0.08830757 -0.9255449   0.19535485 -1.1427423   0.6046585  -1.1955231
   0.7312245  -1.3258915   1.1537099  -1.2734762   1.614879   -0.8369938
   0.7821025  -1.0009924  -0.05474987 -0.55121446  0.08144715 -1.1966816
   0.9157872  -0.73964655  0.26882052 -0.94621813 -0.6524792   0.9742072
   0.21209313  0.68126976  0.60695624  0.9665917   0.53314215  0.25114083
   0.2647437   0.22432345 -0.27708143 -0.4265472   0.87187344 -1.1792935
   0.8304731  -1.1644017   3.1948476   0.13450515 -0.6541345  -0.79727423
  -1.4468615   1.9239253   3.033583   -0.66370124]]

If I initialize net for each batch size (commented init inside for loop) the result is expected:

Output

initialization of net
running with batch size: 1
initialization of net
(1, 46)
[[-0.26091588 -0.71490073 -0.0474911  -1.028769    0.46995926 -1.024709
   0.79288584 -1.1920797   1.2365346  -1.2698926   1.8807425  -0.80999535
   0.6938905  -0.675884   -0.38262892 -0.5509771  -0.06392805 -0.9718448
   1.3061959  -0.7347802   0.1999376  -0.9299036  -0.9454945   1.1263789
   0.07076232  0.7544833   1.0545751   0.95105386  0.40316564  0.1967495
   0.10929835  0.55842555 -0.48845428 -0.28978053  0.1788773  -1.091119
   1.0194228  -1.0266495   3.3518863  -0.19585449 -0.6665389  -0.28525275
  -1.3955094   1.8038036   3.4963293  -0.60448146]]
running with batch size: 2
initialization of net
(2, 46)
[[-0.26091588 -0.71490073 -0.0474911  -1.028769    0.46995926 -1.024709
   0.79288584 -1.1920797   1.2365346  -1.2698926   1.8807425  -0.80999535
   0.6938905  -0.675884   -0.38262892 -0.5509771  -0.06392805 -0.9718448
   1.3061959  -0.7347802   0.1999376  -0.9299036  -0.9454945   1.1263789
   0.07076232  0.7544833   1.0545751   0.95105386  0.40316564  0.1967495
   0.10929835  0.55842555 -0.48845428 -0.28978053  0.1788773  -1.091119
   1.0194228  -1.0266495   3.3518863  -0.19585449 -0.6665389  -0.28525275
  -1.3955094   1.8038036   3.4963293  -0.60448146]
 [-0.26091588 -0.71490073 -0.0474911  -1.028769    0.46995926 -1.024709
   0.79288584 -1.1920797   1.2365346  -1.2698926   1.8807425  -0.80999535
   0.6938905  -0.675884   -0.38262892 -0.5509771  -0.06392805 -0.9718448
   1.3061959  -0.7347802   0.1999376  -0.9299036  -0.9454945   1.1263789
   0.07076232  0.7544833   1.0545751   0.95105386  0.40316564  0.1967495
   0.10929835  0.55842555 -0.48845428 -0.28978053  0.1788773  -1.091119
   1.0194228  -1.0266495   3.3518863  -0.19585449 -0.6665389  -0.28525275
  -1.3955094   1.8038036   3.4963293  -0.60448146]]
System information (version)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions