I'm trying to convert pytorch
model to tf.keras
model including weights conversion and came across an output missmatch between libraries' outputs.
Here I define two convolutional layers, which should be identical
torch_layer = torch.nn.Conv2d(
in_channels=3,
out_channels=64,
kernel_size=(7, 7),
stride=(2, 2),
padding=(3, 3),
dilation=1,
groups=1,
bias=False,
padding_mode='zeros'
)
tf_layer = tf.keras.layers.Conv2D(
filters=64,
kernel_size=(7, 7),
strides=(2, 2),
padding='same',
dilation_rate=(1, 1),
groups=1,
activation=None,
use_bias=False
)
# define model to specify input channel size
tf_model = tf.keras.Sequential([tf.keras.layers.Input((256, 256, 3), batch_size=1), tf_layer])
now I have torch weights and I convert them to tf.keras
format
# output_channels, input_channels, x, y
torch_weights = np.random.rand(64, 3, 7, 7)
# x, y, input_channels, output_channels
tf_weights = np.transpose(torch_weights, (2, 3, 1, 0))
# assign weights
torch_layer.weight = torch.nn.Parameter(torch.Tensor(torch_weights))
tf_model.layers[0].set_weights([tf_weights])
now I define input and the outputs are different (shape is the same, values are different), what am I doing wrong?
torch_inputs = np.random.rand(1, 3, 256, 256)
tf_inputs = np.transpose(torch_inputs, (0, 2, 3, 1))
torch_output = torch_layer(torch.Tensor(torch_inputs))
tf_output = tf_model.layers[0](tf_inputs)