Hi,
I am trying to implement a single convolutional layer (taken as the first layer of SqueezeNet) in both PyTorch and TF to get the same result when I send in the same picture.
Below is my code:
from __future__ import print_function
import torch
import torch.nn as nn
import tensorflow as tf
import numpy as np
import pickle as pkl
from modified_squeezenet import SqueezeNet
from keras.models import Model
from keras.layers import Input, Convolution2D
from keras.engine.topology import get_source_inputs
from keras import backend as K

TF_CONV1_KERNEL = None
TF_CONV1_BIAS = None

def get_torch_image(image_number):
  '''
  Unpickle the specified image and return the tensor
  '''
  with open('imagenet_sample_0.pkl', 'rb') as f:
    data = pkl.load(f)
    # get the 3D array representation of the image
    X = data['X'][image_number]
    # Get integer representation of the label
    y = data['y'][image_number]
    # Print the word label
    print('Current image:', data['labels'][y])
    # construct mean image
    mean_image = np.zeros((227, 227, 3))
    mean_image[:, :, 0] = 103.939 # blue
    mean_image[:, :, 1] = 116.779 # green
    mean_image[:, :, 2] = 123.68 # red
    X_prep = X - mean_image
    # Convert array to D, W, H
    X_prep = torch.from_numpy(X_prep.transpose(2, 0, 1))
    # Tensor shape = 1, 3, 227, 227
    X_prep.unsqueeze_(0)
    X_prep.requires_grad = True
    # get the integer representation of the label
    return (X_prep.float(), y)

def init_conv1_kernel(shape, dtype=None):
  return TF_CONV1_KERNEL

def init_conv1_bias(shape, dtype=None):
  return TF_CONV1_BIAS

def get_tf_net():
  img_input = Input(shape=(227,227,3))
  x = Convolution2D(64, (3, 3), strides=(2, 2), padding='valid',\
                    name='conv1', kernel_initializer=init_conv1_kernel,\
                     bias_initializer=init_conv1_bias)(img_input)
  model = Model(img_input, x)
  return model

def run_tf_model(tf_net, prep_img):
  # prepare input for tf NET
  # convert 1 x D x W x H => 1 x W x H x D
  prep_img = prep_img.detach().numpy().transpose(0, 2, 3, 1).astype(np.float64)

  init_g = tf.global_variables_initializer()
  with tf.Session() as sess:
    sess.run(init_g)
    result = sess.run(tf_net.output, feed_dict={tf_net.input:prep_img})
    return result


if __name__ == "__main__":
  # Specify image
  target_example = 1 # 0 = monarch; 1 = llama; 2 = airedale
  prep_img, target_class = get_torch_image(target_example)
  tf_model = SqueezeNet("relu")
  tf_weights = [i for i in tf_model.get_weights()]

  TF_CONV1_KERNEL = tf_weights[0]
  TF_CONV1_BIAS = tf_weights[1]

  # get the simple tensorflow net
  tf_net = get_tf_net()

  # pytorch
  x = nn.Conv2d(3, 64, kernel_size=3, stride=2)
  # 3, 2, 0, 1 seems to be right transformation from TF to PyTorch weights
  x.weight = nn.Parameter(torch.from_numpy(TF_CONV1_KERNEL.transpose(3, 2, 0, 1)))
  x.bias = nn.Parameter(torch.from_numpy(TF_CONV1_BIAS))
  
  # FINAL COMPARISON
  pytorch_result = x(prep_img)
  tf_result = run_tf_model(tf_net, prep_img)
  print(pytorch_result.detach().numpy().transpose(0, 2, 3, 1))
  print(tf_result)

The results I am getting are different, and I am not sure why. The same weights are applied to both the PyTorch and the TensorFlow model and rotated from one to the other to account for differences in the frameworks. Any suggestions for what I am doing wrong? I am still very much a novice in both frameworks, so apologies for any badly designed or inefficient code.
Thanks in advance.