From 2da9ba964ffef3a663a7d3117f333b925c81e10b Mon Sep 17 00:00:00 2001
From: Yaman Umuroglu <maltanar@gmail.com>
Date: Wed, 6 May 2020 16:41:40 +0100
Subject: [PATCH] [Test] correct value range for CIFAR-10 data, check class
 output

thanks to @quetric for pointing this out
---
 tests/brevitas/test_brevitas_cnv.py                    | 2 ++
 tests/end2end/test_end2end_cnv_w1a1.py                 | 4 ++++
 tests/fpgadataflow/test_convert_to_hls_layers_cnv.py   | 2 ++
 tests/transformation/streamline/test_streamline_cnv.py | 2 ++
 tests/transformation/test_batchnorm_to_affine.py       | 2 ++
 tests/transformation/test_conv_lowering.py             | 2 ++
 6 files changed, 14 insertions(+)

diff --git a/tests/brevitas/test_brevitas_cnv.py b/tests/brevitas/test_brevitas_cnv.py
index 8d21a33f7..c04e16ad1 100644
--- a/tests/brevitas/test_brevitas_cnv.py
+++ b/tests/brevitas/test_brevitas_cnv.py
@@ -59,6 +59,7 @@ def test_brevitas_cnv_export_exec(wbits, abits):
     model = model.transform(FoldConstants())
     fn = pk.resource_filename("finn", "data/cifar10/cifar10-test-data-class3.npz")
     input_tensor = np.load(fn)["arr_0"].astype(np.float32)
+    input_tensor = input_tensor / 255
     assert input_tensor.shape == (1, 3, 32, 32)
     # run using FINN-based execution
     input_dict = {model.graph.input[0].name: input_tensor}
@@ -68,4 +69,5 @@ def test_brevitas_cnv_export_exec(wbits, abits):
     input_tensor = torch.from_numpy(input_tensor).float()
     expected = cnv.forward(input_tensor).detach().numpy()
     assert np.isclose(produced, expected, atol=1e-3).all()
+    assert np.argmax(produced) == 3
     os.remove(export_onnx_path)
diff --git a/tests/end2end/test_end2end_cnv_w1a1.py b/tests/end2end/test_end2end_cnv_w1a1.py
index 34e0df840..1725eb391 100644
--- a/tests/end2end/test_end2end_cnv_w1a1.py
+++ b/tests/end2end/test_end2end_cnv_w1a1.py
@@ -220,6 +220,7 @@ def test_end2end_cnv_w1a1_verify_all():
     # load one of the test vectors
     fn = pk.resource_filename("finn", "data/cifar10/cifar10-test-data-class3.npz")
     input_tensor = np.load(fn)["arr_0"].astype(np.float32)
+    input_tensor = input_tensor / 255
     assert input_tensor.shape == (1, 3, 32, 32)
     x = input_tensor
     # x = np.zeros(ishape, dtype=np.float32)
@@ -253,6 +254,7 @@ def test_end2end_cnv_w1a1_verify_all():
     assert np.isclose(y_golden, y_npysim).all()
     assert np.isclose(y_golden, y_nodebynode_rtlsim).all()
     assert np.isclose(y_golden, y_whole_rtlsim).all()
+    assert np.argmax(y_golden) == 3
 
 
 def test_end2end_cnv_w1a1_make_pynq_proj():
@@ -299,6 +301,7 @@ def test_end2end_cnv_w1a1_run_on_pynq():
     # load one of the test vectors
     fn = pk.resource_filename("finn", "data/cifar10/cifar10-test-data-class3.npz")
     input_tensor = np.load(fn)["arr_0"].astype(np.float32)
+    input_tensor = input_tensor / 255
     assert input_tensor.shape == (1, 3, 32, 32)
     x = input_tensor
     # run using FINN-based execution
@@ -320,6 +323,7 @@ def test_end2end_cnv_w1a1_run_on_pynq():
         ret = execute_onnx(parent_model, {iname: x}, True)
         y = ret[oname]
         assert np.isclose(y, y_golden).all()
+        assert np.argmax(y) == 3
 
     except KeyError:
         pytest.skip("PYNQ board IP address not specified")
diff --git a/tests/fpgadataflow/test_convert_to_hls_layers_cnv.py b/tests/fpgadataflow/test_convert_to_hls_layers_cnv.py
index 4effc0da9..46c39e45a 100644
--- a/tests/fpgadataflow/test_convert_to_hls_layers_cnv.py
+++ b/tests/fpgadataflow/test_convert_to_hls_layers_cnv.py
@@ -72,6 +72,7 @@ def test_convert_to_hls_layers_cnv_w1a1():
     # load one of the test vectors
     fn = pk.resource_filename("finn", "data/cifar10/cifar10-test-data-class3.npz")
     input_tensor = np.load(fn)["arr_0"].astype(np.float32)
+    input_tensor = input_tensor / 255
     assert input_tensor.shape == (1, 3, 32, 32)
     # generate expected value from streamlined net
     input_dict = {"global_in": input_tensor}
@@ -119,4 +120,5 @@ def test_convert_to_hls_layers_cnv_w1a1():
     produced_ctx = oxe.execute_onnx(model, input_dict, True)
     produced = produced_ctx[model.graph.output[0].name]
     assert np.isclose(expected, produced, atol=1e-3).all()
+    assert np.argmax(produced) == 3
     os.remove(export_onnx_path_cnv)
diff --git a/tests/transformation/streamline/test_streamline_cnv.py b/tests/transformation/streamline/test_streamline_cnv.py
index ec5bf441b..cfa5abe58 100644
--- a/tests/transformation/streamline/test_streamline_cnv.py
+++ b/tests/transformation/streamline/test_streamline_cnv.py
@@ -65,6 +65,7 @@ def test_streamline_cnv(size, wbits, abits):
     # load one of the test vectors
     fn = pk.resource_filename("finn", "data/cifar10/cifar10-test-data-class3.npz")
     input_tensor = np.load(fn)["arr_0"].astype(np.float32)
+    input_tensor = input_tensor / 255
     assert input_tensor.shape == (1, 3, 32, 32)
     # run using FINN-based execution
     input_dict = {"global_in": input_tensor}
@@ -77,3 +78,4 @@ def test_streamline_cnv(size, wbits, abits):
     produced = produced_ctx[model.graph.output[0].name]
     assert np.isclose(expected, produced, atol=1e-3).all()
     assert model.graph.node[0].op_type == "MultiThreshold"
+    assert np.argmax(produced) == 3
diff --git a/tests/transformation/test_batchnorm_to_affine.py b/tests/transformation/test_batchnorm_to_affine.py
index 997ca5ab1..43110c6bf 100644
--- a/tests/transformation/test_batchnorm_to_affine.py
+++ b/tests/transformation/test_batchnorm_to_affine.py
@@ -55,6 +55,7 @@ def test_batchnorm_to_affine_cnv_w1a1():
     model = model.transform(FoldConstants())
     fn = pk.resource_filename("finn", "data/cifar10/cifar10-test-data-class3.npz")
     input_tensor = np.load(fn)["arr_0"].astype(np.float32)
+    input_tensor = input_tensor / 255
     assert input_tensor.shape == (1, 3, 32, 32)
     input_dict = {"0": input_tensor}
     output_dict = oxe.execute_onnx(model, input_dict)
@@ -66,6 +67,7 @@ def test_batchnorm_to_affine_cnv_w1a1():
     output_dict_p = oxe.execute_onnx(new_model, input_dict)
     produced = output_dict_p[list(output_dict_p.keys())[0]]
     assert np.isclose(expected, produced).all()
+    assert np.argmax(produced) == 3
     os.remove(export_onnx_path)
 
 
diff --git a/tests/transformation/test_conv_lowering.py b/tests/transformation/test_conv_lowering.py
index 647a2b454..2cbc8e558 100644
--- a/tests/transformation/test_conv_lowering.py
+++ b/tests/transformation/test_conv_lowering.py
@@ -52,6 +52,7 @@ def test_conv_lowering_cnv_w1a1():
     model = model.transform(FoldConstants())
     fn = pk.resource_filename("finn", "data/cifar10/cifar10-test-data-class3.npz")
     input_tensor = np.load(fn)["arr_0"].astype(np.float32)
+    input_tensor = input_tensor / 255
     assert input_tensor.shape == (1, 3, 32, 32)
     # execute imported model to get expected answer
     input_dict = {"0": input_tensor}
@@ -62,4 +63,5 @@ def test_conv_lowering_cnv_w1a1():
     output_dict_p = oxe.execute_onnx(model, input_dict)
     produced = output_dict_p[list(output_dict_p.keys())[0]]
     assert np.isclose(produced, expected).all()
+    assert np.argmax(produced) == 3
     os.remove(export_onnx_path)
-- 
GitLab