How should I extract feature from resnet 50 Pytorch pre-trained model for Deep Learning Coloring?

username: JonathanSum
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/c/acd8bd3247ec45b9018562929c5623a1c73cc7bf.png" data-download-href="https://discuss.pytorch.org/uploads/default/acd8bd3247ec45b9018562929c5623a1c73cc7bf" title="1_kBlZtheCjJiA3F1e0IurCw"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/c/acd8bd3247ec45b9018562929c5623a1c73cc7bf_2_210x500.png" alt="1_kBlZtheCjJiA3F1e0IurCw" data-base62-sha1="oF4u8TYgThlGNKKS3abqLzCKBwr" width="210" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/c/acd8bd3247ec45b9018562929c5623a1c73cc7bf_2_210x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/c/acd8bd3247ec45b9018562929c5623a1c73cc7bf_2_315x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/c/acd8bd3247ec45b9018562929c5623a1c73cc7bf_2_420x1000.png 2x" data-dominant-color="ECEAEA"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">1_kBlZtheCjJiA3F1e0IurCw</span><span class="informations">512×1218 429 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><strong>We usually extract the feature just the relu layer before the pooling layer in vgg19, but which layer I should pick for extracting the feature from Resnet 50?</strong> Maybe if you can give me some suggestions on how I should do it in code and picking layer in Resnet50? I am a newbie who is still keeping Deep learning in Pytorch in 2 months.<br>
<strong>Pytorch VGG19 allows to access with index for extracting the feature layer, but Resnet does not.</strong> I can try using for loop, but I am not sure it will work or not.</p>
<p>Here is the project that I want to extract the feature to redraw, but it is not working great that I just use 3 layers out of 5 relu layers in vgg19. I am planning to train it again in Resnet50 on Colab.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/f/2fe17c17391812b2ebf07df8fc52402228362cde.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/2fe17c17391812b2ebf07df8fc52402228362cde" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/f/2fe17c17391812b2ebf07df8fc52402228362cde_2_690x250.jpeg" alt="image" data-base62-sha1="6Pzxtr26Lfn35sf73TQnlv0n8iW" width="690" height="250" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/f/2fe17c17391812b2ebf07df8fc52402228362cde_2_690x250.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/2/f/2fe17c17391812b2ebf07df8fc52402228362cde.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/f/2fe17c17391812b2ebf07df8fc52402228362cde.jpeg 2x" data-dominant-color="C3B2C2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">851×309 86.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>You can print the layers of your resnet using <code>print(model)</code>.<br>
The output shows you how the submodules are defined.<br>
Since these blocks are not defined by a main <code>nn.Sequential</code> container, you would have to use the attribute names to index specific layers, e.g.:</p>
<pre><code class="lang-python">model.layer4[1].conv3.register_forward_hook
</code></pre>
<p>This line would index the block <code>layer4</code> (which is an <code>nn.Sequential</code> container), the second <code>Bottleneck</code> and the <code>conv3</code> layer inside it.<br>
Let me know, if this would work for you.</p>

username: bioinfo-dirty-jobs
cooked: <p>I want to extract this deep features in the fully connected:</p>
<pre><code class="lang-auto">layer=model._modules.get('avgpool')
handle = model._modules.get('avgpool').register_forward_hook
</code></pre>
<p>Any help on how to write  out</p>

username: ptrblck
cooked: <p>You are trying to register a hook for the <code>avgpool</code> layer, not a fully connected one.<br>
Also, you could access the layer directly via <code>model.avgpool</code>.</p>
<p>What is not working or where are you stuck?</p>

username: JonathanSum
cooked: <p>I used Resnet 50 for a quick toy model to achieve quick result:<br>
Because I trained from nothing, the model can only color a few objects. But I will spend a little bit more time training next month. To answer your question, I suggest you use <a href="https://discuss.pytorch.org/u/ptrblck">ptrblck</a>’s way from above.</p>
<p><strong>If you have more question, I think a nice guy, Ptrblck, and others will help you too. If you need some help from me, please feel free to shoot me any questions. If you need my code and data to redo the examples from below, please email me. I don’t have Physical GPU, but I trained it on Colab</strong></p>
<p>Input Data----&gt; Machine Coloring---------&gt; correct color from original images:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/6/c6dff680c43d89735c10500f69712fcb062036a2.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/c6dff680c43d89735c10500f69712fcb062036a2" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/6/c6dff680c43d89735c10500f69712fcb062036a2_2_562x500.jpeg" alt="image" data-base62-sha1="snknoVTiaMCIqfu9H13niXLsOtA" width="562" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/6/c6dff680c43d89735c10500f69712fcb062036a2_2_562x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/6/c6dff680c43d89735c10500f69712fcb062036a2_2_843x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/6/c6dff680c43d89735c10500f69712fcb062036a2.jpeg 2x" data-dominant-color="8E898C"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">861×765 175 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/f/5f17632e4f78d41bda5d178ff2ed830a55eacad6.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/5f17632e4f78d41bda5d178ff2ed830a55eacad6" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/f/5f17632e4f78d41bda5d178ff2ed830a55eacad6_2_583x500.jpeg" alt="image" data-base62-sha1="dzdsXe3m58DsNr9CGF7eFCYrSzs" width="583" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/f/5f17632e4f78d41bda5d178ff2ed830a55eacad6_2_583x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/5/f/5f17632e4f78d41bda5d178ff2ed830a55eacad6.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/f/5f17632e4f78d41bda5d178ff2ed830a55eacad6.jpeg 2x" data-dominant-color="B2ACB3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">841×721 175 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
I may post the code next month after improvement, and so far my repo only has the output. In fact, there are many coloring examples on Github from other people:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/JonathanSum/Deep_learning_Coloring-Anime-image-and-satellite-image-house-damge-level-colorized">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/JonathanSum/Deep_learning_Coloring-Anime-image-and-satellite-image-house-damge-level-colorized" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/344;"><img src="https://opengraph.githubassets.com/69ad0fbddb901dbe34920363874bf19ff18654289c2951cb10e2d546101de821/JonathanSum/Deep_learning_Coloring-Anime-image-and-satellite-image-house-damge-level-colorized" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/JonathanSum/Deep_learning_Coloring-Anime-image-and-satellite-image-house-damge-level-colorized" target="_blank" rel="noopener nofollow ugc">GitHub -...</a></h3>

  <p>Contribute to JonathanSum/Deep_learning_Coloring-Anime-image-and-satellite-image-house-damge-level-colorized development by creating an account on GitHub.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>