Skip to content

Commit ed09813

Browse files
committed
Add classification tests
1 parent 17ce3fd commit ed09813

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

test/classification_test.exs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
defmodule Image.ClassificationTest do
2+
use ExUnit.Case, async: false
3+
4+
@moduletag :ml
5+
6+
@images Path.join(__DIR__, "support/images")
7+
8+
# Start the classifier serving once for the whole suite. Loading
9+
# ConvNeXt-tiny-224 takes several seconds, so we do it here rather
10+
# than per-test.
11+
setup_all do
12+
spec = Image.Classification.classifier()
13+
start_supervised!(spec)
14+
:ok
15+
end
16+
17+
describe "classify/2" do
18+
test "returns a predictions map with label and score keys" do
19+
image = Image.open!(Path.join(@images, "puppy.webp"))
20+
assert %{predictions: [%{label: label, score: score} | _rest]} =
21+
Image.Classification.classify(image)
22+
23+
assert is_binary(label)
24+
assert is_float(score)
25+
assert score >= 0.0 and score <= 1.0
26+
end
27+
end
28+
29+
describe "labels/2" do
30+
test "classifies a Cavalier King Charles Spaniel as a Blenheim spaniel" do
31+
image = Image.open!(Path.join(@images, "puppy.webp"))
32+
labels = Image.Classification.labels(image)
33+
assert "Blenheim spaniel" in labels
34+
end
35+
36+
test "classifies a Lamborghini as a sports car" do
37+
image = Image.open!(Path.join(@images, "lamborghini-forsennato-concept.jpg"))
38+
labels = Image.Classification.labels(image)
39+
assert "sports car" in labels
40+
end
41+
42+
test "classifies a tabby cat as a cat" do
43+
image = Image.open!(Path.join(@images, "cat.png"))
44+
labels = Image.Classification.labels(image)
45+
assert Enum.any?(labels, &String.contains?(&1, "cat"))
46+
end
47+
48+
test "returns an empty list when min_score is 1.0" do
49+
image = Image.open!(Path.join(@images, "puppy.webp"))
50+
assert Image.Classification.labels(image, min_score: 1.0) == []
51+
end
52+
53+
test "returns more labels with a lower min_score threshold" do
54+
image = Image.open!(Path.join(@images, "puppy.webp"))
55+
high = Image.Classification.labels(image, min_score: 0.8)
56+
low = Image.Classification.labels(image, min_score: 0.1)
57+
assert length(low) >= length(high)
58+
end
59+
end
60+
end

test/support/images/cat.png

201 KB
Loading
108 KB
Loading

test/support/images/puppy.webp

85.6 KB
Loading

0 commit comments

Comments
 (0)