Loading TorchScript Module : class method not recognized during compilation

username: marcoippolito
cooked: <p>My objective is to use the following class and script, derived from this excellent work: <a href="https://github.com/timesler/facenet-pytorch" rel="nofollow noopener">https://github.com/timesler/facenet-pytorch</a> :</p>
<pre><code>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))

class GetFaceEmbedding(torch.nn.Module):
    def __init__(self):
        super(GetFaceEmbedding, self).__init__()

    @classmethod
    def getFaceEmbedding(self, imagePath):

        device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
        print('Running on device: {}'.format(device))

        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
        )
        resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device)
        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)
        aligned = []
        names = []
        for x, y in loader:
            x_aligned, prob = 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(device)
        embeddings = resnet(aligned).detach().cpu()
        return embeddings
</code></pre>
<p>With python it works fine:</p>
<pre><code>(venv373) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3 .  
/getFaceEmbedding-01.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,
         -6.7093e-02,  3.6181e-02, -2.5933e-02, -6.0015e-02,  2.6653e-02,
          9.4335e-02, -2.9241e-02, -2.8357e-02,  7.2207e-02, -3.7747e-02,
          6.3515e-03, -3.0220e-02, -2.4530e-02,  1.0004e-01,  6.6520e-02,
          ....
          3.2497e-02,  2.3421e-02, -5.3921e-02,  1.9589e-02, -2.8655e-03,
          1.3474e-02, -2.2743e-02,  3.2976e-02, -5.6658e-02,  2.0837e-02,
         -4.7152e-02, -6.5534e-02]])
</code></pre>
<p>Following the indications found here: <a href="https://pytorch.org/tutorials/advanced/cpp_export.html" rel="nofollow noopener">https://pytorch.org/tutorials/advanced/cpp_export.html</a></p>
<p>I added to getFaceEmbedding.py these lines:</p>
<pre><code>my_module = GetFaceEmbedding()
sm = torch.jit.script(my_module)
sm.save("annotated_get_face_embedding.pt")
</code></pre>
<p>I then saved the serialized file:</p>
<pre><code>(venv373) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3 
./getFaceEmbedding.py

-rw-r--r-- 1 marco marco 1,4K mar 19 18:52 annotated_get_face_embedding.pt
</code></pre>
<p>And created this cpp file:</p>
<pre><code>(venv373) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ nano 
</code></pre>
<p>faceEmbedding.cpp  :</p>
<pre><code>#include &lt;torch/script.h&gt;
#include &lt;iostream&gt;
#include &lt;memory&gt;
#include &lt;filesystem&gt;

int main(int argc, const char* argv[]) {
    //if(argc != 3) {
    //    std::cerr &lt;&lt; "usage:usage: faceEmbedding &lt;path-to-exported-script-module&gt; &lt;path-to-image-f 
ile&gt; \n";
    //    return -1;
    //}

  torch::jit::script::Module module;
  try {
      // Deserialize the ScriptModule from a file using torch::jit::load().
      module = torch::jit::load(argv[1]);
      std::filesystem::path imgPath = argv[2];

      // Execute the model and turn its output into a tensor
      at::Tensor output = module.getFaceEmbedding(imgPath).ToTensor();
  }
  catch (const c10::Error&amp; e) {
      std::cerr &lt;&lt; "error loading the model\n";
      return -1;
  }
  std::cout &lt;&lt; "ok\n";
} // end of main() function
</code></pre>
<p>But during the compilation phase I get this error :<br>
"struct torch::jit::script::Module’ has no member named ‘getFaceEmbedding’ "</p>
<pre><code>(venv373) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ mkdir build
(venv373) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ cd build
(venv373) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples/build$ cmake 
-DCMAKE_PREFIX_PATH=/home/marco/PyTorchMatters/libtorch ..
-- The C compiler identification is GNU 9.2.1
-- The CXX compiler identification is GNU 9.2.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Found torch: /home/marco/PyTorchMatters/libtorch/lib/libtorch.so  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/marco/PyTorchMatters/facenet_pytorch/examples/build
(venv373) (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples/build$ cmake --build . 
--config Release
Scanning dependencies of target faceEmbedding
[ 50%] Building CXX object CMakeFiles/faceEmbedding.dir/faceEmbedding.cpp.o
/home/marco/PyTorchMatters/facenet_pytorch/examples/faceEmbedding.cpp: In function ‘int 
main(int, const char**)’:
/home/marco/PyTorchMatters/facenet_pytorch/examples/faceEmbedding.cpp:20:34: error: ‘struct 
torch::jit::script::Module’ has no member named ‘getFaceEmbedding’
   20 |       at::Tensor output = module.getFaceEmbedding(imgPath).ToTensor();
      |                                  ^~~~~~~~~~~~~~~~
CMakeFiles/faceEmbedding.dir/build.make:62: recipe for target 'CMakeFiles/faceEmbedding.dir
/faceEmbedding.cpp.o' failed
make[2]: *** [CMakeFiles/faceEmbedding.dir/faceEmbedding.cpp.o] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/faceEmbedding.dir/all' failed
make[1]: *** [CMakeFiles/faceEmbedding.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
</code></pre>
<p>How to solve the problem?<br>
Looking forward to your kind help.<br>
Marco</p>

username: marcoippolito
cooked: <p>I modified the method as follows:</p>
<pre><code>def forward(self, imagePath):
</code></pre>
<p>Now I get this error:<br>
" torch.jit.frontend.UnsupportedNodeError: DictComp aren’t supported"</p>
<pre><code>  (base) marco@pc01:~/PyTorchMatters/facenet_pytorch/examples$ python3 
  ./getFaceEmbedding.py 
  Traceback (most recent call last):
    File "./getFaceEmbedding.py", line 127, in &lt;module&gt;
      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 
  &lt;listcomp&gt;
      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(imagePath)
          dataset.idx_to_class = {i:c for c, i in dataset.class_to_idx.items()}
                                 ~ &lt;--- HERE
          loader = DataLoader(dataset, collate_fn=collate_fn, num_workers=workers)
</code></pre>
<p>I read here: <a href="https://pytorch.org/tutorials/advanced/cpp_export.html" rel="nofollow noopener">https://pytorch.org/tutorials/advanced/cpp_export.html</a><br>
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 <span class="mention">@torch.jit.ignore</span>”<br>
But I guess that excluding the dictionary comprehension from the serialization, will interfere with the method’s functionality.<br>
What would you suggest me to do?</p>

username: marcoippolito
cooked: <p>I refactored everything as follows:</p>
<pre><code>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")
</code></pre>
<p>With python it works fine:</p>
<pre><code>(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]])
</code></pre>
<p>But when trying to serialize I get this new error:<br>
“torch.jit.frontend.UnsupportedNodeError: with statements aren’t supported”<br>
which I do not understand where does it come from</p>
<pre><code>(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 &lt;module&gt;
    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 
&lt;listcomp&gt;
    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():
        ~~~~ &lt;--- HERE
            batch_boxes, batch_probs = self.detect(img)</code></pre>

username: ptrblck
cooked: <p>The error is apparently raised in <a href="https://github.com/timesler/facenet-pytorch/blob/7c01f26ffb15d1d9f7f86ecc2b8484ec9acb8661/models/mtcnn.py#L246-L247">this line of code</a> from <code>MTCNN</code>.<br>
I’m not familiar with the model implementation, but you could try to remove the <code>with</code> statement or replace it with a decorator.</p>

username: marcoippolito
cooked: <p>As far as I understand this line of code</p>
<pre><code>    with torch.no_grad():
</code></pre>
<p>means “apply to the execution of the next line of code”:</p>
<pre><code>        batch_boxes, batch_probs = self.detect(img)
</code></pre>
<p>“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”.<br>
Is my interpretation correct or am I saying something wrong?</p>
<p>Here: <a href="https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html#gradients" rel="nofollow noopener">https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html#gradients</a> in the code example it is actually used the with statement:</p>
<pre><code>print(x.requires_grad)
print((x ** 2).requires_grad)

with torch.no_grad():
    print((x ** 2).requires_grad)
</code></pre>
<p>Or it used detached to get a new Tensor with the same content that does not require gradients:</p>
<pre><code>print(x.requires_grad)
y = x.detach()
print(y.requires_grad)
print(x.eq(y).all())
</code></pre>
<p>I tried to modify in<br>
/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn.py</p>
<p>the lines of code:</p>
<pre><code>    with torch.no_grad():
    batch_boxes, batch_probs = self.detect(img)
</code></pre>
<p>to:</p>
<pre><code>    #with torch.no_grad():
    batch_boxes, batch_probs = self.detect(img)
    batch_boxes = batch_boxes.detach()
    batch_probs = batch_probs.detach()
</code></pre>
<p>But I get this error message:</p>
<pre><code>(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 &lt;module&gt;
    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 
&lt;listcomp&gt;
    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():
        ~~~~ &lt;--- HERE
            batch_boxes, batch_points = detect_face(
                img, self.min_face_size,
</code></pre>
<p>Python decorators, applying a function to another function, could be of help…but how?<br>
Neither my attempts were correct:</p>
<p>First attempt:</p>
<pre><code>    @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 &lt;module&gt;
    from facenet_pytorch import MTCNN, InceptionResnetV1
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/__init__.py", line 2, in
 &lt;module&gt;
    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
</code></pre>
<p>Second attempt:</p>
<pre><code>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 &lt;module&gt;
    from facenet_pytorch import MTCNN, InceptionResnetV1
  File "/home/marco/anaconda3/lib/python3.7/site-packages/facenet_pytorch/__init__.py", line 2, in 
&lt;module&gt;
    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 &lt;module&gt;
    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
</code></pre>
<p>I’m not a Python expert… so… how would you use the decorator to apply the no_grad() runtime context to detect() method?</p>