Medical images segmentation has become a fundamental tool for making more precise the assessment of complex diagnosis and surgical tasks. In particular, this work focuses on multiple sclerosis (MS) disease in which lesion segmentation is useful for getting an accurate diagnosis and for tracking its progression. In recent years, Convolutional Neural Networks (CNNs) have been successfully employed for segmenting MS lesions. However, these methods often fail in defining the boundaries of the MS lesions accurately. This work focuses on segmenting hard-to-classify voxels close to MS lesions boundaries in MRI, where it was determined that the application of a loss function that focuses the penalty on difficult voxels generates an increase in the results with respect to the Dice similarity metric (DSC), where the latter occurs as long as the sufficient representation of these voxels as well as an adequate preprocessing of the images of each patient. The methodology was tested in the public data set ISBI2015 and was compared with alternative methods that are trained using the binary cross entropy loss function and the focal loss function with uniform and stratified sampling, obtaining better results in DSC, reaching a DSC> 0.7, a threshold that is considered comparable to that obtained by another human expert.