Class: Transformers::ImageClassificationPipeline

Inherits:
Pipeline
  • Object
show all
Extended by:
ClassAttribute
Defined in:
lib/transformers/pipelines/image_classification.rb

Instance Method Summary collapse

Methods included from ClassAttribute

class_attribute

Methods inherited from Pipeline

#_ensure_tensor_on_device, #call, #check_model_type, #get_iterator, #torch_dtype

Constructor Details

#initialize(*args, **kwargs) ⇒ ImageClassificationPipeline

Returns a new instance of ImageClassificationPipeline.



13
14
15
16
17
# File 'lib/transformers/pipelines/image_classification.rb', line 13

def initialize(*args, **kwargs)
  super(*args, **kwargs)
  Utils.requires_backends(self, "vision")
  check_model_type(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES)
end

Instance Method Details

#_forward(model_inputs) ⇒ Object



47
48
49
50
# File 'lib/transformers/pipelines/image_classification.rb', line 47

def _forward(model_inputs)
  model_outputs = @model.(**model_inputs)
  model_outputs
end

#_sanitize_parameters(top_k: nil, function_to_apply: nil, timeout: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/transformers/pipelines/image_classification.rb', line 19

def _sanitize_parameters(top_k: nil, function_to_apply: nil, timeout: nil)
  preprocess_params = {}
  if !timeout.nil?
    preprocess_params[:timeout] = timeout
  end
  postprocess_params = {}
  if !top_k.nil?
    postprocess_params[:top_k] = top_k
  end
  if function_to_apply.is_a?(String)
    function_to_apply = ClassificationFunction.new(function_to_apply.downcase).to_s
  end
  if !function_to_apply.nil?
    postprocess_params[:function_to_apply] = function_to_apply
  end
  [preprocess_params, {}, postprocess_params]
end

#postprocess(model_outputs, function_to_apply: nil, top_k: 5) ⇒ Object



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
# File 'lib/transformers/pipelines/image_classification.rb', line 52

def postprocess(model_outputs, function_to_apply: nil, top_k: 5)
  if function_to_apply.nil?
    if @model.config.problem_type == "multi_label_classification" || @model.config.num_labels == 1
      function_to_apply = ClassificationFunction::SIGMOID
    elsif @model.config.problem_type == "single_label_classification" || @model.config.num_labels > 1
      function_to_apply = ClassificationFunction::SOFTMAX
    elsif @model.config.instance_variable_defined?(:@function_to_apply) && function_to_apply.nil?
      function_to_apply = @model.config.function_to_apply
    else
      function_to_apply = ClassificationFunction::NONE
    end
  end

  if top_k > @model.config.num_labels
    top_k = @model.config.num_labels
  end

  outputs = model_outputs[:logits][0]
  if @framework == "pt" && [Torch.bfloat16, Torch.float16].include?(outputs.dtype)
    outputs = outputs.to(Torch.float32).numo
  else
    outputs = outputs.numo
  end

  if function_to_apply == ClassificationFunction::SIGMOID
    scores = sigmoid(outputs)
  elsif function_to_apply == ClassificationFunction::SOFTMAX
    scores = softmax(outputs)
  elsif function_to_apply == ClassificationFunction::NONE
    scores = outputs
  else
    raise ArgumentError, "Unrecognized `function_to_apply` argument: #{function_to_apply}"
  end

  dict_scores =
    scores.to_a.map.with_index do |score, i|
      {label: @model.config.id2label[i], score: score}
    end
  dict_scores.sort_by! { |x| -x[:score] }
  if !top_k.nil?
    dict_scores = dict_scores[...top_k]
  end

  dict_scores
end

#preprocess(image, timeout: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/transformers/pipelines/image_classification.rb', line 37

def preprocess(image, timeout: nil)
  image = ImageUtils.load_image(image, timeout: timeout)
  model_inputs = @image_processor.(image, return_tensors: @framework)
  if @framework == "pt"
    # TODO
    # model_inputs = model_inputs.to(torch_dtype)
  end
  model_inputs
end