# Alter model layers of VGG and rebuild module structure

**URL:** <https://discuss.pytorch.org/t/alter-model-layers-of-vgg-and-rebuild-module-structure/119181>\
**Category:** Uncategorized\
**Created:** [April 23, 2021, 9:34am UTC](https://discuss.pytorch.org/t/alter-model-layers-of-vgg-and-rebuild-module-structure/119181 "2021-04-23T09:34:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rap1ide](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rap1ide/32/35698_2.png) [@rap1ide](https://discuss.pytorch.org/u/rap1ide)\
**Post date:** [April 23, 2021, 9:34am UTC](https://discuss.pytorch.org/t/alter-model-layers-of-vgg-and-rebuild-module-structure/119181/1 "2021-04-23T09:34:45Z")

</div>

Hey everyone,

I want to optimize a vanilla VGG model after training and delete unnecessary feature maps of BatchNorm2d (BN) and Conv2d (C2D) layers.

To achieve that I first flatten all layers of the vanilla VGG, get all the BN and C2D layers, modify them and put those flattened layers back into a nn.Sequential() as my new model.

This fails because in vanilla VGG model the forward pass looks like:

```auto
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.features(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1) # -> flattening tensor does not happen when using nn.Sequential(), shape mismatch
        x = self.classifier(x)
        return x

```

My question now is:  
How can I maintain the model structure when altering the layers? My algorithm currently relies on two lists of all BN and all C2D layers. Is there something like a unique ID a layer carries inside the model structure so that I can just replace it with something like:

```auto
model.modules['uniqueID'] = altered_layer

```

If there are no uniqueIDs can u imagine a way to somehow build these myself (encoding module structure and decoding it)?

I could just make it work and write my own model with this specific forward pass, but I consider replacing the layers to be more elegant.

Help is appreciated!

Christian

---

<div class="post-metadata">

**Author:** ![rap1ide](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rap1ide/32/35698_2.png) [@rap1ide](https://discuss.pytorch.org/u/rap1ide)\
**Post date:** [April 23, 2021, 1:41pm UTC](https://discuss.pytorch.org/t/alter-model-layers-of-vgg-and-rebuild-module-structure/119181/2 "2021-04-23T13:41:40Z")

</div>

Meanwhile I tinkered some code which works:

```auto
import functools
import torch

```

```auto
def get_module_layers_and_keychains(module):
    """ Returns all layers of a pytorch module and a keychain as identifier.
        e.g.
        ...
        (['features', '0', '5'], nn.ReLU())
        ...
        (['classifier', '0'], nn.BatchNorm2D())
        (['classifier', '1'], nn.Linear())
    """
    flattened = []

    o_dict = module._modules
    for key, child in o_dict.items():
        if not child._modules:
            structure = [([key], child)]
        else:
            structure = get_module_layers_and_structure(child)
            structure = [([key] + keylist, child) for keylist, child in structure]

        flattened.extend(structure)

    return flattened

```

```auto
def replace_module_layer(module, layer_keychain, new_layer):
    """Replaces a layer inside a pytorch module for a given keychain.
       Use get_module_layers_and_keychains() to retrieve valid keychains.
    """
    get_fn = lambda self, key: self._modules[key]
    l_root = functools.reduce(get_fn, layer_keychain[:-1], module)
    l_root._modules[layer_keychain[-1]] = new_layer

```

I am still interested in your thoughts and ideas!

Cheers.
