Image Classification Tasks for CLIP Models¶
CLIPVisionModelTaskPool¶
The CLIPVisionModelTaskPool
class is used to define image classification tasks for CLIP models. It provides methods to evaluate the performance of a given model on multiple datasets.
Attributes¶
test_datasets
: A dictionary containing the test datasets.processor
: The processor used for preprocessing the input data. This is used to set up the classifier.data_processor
: The data processor used for processing the input data.clip_model
: The CLIP model used for evaluation.dataloader_kwargs
: Keyword arguments for the data loader.layer_wise_feature_save_path
: Path to save the layer-wise features.layer_wise_feature_first_token_only
: Boolean indicating whether to save only the first token of the features.layer_wise_feature_max_num
: Maximum number of features to save.fast_dev_run
: Boolean indicating whether to run in fast development mode.
Methods¶
setup()
: Sets up the processor, data processor, CLIP model, test datasets, and data loaders.evaluate(model)
: Evaluates the given model on the image classification task.on_task_evaluation_begin(classifier, task_name)
: Called at the beginning of task evaluation to set up hooks for saving layer-wise features.on_task_evaluation_end()
: Called at the end of task evaluation to save features and remove hooks.
Configuration¶
The CLIPVisionModelTaskPool
class can be configured using a YAML file. Here is an example configuration:
test_datasets:
dataset1: ...
dataset2: ...
processor:
_target_: transformers.CLIPProcessor.from_pretrained
pretrained_model_name_or_path: openai/clip-vit-base-patch32
data_processor:
_target_: transformers.CLIPProcessor.from_pretrained
pretrained_model_name_or_path: openai/clip-vit-base-patch32
clip_model:
_target_: transformers.CLIPModel.from_pretrained
pretrained_model_name_or_path: openai/clip-vit-base-patch32
dataloader_kwargs:
batch_size: 32
num_workers: 4
layer_wise_feature_save_path: path/to/save/features
layer_wise_feature_first_token_only: true
layer_wise_feature_max_num: 1000
fast_dev_run: false
References¶
CLIPVisionModelTaskPool
¶
Bases: BaseTaskPool
, LightningFabricMixin
This class is used to define the image classification task for CLIP models.
Attributes:
-
test_datasets
(Union[DictConfig, Dict[str, Dataset]]
) –The test datasets to evaluate the model on.
-
processor
(Union[DictConfig, CLIPProcessor]
) –The processor used for preprocessing the input data.
-
data_processor
(Union[DictConfig, CLIPProcessor]
) –The data processor used for processing the input data.
-
clip_model
(Union[DictConfig, CLIPModel]
) –The CLIP model used for evaluation.
-
dataloader_kwargs
(DictConfig
) –Keyword arguments for the data loader.
-
layer_wise_feature_save_path
(Optional[str]
) –Path to save the layer-wise features.
-
layer_wise_feature_first_token_only
(bool
) –Boolean indicating whether to save only the first token of the features.
-
layer_wise_feature_max_num
(Optional[int]
) –Maximum number of features to save.
-
fast_dev_run
(bool
) –Boolean indicating whether to run in fast development mode.
Source code in fusion_bench/taskpool/clip_vision/taskpool.py
73 74 75 76 77 78 79 80 81 82 83 84 85 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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
__init__(test_datasets, *, processor, data_processor, clip_model, dataloader_kwargs=None, layer_wise_feature_save_path=None, layer_wise_feature_first_token_only=True, layer_wise_feature_max_num=None, fast_dev_run=False, **kwargs)
¶
Initialize the CLIPVisionModelTaskPool.
Source code in fusion_bench/taskpool/clip_vision/taskpool.py
evaluate(model, name=None)
¶
Evaluate the model on the image classification task.
Parameters:
-
model
¶Union[CLIPVisionModel, CLIPVisionTransformer]
) –The model to evaluate.
Returns:
-
–
Dict[str, Any]: A dictionary containing the evaluation results for each task.
Source code in fusion_bench/taskpool/clip_vision/taskpool.py
on_task_evaluation_begin(classifier, task_name)
¶
Called at the beginning of task evaluation to set up hooks for saving layer-wise features.
Parameters:
-
classifier
¶HFCLIPClassifier
) –The classifier being evaluated.
-
task_name
¶str
) –The name of the task being evaluated.
Source code in fusion_bench/taskpool/clip_vision/taskpool.py
on_task_evaluation_end()
¶
Called at the end of task evaluation to save features and remove hooks.
Source code in fusion_bench/taskpool/clip_vision/taskpool.py
setup()
¶
Set up the processor, data processor, CLIP model, test datasets, and data loaders.