## 2 Materials and Methods
### Methodological Framework
#### 2.2.4 Visual Transformer Network Training  
Visual transformers (ViT) [19] are showing promising results in the medical imaging domain [20]. They introduce a transformative approach to image recognition by applying a transformer architecture directly to images without relying on convolutional networks. The vision transformer processes images by splitting them into smaller fixed-size patches, treating these patches as individual tokens (similar to words in natural language processing models). The model embeds these patches into a 1D sequence and passes them through a standard transformer encoder for image classification tasks. This approach eliminates the need for convolutional layers, relying instead on self-attention mechanisms to learn image features.  
Our proposed vision transformer (ViT) model was designed to classify input images into binary categories (healthy or not healthy), leveraging a transformer-based architecture tailored for image analysis tasks. This model used the standard transformer-based approach for image classification. However, it employed a reduced number of layers, which allowed it to be trained on a small dataset (essentially a \"small ViT\" model with around 26 million parameters). Figure 4 shows a concise breakdown of its structure.  
The model received fixed-size RGB input images, which were optionally augmented using a sequence of standard transformations, including random rotations and horizontal/vertical flips. As with the CNN models, data augmentation was applied exclusively to the underrepresented class (healthy) within the training dataset, since the same augmentation strategy was consistently used for both the CNN and ViT models.  
After being processed, each image was divided into non-overlapping patches of equal size for further processing (Figure 5).  
These patches were vectorized and passed through a dense layer to project them into a lower-dimensional embedding space. To preserve spatial information, a trainable positional embedding was added to the patch representations.  
The encoded patches were then processed through a series of transformer blocks. Each block started with layer normalization, followed by a multi-head self-attention mechanism with four attention heads. A residual connection was applied after the attention output. Another layer normalization was performed, and the result was passed through a multilayer perceptron (MLP) block composed of two dense layers with GELU activations and dropout regularization. A second residual connection integrated the MLP output back into the original path.  
After the transformer layers, the output patch sequence underwent layer normalization and was flattened into a single feature vector. A dropout layer was applied to this flattened representation to mitigate overfitting. This feature vector was then passed through a classification head implemented as an MLP composed of two dense layers (with 512 and 256 units, respectively), again using GELU activations and dropout. Finally, the network outputted logits through a dense layer, with the number of units corresponding to the number of target classes (healthy or not healthy).  
The model was trained using the Adam optimizer with a learning rate of 0.0003 and a weight decay of 0.0001. Sparse categorical cross-entropy loss was employed as the loss function. Finally, early stopping was implemented to halt training after 10 consecutive epochs without improvement in validation accuracy, effectively reducing overfitting and saving computational resources.  
Figure 4: Visual transformer conceptual model. We divided an image into fixed-size patches, applied a linear embedding to each patch, incorporated position embeddings, and inputted the resulting sequence of vectors into a standard transformer encoder. For classification, we followed the conventional method of appending an additional learnable “classification token” to the sequence.  
Figure 5: Color image processed by the patcher layer. The image was divided into fixed-size patches.