A computer-implemented method for generating a counterfactual image data sample for an image data classification neural network (300, 401) based on an input image data sample, comprising: • Determining, using the neural network, a class prediction for the input image data sample; • Generating a candidate counterfactual image data sample for which the neural network determines a different class prediction than for the input image data sample, wherein generating the candidate counterfactual image data sample comprises applying a mask to the input image data sample and wherein modifying the candidate counterfactual image data sample comprises modifying the mask; • Determining an estimate of the uncertainty of the class prediction for the candidate counterfactual image data sample, wherein the estimate of the uncertainty is derived from the predictive uncertainty induced by using a Monte-Carlo dropout method or wherein the image data classification neural network is a Bayesian Neural Network, BNN, and the estimate of the uncertainty is derived from the predictive uncertainty induced by the weight probability distributions of the BNN; • Determining a loss function, wherein the loss function comprises the estimate of the uncertainty of the class prediction by the neural network for the candidate counterfactual image data sample, wherein the loss function comprises a first term containing the output of the neural network for the input image data sample and a second term containing the estimate of the uncertainty of the class prediction for the candidate counterfactual image data sample, wherein the loss function further contains a term representative of the difference between the input image data sample and the counterfactual image data sample, wherein the loss function further contains a term indicating a target class for the counterfactual image data sample to be generated by the neural network; • Modifying the candidate counterfactual image data sample to obtain a counterfactual image data sample based on the determined loss function; and • Outputting the counterfactual image data sample. • Determining, using the neural network, a class prediction for the input image data sample; • Generating a candidate counterfactual image data sample for which the neural network determines a different class prediction than for the input image data sample, wherein generating the candidate counterfactual image data sample comprises applying a mask to the input image data sample and wherein modifying the candidate counterfactual image data sample comprises modifying the mask; • Determining an estimate of the uncertainty of the class prediction for the candidate counterfactual image data sample, wherein the estimate of the uncertainty is derived from the predictive uncertainty induced by using a Monte-Carlo dropout method or wherein the image data classification neural network is a Bayesian Neural Network, BNN, and the estimate of the uncertainty is derived from the predictive uncertainty induced by the weight probability distributions of the BNN; • Determining a loss function, wherein the loss function comprises the estimate of the uncertainty of the class prediction by the neural network for the candidate counterfactual image data sample, wherein the loss function comprises a first term containing the output of the neural network for the input image data sample and a second term containing the estimate of the uncertainty of the class prediction for the candidate counterfactual image data sample, wherein the loss function further contains a term representative of the difference between the input image data sample and the counterfactual image data sample, wherein the loss function further contains a term indicating a target class for the counterfactual image data sample to be generated by the neural network; • Modifying the candidate counterfactual image data sample to obtain a counterfactual image data sample based on the determined loss function; and • Outputting the counterfactual image data sample.