-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc68930
commit 69de1a7
Showing
7 changed files
with
122 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from .lerp_2d import sample_image_2d, insert_into_image_2d | ||
from .lerp_3d import sample_image_3d, insert_into_image_3d | ||
from .linear_interpolation_2d import sample_image_2d, insert_into_image_2d | ||
from .linear_interpolation_3d import sample_image_3d, insert_into_image_3d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import einops | ||
import torch | ||
import numpy as np | ||
|
||
from torch_image_lerp import sample_image_2d, insert_into_image_2d | ||
|
||
|
||
def test_sample_image_2d(): | ||
# basic sanity check only | ||
image = torch.rand((28, 28)) | ||
|
||
# make an arbitrary stack (..., 2) of 2d coords | ||
coords = torch.tensor(np.random.randint(low=0, high=27, size=(6, 7, 8, 2))) | ||
|
||
# sample | ||
samples = sample_image_2d(image=image, coordinates=coords) | ||
assert samples.shape == (6, 7, 8) | ||
|
||
|
||
def test_insert_into_image_2d(): | ||
image = torch.zeros((28, 28)).float() | ||
|
||
# single value | ||
value = torch.tensor([5]).float() | ||
coordinate = torch.tensor([10.5, 14.5]).view((1, 2)) | ||
|
||
# sample | ||
image, weights = insert_into_image_2d(value, coordinates=coordinate, image=image) | ||
|
||
# check value (5) is evenly split over 4 nearest pixels | ||
expected = einops.repeat(torch.tensor([5 / 4]), '1 -> 2 2') | ||
assert torch.allclose(image[10:12, 14:16], expected) | ||
|
||
# check for zeros elsewhere | ||
assert torch.allclose(image[:10, :14], torch.zeros_like(image[:10, :14])) | ||
|
||
|
||
def test_insert_into_image_2d_multiple(): | ||
image = torch.zeros((28, 28)).float() | ||
|
||
# multiple values | ||
values = torch.ones(size=(6, 7, 8)).float() | ||
coordinates = torch.tensor(np.random.randint(low=0, high=27, size=(6, 7, 8, 2))) | ||
|
||
# sample | ||
image, weights = insert_into_image_2d(values, coordinates=coordinates, image=image) | ||
|
||
# check for nonzero value at one point | ||
sample_point = coordinates[0, 0, 0] | ||
y, x = sample_point | ||
assert image[y, x] > 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import numpy as np | ||
import torch | ||
import einops | ||
|
||
from torch_image_lerp import sample_image_3d, insert_into_image_3d | ||
|
||
|
||
def test_sample_image_3d(): | ||
# basic sanity check only | ||
image = torch.rand((28, 28, 28)) | ||
|
||
# make an arbitrary stack (..., 3) of 3d coords | ||
coords = torch.tensor(np.random.randint(low=0, high=27, size=(6, 7, 8, 3))) | ||
|
||
# sample | ||
samples = sample_image_3d(image=image, coordinates=coords) | ||
assert samples.shape == (6, 7, 8) | ||
|
||
|
||
def test_insert_into_image_3d(): | ||
image = torch.zeros((28, 28, 28)).float() | ||
|
||
# single value | ||
value = torch.tensor([5]).float() | ||
coordinate = torch.tensor([10.5, 14.5, 18.5]).view((1, 3)) | ||
|
||
# sample | ||
image, weights = insert_into_image_3d(value, coordinates=coordinate, image=image) | ||
|
||
# check value (5) is evenly split over 4 nearest pixels | ||
expected = einops.repeat(torch.tensor([5 / 8]), '1 -> 2 2 2') | ||
assert torch.allclose(image[10:12, 14:16, 18:20], expected) | ||
|
||
# check for zeros elsewhere | ||
assert torch.allclose(image[:10, :14, :18], torch.zeros_like(image[:10, :14, :18])) | ||
|
||
|
||
def test_insert_into_image_3d_multiple(): | ||
image = torch.zeros((28, 28, 28)).float() | ||
|
||
# multiple values | ||
values = torch.ones(size=(6, 7, 8)).float() | ||
coordinates = torch.tensor(np.random.randint(low=0, high=27, size=(6, 7, 8, 3))) | ||
|
||
# sample | ||
image, weights = insert_into_image_3d(values, coordinates=coordinates, image=image) | ||
|
||
# check for nonzero value at one point | ||
sample_point = coordinates[0, 0, 0] | ||
z, y, x = sample_point | ||
assert image[z, y, x] > 0 |