|
175 | 175 | IMAGE_PROCESSOR_MAPPING = _LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES)
|
176 | 176 |
|
177 | 177 |
|
178 |
| -def image_processor_class_from_name(class_name: str): |
| 178 | +def get_image_processor_class_from_name(class_name: str): |
179 | 179 | if class_name == "BaseImageProcessorFast":
|
180 | 180 | return BaseImageProcessorFast
|
181 | 181 |
|
@@ -368,7 +368,7 @@ def from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs):
|
368 | 368 | identifier allowed by git.
|
369 | 369 | use_fast (`bool`, *optional*, defaults to `False`):
|
370 | 370 | Use a fast torchvision-base image processor if it is supported for a given model.
|
371 |
| - If a fast tokenizer is not available for a given model, a normal numpy-based image processor |
| 371 | + If a fast image processor is not available for a given model, a normal numpy-based image processor |
372 | 372 | is returned instead.
|
373 | 373 | return_unused_kwargs (`bool`, *optional*, defaults to `False`):
|
374 | 374 | If `False`, then this function returns just the final image processor object. If `True`, then this
|
@@ -416,6 +416,7 @@ def from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs):
|
416 | 416 | kwargs["token"] = use_auth_token
|
417 | 417 |
|
418 | 418 | config = kwargs.pop("config", None)
|
| 419 | + # TODO: @yoni, change in v4.48 (use_fast set to True by default) |
419 | 420 | use_fast = kwargs.pop("use_fast", None)
|
420 | 421 | trust_remote_code = kwargs.pop("trust_remote_code", None)
|
421 | 422 | kwargs["_from_auto"] = True
|
@@ -451,42 +452,71 @@ def from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs):
|
451 | 452 | if not is_timm_config_dict(config_dict):
|
452 | 453 | raise initial_exception
|
453 | 454 |
|
454 |
| - image_processor_class = config_dict.get("image_processor_type", None) |
| 455 | + image_processor_type = config_dict.get("image_processor_type", None) |
455 | 456 | image_processor_auto_map = None
|
456 | 457 | if "AutoImageProcessor" in config_dict.get("auto_map", {}):
|
457 | 458 | image_processor_auto_map = config_dict["auto_map"]["AutoImageProcessor"]
|
458 | 459 |
|
459 | 460 | # If we still don't have the image processor class, check if we're loading from a previous feature extractor config
|
460 | 461 | # and if so, infer the image processor class from there.
|
461 |
| - if image_processor_class is None and image_processor_auto_map is None: |
| 462 | + if image_processor_type is None and image_processor_auto_map is None: |
462 | 463 | feature_extractor_class = config_dict.pop("feature_extractor_type", None)
|
463 | 464 | if feature_extractor_class is not None:
|
464 |
| - image_processor_class = feature_extractor_class.replace("FeatureExtractor", "ImageProcessor") |
| 465 | + image_processor_type = feature_extractor_class.replace("FeatureExtractor", "ImageProcessor") |
465 | 466 | if "AutoFeatureExtractor" in config_dict.get("auto_map", {}):
|
466 | 467 | feature_extractor_auto_map = config_dict["auto_map"]["AutoFeatureExtractor"]
|
467 | 468 | image_processor_auto_map = feature_extractor_auto_map.replace("FeatureExtractor", "ImageProcessor")
|
468 | 469 |
|
469 | 470 | # If we don't find the image processor class in the image processor config, let's try the model config.
|
470 |
| - if image_processor_class is None and image_processor_auto_map is None: |
| 471 | + if image_processor_type is None and image_processor_auto_map is None: |
471 | 472 | if not isinstance(config, PretrainedConfig):
|
472 | 473 | config = AutoConfig.from_pretrained(
|
473 | 474 | pretrained_model_name_or_path,
|
474 | 475 | trust_remote_code=trust_remote_code,
|
475 | 476 | **kwargs,
|
476 | 477 | )
|
477 | 478 | # It could be in `config.image_processor_type``
|
478 |
| - image_processor_class = getattr(config, "image_processor_type", None) |
| 479 | + image_processor_type = getattr(config, "image_processor_type", None) |
479 | 480 | if hasattr(config, "auto_map") and "AutoImageProcessor" in config.auto_map:
|
480 | 481 | image_processor_auto_map = config.auto_map["AutoImageProcessor"]
|
481 | 482 |
|
482 |
| - if image_processor_class is not None: |
483 |
| - # Update class name to reflect the use_fast option. If class is not found, None is returned. |
484 |
| - if use_fast is not None: |
485 |
| - if use_fast and not image_processor_class.endswith("Fast"): |
486 |
| - image_processor_class += "Fast" |
487 |
| - elif not use_fast and image_processor_class.endswith("Fast"): |
488 |
| - image_processor_class = image_processor_class[:-4] |
489 |
| - image_processor_class = image_processor_class_from_name(image_processor_class) |
| 483 | + image_processor_class = None |
| 484 | + # TODO: @yoni, change logic in v4.48 (when use_fast set to True by default) |
| 485 | + if image_processor_type is not None: |
| 486 | + # if use_fast is not set and the processor was saved with a fast processor, we use it, otherwise we use the slow processor. |
| 487 | + if use_fast is None: |
| 488 | + use_fast = image_processor_type.endswith("Fast") |
| 489 | + if not use_fast: |
| 490 | + logger.warning_once( |
| 491 | + "Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model. " |
| 492 | + "`use_fast=True` will be the default behavior in v4.48, even if the model was saved with a slow processor. " |
| 493 | + "This will result in minor differences in outputs. You'll still be able to use a slow processor with `use_fast=False`." |
| 494 | + ) |
| 495 | + # Update class name to reflect the use_fast option. If class is not found, we fall back to the slow version. |
| 496 | + if use_fast and not is_torchvision_available(): |
| 497 | + logger.warning_once( |
| 498 | + "Using `use_fast=True` but `torchvision` is not available. Falling back to the slow image processor." |
| 499 | + ) |
| 500 | + use_fast = False |
| 501 | + if use_fast: |
| 502 | + if not image_processor_type.endswith("Fast"): |
| 503 | + image_processor_type += "Fast" |
| 504 | + for _, image_processors in IMAGE_PROCESSOR_MAPPING_NAMES.items(): |
| 505 | + if image_processor_type in image_processors: |
| 506 | + break |
| 507 | + else: |
| 508 | + image_processor_type = image_processor_type[:-4] |
| 509 | + use_fast = False |
| 510 | + logger.warning_once( |
| 511 | + "`use_fast` is set to `True` but the image processor class does not have a fast version. " |
| 512 | + " Falling back to the slow version." |
| 513 | + ) |
| 514 | + image_processor_class = get_image_processor_class_from_name(image_processor_type) |
| 515 | + else: |
| 516 | + image_processor_type = ( |
| 517 | + image_processor_type[:-4] if image_processor_type.endswith("Fast") else image_processor_type |
| 518 | + ) |
| 519 | + image_processor_class = get_image_processor_class_from_name(image_processor_type) |
490 | 520 |
|
491 | 521 | has_remote_code = image_processor_auto_map is not None
|
492 | 522 | has_local_code = image_processor_class is not None or type(config) in IMAGE_PROCESSOR_MAPPING
|
|
0 commit comments