The article presents a new framework called Regularization via Gradient Attribution (RGA) to improve the detection of lesions in multiple sclerosis from magnetic resonance imaging. The main problem addressed is the class imbalance in medical imaging data, where healthy tissue is much more abundant than lesions, causing algorithms to frequently miss small lesions. The RGA method uses explainable artificial intelligence to verify whether the network correctly identifies lesions and healthy tissue. The framework was tested on two public datasets with three different types of neural networks and two analysis methods. Results showed improvement in all measured parameters, with the best configuration achieving a DSC value of 0.7056 on the first dataset and 0.7466 on the second dataset. The RGA method adds no additional computational costs during inference and is compatible with medical systems.