Freeze first basic block of sequential layers (layer 1) in a ResNet model

sampa: <p>Is it possible to freeze only the first basic block of sequential layers (layer 1) in a ResNet model?</p>
<pre><code class="lang-auto">model=models.resnet18(pretrained=True)
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 2)
child_counter = 0
for child in model.children():
    if child_counter &lt; 4:
        print("child ",child_counter," was frozen")
        for param in child.parameters():
            param.requires_grad = False
    elif child_counter == 4:
      children_of_child_counter = 0
      for children_of_child in child.children():
          if children_of_child_counter &lt; 1:
              for param in child.parameters():                
                  param.requires_grad = False
              print('child ', children_of_child_counter, 'of child',child_counter,' was frozen')
          else:
            print('child ', children_of_child_counter, 'of child',child_counter,' was not frozen')
          children_of_child_counter += 1
    else:
      print("child ",child_counter," was not frozen")
    child_counter += 1         
summary(model, (3, 224, 224))
</code></pre>
<p>The print statement is ok. However, in summary, it is shown that the whole Layer1 is freezing.<br>
Please help.<br>
Thanks in advance.</p>

gphilip: <p>What is the code for your <code>summary</code> function?</p>

sampa: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/c/5c4c93fbf79ce24bda613d0223863efe4f67fdbb.png" data-download-href="https://discuss.pytorch.org/uploads/default/5c4c93fbf79ce24bda613d0223863efe4f67fdbb" title="1"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/c/5c4c93fbf79ce24bda613d0223863efe4f67fdbb.png" alt="1" data-base62-sha1="davZGSsNOhLy9mT8wbhVCcjJMvN" width="542" height="500" data-dominant-color="243E53"><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</span><span class="informations">646×595 12 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>
Trainable params: 11,532,008<br>
Non-trainable params: 157,504</p>

gphilip: <p>This doesn’t answer my question.</p>
<p>Is <code>summary</code> a function that you use from some library? If so, could you point me to the documentation for this function?</p>
<p>If <code>summary</code> is defined in your own code, could you share the code for this function?</p>
<p>Without knowing what <code>summary</code> is supposed to print, I cannot make sense from its output.</p>

sampa: <pre><code class="lang-auto">from torchsummary import summary


Summarize the given PyTorch model. Summarized information includes:
    1) Layer names,
    2) input/output shapes,
    3) kernel shape,
    4) # of parameters,
    5) # of operations (Mult-Adds)

Args:
    model (nn.Module):
            PyTorch model to summarize. The model should be fully in either train()
            or eval() mode. If layers are not all in the same mode, running summary
            may have side effects on batchnorm or dropout statistics. If you
            encounter an issue with this, please open a GitHub issue.

    input_data (Sequence of Sizes or Tensors):
            Example input tensor of the model (dtypes inferred from model input).
            - OR -
            Shape of input data as a List/Tuple/torch.Size
            (dtypes must match model input, default is FloatTensors).
            You should NOT include batch size in the tuple.
            - OR -
            If input_data is not provided, no forward pass through the network is
            performed, and the provided model information is limited to layer names.
            Default: None

    batch_dim (int):
            Batch_dimension of input data. If batch_dim is None, the input data
            is assumed to contain the batch dimension.
            WARNING: in a future version, the default will change to None.
            Default: 0

    branching (bool):
            Whether to use the branching layout for the printed output.
            Default: True

    col_names (Iterable[str]):
            Specify which columns to show in the output. Currently supported:
            ("input_size", "output_size", "num_params", "kernel_size", "mult_adds")
            If input_data is not provided, only "num_params" is used.
            Default: ("output_size", "num_params")

    col_width (int):
            Width of each column.
            Default: 25

    depth (int):
            Number of nested layers to traverse (e.g. Sequentials).
            Default: 3

    device (torch.Device):
            Uses this torch device for model and input_data.
            If not specified, uses result of torch.cuda.is_available().
            Default: None

    dtypes (List[torch.dtype]):
            For multiple inputs, specify the size of both inputs, and
            also specify the types of each parameter here.
            Default: None

    verbose (int):
            0 (quiet): No output
            1 (default): Print model summary
            2 (verbose): Show weight and bias layers in full detail
            Default: 1

    *args, **kwargs:
            Other arguments used in `model.forward` function.

Return:
    ModelStatistics object
            See torchsummary/model_statistics.py for more information.
</code></pre>

gphilip: <p>Thank you for the reference.</p>
<p>I tried running the code from your original post, and got the following error:</p>
<p><code>RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 3, 7, 7], but got 3-dimensional input of size [3, 224, 224] instead</code></p>
<p>Since I can’t run the code, I can’t debug it either.</p>