How and where does OpenCV preprocess the input array for an ONNX in below code?

73 Views Asked by At

I am doing a face detection project using CenterFace. The authors have given code in their github where they have used OpenCV's DNN module to read the model from it's ONNX file.

class CenterFace(object):
def __init__(self, landmarks=True):
    ...
       self.net = cv2.dnn.readNetFromONNX('models/onnx/centerface.onnx')
    ...

The problem is that using onnxruntime for inference the model requires a input of size (10, 3, 32, 32).

However, in the opencv implementation the shape of the blob is (1, 3, 480, 640) after running blobFromImage() method and after setInput() method. I have checked it personally with a print statement.

I wish to know where the preprocessing step occurs inbetween these lines of code so I can hopefully find ways to optimize it. I have never before used OpenCV nor C++ for DL and only have experience with pytorch and python.

def inference_opencv(self, img, threshold):
    blob = cv2.dnn.blobFromImage(img, scalefactor=1.0, size=(self.img_w_new, self.img_h_new), mean=(0, 0, 0), swapRB=True, crop=False)
    self.net.setInput(blob)

    print(blob.shape) #  ACTUAL blob shape at this point ---> (1, 3, 480, 640)


    # EXPECTED blob shape (by analyzing onnx model) ---> (10, 3, 32, 32)
    heatmap, scale, offset, lms = self.net.forward(["537", "538", "539", "540"])
    
0

There are 0 best solutions below