Git Product home page Git Product logo

Comments (12)

timesler avatar timesler commented on May 17, 2024

Hi @marcoippolito, I often find these compilation errors hard to debug. Could it be that you've used a class method instead of a normal method? How come you didn't write getFaceEmbedding as the forward method of your module?

from facenet-pytorch.

marcoippolito avatar marcoippolito commented on May 17, 2024

Hi @timesler Tim!
Your good questions come in handy.
I'm going to modify what I've done, and I'll be back with a feedback.
Thank you very much

from facenet-pytorch.

marcoippolito avatar marcoippolito commented on May 17, 2024

@timesler

I modified the method as follows:

def forward(self, imagePath):

Now I get this error:

(base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3 
./getFaceEmbedding.py 
Traceback (most recent call last):
  File "./getFaceEmbedding.py", line 127, in <module>
    sm = torch.jit.script(my_module)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1255, in 
script
    return torch.jit._recursive.recursive_script(obj)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 534, in 
recursive_script
    return create_script_module(nn_module, infer_methods_to_compile(nn_module))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 493, in 
infer_methods_to_compile
    stubs.append(make_stub_from_method(nn_module, method))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 40, in 
make_stub_from_method
    return make_stub(func)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 33, in 
make_stub
    ast = torch.jit.get_jit_def(func, self_name="RecursiveScriptModule")
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 171, in 
get_jit_def
    return build_def(ctx, py_ast.body[0], type_line, self_name)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 212, in 
build_def
    build_stmts(ctx, body))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 127, in 
build_stmts
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 127, in 
<listcomp>
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 187, in 
__call__
    return method(ctx, node)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 289, in 
build_Assign
    rhs = build_expr(ctx, stmt.value)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 186, in 
__call__
    raise UnsupportedNodeError(ctx, node)
torch.jit.frontend.UnsupportedNodeError: DictComp aren't supported:
  File "./getFaceEmbedding.py", line 79
        #dataset = datasets.ImageFolder('../data/testFaces/subData02')
        dataset = datasets.ImageFolder(imagePath)
        dataset.idx_to_class = {i:c for c, i in dataset.class_to_idx.items()}
                               ~ <--- HERE
        loader = DataLoader(dataset, collate_fn=collate_fn, num_workers=workers)

I read here: https://pytorch.org/tutorials/advanced/cpp_export.html
that "If you need to exclude some methods in your nn.Module because they use Python features that TorchScript doesnโ€™t support yet, you could annotate those with @torch.jit.ignore"
But I guess that excluding the dictionary comprehension from the serialization, will interfere with the method's functionality.
What would you suggest me to do?

from facenet-pytorch.

timesler avatar timesler commented on May 17, 2024

I think the potential issues are that:

  • you are instantiating the models (mtcnn, resnet) in the forward method - this should be done in the __init__ method
  • your forward method should not include dataset and dataloader creation - this should not be done inside the pytorch Module
  • there is non-standard control flow in the forward method

I think you will probably have more success if you export each model (mtcnn and resnet) to torch script individually (torch.jit.script(mtcnn) and torch.jit.script(resnet)) and perform all other operations in your cpp file.

from facenet-pytorch.

marcoippolito avatar marcoippolito commented on May 17, 2024

@timesler

A)

Before exporting ech model, mtcnn and resnet to torch script individually, and perform all other operations in my cpp file, I would like to understand if, correctly refactoring the python code, it's still possible to use the python module, which could be of great help with your library but with other ones as well.

So, I refactored everything as follows:

from facenet_pytorch import MTCNN, InceptionResnetV1
import torch
from torch.utils.data import DataLoader
from torchvision import datasets
import numpy as np
import pandas as pd
import os

workers = 0 if os.name == 'nt' else 4

def collate_fn(x):
    return x[0]

def describe(x):
    print("Type: {}".format(x.type()))
    print("Shape/size: {}".format(x.shape))
    print("Values: \n{}".format(x))

def dataLoader(imagePath):
    dataset = datasets.ImageFolder(imagePath)
    dataset.idx_to_class = {i:c for c, i in dataset.class_to_idx.items()}
    loader = DataLoader(dataset, collate_fn=collate_fn, num_workers=workers)
    return (dataset, loader)

class GetFaceEmbedding(torch.nn.Module):
    def __init__(self):
        super(GetFaceEmbedding, self).__init__()
        self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
        print('Running on device: {}'.format(self.device))

        self.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=self.device
        )

        self.resnet = InceptionResnetV1(pretrained='vggface2').eval().to(self.device)


    def forward(self, imagePath):
        dataset, loader = dataLoader(imagePath)

        aligned = []
        names = []
        for x, y in loader:
            x_aligned, prob = self.mtcnn(x, return_prob=True)
            if x_aligned is not None:
                print('Face detected with probability: {:8f}'.format(prob))
                aligned.append(x_aligned)
                names.append(dataset.idx_to_class[y])

        aligned = torch.stack(aligned).to(self.device)
        embeddings = self.resnet(aligned).detach().cpu()
        return embeddings


my_module = GetFaceEmbedding()
sm = torch.jit.script(my_module)
sm.save("annotated_get_face_embedding.pt")

With python it works fine:

(opencv4) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3 .   
/getFaceEmbedding.py
Running on device: cpu
Face detected with probability: 0.999430
Type: torch.FloatTensor
Shape/size: torch.Size([1, 512])
Values: 
tensor([[ 3.6307e-02, -8.8092e-02, -3.5002e-02, -8.2932e-02,  1.9032e-02,
          2.3228e-02,  2.4253e-02, -3.7844e-02, -6.8906e-02,  2.0351e-02,
          ....
          1.3474e-02, -2.2743e-02,  3.2976e-02, -5.6658e-02,  2.0837e-02,
         -4.7152e-02, -6.5534e-02]])

But when trying to serialize I get this new error:
"torch.jit.frontend.UnsupportedNodeError: with statements aren't supported"
which I do not understand where does it come from

(opencv4) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3   
./getFaceEmbedding.py
Running on device: cpu
Traceback (most recent call last):
  File "./getFaceEmbedding.py", line 127, in <module>
    sm = torch.jit.script(my_module)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1255, in 
script
    return torch.jit._recursive.recursive_script(obj)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 534, in 
recursive_script
    return create_script_module(nn_module, infer_methods_to_compile(nn_module))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 296, in 
create_script_module
    return create_script_module_impl(nn_module, concrete_type, cpp_module, stubs)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 336, in 
create_script_module_impl
     script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1593, in 
_construct
    init_fn(script_module)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 328, in 
init_fn
    scripted = recursive_script(orig_value)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 534, in 
recursive_script
    return create_script_module(nn_module, infer_methods_to_compile(nn_module))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 493, in 
infer_methods_to_compile
    stubs.append(make_stub_from_method(nn_module, method))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 40, in 
make_stub_from_method
    return make_stub(func)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 33, in 
make_stub
    ast = torch.jit.get_jit_def(func, self_name="RecursiveScriptModule")
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 171, in 
get_jit_def
    return build_def(ctx, py_ast.body[0], type_line, self_name)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 212, in 
build_def
    build_stmts(ctx, body))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 127, in 
build_stmts
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 127, in 
<listcomp>
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 186, in 
__call__
    raise UnsupportedNodeError(ctx, node)
torch.jit.frontend.UnsupportedNodeError: with statements aren't supported:
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py", 
line 246

        # Detect faces
        with torch.no_grad():
        ~~~~ <--- HERE
            batch_boxes, batch_probs = self.detect(img)

What do you mean as "there is non-standard control flow in the forward method" ?
I read here: https://pytorch.org/tutorials/advanced/cpp_export.html, we can use tracing technique to convert to torchscript in case of models that make limited use of control flow.
But I used the the other available technique, the annotation, which, as far as I understand from the bare pytorch cpp documentation, is the correct technique to use in case of not standard control flow.
So.... how could I still refactor the forward method in order to make its control flow standard?
Or may be should I modify the way I use the annotation technique?

B)
I have also another question:

I tried to export mtcnn model individually:

import torch
import torchvision
from facenet_pytorch import MTCNN, InceptionResnetV1

# https://pytorch.org/tutorials/advanced/cpp_export.html

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

mtcnn_module = 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
        )

sm_mtcc = torch.jit.script(mtcnn_module)

But I get this error:

(opencv4) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3 ./mtcc.py
Traceback (most recent call last):
  File "./mtcc.py", line 15, in <module>
    sm_mtcc = torch.jit.script(mtcnn_module)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1255, in     
script
    return torch.jit._recursive.recursive_script(obj)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 534, in 
recursive_script
    return create_script_module(nn_module, infer_methods_to_compile(nn_module))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 493, in 
infer_methods_to_compile
    stubs.append(make_stub_from_method(nn_module, method))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 40, in 
make_stub_from_method
    return make_stub(func)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 33, in 
make_stub
    ast = torch.jit.get_jit_def(func, self_name="RecursiveScriptModule")
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 171, in 
get_jit_def
    return build_def(ctx, py_ast.body[0], type_line, self_name)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 212, in 
build_def
    build_stmts(ctx, body))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 127, in 
build_stmts
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 127, in 
<listcomp>
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 186, in 
__call__
    raise UnsupportedNodeError(ctx, node)
torch.jit.frontend.UnsupportedNodeError: with statements aren't supported:
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py", 
line 246

        # Detect faces
        with torch.no_grad():
        ~~~~ <--- HERE
            batch_boxes, batch_probs = self.detect(img)

from facenet-pytorch.

timesler avatar timesler commented on May 17, 2024

That error is being caused by with statements in facenet_pytorch/models/mtcnn.py as the traceback suggests. In order to get this to work, you will need to alter the source code. I won't be able to modify the default behaviour of MTCNN for this use case as it will complicate the use of the package for the vast majority of users. If you like, you could try modifying the package source code to remove the with statements. Alternatively (if you want to make a PR to this repo), you could add an optional argument for MTCNN that switches the with statements off.

from facenet-pytorch.

marcoippolito avatar marcoippolito commented on May 17, 2024

@timesler As far as I understand this line of code

    with torch.no_grad():

means "apply to the execution of the next line of code":

        batch_boxes, batch_probs = self.detect(img)

"the runtime context of torch.no_grad(), which puts all requires_grad flag to false, meaning we don't want that PyTorch calculates the gradients of the new defined variables batch_boxes and batch_probs".
Is my interpretation correct or am I saying something wrong?

Here: https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html#gradients in the code example it is actually used the with statement:

print(x.requires_grad)
print((x ** 2).requires_grad)

with torch.no_grad():
    print((x ** 2).requires_grad)

Or it used detached to get a new Tensor with the same content that does not require gradients:

print(x.requires_grad)
y = x.detach()
print(y.requires_grad)
print(x.eq(y).all())

I tried to modify in
/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py

the lines of code:

    with torch.no_grad():
    batch_boxes, batch_probs = self.detect(img)

to:

    #with torch.no_grad():
    batch_boxes, batch_probs = self.detect(img)
    batch_boxes = batch_boxes.detach()
    batch_probs = batch_probs.detach()

But I get this error message:

(opencv4) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3 
./getFaceEmbedding.py
Running on device: cpu
Traceback (most recent call last):
  File "./getFaceEmbedding.py", line 126, in <module>
    sm = torch.jit.script(my_module)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1255, in script
    return torch.jit._recursive.recursive_script(obj)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 534, in 
recursive_script
    return create_script_module(nn_module, infer_methods_to_compile(nn_module))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 296, in 
create_script_module
    return create_script_module_impl(nn_module, concrete_type, cpp_module, stubs)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 336, in 
create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1593, in 
_construct
    init_fn(script_module)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 328, in init_fn
    scripted = recursive_script(orig_value)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 534, in 
recursive_script
    return create_script_module(nn_module, infer_methods_to_compile(nn_module))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 296, in 
create_script_module
    return create_script_module_impl(nn_module, concrete_type, cpp_module, stubs)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 340, in 
create_script_module_impl
    create_methods_from_stubs(concrete_type, stubs)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 259, in 
create_methods_from_stubs
    concrete_type._create_methods(defs, rcbs, defaults)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 570, in 
compile_unbound_method
    stub = make_stub(fn)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 33, in 
make_stub
    ast = torch.jit.get_jit_def(func, self_name="RecursiveScriptModule")
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 171, in 
get_jit_def
    return build_def(ctx, py_ast.body[0], type_line, self_name)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 212, in 
build_def
    build_stmts(ctx, body))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 127, in 
build_stmts
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 127, in 
<listcomp>
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 186, in 
__call__
    raise UnsupportedNodeError(ctx, node)
torch.jit.frontend.UnsupportedNodeError: with statements aren't supported:
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py", line 
346
        """

        with torch.no_grad():
        ~~~~ <--- HERE
            batch_boxes, batch_points = detect_face(
                img, self.min_face_size,

Python decorators, applying a function to another function, could be of help....but how?
Neither my attempts were correct:

First attempt:

    @torch.no_grad
    batch_boxes, batch_probs = self.detect(img)


(opencv4) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3 
./getFaceEmbedding.py
Traceback (most recent call last):
  File "./getFaceEmbedding.py", line 16, in <module>
    from facenet_pytorch import MTCNN, InceptionResnetV1
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/__init__.py", line 2, in
 <module>
    from .models.mtcnn import MTCNN, PNet, RNet, ONet, prewhiten, fixed_image_standardization
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py", line 
248
    batch_boxes, batch_probs = self.detect(img)
              ^
SyntaxError: invalid syntax

Second attempt:

batch_boxes, batch_probs = self.detect(img)

@torch.no_grad
def detect(self, img, landmarks=False):

(opencv4) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3 . 
/getFaceEmbedding.py
Traceback (most recent call last):
  File "./getFaceEmbedding.py", line 16, in <module>
    from facenet_pytorch import MTCNN, InceptionResnetV1
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/__init__.py", line 2, in 
<module>
    from .models.mtcnn import MTCNN, PNet, RNet, ONet, prewhiten, fixed_image_standardization
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py", line 
157, in <module>
    class MTCNN(nn.Module):
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py", line 
308, in MTCNN
    def detect(self, img, landmarks=False):
TypeError: no_grad() takes no arguments

I'm not a Python expert... so... how would you use the decorator to apply the no_grad() runtime context to detect() method?

from facenet-pytorch.

marcoippolito avatar marcoippolito commented on May 17, 2024

Hi @timesler Tim!

Today I upgraded PyTorch and libtorch to the fresh 1.5 release .

mtcc.py :

import torch
import torchvision
from facenet_pytorch import MTCNN, InceptionResnetV1

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

mtcnn_module = MTCNN(
            image_size=160, margin=0, min_face_size=160,
            thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=True,
            device=device
        )
sm_mtcc = torch.jit.script(mtcnn_module)

I'm still getting this error:

(base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3 ./mtcc.py
Traceback (most recent call last):
  File "./mtcc.py", line 15, in <module>
    sm_mtcc = torch.jit.script(mtcnn_module)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1261, in 
script
    return torch.jit._recursive.create_script_module(obj, 
torch.jit._recursive.infer_methods_to_compile)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 305, in 
create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 361, in 
create_script_module_impl
    create_methods_from_stubs(concrete_type, stubs)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 279, in 
create_methods_from_stubs
    concrete_type._create_methods(defs, rcbs, defaults)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 587, in 
compile_unbound_method
    create_methods_from_stubs(concrete_type, (stub,))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 279, in 
create_methods_from_stubs
    concrete_type._create_methods(defs, rcbs, defaults)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 568, in 
try_compile_fn
    return torch.jit.script(fn, _rcb=rcb)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1287, in 
script
    ast = get_jit_def(obj)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 173, in 
get_jit_def
    return build_def(ctx, py_ast.body[0], type_line, self_name)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 206, in 
build_def
    build_stmts(ctx, body))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 129, in 
build_stmts
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 129, in 
<listcomp>
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 181, in 
__call__
    return method(ctx, node)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 364, in 
build_If
    build_stmts(ctx, stmt.orelse))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 129, in 
build_stmts
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 129, in 
<listcomp>
    stmts = [build_stmt(ctx, s) for s in stmts]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 181, in 
__call__
    return method(ctx, node)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 362, in 
build_If
    return If(r, build_expr(ctx, stmt.test),
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 181, in 
__call__
    return method(ctx, node)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 455, in 
build_Call
    args = [build_expr(ctx, py_arg) for py_arg in expr.args]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 455, in 
<listcomp>
    args = [build_expr(ctx, py_arg) for py_arg in expr.args]
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 180, in 
__call__
    raise UnsupportedNodeError(ctx, node)
torch.jit.frontend.UnsupportedNodeError: GeneratorExp aren't supported:
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/utils
/detect_face.py", line 19
        if not isinstance(imgs, (list, tuple)):
            imgs = [imgs]
        if any(img.size != imgs[0].size for img in imgs):
               ~ <--- HERE
            raise Exception("MTCNN batch processing only compatible with equal-dimension 
images.")
         imgs = np.stack([np.uint8(img) for img in imgs])
'MTCNN.detect' is being compiled since it was called from 'MTCNN.forward'
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py", 
line 247
        # Detect faces
        #with torch.no_grad():
        batch_boxes, batch_probs = self.detect(img)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        #batch_boxes = batch_boxes.detach()
        #batch_probs = batch_probs.detach()

How to solve the problem?
Looking forward to your kind help.
Marco

from facenet-pytorch.

timesler avatar timesler commented on May 17, 2024

Hi @marcoippolito, looks like you can't use generator expressions. Try changing this:

if any(img.size != imgs[0].size for img in imgs):

to this:

inconsistent_size = False
for img in imgs:
    if img.size != imgs[0].size:
        inconsistent_size = True
if inconsistent_size:

from facenet-pytorch.

marcoippolito avatar marcoippolito commented on May 17, 2024

Hi @timesler Tim!

I changed in /facenet_pytorch/models/utils/detect_face.py

    #if any(img.size != imgs[0].size for img in imgs):
    inconsistent_size = False
    for img in imgs:
        if img.size != imgs[0].size:
            inconsistent_size = True
    if inconsistent_size:
        raise Exception("MTCNN batch processing only compatible with equal-dimension images.")

and get this new error:

(base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3 ./mtcc.py
Traceback (most recent call last):
  File "./mtcc.py", line 15, in <module>
    sm_mtcc = torch.jit.script(mtcnn_module)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1261, in 
script
    return torch.jit._recursive.create_script_module(obj, 
torch.jit._recursive.infer_methods_to_compile)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 305, in 
create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 361, in 
create_script_module_impl
    create_methods_from_stubs(concrete_type, stubs)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 279, in 
create_methods_from_stubs
    concrete_type._create_methods(defs, rcbs, defaults)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 587, in 
compile_unbound_method
    create_methods_from_stubs(concrete_type, (stub,))
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 279, in 
create_methods_from_stubs
    concrete_type._create_methods(defs, rcbs, defaults)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/_recursive.py", line 568, in 
try_compile_fn
    return torch.jit.script(fn, _rcb=rcb)
  File "/home/marco/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1290, in 
script
    fn = torch._C._jit_script_compile(qualified_name, ast, _rcb, get_default_args(obj))
RuntimeError: 
Unknown type name 'np.ndarray':
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/utils
/detect_face.py", line 12
def detect_face(imgs, minsize, pnet, rnet, onet, threshold, factor, device):
    if isinstance(imgs, (np.ndarray, torch.Tensor)):
                         ~~~~~~~~~~ <--- HERE
        imgs = torch.as_tensor(imgs, device=device)
        if len(imgs.shape) == 3:
'detect_face' is being compiled since it was called from 'MTCNN.detect'
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py", 
line 348

        #with torch.no_grad():
        batch_boxes, batch_points = detect_face(
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...  <--- HERE
            img, self.min_face_size,
            self.pnet, self.rnet, self.onet,
'MTCNN.detect' is being compiled since it was called from 'MTCNN.forward'
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py", 
line 247
        # Detect faces
        #with torch.no_grad():
        batch_boxes, batch_probs = self.detect(img)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        #batch_boxes = batch_boxes.detach()
        #batch_probs = batch_probs.detach()

from facenet-pytorch.

timesler avatar timesler commented on May 17, 2024

I doubt there's an easy work around for the above error.

The MTCNN module is designed for ease-of-use directly on images represented in either PIL or numpy format. For that reason, the module does not take torch tensors as inputs (it may do that soon, but the numpy and PIL functionality will remain, and will continue to prevent torch script exports). Torch script is intended for modules that process only torch tensors and I don't believe any numpy functionality is currently supported.

It will likely take a significant refactor of the code to get it to work with torch script, so I would suggest that your best bet is to take only the parts of the code you need from this project and creating a simplified implementation that suits your needs.

from facenet-pytorch.

marcoippolito avatar marcoippolito commented on May 17, 2024

@timesler
It's sad to hear that.
Anyway, I thank you for your kind help and support

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.