Module brevettiai.tests.test_image_loader
Expand source code
import unittest
import tensorflow as tf
import tensorflow_addons as tfa
from brevettiai.tests import get_resource
from brevettiai.data.image import ImagePipeline, ImageLoader, CropResizeProcessor
class TestCropResizeProcessor(unittest.TestCase):
test_image_path = get_resource("0_1543413266626.bmp")
def test_loader_affine_transform(self):
image, _ = ImageLoader().load(self.test_image_path)
processor = CropResizeProcessor(output_height=120, output_width=150,
roi_vertical_offset=45, roi_horizontal_offset=37,
interpolation="bilinear")
# Run on processor
img_out = processor.process(image)
# Run with tfa.image.transform
input_height, input_width = tf.shape(image)[:2]
tr = tfa.image.transform_ops.matrices_to_flat_transforms(processor.affine_transform(input_height, input_width))
img2 = tfa.image.transform(tf.cast(image, tf.float32), tf.cast(tr, tf.float32), processor.interpolation,
output_shape=processor.output_size(input_height, input_width))
tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(img_out - img2)), 1e-4)
class TestImagePipelineToImageLoaderConversion(unittest.TestCase):
test_image_path = get_resource("0_1543413266626.bmp")
def test_ensure_default_settings(self):
ip = ImagePipeline()
sample = {"path": tf.constant([self.test_image_path])}
ip_image = ip(sample)["img"][0]
loader_image, _ = ip.to_image_loader().load(self.test_image_path)
tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 0.0)
def test_ensure_target_size(self):
ip = ImagePipeline(target_size=(120, 150))
sample = {"path": tf.constant([self.test_image_path])}
ip_image = ip(sample)["img"][0]
loader_image, _ = ip.to_image_loader().load(self.test_image_path)
tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 2.0)
def test_ensure_roi(self):
ip = ImagePipeline(rois=(((10, 10), (50, 70)),))
sample = {"path": tf.constant([self.test_image_path])}
ip_image = ip(sample)["img"][0]
loader_image, _ = ip.to_image_loader().load(self.test_image_path)
tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 0.0)
def test_ensure_roi_and_target_size(self):
ip = ImagePipeline(rois=(((10, 10), (50, 70)),), target_size=(90, 70))
sample = {"path": tf.constant([self.test_image_path])}
ip_image = ip(sample)["img"][0]
loader_image, _ = ip.to_image_loader().load(self.test_image_path)
tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 2.0)
if __name__ == '__main__':
unittest.main()
Classes
class TestCropResizeProcessor (methodName='runTest')
-
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
Expand source code
class TestCropResizeProcessor(unittest.TestCase): test_image_path = get_resource("0_1543413266626.bmp") def test_loader_affine_transform(self): image, _ = ImageLoader().load(self.test_image_path) processor = CropResizeProcessor(output_height=120, output_width=150, roi_vertical_offset=45, roi_horizontal_offset=37, interpolation="bilinear") # Run on processor img_out = processor.process(image) # Run with tfa.image.transform input_height, input_width = tf.shape(image)[:2] tr = tfa.image.transform_ops.matrices_to_flat_transforms(processor.affine_transform(input_height, input_width)) img2 = tfa.image.transform(tf.cast(image, tf.float32), tf.cast(tr, tf.float32), processor.interpolation, output_shape=processor.output_size(input_height, input_width)) tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(img_out - img2)), 1e-4)
Ancestors
- unittest.case.TestCase
Class variables
var test_image_path
Methods
def test_loader_affine_transform(self)
-
Expand source code
def test_loader_affine_transform(self): image, _ = ImageLoader().load(self.test_image_path) processor = CropResizeProcessor(output_height=120, output_width=150, roi_vertical_offset=45, roi_horizontal_offset=37, interpolation="bilinear") # Run on processor img_out = processor.process(image) # Run with tfa.image.transform input_height, input_width = tf.shape(image)[:2] tr = tfa.image.transform_ops.matrices_to_flat_transforms(processor.affine_transform(input_height, input_width)) img2 = tfa.image.transform(tf.cast(image, tf.float32), tf.cast(tr, tf.float32), processor.interpolation, output_shape=processor.output_size(input_height, input_width)) tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(img_out - img2)), 1e-4)
class TestImagePipelineToImageLoaderConversion (methodName='runTest')
-
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
Expand source code
class TestImagePipelineToImageLoaderConversion(unittest.TestCase): test_image_path = get_resource("0_1543413266626.bmp") def test_ensure_default_settings(self): ip = ImagePipeline() sample = {"path": tf.constant([self.test_image_path])} ip_image = ip(sample)["img"][0] loader_image, _ = ip.to_image_loader().load(self.test_image_path) tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 0.0) def test_ensure_target_size(self): ip = ImagePipeline(target_size=(120, 150)) sample = {"path": tf.constant([self.test_image_path])} ip_image = ip(sample)["img"][0] loader_image, _ = ip.to_image_loader().load(self.test_image_path) tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 2.0) def test_ensure_roi(self): ip = ImagePipeline(rois=(((10, 10), (50, 70)),)) sample = {"path": tf.constant([self.test_image_path])} ip_image = ip(sample)["img"][0] loader_image, _ = ip.to_image_loader().load(self.test_image_path) tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 0.0) def test_ensure_roi_and_target_size(self): ip = ImagePipeline(rois=(((10, 10), (50, 70)),), target_size=(90, 70)) sample = {"path": tf.constant([self.test_image_path])} ip_image = ip(sample)["img"][0] loader_image, _ = ip.to_image_loader().load(self.test_image_path) tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 2.0)
Ancestors
- unittest.case.TestCase
Class variables
var test_image_path
Methods
def test_ensure_default_settings(self)
-
Expand source code
def test_ensure_default_settings(self): ip = ImagePipeline() sample = {"path": tf.constant([self.test_image_path])} ip_image = ip(sample)["img"][0] loader_image, _ = ip.to_image_loader().load(self.test_image_path) tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 0.0)
def test_ensure_roi(self)
-
Expand source code
def test_ensure_roi(self): ip = ImagePipeline(rois=(((10, 10), (50, 70)),)) sample = {"path": tf.constant([self.test_image_path])} ip_image = ip(sample)["img"][0] loader_image, _ = ip.to_image_loader().load(self.test_image_path) tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 0.0)
def test_ensure_roi_and_target_size(self)
-
Expand source code
def test_ensure_roi_and_target_size(self): ip = ImagePipeline(rois=(((10, 10), (50, 70)),), target_size=(90, 70)) sample = {"path": tf.constant([self.test_image_path])} ip_image = ip(sample)["img"][0] loader_image, _ = ip.to_image_loader().load(self.test_image_path) tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 2.0)
def test_ensure_target_size(self)
-
Expand source code
def test_ensure_target_size(self): ip = ImagePipeline(target_size=(120, 150)) sample = {"path": tf.constant([self.test_image_path])} ip_image = ip(sample)["img"][0] loader_image, _ = ip.to_image_loader().load(self.test_image_path) tf.debugging.assert_less_equal(tf.reduce_mean(tf.abs(loader_image-ip_image)), 2.0)