Trying to use jit.trace, results in 'AttributeError: 'FaceAlignment' object has no attribute '__name__' error

Shisho_Sama: <p>Hello everyone,<br>
I’m trying to have weight caching , but on one of my models it fails . for caching I’m simply running this snippet :</p>
<pre><code class="lang-python">fname = 'cached_weights_s3fd.pt'
test_imgs = Detector.gen_fake_img(batch=1)
traced = torch.jit.trace(self.detector, test_imgs, check_trace=False)
script = traced.save(fname)
self.detector = torch.jit.load(fname)
</code></pre>
<p>and the full stacktrace is as follows:</p>
<pre><code class="lang-python">Traceback (most recent call last):
  File "d:\Codes\Pytorch_Retinaface\detection_core\main_detector.py", line 2074, in &lt;module&gt;
    run_test()
  File "d:\Codes\Pytorch_Retinaface\detection_core\main_detector.py", line 2065, in run_test
    run_capture_sfd()
  File "d:\Codes\Pytorch_Retinaface\detection_core\main_detector.py", line 2002, in run_capture_sfd
    after_detection_fn=None)
  File "Pytorch\detection_core\main_detector.py", line 1523, in __init__
    self._init()
  File "Pytorch\detection_core\main_detector.py", line 1536, in _init
    traced = torch.jit.trace(self.detector, test_imgs, check_trace=False)
  File "C:\Users\User\Anaconda3\lib\site-packages\torch\jit\__init__.py", line 911, in trace
    name = _qualified_name(func)
  File "C:\Users\User\Anaconda3\lib\site-packages\torch\_jit_internal.py", line 683, in _qualified_name
    name = obj.__name__
AttributeError: 'FaceAlignment' object has no attribute '__name__'

</code></pre>
<p>Whats wrong?<br>
As far as  I know all classes has <code>__name__</code> attribute, including the mentioned class in the error.(the name is ‘FaceAlignment’ obviously)<br>
So I’m not sure why I’m getting this. Any help is greatly appreciated.</p>

Shisho_Sama: <p>Upon further investigations, I turns out in <code>\torch\_jit_internal.py", line 684,</code> the object that is causing the error is</p>
<pre><code class="lang-python">Object is type: &lt;class 'function'&gt; and the object itself is : &lt;function LSTM.forward at 0x00000214C495D048&gt;
</code></pre>
<p>The Irony is, I do not have any LSTM cell in my model!<br>
What am I seeing this?</p>

driazati: <p>Thanks for doing some investigation! Are you able to share the binary for <code>cached_weights_s3fd.pt</code> so we can try to debug this on our end?</p>
<p>I think what you’re seeing here is some initialization the jit does when you first call <code>torch.jit.script</code> or <code>torch.jit.trace</code>. We have to grab the qualified names for a few modules that have overloaded forward methods (only <code>nn.LSTM</code> and <code>nn.GRU</code>), which is what you’re seeing. You could try setting a breakpoint and skipping past these calls to get to the time it’s actually invoked with your class.</p>

Shisho_Sama: <aside class="quote no-group quote-modified" data-username="Shisho_Sama" data-post="1" data-topic="70122">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shisho_sama/48/6926_2.png" class="avatar"> Shisho_Sama:</div>
<blockquote>
<p>fname = ‘cached_weights_s3fd.pt’ test_imgs = Detector.gen_fake_img(batch=1) traced = torch.jit.trace(self.detector, test_imgs, check_trace=False) script = traced.save(fname) self.detector = torch.jit.load(fname)</p>
</blockquote>
</aside>
<p>The cached weights are not generated, it fails before that.<br>
You can replicate  the issue by cloning this repo : <a href="https://github.com/1adrianb/face-alignment" class="inline-onebox" rel="noopener nofollow ugc">GitHub - 1adrianb/face-alignment: 🔥 2D and 3D Face alignment library build using pytorch</a><br>
and running this snippet :</p>
<pre data-code-wrap="python"><code class="lang-python">import face_alignment
import torch
import torchvision as tv

def gen_fake_img(samples=1000, batch=10, image_size=(3, 224, 224), num_classes=2):
        fake_dt = tv.datasets.FakeData(samples,
                             image_size, num_classes,
                             tv.transforms.ToTensor())
        dt_ldr = torch.utils.data.DataLoader(fake_dt, batch, pin_memory=True)
        sample_imgs, _ = next(iter(dt_ldr))
        return sample_imgs

detector = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D, device='cpu')

fname = 'cached_weights_s3fd.pt'
test_imgs = gen_fake_img(batch=1)
traced = torch.jit.trace(detector, test_imgs, check_trace=False)
script = traced.save(fname)
detector = torch.jit.load(fname)

fa.get_landmarks('test/assets/aflw-test.jpg')

</code></pre>

Shisho_Sama: <p>Found the casue, I was sending the wrong model!<br>
its all fixed and fine now!</p>

asgardian: <p>Hi <a class="mention" href="/u/shisho_sama">@Shisho_Sama</a> ,<br>
I am also having the same error.<br>
I used the same github repo.</p>
<p>What changes did you make in the code to make it up and running<br>
?</p>
<p>I used the below code.</p>
<p>import torch<br>
import torchvision<br>
import face_alignment</p>
<h1>
<a name="optionally-set-detector-and-some-additional-detector-parameters-1" class="anchor" href="#optionally-set-detector-and-some-additional-detector-parameters-1"></a>Optionally set detector and some additional detector parameters</h1>
<p>face_detector = ‘sfd’<br>
face_detector_kwargs = {<br>
“filter_threshold” : 0.8<br>
}</p>
<p>model = face_alignment.FaceAlignment(face_alignment.LandmarksType._3D, device=‘cpu’, flip_input=True,face_detector=face_detector)</p>
<p>example = io.imread(’/test/assets/aflw-test.jpg’)<br>
traced_script_module = torch.jit.trace(model,example)<br>
traced_script_module.save(“traced_facealignment_model.pt”)</p>