informers 1.0.3 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,75 @@
1
1
  module Informers
2
2
  module Utils
3
+ def self.interpolate_data(input, in_shape, out_shape, mode = "bilinear", align_corners = false)
4
+ in_channels, in_height, in_width = in_shape
5
+ out_height, out_width = out_shape
6
+
7
+ # TODO use mode and align_corners
8
+
9
+ # Output image dimensions
10
+ x_scale = out_width / in_width.to_f
11
+ y_scale = out_height / in_height.to_f
12
+
13
+ # Output image
14
+ out_img = Array.new(out_height * out_width * in_channels)
15
+
16
+ # Pre-calculate strides
17
+ in_stride = in_height * in_width
18
+ out_stride = out_height * out_width
19
+
20
+ out_height.times do |i|
21
+ out_width.times do |j|
22
+ # Calculate output offset
23
+ out_offset = i * out_width + j
24
+
25
+ # Calculate input pixel coordinates
26
+ x = (j + 0.5) / x_scale - 0.5
27
+ y = (i + 0.5) / y_scale - 0.5
28
+
29
+ # Calculate the four nearest input pixels
30
+ # We also check if the input pixel coordinates are within the image bounds
31
+ x1 = x.floor
32
+ y1 = y.floor
33
+ x2 = [x1 + 1, in_width - 1].min
34
+ y2 = [y1 + 1, in_height - 1].min
35
+
36
+ x1 = [x1, 0].max
37
+ y1 = [y1, 0].max
38
+
39
+ # Calculate the fractional distances between the input pixel and the four nearest pixels
40
+ s = x - x1
41
+ t = y - y1
42
+
43
+ # Perform bilinear interpolation
44
+ w1 = (1 - s) * (1 - t)
45
+ w2 = s * (1 - t)
46
+ w3 = (1 - s) * t
47
+ w4 = s * t
48
+
49
+ # Calculate the four nearest input pixel indices
50
+ y_stride = y1 * in_width
51
+ x_stride = y2 * in_width
52
+ idx1 = y_stride + x1
53
+ idx2 = y_stride + x2
54
+ idx3 = x_stride + x1
55
+ idx4 = x_stride + x2
56
+
57
+ in_channels.times do |k|
58
+ # Calculate channel offset
59
+ c_offset = k * in_stride
60
+
61
+ out_img[k * out_stride + out_offset] =
62
+ w1 * input[c_offset + idx1] +
63
+ w2 * input[c_offset + idx2] +
64
+ w3 * input[c_offset + idx3] +
65
+ w4 * input[c_offset + idx4]
66
+ end
67
+ end
68
+ end
69
+
70
+ out_img
71
+ end
72
+
3
73
  def self.softmax(arr)
4
74
  # Compute the maximum value in the array
5
75
  max_val = arr.max
@@ -17,6 +87,9 @@ module Informers
17
87
  end
18
88
 
19
89
  def self.sigmoid(arr)
90
+ if arr[0].is_a?(Array)
91
+ return arr.map { |a| sigmoid(a) }
92
+ end
20
93
  arr.map { |v| 1 / (1 + Math.exp(-v)) }
21
94
  end
22
95
 
@@ -22,5 +22,51 @@ module Informers
22
22
  row.map { |v| v / norm }
23
23
  end
24
24
  end
25
+
26
+ def self.stack(tensors, dim = 0)
27
+ tensors
28
+ end
29
+
30
+ def self.ones_like(tensor)
31
+ if tensor[0].is_a?(Array)
32
+ return tensor.map { |v| ones_like(v) }
33
+ end
34
+ tensor.map { |_| 1 }
35
+ end
36
+
37
+ def self.dims(tensor)
38
+ dims = []
39
+ while tensor.is_a?(Array)
40
+ dims << tensor.size
41
+ tensor = tensor[0]
42
+ end
43
+ dims
44
+ end
45
+
46
+ def self.interpolate(input, shape, mode = "bilinear", align_corners = false)
47
+ out_height, out_width = shape
48
+
49
+ # Input image dimensions
50
+ in_channels = dims(input)[-3] || 1
51
+ in_height = dims(input)[-2]
52
+ in_width = dims(input)[-1]
53
+
54
+ output = interpolate_data(
55
+ input.flatten,
56
+ [in_channels, in_height, in_width],
57
+ [out_height, out_width],
58
+ mode,
59
+ align_corners
60
+ )
61
+ reshape(output, [in_channels, out_height, out_width])
62
+ end
63
+
64
+ def self.reshape(arr, dims)
65
+ arr = arr.flatten
66
+ dims[1..-1].reverse_each do |dim|
67
+ arr = arr.each_slice(dim)
68
+ end
69
+ arr.to_a
70
+ end
25
71
  end
26
72
  end
@@ -1,3 +1,3 @@
1
1
  module Informers
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.1"
3
3
  end
data/lib/informers.rb CHANGED
@@ -6,18 +6,24 @@ require "tokenizers"
6
6
  require "io/console"
7
7
  require "json"
8
8
  require "open-uri"
9
+ require "open3"
9
10
  require "stringio"
10
11
  require "uri"
11
12
 
12
13
  # modules
14
+ require_relative "informers/utils/audio"
13
15
  require_relative "informers/utils/core"
16
+ require_relative "informers/utils/generation"
17
+ require_relative "informers/utils/ffmpeg"
14
18
  require_relative "informers/utils/hub"
19
+ require_relative "informers/utils/image"
15
20
  require_relative "informers/utils/math"
16
21
  require_relative "informers/utils/tensor"
17
22
  require_relative "informers/configs"
18
23
  require_relative "informers/env"
19
24
  require_relative "informers/model"
20
25
  require_relative "informers/models"
26
+ require_relative "informers/processors"
21
27
  require_relative "informers/tokenizers"
22
28
  require_relative "informers/version"
23
29
  require_relative "informers/pipelines"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: informers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-29 00:00:00.000000000 Z
11
+ date: 2024-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: onnxruntime
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.5.2
33
+ version: 0.5.3
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 0.5.2
40
+ version: 0.5.3
41
41
  description:
42
42
  email: andrew@ankane.org
43
43
  executables: []
@@ -53,9 +53,14 @@ files:
53
53
  - lib/informers/model.rb
54
54
  - lib/informers/models.rb
55
55
  - lib/informers/pipelines.rb
56
+ - lib/informers/processors.rb
56
57
  - lib/informers/tokenizers.rb
58
+ - lib/informers/utils/audio.rb
57
59
  - lib/informers/utils/core.rb
60
+ - lib/informers/utils/ffmpeg.rb
61
+ - lib/informers/utils/generation.rb
58
62
  - lib/informers/utils/hub.rb
63
+ - lib/informers/utils/image.rb
59
64
  - lib/informers/utils/math.rb
60
65
  - lib/informers/utils/tensor.rb
61
66
  - lib/informers/version.rb
@@ -78,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
83
  - !ruby/object:Gem::Version
79
84
  version: '0'
80
85
  requirements: []
81
- rubygems_version: 3.5.11
86
+ rubygems_version: 3.5.16
82
87
  signing_key:
83
88
  specification_version: 4
84
89
  summary: Fast transformer inference for Ruby