vision_text_dual_encoder
mindnlp.transformers.models.vision_text_dual_encoder.configuration_vision_text_dual_encoder
¶
VisionTextDualEncoder model configuration
mindnlp.transformers.models.vision_text_dual_encoder.configuration_vision_text_dual_encoder.VisionTextDualEncoderConfig
¶
Bases: PretrainedConfig
[VisionTextDualEncoderConfig] is the configuration class to store the configuration of a
[VisionTextDualEncoderModel]. It is used to instantiate [VisionTextDualEncoderModel] model according to the
specified arguments, defining the text model and vision model configs.
Configuration objects inherit from [PretrainedConfig] and can be used to control the model outputs. Read the
documentation from [PretrainedConfig] for more information.
| PARAMETER | DESCRIPTION |
|---|---|
projection_dim
|
Dimentionality of text and vision projection layers.
TYPE:
|
logit_scale_init_value
|
The inital value of the logit_scale paramter. Default is used as per the original CLIP implementation.
TYPE:
|
kwargs
|
Dictionary of keyword arguments.
TYPE:
|
Example
>>> from transformers import ViTConfig, BertConfig, VisionTextDualEncoderConfig, VisionTextDualEncoderModel
...
>>> # Initializing a BERT and ViT configuration
>>> config_vision = ViTConfig()
>>> config_text = BertConfig()
...
>>> config = VisionTextDualEncoderConfig.from_vision_text_configs(config_vision, config_text, projection_dim=512)
...
>>> # Initializing a BERT and ViT model (with random weights)
>>> model = VisionTextDualEncoderModel(config=config)
...
>>> # Accessing the model configuration
>>> config_vision = model.config.vision_config
>>> config_text = model.config.text_config
...
>>> # Saving the model, including its configuration
>>> model.save_pretrained("vit-bert")
...
>>> # loading model and config from pretrained folder
>>> vision_text_config = VisionTextDualEncoderConfig.from_pretrained("vit-bert")
>>> model = VisionTextDualEncoderModel.from_pretrained("vit-bert", config=vision_text_config)
Source code in mindnlp\transformers\models\vision_text_dual_encoder\configuration_vision_text_dual_encoder.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 | |
mindnlp.transformers.models.vision_text_dual_encoder.configuration_vision_text_dual_encoder.VisionTextDualEncoderConfig.from_vision_text_configs(vision_config, text_config, **kwargs)
classmethod
¶
Instantiate a [VisionTextDualEncoderConfig] (or a derived class) from text model configuration and vision
model configuration.
| RETURNS | DESCRIPTION |
|---|---|
|
[ |
Source code in mindnlp\transformers\models\vision_text_dual_encoder\configuration_vision_text_dual_encoder.py
104 105 106 107 108 109 110 111 112 113 114 | |
mindnlp.transformers.models.vision_text_dual_encoder.modeling_vision_text_dual_encoder
¶
MindSpore VisionTextDualEncoder model.
mindnlp.transformers.models.vision_text_dual_encoder.modeling_vision_text_dual_encoder.VisionTextDualEncoderModel
¶
Bases: PreTrainedModel
Source code in mindnlp\transformers\models\vision_text_dual_encoder\modeling_vision_text_dual_encoder.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
mindnlp.transformers.models.vision_text_dual_encoder.modeling_vision_text_dual_encoder.VisionTextDualEncoderModel.forward(input_ids=None, pixel_values=None, attention_mask=None, position_ids=None, return_loss=None, token_type_ids=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
| RETURNS | DESCRIPTION |
|---|---|
Union[Tuple[Tensor], CLIPOutput]
|
Union[Tuple[ms.Tensor], CLIPOutput] |
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import (
... VisionTextDualEncoderModel,
... VisionTextDualEncoderProcessor,
... AutoImageProcessor,
... AutoTokenizer,
... )
...
>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
>>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
>>> processor = VisionTextDualEncoderProcessor(image_processor, tokenizer)
>>> model = VisionTextDualEncoderModel.from_vision_text_pretrained(
... "google/vit-base-patch16-224", "google-bert/bert-base-uncased"
... )
...
>>> # contrastive training
>>> urls = [
... "http://images.cocodataset.org/val2017/000000039769.jpg",
... "https://farm3.staticflickr.com/2674/5850229113_4fe05d5265_z.jpg",
... ]
>>> images = [Image.open(requests.get(url, stream=True).raw) for url in urls]
>>> inputs = processor(
... text=["a photo of a cat", "a photo of a dog"], images=images, return_tensors="ms", padding=True
... )
>>> outputs = model(
... input_ids=inputs.input_ids,
... attention_mask=inputs.attention_mask,
... pixel_values=inputs.pixel_values,
... return_loss=True,
... )
>>> loss, logits_per_image = outputs.loss, outputs.logits_per_image # this is the image-text similarity score
...
>>> # save and load from pretrained
>>> model.save_pretrained("vit-bert")
>>> model = VisionTextDualEncoderModel.from_pretrained("vit-bert")
...
>>> # inference
>>> outputs = model(**inputs)
>>> logits_per_image = outputs.logits_per_image # this is the image-text similarity score
>>> probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
Source code in mindnlp\transformers\models\vision_text_dual_encoder\modeling_vision_text_dual_encoder.py
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 | |
mindnlp.transformers.models.vision_text_dual_encoder.modeling_vision_text_dual_encoder.VisionTextDualEncoderModel.from_vision_text_pretrained(*model_args, vision_model_name_or_path=None, text_model_name_or_path=None, **kwargs)
classmethod
¶
| PARAMETER | DESCRIPTION |
|---|---|
vision_model_name_or_path
|
Information necessary to initiate the vision model. Can be either:
TYPE:
|
text_model_name_or_path
|
Information necessary to initiate the text model. Can be either:
TYPE:
|
model_args
|
All remaning positional arguments will be passed to the underlying model's
TYPE:
|
kwargs
|
Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,
Behaves differently depending on whether a
TYPE:
|
Example
>>> from transformers import VisionTextDualEncoderModel
...
>>> # initialize a model from pretrained ViT and BERT models. Note that the projection layers will be randomly initialized.
>>> model = VisionTextDualEncoderModel.from_vision_text_pretrained(
... "google/vit-base-patch16-224", "google-bert/bert-base-uncased"
... )
>>> # saving model after fine-tuning
>>> model.save_pretrained("./vit-bert")
>>> # load fine-tuned model
>>> model = VisionTextDualEncoderModel.from_pretrained("./vit-bert")
Source code in mindnlp\transformers\models\vision_text_dual_encoder\modeling_vision_text_dual_encoder.py
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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
mindnlp.transformers.models.vision_text_dual_encoder.modeling_vision_text_dual_encoder.VisionTextDualEncoderModel.get_image_features(pixel_values=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
| RETURNS | DESCRIPTION |
|---|---|
image_features
|
The image embeddings obtained by
applying the projection layer to the pooled output of [
TYPE:
|
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import VisionTextDualEncoderModel, AutoImageProcessor
...
>>> model = VisionTextDualEncoderModel.from_pretrained("clip-italian/clip-italian")
>>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
...
>>> inputs = image_processor(images=image, return_tensors="ms")
...
>>> image_features = model.get_image_features(**inputs)
Source code in mindnlp\transformers\models\vision_text_dual_encoder\modeling_vision_text_dual_encoder.py
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 | |
mindnlp.transformers.models.vision_text_dual_encoder.modeling_vision_text_dual_encoder.VisionTextDualEncoderModel.get_text_features(input_ids=None, attention_mask=None, position_ids=None, token_type_ids=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
| RETURNS | DESCRIPTION |
|---|---|
text_features
|
The text embeddings obtained by
applying the projection layer to the pooled output of [
TYPE:
|
Example
>>> from transformers import VisionTextDualEncoderModel, AutoTokenizer
...
>>> model = VisionTextDualEncoderModel.from_pretrained("clip-italian/clip-italian")
>>> tokenizer = AutoTokenizer.from_pretrained("clip-italian/clip-italian")
...
>>> inputs = tokenizer(["una foto di un gatto", "una foto di un cane"], padding=True, return_tensors="ms")
>>> text_features = model.get_text_features(**inputs)
Source code in mindnlp\transformers\models\vision_text_dual_encoder\modeling_vision_text_dual_encoder.py
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 | |
mindnlp.transformers.models.vision_text_dual_encoder.processing_vision_text_dual_encoder
¶
Processor class for VisionTextDualEncoder
mindnlp.transformers.models.vision_text_dual_encoder.processing_vision_text_dual_encoder.VisionTextDualEncoderProcessor
¶
Bases: ProcessorMixin
Constructs a VisionTextDualEncoder processor which wraps an image processor and a tokenizer into a single processor.
[VisionTextDualEncoderProcessor] offers all the functionalities of [AutoImageProcessor] and [AutoTokenizer].
See the [~VisionTextDualEncoderProcessor.__call__] and [~VisionTextDualEncoderProcessor.decode] for more
information.
| PARAMETER | DESCRIPTION |
|---|---|
image_processor
|
The image processor is a required input.
TYPE:
|
tokenizer
|
The tokenizer is a required input.
TYPE:
|
Source code in mindnlp\transformers\models\vision_text_dual_encoder\processing_vision_text_dual_encoder.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 | |
mindnlp.transformers.models.vision_text_dual_encoder.processing_vision_text_dual_encoder.VisionTextDualEncoderProcessor.__call__(text=None, images=None, return_tensors=None, **kwargs)
¶
Main method to prepare for the model one or several sequences(s) and image(s). This method forwards the text
and kwargs arguments to VisionTextDualEncoderTokenizer's [~PreTrainedTokenizer.__call__] if text is not
None to encode the text. To prepare the image(s), this method forwards the images and kwargs arguments to
AutoImageProcessor's [~AutoImageProcessor.__call__] if images is not None. Please refer to the doctsring
of the above two methods for more information.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
(pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
TYPE:
|
return_tensors
|
If set, will return tensors of a particular framework. Acceptable values are:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
[
|
Source code in mindnlp\transformers\models\vision_text_dual_encoder\processing_vision_text_dual_encoder.py
64 65 66 67 68 69 70 71 72 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 | |
mindnlp.transformers.models.vision_text_dual_encoder.processing_vision_text_dual_encoder.VisionTextDualEncoderProcessor.batch_decode(*args, **kwargs)
¶
This method forwards all its arguments to VisionTextDualEncoderTokenizer's
[~PreTrainedTokenizer.batch_decode]. Please refer to the docstring of this method for more information.
Source code in mindnlp\transformers\models\vision_text_dual_encoder\processing_vision_text_dual_encoder.py
118 119 120 121 122 123 | |
mindnlp.transformers.models.vision_text_dual_encoder.processing_vision_text_dual_encoder.VisionTextDualEncoderProcessor.decode(*args, **kwargs)
¶
This method forwards all its arguments to VisionTextDualEncoderTokenizer's [~PreTrainedTokenizer.decode].
Please refer to the docstring of this method for more information.
Source code in mindnlp\transformers\models\vision_text_dual_encoder\processing_vision_text_dual_encoder.py
125 126 127 128 129 130 | |