Class: Transformers::AutoImageProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/transformers/models/auto/image_processing_auto.rb

Class Method Summary collapse

Class Method Details

.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs) ⇒ Object

Raises:

  • (ArgumentError)


23
24
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
# File 'lib/transformers/models/auto/image_processing_auto.rb', line 23

def self.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
  config = kwargs.delete(:config)
  use_fast = kwargs.delete(:use_fast)
  trust_remote_code = kwargs.delete(:trust_remote_code)
  kwargs[:_from_auto] = true

  config_dict, _ = ImageProcessingMixin.get_image_processor_dict(pretrained_model_name_or_path, **kwargs)
  image_processor_class = config_dict[:image_processor_type]
  image_processor_auto_map = nil
  if (config_dict[:auto_map] || {}).include?("AutoImageProcessor")
    image_processor_auto_map = config_dict[:auto_map]["AutoImageProcessor"]
  end

  # If we still don't have the image processor class, check if we're loading from a previous feature extractor config
  # and if so, infer the image processor class from there.
  if image_processor_class.nil? && image_processor_auto_map.nil?
    feature_extractor_class = config_dict.delete(:feature_extractor_type)
    if !feature_extractor_class.nil?
      image_processor_class = feature_extractor_class.sub("FeatureExtractor", "ImageProcessor")
    end
    if (config_dict[:auto_map] || {}).include?("AutoFeatureExtractor")
      feature_extractor_auto_map = config_dict[:auto_map]["AutoFeatureExtractor"]
      image_processor_auto_map = feature_extractor_auto_map.sub("FeatureExtractor", "ImageProcessor")
    end
  end

  # If we don't find the image processor class in the image processor config, let's try the model config.
  if image_processor_class.nil? && image_processor_auto_map.nil?
    if !config.is_a?(PretrainedConfig)
      config = AutoConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
    end
    # It could be in `config.image_processor_type``
    image_processor_class = config.instance_variable_get(:@image_processor_type)
  end

  if !image_processor_class.nil?
    raise Todo
  end

  has_remote_code = !image_processor_auto_map.nil?
  has_local_code = !image_processor_class.nil? || IMAGE_PROCESSOR_MAPPING.include?(config.class.name.split("::").last)
  trust_remote_code = DynamicModuleUtils.resolve_trust_remote_code(
    trust_remote_code, pretrained_model_name_or_path, has_local_code, has_remote_code
  )

  if !image_processor_auto_map.nil? && !image_processor_auto_map.is_a?(Array)
    raise Todo
  end

  if has_remote_code && trust_remote_code
    raise Todo
  elsif !image_processor_class.nil?
    return image_processor_class.from_dict(config_dict, **kwargs)
  # Last try: we use the IMAGE_PROCESSOR_MAPPING.
  elsif IMAGE_PROCESSOR_MAPPING.include?(config.class.name.split("::").last)
    image_processor_tuple = IMAGE_PROCESSOR_MAPPING[config.class.name.split("::").last]

    image_processor_class_py, image_processor_class_fast = image_processor_tuple

    if !use_fast && !image_processor_class_fast.nil?
      _warning_fast_image_processor_available(image_processor_class_fast)
    end

    if image_processor_class_fast && (use_fast || image_processor_class_py.nil?)
      return image_processor_class_fast.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
    else
      if !image_processor_class_py.nil?
        return image_processor_class_py.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
      else
        raise ArgumentError,
          "This image processor cannot be instantiated. Please make sure you have `Pillow` installed."
      end
    end
  end

  raise ArgumentError,
    "Unrecognized image processor in #{pretrained_model_name_or_path}. Should have a " +
    "`image_processor_type` key in its #{IMAGE_PROCESSOR_NAME} of #{CONFIG_NAME}, or one of the following " +
    "`model_type` keys in its #{CONFIG_NAME}: #{IMAGE_PROCESSOR_MAPPING_NAMES.keys.join(", ")}"
end