# Converting keras model to pytorch

**URL:** <https://discuss.pytorch.org/t/converting-keras-model-to-pytorch/118585>\
**Category:** Uncategorized\
**Created:** [April 18, 2021, 12:39pm UTC](https://discuss.pytorch.org/t/converting-keras-model-to-pytorch/118585 "2021-04-18T12:39:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ananthceh19](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@ananthceh19](https://discuss.pytorch.org/u/ananthceh19)\
**Post date:** [April 18, 2021, 12:39pm UTC](https://discuss.pytorch.org/t/converting-keras-model-to-pytorch/118585/1 "2021-04-18T12:39:32Z")

</div>

Hi,  
My Keras model looks something like this:  
def **init** (self, in\_channels=1):

```
    super(CNN, self). __init__ ()
    self.base_model = ResNet50(weights='imagenet', include_top=False)
    self.NUM_CLASSES = 22

    self.DENSE_LAYER_ACTIVATION = 'softmax'

    for layer in self.base_model.layers:
        layer.trainable = True
    #layer_name = 'conv1_relu'
    self.layer_name = 'conv3_block4_2_relu'
    #layer_name = 'conv2_block2_1_relu'
    self.intermediate_layer_model = Model(inputs=self.base_model.input,
                                    outputs=self.base_model.get_layer(self.layer_name).output)

    #w = self.intermediate_layer_model.output
    self.GAP = GlobalAveragePooling2D()
    self.Dropout = Dropout(rate=0.2)

    self.Dense1 = Dense(256, activation = 'relu')
    self.Dense2 = Dense(128, activation = 'relu')
    self.predictions = Dense(self.NUM_CLASSES,
            activation = self.DENSE_LAYER_ACTIVATION)

def forward(self, x):
    x=self.base_model.get_layer(self.layer_name)(x)
    print(x.shape)
    x=self.GAP(x)
    x=self.Dropout(x)
    x=self.Dense1(x)
    x=self.Dropout(x)
    x=self.Dense2(x)
    x=self.Dropout(x)
    x=self.predictions(x)

    return x

```

I want to convert this to pytorch. Any help would be greatly appreciated.  
Thanks.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 19, 2021, 7:46am UTC](https://discuss.pytorch.org/t/converting-keras-model-to-pytorch/118585/2 "2021-04-19T07:46:01Z")

</div>

Where are you currently stuck in the conversion?  
Based on the provided code I would assume that changing the layer names (e.g. `Dense` to `nn.Linear`) could solve the majority of the issues.

---

<div class="post-metadata">

**Author:** ![ananthceh19](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/87869e/32.png) [@ananthceh19](https://discuss.pytorch.org/u/ananthceh19)\
**Post date:** [April 19, 2021, 11:08am UTC](https://discuss.pytorch.org/t/converting-keras-model-to-pytorch/118585/3 "2021-04-19T11:08:34Z")

</div>

Thanks for the response.  
Specifically:  
a) How do I specify conv3\_block4\_2\_relu?  
b) How do i convert this line:  
self.intermediate\_layer\_model = Model(inputs=self.base\_model.input,  
outputs=self.base\_model.get\_layer(self.layer\_name).output)

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 20, 2021, 4:37am UTC](https://discuss.pytorch.org/t/converting-keras-model-to-pytorch/118585/4 "2021-04-20T04:37:55Z")

</div>

I don’t know how `Model` is defined but assuming that `self.layer_name` gets an intermediate activation from `ResNet50`, you could either use forward hooks as described [here](https://discuss.pytorch.org/t/how-can-l-load-my-best-model-as-a-feature-extractor-evaluator/17254/6) or override the `forward` of the resnet implementation so that the desired activation will be returned.
