49 lines
1.9 KiB
Markdown
49 lines
1.9 KiB
Markdown
The two tiny ONNX models are deterministic classifiers used by the image upload API tests.
|
|
They accept the same `[1, 224, 224, 3]` float input as the production nsfw model and return
|
|
`[Drawings, Hentai, Neutral, Porn, Sexy]` scores. One always returns Neutral = 1 and the
|
|
other always returns Porn = 1. They exercise the real nsfw inference path without requiring
|
|
image fixtures.
|
|
|
|
## Image fixtures for the bundled model
|
|
|
|
Add image pairs to these project directories:
|
|
|
|
* `tests/fixtures/nsfw/` — images expected to be rejected by moderation.
|
|
* `tests/fixtures/non_nsfw/` — images expected to be accepted by moderation.
|
|
|
|
Install the splitter's dependency with `python3 -m pip install Pillow`, then run from the
|
|
project root:
|
|
|
|
```sh
|
|
python3 scripts/split_nsfw_test_image.py /path/to/input.jpg --output-dir tests/fixtures/nsfw
|
|
python3 scripts/split_nsfw_test_image.py /path/to/safe.jpg --output-dir tests/fixtures/non_nsfw
|
|
```
|
|
|
|
To run the tests:
|
|
|
|
```sh
|
|
SQLX_OFFLINE=true cargo test --locked directory_images -- --ignored
|
|
```
|
|
|
|
## Deterministic model fixtures
|
|
|
|
To regenerate them with Python's `onnx` package:
|
|
|
|
```python
|
|
from pathlib import Path
|
|
from onnx import TensorProto, checker, helper
|
|
|
|
for name, scores in [("safe", [0., 0., 1., 0., 0.]), ("unsafe", [0., 0., 0., 1., 0.])]:
|
|
tensor = helper.make_tensor("probabilities", TensorProto.FLOAT, [1, 5], scores)
|
|
graph = helper.make_graph(
|
|
[helper.make_node("Identity", ["probabilities"], ["output"])],
|
|
"constant classifier for upload endpoint tests",
|
|
[helper.make_tensor_value_info("input", TensorProto.FLOAT, [1, 224, 224, 3])],
|
|
[helper.make_tensor_value_info("output", TensorProto.FLOAT, [1, 5])],
|
|
[tensor],
|
|
)
|
|
model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 13)], ir_version=8)
|
|
checker.check_model(model)
|
|
Path(f"tests/fixtures/image-upload-{name}.onnx").write_bytes(model.SerializeToString())
|
|
```
|