Using HourGlass Network pretrained on human pose in a similar scenario such as calling models.resnet18()

Mona_Jalal: <p>Could you please guide me how to use an HourGlass Network here that is pretrained on human pose instead of this ResNet18?</p>
<pre><code class="lang-auto">num_classes = 4 * 2 #4 coordinates X and Y flattened --&gt; 4 of 2D keypoints or landmarks

class Network(nn.Module):
    def __init__(self,num_classes=8):
        super().__init__()
        self.model_name = 'resnet18'
        self.model = models.resnet18()
        self.model.fc = nn.Linear(self.model.fc.in_features, num_classes)
        
    def forward(self, x):
        x = x.float()
        out = self.model(x)
        return out
</code></pre>
<p>For example, I found this Stacked Hourglass code for facial landmark detection which is a similar problem. How can I use it in my own PyTorch code such as I am calling models.resnet18()? Also, are there more standard implementations of Stacked hourglass network since it is a pretty much famous network (or any other baseline for human pose estimation with pretraining enabled)?<br>
</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/raymon-tian/hourglass-facekeypoints-detection/blob/master/models.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/raymon-tian/hourglass-facekeypoints-detection/blob/master/models.py" target="_blank" rel="noopener nofollow ugc">raymon-tian/hourglass-facekeypoints-detection/blob/master/models.py</a></h4>
<pre><code class="lang-py">#coding=utf-8
import torch
import torch.nn as nn
from torch.nn import Upsample
from torch.autograd import Variable

class HourGlass(nn.Module):
    """不改变特征图的高宽"""
    def __init__(self,n=4,f=128):
        """
        :param n: hourglass模块的层级数目
        :param f: hourglass模块中的特征图数量
        :return:
        """
        super(HourGlass,self).__init__()
        self._n = n
        self._f = f
        self._init_layers(self._n,self._f)

    def _init_layers(self,n,f):
</code></pre>

  This file has been truncated. <a href="https://github.com/raymon-tian/hourglass-facekeypoints-detection/blob/master/models.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>Here’s another Hourglass code I found in PyTorch <a href="https://github.com/bearpaw/pytorch-pose/blob/master/pose/models/hourglass.py" rel="noopener nofollow ugc">https://github.com/bearpaw/pytorch-pose/blob/master/pose/models/hourglass.py</a></p>

Nikronic: <p>Hi,</p>
<aside class="quote no-group" data-username="Mona_Jalal" data-post="1" data-topic="98629">
<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/mona_jalal/48/25715_2.png" class="avatar"> Mona_Jalal:</div>
<blockquote>
<p><code>self.model = models.resnet18()</code></p>
</blockquote>
</aside>
<p>You just need to instantiate the desired model for instance <code>self.model = HourGlass(args)</code>. Also, you need to load weights and state dict which you can find it <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html#saving-loading-model-for-inference" rel="noopener nofollow ugc">here</a>. (note that do not set model in <code>eval</code> mode)</p>
<p>But as you might know, in the following line,</p>
<pre><code class="lang-auto">        self.model.fc = nn.Linear(self.model.fc.in_features, num_classes)

</code></pre>
<p>the author replaces last linear layer to match their desired number of output classes. In your case, you need to find model definition (you can use <a href="https://discuss.pytorch.org/t/how-to-add-dropout-to-a-squeezenet/83273/2">this thread</a>), and the change the appropriate layer based on the model definition.</p>
<p>Unfortunately I don’t know any implemenation of <code>HourGlass</code>.</p>
<p>Let me know if you had any troubles.</p>
<p>Bests</p>