Skip to content

Commit

Permalink
feat(crafter): add test arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
nan-wang committed Apr 8, 2020
1 parent d2db40e commit 7f0fc88
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
8 changes: 7 additions & 1 deletion tests/executors/crafters/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ def create_test_image(output_fn, size=50):
image.save(f, 'jpeg')

@staticmethod
def create_test_img_array(img_height, img_width):
def create_random_img_array(img_height, img_width):
import numpy as np
return np.random.randint(0, 256, (img_height, img_width, 3))

@staticmethod
def create_test_img_array():
import numpy as np
img = np.array([i for i in range(100)]).reshape(10, 10)
return np.repeat(img[:, :, np.newaxis], 3, axis=2)
10 changes: 5 additions & 5 deletions tests/executors/crafters/image/test_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class MyTestCase(JinaImageTestCase):
def test_crop(self):
img_size = 217
img_array = self.create_test_img_array(img_size, img_size)
img_array = self.create_random_img_array(img_size, img_size)
left = 2
top = 17
width = 20
Expand All @@ -25,15 +25,15 @@ def test_crop(self):

def test_center_crop(self):
img_size = 217
img_array = self.create_test_img_array(img_size, img_size)
img_array = self.create_random_img_array(img_size, img_size)
output_dim = 20
crafter = CenterImageCropper(output_dim)
crafted_chunk = crafter.craft(img_array, 0, 0)
self.assertEqual(crafted_chunk["blob"].shape, (20, 20, 3))

def test_random_crop(self):
img_size = 217
img_array = self.create_test_img_array(img_size, img_size)
img_array = self.create_random_img_array(img_size, img_size)
output_dim = 20
num_pathes = 20
crafter = RandomImageCropper(output_dim, num_pathes)
Expand All @@ -42,15 +42,15 @@ def test_random_crop(self):

def test_random_crop(self):
img_size = 217
img_array = self.create_test_img_array(img_size, img_size)
img_array = self.create_random_img_array(img_size, img_size)
output_dim = 20
crafter = FiveImageCropper(output_dim)
crafted_chunk_list = crafter.craft(img_array, 0, 0)
self.assertEqual(len(crafted_chunk_list), 5)

def test_sliding_windows(self):
img_size = 14
img_array = self.create_test_img_array(img_size, img_size)
img_array = self.create_random_img_array(img_size, img_size)
output_dim = 4
strides = (6, 6)
crafter = SlidingWindowCropper(output_dim, strides, 'VALID')
Expand Down
2 changes: 1 addition & 1 deletion tests/executors/crafters/image/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MyTestCase(JinaImageTestCase):
def test_transform_results(self):
img_size = 217
crafter = ImageNormalizer(output_dim=img_size)
img_array = self.create_test_img_array(img_size, img_size)
img_array = self.create_random_img_array(img_size, img_size)
crafted_chunk = crafter.craft(img_array, chunk_id=0, doc_id=0)
self.assertEqual(crafted_chunk["blob"].shape, (224, 224, 3))

Expand Down
2 changes: 1 addition & 1 deletion tests/executors/crafters/image/test_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_resize(self):
img_height = 17
output_dim = 71
crafter = ImageResizer(output_dim=output_dim)
img_array = self.create_test_img_array(img_width, img_height)
img_array = self.create_random_img_array(img_width, img_height)
crafted_chunk = crafter.craft(img_array, chunk_id=0, doc_id=0)
self.assertEqual(min(crafted_chunk['blob'].shape[:-1]), output_dim)

Expand Down

0 comments on commit 7f0fc88

Please sign in to comment.