Print network architecture in cpp jit

tancl: <p>Hi,</p>
<p>In python,  we could use print (model) to show the network architecture,  how about in cpp?</p>
<p>Say i hv loaded jit model with jit load with the name model, how could i do the similar in cpp.</p>
<p>Thanks,<br>
Rgds,<br>
CL</p>

driazati: <p>We don’t have any built-in utils for this, but you can do a simple version manually:</p>
<pre><code class="lang-cpp">#include &lt;torch/script.h&gt;

#include &lt;iostream&gt;
#include &lt;memory&gt;

void tabs(size_t num) {
  for (size_t i = 0; i &lt; num; i++) {
    std::cout &lt;&lt; "\t";
  }
}

void print_modules(const torch::jit::script::Module&amp; module, size_t level = 0) {
  std::cout &lt;&lt; module.name().qualifiedName() &lt;&lt; " (\n";
  for (const auto&amp; module : module.get_modules()) {
    tabs(level + 1);
    print_modules(module.module, level + 1);
  }
  tabs(level);
  std::cout &lt;&lt; ")\n";
}

int main(int argc, const char *argv[]) {
  torch::jit::script::Module container = torch::jit::load("m.pt");
  print_modules(container);
  return 0;
}
</code></pre>

tancl: <p>Hi Driazati,</p>
<p>Thanks for the codes, it helps. just some typo i think, the</p>
<p><code>print_modules(module.module, level + 1);</code></p>
<p>should be</p>
<pre><code class="lang-auto">print_modules(module, level + 1);
</code></pre>
<p>Appreciate your help. The references for these seems to be not much, could you share some good reference for this ? For e.g., the method (or the var) that can return the input size of a pre-trained network etc.</p>
<p>Thanks again.</p>
<p>Regards,<br>
CL</p>

driazati: <p>You can find the generated docs here <a href="https://pytorch.org/cppdocs/api/namespace_torch__jit.html#namespace-torch-jit" rel="nofollow noopener">https://pytorch.org/cppdocs/api/namespace_torch__jit.html#namespace-torch-jit</a>. Unfortunately they are pretty sparse, we have ongoing efforts to improve them.</p>
<p>As for getting the input size a network expects, we don’t have any utilities for that since TorchScript models aren’t specialized to specific shapes.</p>

tancl: <p>hi,</p>
<p>thanks again for the reply and link. yes i’ve visited the link before, might be too sparse for non hardcore programmer. anyway, thanks for your reply again.</p>
<p>rgds,<br>
CL</p>

tancl: <p>Hi,</p>
<p>Hope you don’t mind, I created another topic to get more details from the jit model. I understand that there are no shape properties for the input, I was wondering whether we could print the each layers details as stated in :</p>
<aside class="quote quote-modified" data-post="1" data-topic="60465">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/9fc348/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/model-summary-for-libtorch-jit-model/60465">Model Summary for libTorch jit model?</a> <a class="badge-category__wrapper " href="/c/jit/13"><span data-category-id="13" data-drop-close="true" class="badge-category " title="A category for TorchScript and the PyTorch JIT compiler"><span class="badge-category__name">jit</span></span></a>
  </div>
  <blockquote>
    Hi, 
Is it possible to get the model summary described in this link 
 
 
, but for cpp jit model? 
This is an enhancement for printing the model in this link; 
 
 
Thanks. 
rgds, 
CL
  </blockquote>
</aside>


tancl: <p>hi,</p>
<p>adding these lines</p>
<pre><code class="lang-auto">  for (const auto&amp; parameter : module.get_parameters()) {
	tabs(level + 1);
	std::cout &lt;&lt; parameter.name() &lt;&lt; '\t';
	std::cout &lt;&lt; parameter.value().toTensor().sizes() &lt;&lt; '\n';
  } 

</code></pre>
<p>shall give the summary for the network.</p>
<p>if anyone having better way to show the network summary for jit model in C++, please feel free to share.</p>
<p>Thanks.</p>
<p>rgds,<br>
CL</p>

Bryan_Wang: <p>does anybody know how to do this using libtorch (iOs)? <code>module.get_parameters()</code> is not defined.</p>