Pytorch to keras

Hello everybody,
I am sorry if the post is uncategorized but this is my first post in the forum and I am learning how it works.
I need to convert a pytorch based CNN into a keras based one. In particular I need to convert the pytorch code

conv2 = torch.nn.Conv2d(32, 64, 3, 2, 1)

into a keras one. I tried with
conv2 = keras.layers.Conv2D(64,3,strides=(2,2),padding=‘same’,activation=‘relu’,name=‘conv2’)

but when I call the keras layer and the pytorch one, starting from the same input data and with the same initialization for weights and biases, I obtain two tensors (one in pytorch and one in keras) with the same size but different values. Everything works if I set stride=1 in both the cases.

Does the stride work differently in keras and in pytorch?
Does anyone have some suggestions for my issue?

Thank you.

Really late response, so you probably solved the problem but in case somebody else has similar problems

Could you try pt2keras and see if it works?
To install pt2keras, type the following in the terminal:

pip install -U pt2keras

Below is an example for converting your model.

from copy import deepcopy

import numpy as np
import tensorflow as tf
import torch
from pt2keras import Pt2Keras

class Model(torch.nn.Module):
    def __init__(self):
        self.conv2 = torch.nn.Conv2d(3, 64, (3, 3), (2, 2), 1)

    def forward(self, x):
        return torch.nn.functional.relu(self.conv2(x))

if __name__ == '__main__':
    input_shape = (1, 3, 224, 224)

    # Grab model
    model = Model().eval()

    # Create pt2keras object
    converter = Pt2Keras()

    # convert model
    keras_model: tf.keras.Model = converter.convert(model, input_shape, strict=True)

    x_pt = torch.randn(input_shape)
    # Generate dummy inputs
    x_keras = tf.convert_to_tensor(deepcopy(x_pt.numpy()))

    # input dimensions for PyTorch are BCHW, whereas TF / Keras default is BHWC
    if len(x_keras.shape) == 4:
        x_keras = tf.transpose(x_keras, (0, 2, 3, 1))

    print(f'pt shape: {x_pt.shape}, x_keras.shape: {x_keras.shape}')

    # Make PT model the same input dimension as Keras
    # If the output is >= 4 dimensional
    pt_output = model(x_pt).cpu().detach().numpy()
    keras_output = keras_model(x_keras).numpy()
    if len(keras_output.shape) == 4:
        keras_output = keras_output.transpose(0, 3, 1, 2)
    # Mean average diff over all axis

    average_diff = np.mean(pt_output - keras_output)
    print(f'pytorch: {pt_output.shape}')
    print(f'keras: {keras_output.shape}')
    # The differences will be precision errors from multiplication / division
    print(f'Mean average diff: {average_diff}')
    print(f'Pytorch output tensor: {pt_output}')
    print(f'Keras output tensor: {keras_output}')