John Curry
John Curry

Reputation: 501

Why does a saved PyTorch model in eval mode return different results every time inference is ran?

In this case, I tried all the usual steps to make sure there was no stochastic element to the inference code itself i.e. the model shouldn't change it's predictions based on the inputs. See for details of this:

Why am I getting different results after saving and loading model weights in pytorch?

Upvotes: 0

Views: 721

Answers (1)

John Curry
John Curry

Reputation: 501

In my case, it was the data. The input to the model was changing on each run. At training time, I had set randomized data transforms which were appropriate for training. But, of course, this introduced randomness as I prepared the data for inference. So I changed

data_transforms = {
"train": transforms.Compose(
    [
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    ]
),

to:

data_transforms = {
"train": transforms.Compose(
    [
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),

which ensured consistency.

Upvotes: 0

Related Questions