Fine-Tune Your Own Vision Transformer¶
In this guide, we will show you how to fine-tune your own Vision Transformer (ViT) model on a custom dataset using fusion_bench
CLI.
FusionBench provides a simple and easy-to-use interface to fine-tune clip vision transformer in a single-task learning setting or traditional multi-task learning setting.
Basic Examples¶
Single-Task Learning¶
Refer to examples/clip_finetune/clip_finetune.sh
for a complete example of fine-tuning a CLIP-ViT model, including full fine-tuning, lora fine-tuning and linearized lora fine-tuning.
Multi-Task Learning¶
Fine-tune CLIP-ViT-B/32:
fusion_bench \
method=clip_finetune \
modelpool=CLIPVisionModelPool/clip-vit-base-patch32_mtl \
taskpool=dummy
Fine-tune CLIP-ViT-L/14 on eight GPUs with a per-device per-task batch size of 2.
fusion_bench \
fabric.devices=8 \
method=clip_finetune \
method.batch_size=2 \
modelpool=CLIPVisionModelPool/clip-vit-base-patch32_mtl \
modelpool.models._pretrained_.pretrained_model_name_or_path=openai/clip-vit-large-patch14 \
taskpool=dummy
This will save the state dict of the vision model (transformers.models.clip.CLIPVisionModel.CLIPVisionTransformer
) to the log directory.
Subsequently, we can use fusion_bench/scripts/clip/convert_checkpoint.py
to convert the state dict to a HuggingFace model (CLIPVisionModel
).
# or CLIP-ViT-L/14, add option: --model openai/clip-vit-large-patch14
python fusion_bench/scripts/clip/convert_checkpoint.py \
--checkpoint /path/to/checkpoint \
--output /path/to/output
After converting the checkpoint, you can use FusionBench to evaluate the model. For example, you can use the following command to evaluate the model on the eight tasks documented here.
path_to_clip_model=/path/to/converted/output
fusion_bench method=dummy \
modelpool=CLIPVisionModelPool/clip-vit-base-patch32_individual \
modelpool.models._pretrained_.pretrained_model_name_or_path="'${path_to_clip_model}'" \
taskpool=clip-vit-classification_TA8
Single-Task Learning¶
Simply remove some of the datasets from the train_datasets
field in the model pool configuration.
References¶
ImageClassificationFineTuningForCLIP
¶
Bases: CLIPClassificationMixin
, SimpleProfilerMixin
, ModelFusionAlgorithm
A class for fine-tuning CLIP models for image classification tasks.
Source code in fusion_bench/method/classification/clip_finetune.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
|
run(modelpool)
¶
Executes the fine-tuning process.
Parameters:
-
modelpool
¶HuggingFaceClipVisionPool
) –The modelpool is responsible for loading the pre-trained model and training datasets.
Returns:
-
VisionModel
–The fine-tuned vision model.
Source code in fusion_bench/method/classification/clip_finetune.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
|
save_model(model, save_path)
¶
Save the vision model to the specified path.
Parameters:
-
model
¶Union[HFCLIPClassifier, CLIPModel, CLIPVisionModel, CLIPVisionTransformer]
) –The model to save.
-
save_path
¶str
) –The path to save the model.
Source code in fusion_bench/method/classification/clip_finetune.py
setup_model()
¶
Sets up the model, optimizer, and learning rate scheduler.
This method initializes the CLIP model, applies LoRA if specified, and configures the optimizer and learning rate scheduler.
Returns:
-
Tuple
–A tuple containing the processor, classifier, optimizer, and learning rate scheduler.