Getting reproducible training and inference

So, I was able to achieve deterministic training using below

seed = 1
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.cuda.manual_seed_all(seed) if torch.cuda.is_available() else ''
np.random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed) 

However for inference, each time I run below, I get different result

classes = ['benign', 'malignant']
data = ImageDataBunch.single_from_classes('weights', classes, ds_tfms=None, size=224).normalize(imagenet_stats)
arch_model = torchvision.models.resnet34
learn = cnn_learner(data, arch_model)

defaults.device = torch.device('cpu')
pred_class, pred_idx, outputs = learn.predict(img)

Any ideas?

So, I was able to figure it out
It appears ImageDataBunch..normalize(imagenet_stats) introduces variability

So, to get it to work, simply replace

data = ImageDataBunch.single_from_classes('weights', classes, ds_tfms=None, size=224).normalize(imagenet_stats)

with

data = ImageDataBunch.single_from_classes('weights', classes, ds_tfms=None, size=224)

1 Like