Organizations: Research Center for Medical Image Analysis and Artificial Intelligence, Department of Medicine, Faculty of Medicine and Dentistry, Danube Private University, Krems an der Donau, 3500, Austria
Vision Transformers (ViTs) and their variants have become state-of-the-art in many computer vision tasks and are widely used as backbones in large-scale vision and vision-language foundation models. While substantial research has focused on architectural improvements, the impact of patch size, a crucial initial design choice in ViTs, remains underexplored, particularly in medical domains where both two-dimensional (2D) and three-dimensional (3D) imaging modalities exist. In this study, using 12 medical imaging datasets from various imaging modalities (including seven 2D and five 3D datasets), we conduct a thorough evaluation of how different patch sizes affect ViT classification performance. Using a single graphical processing unit (GPU) and a range of patch sizes (1, 2, 4, 7, 14, 28), we fine-tune ViT models and observe consistent improvements in classification performance with smaller patch sizes (1, 2, and 4), which achieve the best results across nearly all datasets. More specifically, our results indicate improvements in balanced accuracy of up to 12.78% for 2D datasets (patch size 2 vs. 28) and up to 23.78% for 3D datasets (patch size 1 vs. 14), at the cost of increased computational expense. Moreover, by applying a straightforward ensemble strategy that fuses the predictions of the models trained with patch sizes 1, 2, and 4, we demonstrate a further boost in performance in most cases, especially for the 2D datasets. Our implementation is publicly available on GitHub: https://github.com/HealMaDe/MedViT
Figures & tables
Dataset
Type
# train/val/test
classes
Modality
Breast
2D
546/78/156
2
Ultrasound
Retina
2D
1080/120/400
5
Fundus imaging
Blood
2D
11959/1712/3421
8
Microscopy
Derma
2D
7007/1003/2005
7
Dermatoscopy
OCT
2D
97477/10832/1000
4
Retinal OCT
OrganS
2D
13932/2452/8827
11
Abdominal CT
Table 1: Summary of the selected datasets from MedMNIST V2 collection [ 37 ] .
Figure 1: Overview of the 12 selected datasets from the MedMNIST V2 collection used in this study. Each sub-figure shows some representative example images along with the dataset name, imaging modality, and classification task. For the 3D datasets, the middle slices from random samples are shown.
Figure 2: Example of splitting the image into patches with different sizes for 2D models.
Figure 3: Example of splitting the image into patches with different sizes for 3D models.
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
84.40 ± 0.98
77.05 ± 1.55
89.60 ± 2.02
16.71
2
85.04 ± 1.48
77.24 ± 0.38
90.07 ± 2.68
4.19
4
84.62 ± 1.93
76.94 ± 4.69
90.54 ± 0.90
1.06
7
85.26 ± 0.64
75.63 ± 0.44
87.29 ± 2.73
0.36
14
83.55 ± 1.34
76.21 ± 3.07
88.25 ± 1.75
0.11
28
83.76 ± 1.96
75.36 ± 2.59
84.11 ± 2.18
0.04
Table 2: Effect of patch size on classification performance for the BreastMNIST dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations.
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
54.67 ± 1.63
36.71 ± 2.11
74.44 ± 0.57
16.71
2
51.08 ± 1.01
32.02 ± 2.67
71.78 ± 0.90
4.19
4
52.17 ± 0.76
34.51 ± 0.80
73.77 ± 1.94
1.06
7
53.25 ± 1.75
36.01 ± 0.51
73.68 ± 1.33
0.36
14
53.33 ± 2.32
35.58 ± 2.83
72.63 ± 1.72
0.11
28
54.08 ± 0.72
36.99 ± 0.50
72.73 ± 0.79
0.04
Table 3: Effect of patch size on classification performance for the RetinaMNIST dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations.
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
96.50 ± 0.13
96.03 ± 0.12
99.82 ± 0.03
16.71
2
96.99 ± 0.28
96.56 ± 0.41
99.87 ± 0.02
4.19
4
96.62 ± 0.92
96.09 ± 1.31
99.85 ± 0.03
1.06
7
96.46 ± 0.26
95.95 ± 0.44
99.82 ± 0.02
0.36
14
94.96 ± 0.14
94.33 ± 0.41
99.67 ± 0.02
0.11
28
93.59 ± 0.46
92.36 ± 0.60
99.50 ± 0.05
0.04
Table 4: Effect of patch size on classification performance for the BloodMNIST dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations.
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
78.70 ± 0.64
56.16 ± 3.02
94.18 ± 0.20
16.71
2
78.12 ± 0.33
54.86 ± 3.75
94.16 ± 0.36
4.19
4
78.65 ± 0.99
56.64 ± 2.30
94.37 ± 0.67
1.06
7
77.18 ± 0.94
54.38 ± 1.90
93.95 ± 0.38
0.36
14
75.86 ± 0.09
48.76 ± 3.03
92.16 ± 0.41
0.11
28
73.76 ± 0.56
42.23 ± 6.01
90.63 ± 0.51
0.04
Table 5: Effect of patch size on classification performance for the DermaMNIST dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations.
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
76.60 ± 2.10
76.60 ± 2.10
95.66 ± 0.28
16.71
2
79.10 ± 0.85
79.10 ± 0.85
96.68 ± 0.39
4.19
4
78.63 ± 1.26
78.63 ± 1.26
96.25 ± 0.23
1.06
7
78.03 ± 0.15
78.03 ± 0.15
95.62 ± 0.05
0.36
14
71.70 ± 0.66
71.70 ± 0.66
93.02 ± 0.41
0.11
28
67.37 ± 0.06
67.37 ± 0.06
91.24 ± 0.07
0.04
Table 6: Effect of patch size on classification performance for the OCTMNIST dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations.
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
78.63 ± 0.23
74.96 ± 0.57
97.65 ± 0.07
16.71
2
80.54 ± 0.29
76.51 ± 0.10
98.08 ± 0.11
4.19
4
80.50 ± 0.37
76.35 ± 0.57
97.98 ± 0.11
1.06
7
79.06 ± 0.80
74.42 ± 0.95
97.71 ± 0.14
0.36
14
74.82 ± 1.25
70.71 ± 1.29
96.95 ± 0.33
0.11
28
68.42 ± 0.77
63.73 ± 1.25
95.29 ± 0.09
0.04
Table 7: Effect of patch size on classification performance for the OrganMNIST dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations.
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
90.28 ± 0.67
87.27 ± 0.84
98.53 ± 0.55
16.71
2
91.93 ± 1.53
89.62 ± 2.12
98.70 ± 0.07
4.19
4
90.33 ± 0.34
87.45 ± 0.45
98.55 ± 0.08
1.06
7
89.47 ± 0.49
86.25 ± 0.86
97.91 ± 0.09
0.36
14
90.38 ± 1.00
87.66 ± 1.51
97.66 ± 0.21
0.11
28
89.10 ± 0.73
85.84 ± 1.01
97.49 ± 0.20
0.04
Table 8: Effect of patch size on classification performance for the PneumoniaMNIST dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations).
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
80.20 ± 2.51
68.72 ± 3.61
82.56 ± 2.89
800.85
2
81.88 ± 0.72
65.76 ± 1.15
79.16 ± 1.47
117.83
4
78.64 ± 0.63
61.12 ± 2.28
73.92 ± 1.59
19.90
7
76.29 ± 0.88
57.74 ± 1.23
76.47 ± 2.28
3.10
14
77.29 ± 1.61
60.59 ± 3.42
78.58 ± 1.97
0.57
28
76.96 ± 1.04
58.68 ± 1.53
78.76 ± 1.28
0.40
Table 9: Effect of patch size on classification performance for the AdrenalMNIST3D dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations.
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
56.11 ± 2.21
52.56 ± 5.22
73.79 ± 2.13
816.97
2
53.33 ± 1.80
49.51 ± 2.45
69.23 ± 2.33
120.2
4
55.97 ± 3.07
47.79 ± 2.99
68.78 ± 0.61
20.3
7
49.86 ± 1.61
44.51 ± 2.67
64.57 ± 1.44
3.16
14
46.11 ± 2.73
40.05 ± 1.54
61.42 ± 1.61
0.58
28
47.22 ± 1.53
42.34 ± 0.95
62.25 ± 0.42
0.41
Table 10: Effect of patch size on classification performance for the FractureMNIST3D dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations.
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
85.38 ± 1.06
76.91 ± 2.58
89.41 ± 0.39
811.74
2
84.84 ± 0.70
78.50 ± 0.54
88.79 ± 1.61
119.43
4
84.84 ± 0.70
75.23 ± 0.42
85.75 ± 0.87
20.17
7
83.87 ± 0.95
72.69 ± 0.82
84.84 ± 0.35
3.14
14
83.87 ± 0.70
72.11 ± 1.58
82.06 ± 0.90
0.57
28
84.73 ± 0.40
72.85 ± 2.37
81.49 ± 1.32
0.40
Table 11: Effect of patch size on classification performance for the NoduleMNIST3D dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations.
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
71.02 ± 0.70
56.93 ± 1.84
64.12 ± 1.27
816.97
2
70.74 ± 1.62
53.42 ± 2.32
58.76 ± 2.43
120.20
4
72.73 ± 0.40
50.80 ± 0.65
56.72 ± 4.38
20.30
7
73.58 ± 0.80
51.05 ± 1.49
57.60 ± 2.04
3.16
14
73.01 ± 0.00
50.00 ± 0.00
52.98 ± 0.51
0.58
28
69.70 ± 1.14
50.27 ± 1.05
57.22 ± 0.24
0.41
Table 12: Effect of patch size on classification performance for the SynapseMNIST3D dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations.
Patch size
Acc. (%)
Bal. Acc. (%)
AUC (%)
GFLOPs
1
92.76 ± 0.33
79.67 ± 1.41
94.28 ± 1.48
812.72
2
91.19 ± 0.65
72.70 ± 5.38
90.74 ± 1.05
119.57
4
90.49 ± 1.23
70.95 ± 5.23
87.49 ± 2.96
20.19
7
88.05 ± 1.58
57.05 ± 1.92
73.89 ± 1.55
3.14
14
87.78 ± 0.49
55.89 ± 3.09
74.91 ± 0.32
0.57
28
88.31 ± 0.99
57.88 ± 5.05
74.59 ± 0.82
0.40
Table 13: Effect of patch size on classification performance for the VesselMNIST3D dataset. Acc.: Accuracy, Bal. Acc.: Balanced Accuracy, AUC: Area under the ROC curve, GFLOPs: Giga Floating-Point Operations.
Figure 4: Comparison of attention maps for patch sizes of 2 and 28 for some example images. The figure shows original images with patch grids (left three columns) and corresponding attention heatmaps (right two columns). Models with smaller patch sizes (P2) consistently achieve correct predictions with more focused attention patterns, while larger patch sizes (P28) lead to incorrect predictions with less informative attention distributions.
Figure 5: Effect of patch size on vision transformer classification performance across 12 medical image datasets (seven 2D and five 3D datasets from the MedMNIST V2 collection)
Figure 6: Effect of patch size on vision transformer performance: average across different dataset groups
Vision Transformers (ViTs) have emerged as the state-of-the-art architecture in representation learning, leveraging self-attention mechanisms to excel in various tasks. ViTs split images into fixed-size patches, constraining them to a predefined size and necessitating pre-processing steps like resizing, padding, or cropping. This poses challenges in medical imaging, particularly with irregularly shaped structures like tumors. A fixed bounding box crop size produces input images with highly variable foreground-to-background ratios. Resizing medical images can degrade information and introduce artefacts, impacting diagnosis. Hence, tailoring variable-sized crops to regions of interest can enhance feature representation capabilities. Moreover, large images are computationally expensive, and smaller sizes risk information loss, presenting a computation-accuracy tradeoff. We propose VariViT, an improved ViT model crafted to handle variable image sizes while maintaining a consistent patch size. VariViT employs a novel positional embedding resizing scheme for a variable number of patches. We also implement a new batching strategy within VariViT to reduce computational complexity, resulting in faster training and inference times. In our evaluations on two 3D brain MRI datasets, VariViT surpasses vanilla ViTs and ResNet in glioma genotype prediction and brain tumor classification. It achieves F1-scores of 75.5% and 76.3%, respectively, learning more discriminative features. Our proposed batching strategy reduces computation time by up to 30% compared to conventional architectures. These findings underscore the efficacy of VariViT in image representation learning. Our code can be found here: https://github.com/Aswathi-Varma/varivit
Department of Neuroradiology, Technical University of Munich · Institute for Artificial Intelligence and Informatics in Medicine, Technical University of Munich · Department of Quantitative Biomedicine, University of Zurich +1
Vision Transformers rely on positional embeddings and class tokens encoding fixed spatial priors. While effective for natural images, these priors may be suboptimal when spatial layout is weakly informative, a frequent condition in medical imaging. We introduce ZACH-ViT (Zero-token Adaptive Compact Hierarchical Vision Transformer), a compact Vision Transformer that removes positional embeddings and the [CLS] token, achieving permutation-invariant patch processing via global average pooling. Zero-token denotes removal of the dedicated aggregation token and positional encodings. Patch tokens remain unchanged. Adaptive residual projections preserve training stability under strict parameter constraints. We evaluate ZACH-ViT across seven MedMNIST datasets under a strict few-shot protocol (50 samples/class, fixed hyperparameters, five seeds). Results reveal regime-dependent behavior: ZACH-ViT (0.25M parameters, trained from scratch) achieves strongest advantage on BloodMNIST and remains competitive on PathMNIST, while relative advantage decreases on datasets with stronger anatomical priors (OCTMNIST, OrganAMNIST), consistent with our hypothesis. Component and pooling ablations show positional support becomes mildly beneficial as spatial structure increases, whereas reintroducing a [CLS] token is consistently unfavorable. These findings support that architectural alignment with data structure can outweigh universal benchmark dominance. Despite minimal size and no pretraining, ZACH-ViT achieves competitive performance under data-scarce conditions, relevant for compact medical imaging and low-resource settings. Code: https://github.com/Bluesman79/ZACH-ViT
Vision Transformer (ViT) has brought new breakthroughs to the field of image classification by introducing the self-attention mechanism and Graph Convolutional Networks(GCN) have been proposed and successfully applied in data representation and analysis. However, there are key challenges which limit their further development: (1) The patch size selected by ViT is crucial for accurate predictions, which raises a natural question: How to select the size of patches properly or how to comprehensively combine small patches and larger patches; (2) While the spatial structure information is important in vision tasks, the 1D position embeddings fails to capture the spatial structure information of patches more accurately; (3) The GCN can capture the local connectivity relationships between image nodes, but it lacks the ability to capture global graph structural information. On the contrary, the self-attention mechanism of ViT can draw the global relation on image patches, but it is unable to model the local structure of image. To overcome such limitations, we propose the Hierarchical Vision Transformer Enhanced by Graph Convolutional Network (GCN-HViT) for image classification. Specifically, the Hierarchical ViT we designed can model patch-wise information interactions on a global scale within each level and model hierarchical relationships between small patches and large patches across multiple levels. In addition, the proposed GCN method functions as a local feature extractor to obtain the local representation of each image patch which serves as a 2D position embedding of each patch in the 2D space. Meanwhile, it models patch-wise information interactions on a local scale within each level. Extensive experiments on 3 real-world datasets demonstrate that GCN-HViT achieves state-of-the-art performance.