Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added binarize preprocessing function #156

Merged
merged 2 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/test_server/test_prediction_pipeline/test_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def test_zero_mean_unit_across_axes():
xr.testing.assert_allclose(expected, result[0])


def test_binarize():
binarize_spec = Preprocessing(name="binarize", kwargs={"threshold": 14})
data = xr.DataArray(np.arange(30).reshape(2, 3, 5), dims=("x", "y", "c"))
expected = xr.zeros_like(data)
expected[{"x": slice(1, None)}] = 1
preprocessing = make_preprocessing([binarize_spec])
result = preprocessing(data)
xr.testing.assert_allclose(expected, result)


def test_unknown_preprocessing_should_raise():
mypreprocessing = Preprocessing(name="mycoolpreprocessing", kwargs={"axes": ("x", "y")})
with pytest.raises(NotImplementedError):
Expand Down
5 changes: 5 additions & 0 deletions tiktorch/server/prediction_pipeline/_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def zero_mean_unit_variance(tensor: xr.DataArray, axes=None, eps=1.0e-6, mode="p
return (tensor - mean) / (std + 1.0e-6)


def binarize(tensor: xr.DataArray, *, threshold) -> xr.DataArray:
return tensor > threshold


def ensure_dtype(tensor: xr.DataArray, *, dtype):
"""
Convert array to a given datatype
Expand All @@ -46,6 +50,7 @@ def add_batch_dim(tensor: xr.DataArray):
KNOWN_PREPROCESSING = {
"scale_linear": scale_linear,
"zero_mean_unit_variance": zero_mean_unit_variance,
"binarize": binarize,
"__tiktorch_add_batch_dim": add_batch_dim,
"__tiktorch_ensure_dtype": ensure_dtype,
}
Expand Down