2.2. Step 2. Training process In this project, a key focus was exploring different…
- User
2.2. Step 2. Training process In this project, a key focus was exploring different configurations for the fully connected layers in order to optimize performance with pretrained models. By experimenting with various layer architectures, the goal was to effectively adapt these pretrained models (e.g., ResNet, DenseNet) to the specific task of Helicobacter pylori detection in histological images. This approach allowed for leveraging the pretrained features while customizing the final classification layers to improve sensitivity and accuracy in detecting H. pylori. Before training our deep learning models, a set of different configuartions are estab- lished to explore different model architectures and hyperparameters (model config.py). This systematic approach ensures that the most effective configurations can be iden- tified. 6 These are the guidelines: • Model Architectures (model options): Consider two pre-trained models as the base of the architecture: resnet50 and densenet201. • Number of Dense Layers (num layers options): We explore models with 1 dense layer, 2 dense layers, and 3 dense layers. • Number of units (units options): For each dense layer consider 64 units, 128 units, and 256 units. • Dropout Rate (dropout options): Finally, a fixed dropout rate of 0.25 is used. For each num layers (1, 2, or 3), we generate all possible combinations of units per- layer options repeated num layers times. We then filter these combinations to re- duce redundancy and focus on diverse architectures. Before applying any filtering, there was a total of 90 different configurations. So, we implemented a line of code removing configurations where all two layers or three layers had identical units or insufficient diversity in unit values. The total number of different was achieved to be reduced to 66 configurations. Therefore, with the 66 configurations that had been generated, we proceeded to the training phase of our project. The goal was to train each of these models individually to evaluate their performance and identify the most effective architectures for our task. During the training of each model, the key performance metrics were meticously recorded, specifically the training accuracy and training loss at each epoch. These metrics are essential for assessing how well the model is learning from the data and for detecting any issues such as overfitting or underfitting. Upon completion of the training phase for all configurations, a comparative analysis of the collected performance metrics was undertaken. This analysis involves reviewing the accuracy and loss trends to determine which models have achieved the highest levels of accuracy while maintaining acceptable loss values. Models that exhibit superior performance in terms of converging quickly to high accuracy and demonstrating stable loss reduction are considered strong candidates. In the following figure (Figure 2 ) it can be seen what we had to choose the best models. Figure 2: Accuracies and losses lists for all configurations. 7 With the chosen best models, we will undergo further phases of training, exper- imenting with epochs, learning rate, etc. And after that, they will be tested and evaluated. For the model architecture, pre-trained models like ResNet50 and DenseNet201 have been used as backbones, leveraging transfer learning to fine-tune them for our classification task. Custom fully connected layers are appended to the backbone networks to adapt them to our specific problem, with configurations based on task complexity and data availability. Cross-entropy loss with class weights is selected to address class imbalance, ensuring the model pays equal attention to all classes. On the other side, the Adam optimizer is chosen for its adaptive learning rate capabilities, enhancing convergence speed and stability. Training involves iterating over the dataset for multiple epochs, where the model processes batches of data, computes loss, and updates weights through backpropaga- tion. We record metrics such as training loss, accuracy, and recall after each epoch to monitor progress and determine the optimal stopping point. Upon completion of training, the model is evaluated on a validation set using met- rics like validation loss, accuracy, precision, recall, and F1-score to assess performance comprehensively. The trained models are saved for future use, and plots of training and validation metrics are generated to visualize learning dynamics. By experimenting with various architectures and hyperparameters, and by making informed choices based on data characteristics and theoretical considerations, the aim was to identify the model configuration that achieves optimal performance. This methodical approach is essential for developing robust models capable of accurate predictions on complex datasets. 2.2.2. Metrics The following figure (Figure 3 ) illustrates the plots of accuracy, loss, and recall that were shown after each configuration was trained. This is an example. Figure 3: Accuracy, Loss training and Recall plot of a DenseNet201 with 2 layers. 8 2.3. Step 3. Testing Process (patch classifier) The testing process performs the patch-level recognition. By taking the loader for test created by the stratified Kfold, it can be seen how each model is able to predict the correct class for patches. This is done in the file evaluation test patces.py 2.3.1. Models Based in the code created the evaluation is done sequencially in a loop for all the configuartion models (previously saved) and each of the folds. 2.3.2. Metrics There are several function related to the matrices computation: plot confusion matrix(conf matrix, save path=None): This function visualizes the confusion matrix using a heatmap generated by seaborn. It labels the axes with ”True label” and ”Predicted label” and saves the confusion ma- trix as an image if a save path is provided. save evaluation results(evaluation results, save folder, model filename): This function saves the evaluation metrics (accuracy, precision, recall, F1 score, AUC) to a CSV file. It helps in tracking the performance of models across multiple runs. evaluate model with classifier(model, val loader, device, save curve folder, model name): This function is used to evaluate classification models. The model’s predictions are compared to the ground truth labels to compute traditional metrics like accuracy, precision, recall, F1 score, and confusion matrix. Additionally, an ROC curve is gen- erated, and the AUC is calculated to assess the modelˆas ability to distinguish between classes. They are crucial for understanding how a model performs on test data. Each of these metrics has its own focus for measuring performance, and together they provide a more complete picture of the model’s effectiveness. Below are detailed descriptions of the main metrics used and their relevance: Accuracy: It measures how well the model classifies both positive and negative classes correctly. Its formula is: Accuracy = Correct Predictions Total Predictions Although it is useful, it may not be a reliable metric in this case as we have an imbalanced datasets. In these cases, a model may predict the majority class all the time and still achieve high accuracy, without truly being effective. 9 Precision: Measures the proportion of positive cases correctly predicted in relation to all cases the model classified as positive. Its formula is: Precision = True Positives (TP) True Positives (TP) + False Positives (FP) High precision means the model has few false positives, which is important in situ- ations where a false positive can have bad consequences, such as in disease detection. Recall (Sensitivity): Measures the model’s ability to identify all the actual positive cases. Its formula is: Recall = True Positives (TP) True Positives (TP) + False Negatives (FN) A model with high recall is particularly useful when you want to minimize false negatives, such as in the diagnosis of serious diseases, where missing a positive case could have serious consequences. F1 Score: This metric is especially useful when a balance between precision and recall is needed, and is commonly used when classes are imbalanced. The F1 score is particularly useful when it’s not possible to achieve both metrics at once. Its formula is: F1 Score = 2 × Precision × Recall Precision + Recall A higher F1 score indicates better model performance in terms of balancing preci- sion and recall. Confusion Matrix: The confusion matrix is a visual tool that shows the classifica- tion results by comparing the model’s predictions to the actual values. This matrix contains four key values:
- Assistant