# Exploring Attributions in Convolutional Neural Networks for Cow Identification
## 2 Materials and Methods  
The present study, which is the first phase of an ongoing two-year project on cow face visual identification, included creation of datasets, training, and exploration of different methods to evaluate influence of background in process of cow identification. An experimental dataset was created and introduced, containing images of 168 dairy cows from the Holstein breed, consisting of 760 images in total. The images were taken in several farms, situated in Southern Bulgaria in the period from 1 April to 30 June 2024. The photos were manually captured on-site in daylight with Panasonic Lumix DMC-GX8 camera (Kadoma City, Japan) with a Panasonic Leica 15 mm f/1.7 lens. Each cow has at least three images. Near-duplicate images were removed by visual inspection to make sure the test results would not be compromised. It should be noted that on some of the images more than one animal was visible. A record kept for correct identification of the individual cows. The image resolution was 908 pixels (width) and 1210 pixels (height) with a horizontal and vertical resolution of 72 dpi. The images from the dataset were annotated after with the identity of the particular animal. In this process of the dataset preparation, we used Roboflow resources [37,38] which is a cloud-based platform that streamlines image annotation and preprocessing. The images were uploaded and segmented using built-in tools, ensuring a standardized dataset for training and evaluation. During the procedure the cow regions were manually annotated, ensuring precise bounding lines for segmentation between cow and foreground. Using standardized annotation techniques with Roboflow enhanced the model reliability, as previous studies have shown that consistent dataset preprocessing significantly improves classification accuracy [39]. The segmentation was completed for all 760 images, clearly defining foreground and background regions. Then, the background was replaced with white noise in the preparation of foreground images and the reverse for the background images, respectively. The prepared data were afterwards split into training, validation, and test datasets with approximate ratios of 34%, 33% and 33% of the cows. When a cow was randomly assigned to one of the splits, all its images became part of the split. This ensured that the cows in the validation and the test set were not seen during training. For the training process, the last layer of a ResNet50 [35] model was fine-tuned on an Nvidia RTX A4500 (Nvidia, Santa Clara, CA, USA) graphical processing unit with 20 GB of VRAM on a cloud-based platform. The use of a pre-trained model allowed the efficient of the network on high-performance hardware, significantly reducing the computational time. During training, the following random augmentations were applied: resized crop, horizontal flip, rotation, color jitter, affine transform, erasing, and Gaussian noise. The loss function used was triplet loss [26], which is designed for identification. The model learns by pulling similar items closer together in an embedding space while pushing dissimilar items further apart. The loss function operates on triplets of samples, each consisting of an anchor (a reference sample), a positive sample (similar to the anchor), and a negative sample (dissimilar to the anchor). The triplet loss function can be mathematically expressed as follows:  
\[L(a,p,n)=\max(d(a,p)-d(a,n)+\alpha,0) \tag{1}\]  
where \(d(a,p)\) is the distance between the anchor, \(a\), and the positive sample, \(p\); \(d(a,n)\) is the distance between the anchor and the negative sample, \(n\); and \(\alpha\) is a positive number that ensures a gap between the distances of positive and negative pairs. Triplet loss is widely used in various fields, including face recognition, where it was introduced in the FaceNet paper [26]. For animal re-identification, it has been used in [31].