imageproc_test.go 4.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package pixtral

import (
	"bytes"
	"encoding/binary"
	"image"
	"image/png"
	"math"
	"os"
	"testing"

	"github.com/google/go-cmp/cmp"
)

func TestGetNumImageTokens(t *testing.T) {
	type numImageTokensCase struct {
		ImageSize image.Point
		PatchSize image.Point
		Expected  image.Point
	}

	cases := []numImageTokensCase{
		{
			ImageSize: image.Point{1024, 764},
			PatchSize: image.Point{16, 16},
			Expected:  image.Point{64, 48},
		},
		{
			ImageSize: image.Point{800, 600},
			PatchSize: image.Point{16, 16},
			Expected:  image.Point{50, 38},
		},
		{
			ImageSize: image.Point{640, 480},
			PatchSize: image.Point{16, 16},
			Expected:  image.Point{40, 30},
		},
		{
			ImageSize: image.Point{320, 200},
			PatchSize: image.Point{16, 16},
			Expected:  image.Point{20, 13},
		},
		{
			ImageSize: image.Point{1320, 200},
			PatchSize: image.Point{16, 16},
			Expected:  image.Point{83, 13},
		},
		{
			ImageSize: image.Point{2000, 200},
			PatchSize: image.Point{16, 16},
			Expected:  image.Point{125, 13},
		},
		{
			ImageSize: image.Point{10000, 200},
			PatchSize: image.Point{16, 16},
			Expected:  image.Point{625, 13},
		},
		{
			ImageSize: image.Point{1131, 577},
			PatchSize: image.Point{16, 16},
			Expected:  image.Point{71, 37},
		},
		{
			ImageSize: image.Point{16, 16},
			PatchSize: image.Point{16, 16},
			Expected:  image.Point{1, 1},
		},
	}

	for _, c := range cases {
		actual := getNumImageTokens(c.ImageSize, c.PatchSize)

		if diff := cmp.Diff(actual, c.Expected); diff != "" {
			t.Errorf("mismatch (-got +want):\n%s", diff)
		}
	}
}

func TestGetResizeOutputImageSize(t *testing.T) {
	type resizeCase struct {
		Image       image.Image
		LongestEdge int
		PatchSize   image.Point
		Expected    image.Point
	}

	cases := []resizeCase{
		{
			Image:       image.NewRGBA(image.Rect(0, 0, 1024, 768)),
			LongestEdge: 1024,
			PatchSize:   image.Point{16, 16},
			Expected:    image.Point{1024, 768},
		},
		{
			Image:       image.NewRGBA(image.Rect(0, 0, 1162, 690)),
			LongestEdge: 1024,
			PatchSize:   image.Point{16, 16},
			Expected:    image.Point{1024, 624},
		},
		{
			Image:       image.NewRGBA(image.Rect(0, 0, 300, 200)),
			LongestEdge: 1024,
			PatchSize:   image.Point{16, 16},
			Expected:    image.Point{304, 208},
		},
		{
			Image:       image.NewRGBA(image.Rect(0, 0, 1862, 522)),
			LongestEdge: 1024,
			PatchSize:   image.Point{16, 16},
			Expected:    image.Point{1024, 288},
		},
	}

	for _, c := range cases {
		actual := getResizeOutputImageSize(c.Image, c.LongestEdge, c.PatchSize)

		if diff := cmp.Diff(actual, c.Expected); diff != "" {
			t.Errorf("mismatch (-got +want):\n%s", diff)
		}
	}
}

func TestResize(t *testing.T) {
	type resizeCase struct {
		Image       image.Image
		LongestEdge int
		PatchSize   image.Point
		Expected    image.Image
	}

	cases := []resizeCase{
		{
			Image:       image.NewRGBA(image.Rect(0, 0, 1862, 522)),
			LongestEdge: 1024,
			PatchSize:   image.Point{16, 16},
			Expected:    image.NewRGBA(image.Rect(0, 0, 1024, 288)),
		},
		{
			Image:       image.NewRGBA(image.Rect(0, 0, 10, 10)),
			LongestEdge: 1024,
			PatchSize:   image.Point{16, 16},
			Expected:    image.NewRGBA(image.Rect(0, 0, 16, 16)),
		},
	}

	for _, c := range cases {
		actual := resizeImage(c.Image, "png", c.LongestEdge, c.PatchSize)

		if actual.Bounds() != c.Expected.Bounds() {
			t.Errorf("image size incorrect: '%#v': expected: '%#v'", actual.Bounds(), c.Expected.Bounds())
		}
	}
}

func TestPreprocess(t *testing.T) {
	type preprocessCase struct {
		TestImage   image.Image
		ExpectedLen int
	}

	cases := []preprocessCase{
		{
			TestImage:   image.NewRGBA(image.Rect(0, 0, 10, 10)),
			ExpectedLen: 16 * 16 * 3 * 1,
		},
		{
			TestImage:   image.NewRGBA(image.Rect(0, 0, 2000, 2000)),
			ExpectedLen: 1024 * 1024 * 3 * 1,
		},
	}

	for _, c := range cases {
		var buf bytes.Buffer
		err := png.Encode(&buf, c.TestImage)
		if err != nil {
			t.Fatal(err)
		}

		imgData, _, err := Preprocess(&buf)
		if err != nil {
			t.Fatalf("error processing: %q", err)
		}

		switch len(imgData) {
		case 0:
			t.Errorf("no image data returned")
		case c.ExpectedLen:
			// ok
		default:
			t.Errorf("unexpected image data length: %d, expected: %d", len(imgData), c.ExpectedLen)
		}
	}
}

func TestPreprocessImages(t *testing.T) {
	for _, testFile := range []string{"flight.png", "sportsball.png"} {
		f, err := os.Open(testFile)
		if err != nil {
			t.Skipf("skipping test, no test image found at %s", testFile)
		}
		defer f.Close()

		imgData, _, err := Preprocess(f)
		if err != nil {
			t.Fatalf("error processing: %q", err)
		}

		byteData := make([]byte, len(imgData)*4) // float32 is 4 bytes
		for i, f := range imgData {
			binary.LittleEndian.PutUint32(byteData[i*4:], math.Float32bits(f))
		}

		outputPath := "processed_" + testFile + ".bin"
		err = os.WriteFile(outputPath, byteData, 0o644)
		if err != nil {
			t.Fatalf("error writing processed image: %q", err)
		}
	}
}