Class: Transformers::Vit::ViTImageProcessor

Inherits:
BaseImageProcessor show all
Defined in:
lib/transformers/models/vit/image_processing_vit.rb

Instance Method Summary collapse

Methods inherited from BaseImageProcessor

#call, #normalize, #rescale

Methods inherited from ImageProcessingMixin

from_dict, from_pretrained, get_image_processor_dict

Constructor Details

#initialize(do_resize: true, size: nil, resample: :bilinear, do_rescale: true, rescale_factor: 1 / 255.0, do_normalize: true, image_mean: nil, image_std: nil, **kwargs) ⇒ ViTImageProcessor

Returns a new instance of ViTImageProcessor.



18
19
20
21
22
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
# File 'lib/transformers/models/vit/image_processing_vit.rb', line 18

def initialize(
  do_resize: true,
  size: nil,
  resample: :bilinear,
  do_rescale: true,
  rescale_factor: 1 / 255.0,
  do_normalize: true,
  image_mean: nil,
  image_std: nil,
  **kwargs
)
  super(**kwargs)
  size = !size.nil? ? size : {height: 224, width: 224}
  size = ImageProcessingUtils.get_size_dict(size)
  @do_resize = do_resize
  @do_rescale = do_rescale
  @do_normalize = do_normalize
  @size = size
  @resample = resample
  @rescale_factor = rescale_factor
  @image_mean = !image_mean.nil? ? image_mean : IMAGENET_STANDARD_MEAN
  @image_std = !image_std.nil? ? image_std : IMAGENET_STANDARD_STD
  @valid_processor_keys = [
    :images,
    :do_resize,
    :size,
    :resample,
    :do_rescale,
    :rescale_factor,
    :do_normalize,
    :image_mean,
    :image_std,
    :return_tensors,
    :data_format,
    :input_data_format
  ]
end

Instance Method Details

#preprocess(images, do_resize: nil, size: nil, resample: nil, do_rescale: nil, rescale_factor: nil, do_normalize: nil, image_mean: nil, image_std: nil, return_tensors: nil, data_format: ChannelDimension::FIRST, input_data_format: nil, **kwargs) ⇒ Object



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
# File 'lib/transformers/models/vit/image_processing_vit.rb', line 79

def preprocess(
  images,
  do_resize: nil,
  size: nil,
  resample: nil,
  do_rescale: nil,
  rescale_factor: nil,
  do_normalize: nil,
  image_mean: nil,
  image_std: nil,
  return_tensors: nil,
  data_format: ChannelDimension::FIRST,
  input_data_format: nil,
  **kwargs
)
  do_resize = !do_resize.nil? ? do_resize : @do_resize
  do_rescale = !do_rescale.nil? ? do_rescale : @do_rescale
  do_normalize = !do_normalize.nil? ? do_normalize : @do_normalize
  resample = !resample.nil? ? resample : @resample
  rescale_factor = !rescale_factor.nil? ? rescale_factor : @rescale_factor
  image_mean = !image_mean.nil? ? image_mean : @image_mean
  image_std = !image_std.nil? ? image_std : @image_std

  size = !size.nil? ? size : @size
  size_dict = ImageProcessingUtils.get_size_dict(size)

  images = ImageUtils.make_list_of_images(images)

  ImageUtils.validate_kwargs(captured_kwargs: kwargs.keys, valid_processor_keys: @valid_processor_keys)

  if !ImageUtils.valid_images(images)
    raise ArgumentError,
      "Invalid image type. Must be of type Vips::Image, Numo::NArray, or Torch::Tensor."
  end
  ImageUtils.validate_preprocess_arguments(
    do_rescale: do_rescale,
    rescale_factor: rescale_factor,
    do_normalize: do_normalize,
    image_mean: image_mean,
    image_std: image_std,
    do_resize: do_resize,
    size: size,
    resample: resample
  )

  # All transformations expect numo arrays.
  images = images.map { |image| ImageUtils.to_numo_array(image) }

  if ImageUtils.is_scaled_image(images[0]) && do_rescale
    Transformers.logger.warn(
      "It looks like you are trying to rescale already rescaled images. If the input" +
      " images have pixel values between 0 and 1, set `do_rescale: false` to avoid rescaling them again."
    )
  end

  if input_data_format.nil?
    # We assume that all images have the same channel dimension format.
    input_data_format = ImageUtils.infer_channel_dimension_format(images[0])
  end

  if do_resize
    images =
      images.map do |image|
        resize(image, size_dict, resample: resample, input_data_format: input_data_format)
      end
  end

  if do_rescale
    images =
      images.map do |image|
        rescale(image, rescale_factor, input_data_format: input_data_format)
      end
  end

  if do_normalize
    images =
      images.map do |image|
        normalize(image, image_mean, image_std, input_data_format: input_data_format)
      end
  end

  images =
    images.map do |image|
      ImageTransforms.to_channel_dimension_format(image, data_format, input_channel_dim: input_data_format)
    end

  data = {pixel_values: images}
  BatchFeature.new(data: data, tensor_type: return_tensors)
end

#resize(image, size, resample: :bilinear, data_format: nil, input_data_format: nil, **kwargs) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/transformers/models/vit/image_processing_vit.rb', line 56

def resize(
  image,
  size,
  resample: :bilinear,
  data_format: nil,
  input_data_format: nil,
  **kwargs
)
  size = ImageProcessingUtils.get_size_dict(size)
  if !size.include?(:height) || !size.include?(:width)
    raise ArgumentError, "The `size` dictionary must contain the keys `height` and `width`. Got #{size.keys}"
  end
  output_size = [size[:height], size[:width]]
  ImageTransforms.resize(
    image,
    output_size,
    resample: resample,
    data_format: data_format,
    input_data_format: input_data_format,
    **kwargs
  )
end