Visual Prompting (Fine-tuning) ================= Visual prompting is a computer vision task that uses a combination of an image and prompts, such as texts, bounding boxes, points, and so on to troubleshoot problems. Using these useful prompts, the main purpose of this task is to obtain labels from unlabeled datasets, and to use generated label information on particular domains or to develop a new model with the generated information. This section examines the solutions for visual prompting offered by the OpenVINO Training Extensions library. `Segment Anything (SAM) `_, is one of the most famous visual prompting methods and this model will be used to adapt a new dataset domain. Because `SAM `_ was trained by using web-scale dataset and has huge backbone network, fine-tuning the whole network is difficult and lots of resources are required. Therefore, in this section, we try to fine-tune only mask decoder only for several epochs to increase performance on the new dataset domain. For fine-tuning `SAM `_, we use following algorithms components: .. _visual_prompting_finetuning_pipeline: - ``Pre-processing``: Resize an image according to the longest axis and pad the rest with zero. - ``Optimizer``: We use `Adam `_ optimizer. - ``Loss function``: We use standard loss combination, 20 * focal loss + dice loss + iou loss, used in `SAM `_ as it is. - ``Additional training techniques`` - ``Early stopping``: To add adaptability to the training pipeline and prevent overfitting. Early stopping will be automatically applied. .. note:: Currently, fine-tuning `SAM `_ with bounding boxes in the OpenVINO Training Extensions is only supported. We will support fine-tuning with other prompts (points and texts) and continuous fine-tuning with predicted mask information in the near future. .. note:: Currently, Post-Training Quantization (PTQ) for `SAM `_ is only supported, not Quantization Aware Training (QAT). ************** Dataset Format ************** .. _visual_prompting_dataset: For the dataset handling inside OpenVINO™ Training Extensions, we use `Dataset Management Framework (Datumaro) `_. We support three dataset formats for visual prompting: - `Common Semantic Segmentation `_ for semantic segmentation - `COCO `_ for instance segmentation - `Pascal VOC `_ for instance segmentation and semantic segmentation If you organized supported dataset format, starting training will be very simple. We just need to pass a path to the root folder and desired model template to start training: .. code-block:: $ otx train \ --train-data-roots \ --val-data-roots .. note:: During training, mDice for binary mask without label information is used for train/validation metric. After training, if using ``otx eval`` to evaluate performance, mDice for binary or multi-class masks with label information will be used. As you can expect, performance will be different between ``otx train`` and ``otx eval``, but if unlabeled mask performance is high, labeld mask performance is high as well. ****** Models ****** .. _visual_prompting_model: We support the following model templates in experimental phase: +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+---------------------+-----------------+ | Template ID | Name | Complexity (GFLOPs) | Model size (MB) | +============================================================================================================================================================================================+==============+=====================+=================+ | `Visual_Prompting_SAM_Tiny_ViT `_ | SAM_Tiny_ViT | 38.95 | 47 | +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+---------------------+-----------------+ | `Visual_Prompting_SAM_ViT_B `_ | SAM_ViT_B | 483.71 | 362 | +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+---------------------+-----------------+ To check feasibility of `SAM `_, we did experiments using three public datasets with each other domains: `WGISD `_, `Trashcan `_, and `FLARE22 `_, and checked `Dice score `_. We used sampled training data from `Trashcan `_ and `FLARE22 `_, and full training data (=110) from `WGISD `_. +---------------------------------------------------------------+--------------------+ | Dataset | #samples | +===============================================================+====================+ | `WGISD `_ | 110 | +---------------------------------------------------------------+--------------------+ | `Trashcan `_ | 500 | +---------------------------------------------------------------+--------------------+ | `FLARE22 `_ | 1 CT (=100 slices) | +---------------------------------------------------------------+--------------------+ The below table shows performance improvement after fine-tuning. +------------+--------------------------------------------+---------------------------------------------------------------+---------------------------------------------------+ | Model name | `WGISD `_ | `Trashcan `_ | `FLARE22 `_ | +============+============================================+===============================================================+===================================================+ | Tiny_ViT | 90.32 → 92.29 (+1.97) | 82.38 → 85.01 (+2.63) | 89.69 → 93.05 (+3.36) | +------------+--------------------------------------------+---------------------------------------------------------------+---------------------------------------------------+ | ViT_B | 92.32 → 92.46 (+0.14) | 79.61 → 81.50 (+1.89) | 91.48 → 91.68 (+0.20) | +------------+--------------------------------------------+---------------------------------------------------------------+---------------------------------------------------+ According to datasets, ``learning rate`` and ``batch size`` can be adjusted like below: .. code-block:: $ otx train \ --train-data-roots \ --val-data-roots \ params \ --learning_parameters.dataset.train_batch_size \ --learning_parameters.optimizer.lr