## 2 Materials and Methods
### Training  
The application of a neural network involves a series of tensor operations that take an \(n\)-dimensional input matrix and produce the desired output. To achieve the desired result, the parameters that govern these tensor operations need to be adjusted. This is where the training operation comes into play. First, a cost function needs to be defined, which measures how close the network's predictions are to the actual values. Then, using a network with randomly chosen parameters, an initial prediction is made, typically with poor performance. An optimization algorithm is used to update the parameters in each prediction iteration, attempting to minimize the cost function. This process is repeated until the parameters reach values that make the predictions as close as possible to the provided training data. The training process occurs in iterations involving a subset of images from the training set (batch size). The term \"epoch\" refers to the number of iterations required to use all the images in the training set for the optimization algorithm (1 epoch is approximately N/batch_size iterations, where N is the number of samples in the training set). The cost function used by the Mask-RCNN for training is a weighted combination of four terms:  
RPN classification loss: This term calculates the classification error for the proposals generated by the region proposal network (RPN). It is the cross-entropy between the predicted and target classification probabilities. RPN regression loss: This term calculates the regression error for the RPN proposals. It measures the error between the predicted and target coordinates using the mean absolute error. Box classification loss: This term calculates the classification error for the regions of interest (ROI) generated by the RPN and passed to the second stage of the model, which refines the position and shape of the object within the region. The cost function is again the cross-entropy between the predicted and target classification probabilities. Box regression loss: Finally, this term calculates the regression error for the regions of interest. It measures the error between the predicted and target coordinates using the negative of the Dice similarity index (for contours) and the mean absolute error for the box coordinates. The actual training was performed for 300 epochs using a batch size of 10 images. The optimization algorithm used was stochastic gradient descent (SGD) with momentum,  
Figure 1: Schematic representation of the Mask-RCNN implemented in the detectron2 library and the 3 main components: Convolutional backbone, Region Proposal Network (RPN), and Head part (ROI/box head). which is the default in the detectron2 library. The algorithm can be described by the following equation:  
\[\left\{v^{(0)}=0\;v^{(t)}=\beta v^{(t-1)}+(1-\beta)\
abla f\Big{(}x^{(t-1)}\Big{)} \;x^{(t)}=x^{(t-1)}-\eta v^{(t)}\right.\]  
where \(x\) is the value of the parameters updated in the new iteration, \(\theta\) is the value of the parameters in the previous iteration, \(\
abla\theta\) is the gradient of the cost function for the model parameters, \(m\) is the exponential moving average of the gradient at iteration \(t\), \(\beta\) is a weight coefficient, and \(\eta\) is the learning rate that controls how much the gradient influences the parameter update between iterations. The learning rate used in the algorithm is not fixed but varies from iteration to iteration using a scheduler. The scheduler used is WarmupMultiStepLR in detectron2 (base learning rate = 0.001, gamma = 0.1). During training, the technique of data augmentation was applied. It involves applying various transformations to the original images so that in different iterations of the parameter optimization algorithm, the same image is presented differently while retaining the same informational content. This improves the algorithm's generalizability and performance on unseen data.