How to use part of the layers of a model

username: nikiguo93
cooked: <p>Hi I want to use the MoCo model. It is like the Resnet. And I don’t need all of the layers. How to quit some of the layers I don’t want. I find some codes regarding quiting layers after fc6 of Alexnet. But I don’t know how to realize that in MoCo model, something like RESNET50.</p>
<p><span class="hashtag">#below</span> cnn is the pretrained alexnet. layers_to_use=‘fc6’.<br>
cnn = get_pretrained_net(pretrained_net).type(dtype)</p>
<p>opt_content = {‘layers’: layers_to_use, ‘what’:‘features’}</p>
<h1>
<a name="remove-the-layers-we-dont-need" class="anchor" href="#remove-the-layers-we-dont-need"></a>Remove the layers we don’t need</h1>
<p>keys = [x for x in cnn._modules.keys()]</p>
<p>max_idx = max(keys.index(x) for x in opt_content[‘layers’].split(’,’))</p>
<p>for k in keys[max_idx+1:]:</p>
<pre><code>cnn._modules.pop(k)   
</code></pre>
<p>print(cnn)</p>

username: rad
cooked: <p>Just to make sure, you want to get a pre-trained ResNet and drop some of its layers? If so, which layers, or based on what condition?</p>

username: nikiguo93
cooked: <p>Hi, there are encoder q and encoder k in the model and each looks like something in the following; If I want to remove the layer after Flatten or Linear, how to realize that? I just want to do feature inversion using the code here: <a href="https://github.com/DmitryUlyanov/deep-image-prior;" rel="noopener nofollow ugc">https://github.com/DmitryUlyanov/deep-image-prior;</a> But I want to replace the pretrained net(alexnet/vgg  with their layers after fc6 removed）with MoCo.<br>
ModelMoCo(<br>
(encoder_q): ModelBase(<br>
(net): Sequential(<br>
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(1): SplitBatchNorm(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(2): ReLU(inplace=True)<br>
(3): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): SplitBatchNorm(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): SplitBatchNorm(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): SplitBatchNorm(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): SplitBatchNorm(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(4): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)<br>
(bn1): SplitBatchNorm(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): SplitBatchNorm(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(downsample): Sequential(<br>
(0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)<br>
(1): SplitBatchNorm(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): SplitBatchNorm(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): SplitBatchNorm(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(5): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)<br>
(bn1): SplitBatchNorm(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): SplitBatchNorm(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(downsample): Sequential(<br>
(0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)<br>
(1): SplitBatchNorm(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): SplitBatchNorm(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): SplitBatchNorm(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(6): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)<br>
(bn1): SplitBatchNorm(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): SplitBatchNorm(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(downsample): Sequential(<br>
(0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)<br>
(1): SplitBatchNorm(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): SplitBatchNorm(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): SplitBatchNorm(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(7): AdaptiveAvgPool2d(output_size=(1, 1))<br>
(8): Flatten(start_dim=1, end_dim=-1)<br>
(9): Linear(in_features=512, out_features=128, bias=True)<br>
)<br>
)</p>