Git Product home page Git Product logo

Comments (5)

florianblume avatar florianblume commented on June 1, 2024

I ran into the same problem. My solution is to remove the boxes that are None and delete the corresponding images. I achieved this by returning the images from this function back to forward, like so:

forward function

# Detect faces
batch_boxes, batch_probs, batch_points = self.detect(img, landmarks=True)
# Select faces
if not self.keep_all:
    img, batch_boxes, batch_probs, batch_points = self.select_boxes(
        batch_boxes, batch_probs, batch_points, img, method=self.selection_method,
    )
# Extract faces
faces = self.extract(img, batch_boxes, save_path)

if return_prob:
    return faces, batch_probs
else:
    return faces

and the select_boxes function (omitted everything before if batch_mode because nothing changed there)

if batch_mode:
    sanitized_selected_boxes = []
    sanitized_selected_probs = []
    sanitized_selected_points = []
    for idx, box in enumerate(selected_boxes):
        if box is None:
            if isinstance(imgs, list):
                imgs.pop(idx)
            elif isinstance(imgs, np.ndarray):
                imgs = np.delete(imgs, idx, 0)
            elif isinstance(imgs, torch.Tensor):
                imgs = torch.cat((imgs[:idx], imgs[idx+1:]), 0)
            else:
                raise TypeError("imgs must be a list, np.ndarray, or torch.Tensor")
        else:
            sanitized_selected_boxes.append(box)
            sanitized_selected_probs.append(selected_probs[idx])
            sanitized_selected_points.append(selected_points[idx])
    selected_boxes = np.array(sanitized_selected_boxes)
    selected_probs = np.array(sanitized_selected_probs)
    selected_points = np.array(sanitized_selected_points)
else:
    selected_boxes = selected_boxes[0]
    selected_probs = selected_probs[0][0]
    selected_points = selected_points[0]

return imgs, selected_boxes, selected_probs, selected_points

from facenet-pytorch.

beicodewarrior avatar beicodewarrior commented on June 1, 2024

Hi, @florianblume

Your code fixed a certain issue when some images don't contain faces. However, it threw a new error when testing one image in the inference script.

mtcnn = MTCNN(
    image_size=160, margin=0, min_face_size=20,
    thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=True,
    device=device
)

img = Image.open('1.jpg')
img_cropped = mtcnn(img)

This code throws the error like

lib\site-packages\facenet_pytorch\models\utils\detect_face.py", line 359, in extract_face
    margin * (box[2] - box[0]) / (image_size - margin),
TypeError: 'float' object is not subscriptable

Can you check it on your side?

from facenet-pytorch.

florianblume avatar florianblume commented on June 1, 2024

I would've expected that this doesn't affect a single image because of the if batch_mode: check in the original code. I don't need to process a single image so I'll just use the solution I posted. But I guess it should be an easy fix if you debug the code a bit.

from facenet-pytorch.

beicodewarrior avatar beicodewarrior commented on June 1, 2024

The returned image value you create seems to pass the if batch_mode:
So it doesn't recognize the single image and batch group. Could you please update your code for that?

from facenet-pytorch.

florianblume avatar florianblume commented on June 1, 2024

Sorry, I don't have time for that, you'll have to fix it yourself (and you seem to be on the right track).

from facenet-pytorch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.