Calling some layers of a pretrained model in Pytorchvideo library

username: Tm4
cooked: <p>I am using <a href="https://github.com/facebookresearch/pytorchvideo/blob/main/pytorchvideo/models/r2plus1d.py" rel="noopener nofollow ugc">Pytorchvideo</a> library for using the pretrained <strong>r2plus1d</strong> model. My goal is to extract features from different layers of this model. For this aim I extracted the features from different stages (I call the extracted features as stage_1, stage_2, stage_3 in the script):</p>
<h1><a name="create-each-stage-for-r21d-1" class="anchor" href="#create-each-stage-for-r21d-1"></a>Create each stage for R(2+1)D.</h1>
<pre><code>for idx in range(len(stage_depths)):
    stage_dim_inner = stage_dim_out // 4
    depth = stage_depths[idx]

    stage_conv_b_stride = (
        stage_temporal_stride[idx],
        stage_spatial_stride[idx],
        stage_spatial_stride[idx],
    )

    stage = create_res_stage(
        depth=depth,
        dim_in=stage_dim_in,
        dim_inner=stage_dim_inner,
        dim_out=stage_dim_out,
        bottleneck=stage_bottleneck[idx],
        conv_a_kernel_size=stage_conv_a_kernel_size[idx],
        conv_a_stride=[1, 1, 1],
        conv_a_padding=[size // 2 for size in stage_conv_a_kernel_size[idx]],
        conv_b_kernel_size=stage_conv_b_kernel_size[idx],
        conv_b_stride=stage_conv_b_stride,
        conv_b_padding=[size // 2 for size in stage_conv_b_kernel_size[idx]],
        conv_b_num_groups=stage_conv_b_num_groups[idx],
        conv_b_dilation=stage_conv_b_dilation[idx],
        norm=norm,
        activation=activation,
    )

    blocks.append(stage)

    if idx == 0:
        stage_1 = Net(blocks=nn.ModuleList(blocks))
        

        
    if idx == 1:
        stage_2 = Net(blocks=nn.ModuleList(blocks))
        

       
       
     
    if idx == 2:
        stage_3 = Net(blocks=nn.ModuleList(blocks))

    
    
    blocks.append(stage)
    
    stage_dim_in = stage_dim_out
    stage_dim_out = stage_dim_out * 2

return stage_1, stage_2, stage_3
</code></pre>
<p>The problem is that I face an error expressing that<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/2/9/299a7b65fa442e3e12a6c6bc740ed42d82b75aa6.png" alt="image" data-base62-sha1="5W2yfOffTULU55yd9se9hxuSTRA" width="292" height="37"></p>
<p>How can I call the extracted layers?</p>
<blockquote>
<p>Update<br>
class Net(nn.Module):<br>
“”"<br>
Build a general Net models with a list of blocks for video recognition.</p>
</blockquote>
<pre><code>::

                                     Input
                                       ↓
                                     Block 1
                                       ↓
                                       .
                                       .
                                       .
                                       ↓
                                     Block N
                                       ↓

The ResNet builder can be found in `create_resnet`.
"""

def __init__(self, *, blocks: nn.ModuleList) -&gt; None:
    """
    Args:
        blocks (torch.nn.module_list): the list of block modules.
    """
    super().__init__()
    assert blocks is not None
    self.blocks = blocks
    init_net_weights(self)

def forward(self, x: torch.Tensor) -&gt; torch.Tensor:
    for idx in range(len(self.blocks)):
        x = self.blocks[idx](x)
    return x
</code></pre>

username: suraj.pt
cooked: <p>From the error message, it looks like you’re treating some tuple as a function, but it’s difficult to debug this without knowing more about which line was throwing the error.</p>
<p>Check out <a href="https://pytorch.org/vision/stable/feature_extraction.html#torchvision.models.feature_extraction.create_feature_extractor"><code>create_feature_extractor</code></a> that does precisely what you need.  Here’s a <a href="https://pytorch.org/blog/FX-feature-extraction-torchvision">blog post</a> if you want to learn more about feature extraction best practices. Hope this helps!</p>

username: Tm4
cooked: <p>Thanks for your answer. In fact, if I only call one of the stage_1, stage_2 or stage_3 it is working but when call three of them (using ''return stage_1, stage_2, stage_3 ‘’) it faces with error. Seemingly, the problem is related to the ‘Net’ class. I updated the question with adding the ‘Net’ function.</p>