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?