def test_video_activations(self): if not self.have_fixtures: return # Use /tmp for test fixtures from _pytest.monkeypatch import MonkeyPatch monkeypatch = MonkeyPatch() TEST_TEMPDIR = os.path.join( testconf.TEST_TEMPDIR_ROOT, 'test_bdd100k_mobilenet') testconf.use_tempdir(monkeypatch, TEST_TEMPDIR) from au.fixtures import nnmodel from au.fixtures.tf import mobilenet with testutils.LocalSpark.sess() as spark: TestVideoFrameTable.setup(spark=spark) class TestTable(nnmodel.ActivationsTable): TABLE_NAME = 'bdd100k_mobilenet_activations_test' NNMODEL_CLS = mobilenet.Mobilenet MODEL_PARAMS = mobilenet.Mobilenet.Small() IMAGE_TABLE_CLS = TestVideoFrameTable TestTable.MODEL_PARAMS.INFERENCE_BATCH_SIZE = 10 TestTable.setup(spark=spark)
def test_mnist_igraph(monkeypatch): testconf.use_tempdir(monkeypatch, TEST_TEMPDIR) # TODO: model fixture params = mnist.MNIST.Params() params.TRAIN_EPOCHS = 1 params.LIMIT = 10 model = mnist.MNIST.load_or_train(params) igraph = model.get_inference_graph() assert igraph != nnmodel.TFInferenceGraphFactory() # params = mnist.MNIST.Params() # params.LIMIT = 100 # num images mnist.MNISTDataset.setup(params=params) rows = list(mnist.MNISTDataset.iter_all_rows()) filler = nnmodel.FillActivationsTFDataset(model=model) out_rows = list(filler(rows)) assert len(out_rows) == len(rows) for row in out_rows: acts = row.attrs['activations'] act = acts[0] assert act.model_name == igraph.model_name tensor_to_value = act.tensor_to_value for tensor_name in model.igraph.output_names: assert tensor_name in tensor_to_value # Check that we have a non-empty array assert tensor_to_value[tensor_name].shape
def test_mnist_dataset(monkeypatch): testconf.use_tempdir(monkeypatch, TEST_TEMPDIR) params = mnist.MNIST.Params() params.LIMIT = 100 mnist.MNISTDataset.setup(params=params) rows = mnist.MNISTDataset.get_rows_by_uris( ('mnist_train_0', 'mnist_test_0', 'not_in_mnist')) assert len(rows) == 2 rows = sorted(rows) assert rows[0].uri == 'mnist_test_0' assert rows[1].uri == 'mnist_train_0' expected_bytes = open(testconf.MNIST_TEST_IMG_PATH, 'rb').read() assert rows[0].image_bytes == expected_bytes mnist.MNISTDataset.save_datasets_as_png(params=params) TEST_PATH = os.path.join( TEST_TEMPDIR, 'data/MNIST/test/MNIST-test-label_7-mnist_test_0.png') assert os.path.exists(TEST_PATH) import imageio expected = imageio.imread(testconf.MNIST_TEST_IMG_PATH) import numpy as np image = imageio.imread(TEST_PATH) np.testing.assert_array_equal(image, expected)
def setUpClass(cls): cls.have_fixtures = False try: mscoco.Fixtures.create_test_fixtures() cls.have_fixtures = True except Exception as e: print "Failed to create test fixtures: %s" % (e, ) from _pytest.monkeypatch import MonkeyPatch monkeypatch = MonkeyPatch() TEST_TEMPDIR = os.path.join(testconf.TEST_TEMPDIR_ROOT, 'test_mscoco') testconf.use_tempdir(monkeypatch, TEST_TEMPDIR)
def test_mobilenet_activation_tables(monkeypatch): testconf.use_tempdir(monkeypatch, TEST_TEMPDIR) dataset.ImageTable.setup() with testutils.LocalSpark.sess() as spark: for params_cls in mobilenet.Mobilenet.ALL_PARAMS_CLSS: params = params_cls() class TestTable(nnmodel.ActivationsTable): TABLE_NAME = 'Mobilenet_test_' + params_cls.__name__ NNMODEL_CLS = mobilenet.Mobilenet MODEL_PARAMS = params IMAGE_TABLE_CLS = dataset.ImageTable TestTable.setup(spark=spark)
def _create_fixture(monkeypatch): TEST_TEMPDIR = os.path.join(testconf.TEST_TEMPDIR_ROOT, 'sobel_test') testconf.use_tempdir(monkeypatch, TEST_TEMPDIR) dataset.ImageTable.setup() model = Sobel.load_or_train() filler = nnmodel.FillActivationsTFDataset(model=model) rows = list(dataset.ImageTable.iter_all_rows()) assert rows class Fixture(object): pass fixture = Fixture() fixture.rows = rows fixture.filler = filler fixture.model = model return fixture
def test_mobilenet_inference_graph(monkeypatch): testconf.use_tempdir(monkeypatch, TEST_TEMPDIR) dataset.ImageTable.setup() params = mobilenet.Mobilenet.Small() model = mobilenet.Mobilenet.load_or_train(params) igraph = model.get_inference_graph() rows = dataset.ImageTable.iter_all_rows() filler = nnmodel.FillActivationsTFDataset(model=model) out_rows = list(filler(rows)) # Test for smoke: logits for each image should be different ;) all_preds = set() for row in out_rows: acts = row.attrs['activations'] tensor_to_value = acts[0].tensor_to_value for tensor_name in model.igraph.output_names: assert tensor_name in tensor_to_value # Check that we have a non-empty array assert tensor_to_value[tensor_name].shape # If weights fail to load, the net will predict uniformly for # everything. Make sure that doesn't happen! if tensor_name == 'MobilenetV2/Predictions/Reshape_1:0': preds = tuple(tensor_to_value[tensor_name]) assert preds not in all_preds all_preds.add(preds) # The Small model consistently gets this one right assert '202228408_eccfe4790e.jpg' in ' '.join( row.uri for row in out_rows) if '202228408_eccfe4790e.jpg' in row.uri: from slim.datasets import imagenet label_map = imagenet.create_readable_names_for_imagenet_labels( ) predicted = label_map[np.array(preds).argmax()] assert predicted == 'soccer ball'