added formatter test; refactored batch_size fixture
This commit is contained in:
parent
ad6bb80900
commit
6835394d30
@ -9,13 +9,11 @@ logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("estimator_type", ["mock", "keras"])
|
||||
@pytest.mark.parametrize("batch_size", [0, 1, 2, 16, 32, 64])
|
||||
def test_predict(classifier, input_batch, expected_predictions):
|
||||
predictions = classifier.predict(input_batch)
|
||||
assert predictions == expected_predictions
|
||||
|
||||
|
||||
@pytest.mark.parametrize("batch_size", [0, 1, 2, 16, 32, 64])
|
||||
def test_batch_format(input_batch):
|
||||
def channels_are_last(input_batch):
|
||||
return input_batch.shape[-1] == 3
|
||||
|
||||
@ -107,6 +107,16 @@ def input_batch(batch_size, input_size):
|
||||
return np.random.random_sample(size=(batch_size, *input_size))
|
||||
|
||||
|
||||
@pytest.fixture(params=[0, 1, 2, 16, 32])
|
||||
def batch_size(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[{"width": 10, "height": 15, "depth": 3}, {"width": 150, "height": 100, "depth": 3}])
|
||||
def input_size(request):
|
||||
return itemgetter("width", "height", "depth")(request.param)
|
||||
|
||||
|
||||
def array_to_image(array):
|
||||
assert np.all(array <= 1)
|
||||
assert np.all(array >= 0)
|
||||
@ -143,7 +153,7 @@ def map_labels(numeric_labels, classes):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def metadata(images):
|
||||
def metadata(images, info_label_map):
|
||||
page_idx = 0
|
||||
|
||||
def current_page_idx():
|
||||
@ -160,21 +170,35 @@ def metadata(images):
|
||||
y1 = random.randint(0, page_height - height)
|
||||
y2 = y1 + height
|
||||
metadata = {
|
||||
Info.PAGE_WIDTH: page_width,
|
||||
Info.PAGE_HEIGHT: page_height,
|
||||
Info.PAGE_IDX: current_page_idx(),
|
||||
Info.WIDTH: width,
|
||||
Info.HEIGHT: height,
|
||||
Info.X1: x1,
|
||||
Info.X2: x2,
|
||||
Info.Y1: y1,
|
||||
Info.Y2: y2,
|
||||
info_label_map.PAGE_WIDTH: page_width,
|
||||
info_label_map.PAGE_HEIGHT: page_height,
|
||||
info_label_map.PAGE_IDX: current_page_idx(),
|
||||
info_label_map.WIDTH: width,
|
||||
info_label_map.HEIGHT: height,
|
||||
info_label_map.X1: x1,
|
||||
info_label_map.X2: x2,
|
||||
info_label_map.Y1: y1,
|
||||
info_label_map.Y2: y2,
|
||||
}
|
||||
return metadata
|
||||
|
||||
return list(map(build_image_metadata, images))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def info_label_map():
|
||||
return Info
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def metadata_formatted(metadata):
|
||||
|
||||
def format_metadata(metadata):
|
||||
return {key.value: val for key, val in metadata.items()}
|
||||
|
||||
return list(map(format_metadata, metadata))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def image_metadata_pairs(images, metadata):
|
||||
return list(starmap(ImageMetadataPair, zip(images, metadata)))
|
||||
|
||||
@ -7,7 +7,6 @@ from image_prediction.extractor_classifier.extractor_classifier import Extractor
|
||||
|
||||
@pytest.mark.parametrize("extractor_type", ["mock"])
|
||||
@pytest.mark.parametrize("estimator_type", ["mock", "keras"])
|
||||
@pytest.mark.parametrize("batch_size", [0, 1, 2, 16, 32, 64])
|
||||
def test_extractor_classifier(image_extractor, image_classifier, images, expected_predictions):
|
||||
extractor_classifier = ExtractorClassifier(image_extractor, image_classifier)
|
||||
results = extractor_classifier(images)
|
||||
|
||||
5
test/unit_tests/formatter_test.py
Normal file
5
test/unit_tests/formatter_test.py
Normal file
@ -0,0 +1,5 @@
|
||||
from image_prediction.formatter.formatters.info_formatter import EnumFormatter
|
||||
|
||||
|
||||
def test_formatter(metadata, metadata_formatted):
|
||||
assert list(EnumFormatter()(metadata)) == metadata_formatted
|
||||
@ -4,7 +4,6 @@ from image_prediction.utils import chunk_iterable
|
||||
|
||||
|
||||
@pytest.mark.parametrize("estimator_type", ["mock", "keras"])
|
||||
@pytest.mark.parametrize("batch_size", [0, 1, 2, 16, 32, 64])
|
||||
def test_predict(image_classifier, images, expected_predictions):
|
||||
predictions = list(image_classifier.predict(images))
|
||||
assert predictions == expected_predictions
|
||||
|
||||
@ -6,14 +6,13 @@ from image_prediction.extraction import extract_images_from_pdf
|
||||
|
||||
|
||||
@pytest.mark.parametrize("extractor_type", ["mock"])
|
||||
@pytest.mark.parametrize("batch_size", [1, 2, 4])
|
||||
@pytest.mark.parametrize("batch_size", [1, 2, 16])
|
||||
def test_image_extractor_mock(image_extractor, images):
|
||||
images_extracted, metadata = map(list, zip(*image_extractor(images)))
|
||||
assert images_extracted == images
|
||||
|
||||
|
||||
@pytest.mark.parametrize("extractor_type", ["parsable_pdf", "default"])
|
||||
@pytest.mark.parametrize("batch_size", [0, 1, 2, 64])
|
||||
@pytest.mark.parametrize("input_size", [{"depth": 3, "width": 170, "height": 220}], indirect=["input_size"])
|
||||
def test_parsable_pdf_image_extractor(image_extractor, pdf, images, metadata, input_size):
|
||||
images_extracted, metadata_extracted = map(list, extract_images_from_pdf(pdf, image_extractor))
|
||||
|
||||
@ -7,38 +7,32 @@ from image_prediction.estimator.preprocessor.preprocessors.identity import Ident
|
||||
from image_prediction.estimator.preprocessor.utils import image_to_normalized_tensor, images_to_batch_tensor
|
||||
|
||||
|
||||
@pytest.mark.parametrize("batch_size", [0, 1, 2, 16, 32, 64])
|
||||
def image_conversion_is_correct(image):
|
||||
tensor = image_to_normalized_tensor(image)
|
||||
image_re = Image.fromarray(np.uint8(tensor * 255), mode="RGB")
|
||||
return image == image_re and tensor.ndim == 3
|
||||
|
||||
|
||||
@pytest.mark.parametrize("batch_size", [0, 1, 2, 16, 32, 64])
|
||||
def images_conversion_is_correct(images, tensor):
|
||||
if not (images or tensor.size > 0):
|
||||
return True
|
||||
return all([isinstance(tensor, np.ndarray), tensor.ndim == 4, tensor.shape[0] == len(images)])
|
||||
|
||||
|
||||
@pytest.mark.parametrize("batch_size", [0, 1, 2, 16, 32, 64])
|
||||
def test_image_to_tensor(images):
|
||||
assert all(map(image_conversion_is_correct, images))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("batch_size", [0, 1, 2, 16, 32, 64])
|
||||
def test_images_to_batch_tensor(images):
|
||||
tensor = images_to_batch_tensor(images)
|
||||
assert images_conversion_is_correct(images, tensor)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("batch_size", [0, 1, 2, 4, 6], scope="session")
|
||||
def test_basic_preprocessor(images):
|
||||
tensor = BasicPreprocessor()(images)
|
||||
assert images_conversion_is_correct(images, tensor)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("batch_size", [0, 1, 2, 4, 6], scope="session")
|
||||
def test_identity_preprocessor(images):
|
||||
images_preprocessed = IdentityPreprocessor()(images)
|
||||
assert images_preprocessed == images
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user