I am trying to get a PyTorch version of EfficientNetB0 based off a working example from Keras. From Keras, I am using https://www.tensorflow.org/api_docs/python/tf/keras/applications/efficientnet/EfficientNetB0
Meanwhile, in PyTorch, I have found three implementations of EfficientNetB0. However, I noticed that in each, I am getting results that differ from my Keras model. I am wondering if there is something I am doing incorrectly or are the Keras and PyTorch implementations incompatible?
To test this, I generate an input of all ones or zeros of the appropriate dimensions. Because the output is a 1000 dimensional vector and to account for the possibility that the ordering of the class probabilities may not be the same, I decided to compare the mean of the outputs rather than the outputs directly.
import tensorflow as tf
import numpy as np
# Keras version
model = tf.keras.applications.EfficientNetB0()
x = tf.zeros([1, 224, 224, 3])
y = model(x).numpy()
print(np.mean(y))
# Outputs 0.001
x = tf.ones([1, 224, 224, 3])
print(np.mean(model(x).numpy()))
# Outputs 0.0009999999
Meanwhile, trying the various PyTorch implementations:
import torch
import torchvision
model = torchvision.models.efficientnet_b0(pretrained=True)
_ = model.eval()
x = torch.ones((1, 3, 224, 224))
print(torch.mean(model(x)))
# Outputs tensor(0.0160, grad_fn=<MeanBackward0>), which isn't 0.0009
I do a similar procedure with the zero matrix, and also with the models from https://github.com/lukemelas/EfficientNet-PyTorch and from TorchHub
Namely,
model = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_efficientnet_b0', pretrained=True)
model = EfficientNet.from_pretrained('efficientnet-b0')
Checking the source code for the torchvision model, it seems that everything matches up with the Keras parameters.