question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fix remaining issues with python/tensorflow implemenation of MTCNN

See original GitHub issue

MTCNN has been implemented using python/tensorflow and can be found here. However this implementation gives slightly different results compared to the matlab/caffe implementation from the authors.

  • In the matlab/caffe implementation a call to imResample is used to down-sample image patches. For the python implementation, different implementations has been attempted and opencv:s cv2.resize(img, (sz[1], sz[0]), interpolation=cv2.INTER_AREA) seems to perform best. But the result is not identical and this seems to impact performance quite a bit.
  • The probablity scores for the bounding box hypotheses differs in the two implementations. This is probably due to that the convolutions are performed slightly different.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
davidsandbergcommented, Nov 15, 2016

@jrabary The MTCNN model was originally a Caffe model which has been imported to tensorflow. And since Caffe uses a different ordering of the dimensions it required some reshaping of model inputs/outputs.

2reactions
scotthongcommented, Jan 31, 2017

Hi David,

I finally got a chance to play with MTCNN again, and the following is the code to fix the image stretching and improvement on how the margin (padding) is added to the image.

Change the align_dateset_mtcnn.py of the following lines From:

                            bb[0] = np.maximum(det[0]-args.margin/2, 0)
                            bb[1] = np.maximum(det[1]-args.margin/2, 0)
                            bb[2] = np.minimum(det[2]+args.margin/2, img_size[1])
                            bb[3] = np.minimum(det[3]+args.margin/2, img_size[0])

To:

                            # To prevent the resized image from been skewed, the bounding box needs to be
                            # adjusted to become a square. Also, the size of the margin needs to be mapped
                            # from the the target scaled image space to the size in the original image space.

                            dx = det[2] - det[0]
                            dy = det[3] - det[1]
                            margin = 0
                            if(dy >= dx):
                               margin =  dy * args.margin / (args.image_size - args.margin)
                                bb[0] = np.maximum(det[0] - (dy-dx)/2 - margin/2, 0)
                                bb[2] = np.minimum(det[2] + (dy-dx)/2 + margin/2, img_size[1])
                                bb[1] = np.maximum(det[1] - margin/2, 0)
                                bb[3] = np.minimum(det[3] + margin/2, img_size[0])
                            else:
                                margin =  dx * args.margin / (args.image_size - args.margin)
                                bb[0] = np.maximum(det[0] - margin/2, 0)
                                bb[2] = np.minimum(det[2] + margin/2, img_size[1])
                                bb[1] = np.maximum(det[1]-(dx-dy)/2 - margin/2, 0)
                                bb[3] = np.minimum(det[3] + (dx-dy)/2 + margin/2, img_size[0])

After this adjustment, the original image won’t get stretched along x or y directions (except for these clamped by min/max) .

–Scott Hong

Read more comments on GitHub >

github_iconTop Results From Across the Web

A very simple Face Recognition system — That works - Medium
Recognize faces and identify people. Our Tool-set. Let's just start by setting up things first. This blog deals with Python based implementation ......
Read more >
How to Perform Face Recognition With VGGFace2 in Keras
Face detection is the process of automatically locating faces in a photograph and localizing them by drawing a bounding box around their extent....
Read more >
Model Zoo - Deep learning code and pretrained models for ...
This is an implementation of Mask R-CNN on Python 3, Keras, and TensorFlow. The model generates bounding boxes and segmentation masks for each...
Read more >
MTCNN Face Detection Implementation for TensorFlow, As A ...
MTCNN face detection implementation for TensorFlow, as a PIP package. MTCNN: Implementation of the MTCNN face detector for Keras in Python3.4+.
Read more >
Research on MTCNN Face Recognition System in Low ...
Since there is no engineering implementation of. MTCNN algorithm based on Python language and. TensorFlow framework, it is reproduced and trained.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found