How can l use the pre-trained Resnet to extract feautres from my own dataset?

username: DeepLearner17
cooked: <p>Hello,<br>
l want to extract features of my own dataset from the last hidden layer of ResNet (before softmax).<br>
l defined the following :<br>
import torchvision.models as models</p>
<pre><code>resnet152 = models.resnet152(pretrained=True,requires_grad=False)
modules=list(resnet152.children()[:-1])
resnet152=nn.Sequential(*modules)  
</code></pre>
<p>Is it correct ?<br>
What’s next ?</p>
<p>Thank you</p>

username: jytug
cooked: <p>Have you run the code? There are several mistakes. First, <code>resnet152.children()</code> is a generator, and hence is not subscriptable. Also, <code>resnet152</code> doesn’t take a <code>requires_grad</code> argument. The following code will work:</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torchvision.models as models
from torch.autograd import Variable
resnet152 = models.resnet152(pretrained=True)
modules=list(resnet152.children())[:-1]
resnet152=nn.Sequential(*modules)
for p in resnet152.parameters():
    p.requires_grad = False
</code></pre>
<p>Of course, the outcome depends on what you want to achieve. This code returns a model consisting of all layers of <code>resnet152</code> bar the last one (a fully connected layer), with fixed parameters</p>

username: DeepLearner17
cooked: <p>Thank you for the clarification.<br>
Actually, my question is that l would like to use this pre-trained ResNet to extract features (from last hidden layer) from a new dataset (my own dataset). Let’s say UCF-sport, HMDB …</p>
<p>Hope my question is more clear.</p>
<p>Thank you</p>

username: jytug
cooked: <p>Yes, then this is exactly the way to go. You can see more in the tutorial:<br>
<a href="http://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html#sphx-glr-beginner-transfer-learning-tutorial-py" class="onebox" target="_blank" rel="nofollow noopener">http://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html#sphx-glr-beginner-transfer-learning-tutorial-py</a></p>

username: DeepLearner17
cooked: <p><a class="mention" href="/u/jytug">@jytug</a> , l’m l don’t need neither transfer learning nor retraining the last hidden layer. l just want to use resnet to get a representation of a given input from the last hidden layer<br>
My purpose is as follow :<br>
Given a new input data and the pre-trained Resnet :<br>
Get the features of that input  from the last hidden layer (before softmax) of Resnet.</p>
<p>Hope it helps</p>

username: SimonW
cooked: <p>What’s wrong with <a class="mention" href="/u/jytug">@jytug</a>’s code? It seems correct and completely aligned with what you are trying to achieve.</p>

username: Federico_Pala
cooked: <p>Maybe I got your point. Once you get the iytug resnet152 variable, you have to get one image:</p>
<p>img = torch.Tensor(3, 224, 224).normal_() # random image<br>
img_var = Variable(img) # assign it to a variable<br>
features_var = resnet152(img_var) # get the output from the last hidden layer of the pretrained resnet<br>
features = features_var.data # get the tensor out of the variable</p>

username: DeepLearner17
cooked: <p>Thank you a lot <a class="mention" href="/u/federico_pala">@Federico_Pala</a> this is what l’m lookng for.<br>
I have a question for you :<br>
does the image should be of that shape and dimension (3,224,224)  ?<br>
Is it the required dimension input of the image for the ResNet ?</p>
<p>Thank you</p>

username: Federico_Pala
cooked: <p>Yes, the network has been trained with color images of size 224x224. If your images are for instance grayscale you have  to copy the single channel three times. If your images are of a different size, you can resize them or get a 224x224 crop out of them. Keep in mind that you can get your features faster by creating a big tensor with let’s say 100 images (shape 100x3x224x224) and process them in a single round.</p>

username: DeepLearner17
cooked: <p>Thank you. It works. it’s a little bit slow sow l made 50 images by tensor</p>

username: Jaideep_Valani
cooked: <p>hi jytug<br>
I tried to above but then i face the issues while loading the weight<br>
Could u help in overcomin the issues<br>
here is the link</p>
<aside class="quote quote-modified" data-post="10" data-topic="33591">
  <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/jaideep_valani/48/8482_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/unable-to-add-deconv-layers-kernel-dies/33591/10">Unable to add deconv Layers-Kernel dies</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    Model summary with listed number format.The end of the model. 
(124): AvgPool2d(kernel_size=7, stride=1, padding=0) 
(125): Conv2d(1024, 1024, kernel_size=(1, 1), stride=(1, 1)) 
(126): ConvTranspose2d(2048, 1024, kernel_size=(2, 2), stride=(2, 2)) 
(127): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 
(128): Linear(in_features=2048, out_features=28, bias=True) 
Layerwise : 
(2): Bottleneck( 
(conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=F…
  </blockquote>
</aside>

<p>Look at the latest topic…</p>

username: Ben_Drewry
cooked: <p>after looking at a few other blogs, this seemed to work for me</p>
<aside class="quote quote-modified" data-post="1" data-topic="30153">
  <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">
    <a href="https://discuss.pytorch.org/t/runtimeerror-given-groups-1-weight-of-size-64-3-7-7-expected-input-3-1-224-224-to-have-3-channels-but-got-1-channels-instead/30153">RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[3, 1, 224, 224] to have 3 channels, but got 1 channels instead</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    I see a couple of such posts in forum but I have hardtime generalizing it to my own problem. Here’s the error: 
torch.Size([3, 1, 224, 224])
Traceback (most recent call last):
  File "test_loocv.py", line 245, in &lt;module&gt;
    output = model_ft(test_data)
  File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torchvis…
  </blockquote>
</aside>
<aside class="quote" data-post="65" data-topic="119">
  <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/fmassa/48/58_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-extract-features-of-an-image-from-a-trained-model/119/65">How to extract features of an image from a trained model</a> 
  </div>
  <blockquote>
    resnet18 doesn’t have a classifier field, and the solution I gave was for vgg.
  </blockquote>
</aside>

<p>new_classifier = nn.Sequential(*list(loaded_model.children())[:-1])<br>
model = new_classifier<br>
outputs = model(input_image.unsqueeze_(0).cuda())<br>
outputs = outputs.view(-1)</p>

username: Shubhankar
cooked: <p>But in this feature extractor tutorial why do you need to reinitialize last layer when for feature extractor we don’t need it in the first place?</p>

username: D_B
cooked: <p>I tried using the following model for feature extraction instead, which would maintain the original layer connection from the original Resnet, and got slightly better training accuracy, but results were almost the same:</p>
<pre><code class="lang-python">import torch.nn as nn
import torchvision.models as models

resnet = models.resnet152(pretrained=True)
num_ftrs_resnet = resnet.fc.in_features
for param in resnet.parameters():
    param.requires_grad = False
resnet.fc = nn.Flatten()
</code></pre>

username: sparshgarg23
cooked: <p>Hi,I have a question regarding image size,it seems that the resnet family can only work on 224x224 images,are there other pretrained models which can work on larger image dimensions such as 512x512 or even larger 1024.</p>