How to extract features of an image from a trained model


(Fanta) #85

What if the model was not build with nn.Sequential? How can I extract a part of it? For instance, I have derived a class from nn.Module and implemented its forward() method as

    def forward(self, x):
        x = F.conv2d(input=x, weight=self.conv1_w, bias=self.conv1_b, padding=self.conv1_pad)
        x = F.max_pool2d(F.relu(x), 2)
        x = F.conv2d(input=x, weight=self.conv2_w, bias=self.conv2_b, padding=self.conv2_pad)
        x = F.max_pool2d(F.relu(x), 2)
        x = x.view(x.size(0), -1)  # Flatten but keep the same batch size
        x = F.linear(input=x, weight=self.fc1_w, bias=self.fc1_b)
        x = F.relu(x)
        x = F.linear(input=x, weight=self.fc2_w, bias=self.fc2_b)
        return x

Can I extract the model until the second convolutional layer, to see its output? That is, without doing copy-and-paste of the code.


(Pan He) #86

Is there any update for iterating over modules with names?


(Woaichipingguo Tsh) #87

I have an error . My code is :

class FeatureExtractor(nn.Module):
    def __init__(self, submodule, extracted_layers):
        super(FeatureExtractor,self).__init__()
        self.submodule = submodule
        self.extracted_layers= extracted_layers

    def forward(self, x):
        outputs = []
        for name, module in self.submodule._modules.items():
            x = module(x)
            print(name)
            if name in self.extracted_layers:
                outputs.append(x)
        return outputs
myresnet=resnet18(pretrained=False)
num_ftrs=myresnet.fc.in_features
myresnet.fc=nn.Linear(num_ftrs,10)
exact_list=["conv1","layer1","avgpool"]
myexactor=FeatureExtractor(myresnet,exact_list)
a=torch.randn(1,3,32,32)
a=Variable(a)
x=myexactor(a)
```RuntimeError: size mismatch, m1: [512 x 1], m2: [512 x 10] at c:\miniconda2\conda-bld\pytorch-cpu_1519449358620\work\torch\lib\th\generic/THTensorMath.c:1434

type or paste code here


(Woaichipingguo Tsh) #88

sorry. I forgot x.view().


#89

can anyone please tell me what happens in the line above?


#91

Is there anyone who could figure it out and show it with examples? any blog or website or tutorial? anything?


#92

can someone please explain to me what is happening here and what this class Net is donig ??

If we are interested in using the feature part, why cannot we just keep the features and get rid of classifier?


(Ganesh Venkatesh) #93

I am not sure I understand why the FeatureExtractor function by @woaichipingguo_TSH works for Resnet network that has residual connections and not just a sequential list of convolutions. It is not always the case the output of current layer is the input to the next one because of the residual connections. How does it handle that?


(Ravi Kumar) #94

Hi, could you kindly point out, where to put x.view() in the above FeatureExtractor to avoid the mismatch error and the dimensions?
I am still a beginner to pytorch and I am trying to learn to perform feature extraction using pretrained models.


(highhig) #95

I’d like to know the result of calling forward (x) in FeatureExtractor (submodule, extracted_layers)


(highhig) #96

I want to know that Net().forward(x) is called, where x is the image, and then what is returned is the feature result that we want to extract from the layer, and this result exists as a list?So what do we say if we extract the results of each layer separately?Could you please write out the detailed process for me to see?thanks