def test_simple(self): input = Container("input", Memory((3, ))) output = Activation("output", "input", ActivationFunctions.NONE) network = Network([output, input]) net_out = network.execute() self.assertEqual(len(net_out), 1) self.assertIsNotNone(net_out["output"])
def test_simple_RELU(self): input = Container("input", Memory((3,))) output = Activation("output", "input", ActivationFunctions.RELU) input.fill([-5, 10, -0.5]) network = Network([output, input]) net_out = network.execute() self.assertEqual(len(net_out), 1) self.assertIsNotNone(net_out["output"]) self.assertEqual(net_out["output"], [0, 10, 0])
def test_simple_batch_1(self): input = Container("input", Memory((1, 1, 4, 4))) output = Softmax("output", "input") inp_array = np.array( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]).reshape( (1, 1, 4, 4)) input.fill(inp_array) network = Network([output, input]) net_out = network.execute() self.assertEqual(len(net_out), 1) self.assertEqual(len(net_out["output"].shape), 4) self.assertAlmostEqual(1.0, np.sum(net_out["output"]))
def test_simple_batch_8(self): input = Container("input", Memory((8, 2, 4, 4))) output = Softmax("output", "input") inp_array = np.arange(8 * 2 * 4 * 4).reshape((8, 2, 4, 4)) input.fill(inp_array) network = Network([output, input]) net_out = network.execute() self.assertEqual(len(net_out), 1) self.assertEqual(len(net_out["output"].shape), 4) out = net_out["output"] sum = np.sum(out) self.assertAlmostEqual(8.0, sum)
def test_simple_RELU(self): input = Container("input", Memory((1, 1, 5, 5))) inp_array = np.array([ 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5 ]).reshape((1, 1, 5, 5)) input.fill(inp_array) reshape = Reshape("reshape", "input", (25, )) net2 = Network([input, reshape]) outputs = net2.execute() self.assertEqual(len(outputs), 1) self.assertEqual(outputs["reshape"].shape, (25, ))
def test_simple_MAX_0(self): input = Container("input", Memory((1, 1, 4, 4))) output = Pooling("output", "input", (2, 2), PoolingType.MAX) inp_array = np.array( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]).reshape( (1, 1, 4, 4)) input.fill(inp_array) network = Network([output, input]) net_out = network.execute() self.assertEqual(len(net_out), 1) self.assertEqual(len(net_out["output"].shape), 4) out_reference = np.array([6.0, 8.0, 14.0, 16.0]).reshape((1, 1, 2, 2)) self.assertTrue(np.array_equal(out_reference, net_out["output"]))
def test_simple_MAX_1(self): input = Container("input", Memory((2, 2, 4, 4))) output = Pooling("output", "input", (2, 2), PoolingType.MAX) inp_array = np.arange(2 * 2 * 4 * 4).reshape((2, 2, 4, 4)) input.fill(inp_array) network = Network([output, input]) net_out = network.execute() self.assertEqual(len(net_out), 1) self.assertEqual(len(net_out["output"].shape), 4) out_reference = np.array( [5, 7, 13, 15, 21, 23, 29, 31, 37, 39, 45, 47, 53, 55, 61, 63]).reshape((2, 2, 2, 2)) self.assertTrue(np.array_equal(out_reference, net_out["output"]))
def test_generic_many_shapes(self): input_shape = (8, 3, 32, 32) input = Container("input", Memory(input_shape)) input_values = np.random.uniform(-2, 2, input_shape) input.fill(input_values) shapes = [(8, 3, 1, 1024), (8, 3, 1024, 1), (24, 1, 32, 32), (1, 1, 1, 24576)] for shape in shapes: output = Reshape("output", "input", shape) network = Network([output, input]) net_out = network.execute() self.assertEqual(len(net_out), 1) self.assertIsNotNone(net_out["output"]) self.assertEqual(net_out["output"].shape, shape)
def test_multiple_outputs(self): input = Container("A", Memory((3, ))) b = Activation("B", "A", ActivationFunctions.RELU) d = Activation("D", "B", ActivationFunctions.RELU) f = Activation("F", "B", ActivationFunctions.RELU) c = Activation("C", "A", ActivationFunctions.RELU) g = Activation("G", "C", ActivationFunctions.RELU) e = Activation("E", "A", ActivationFunctions.RELU) network = Network([b, d, f, c, g, e, input]) net_out = network.execute() self.assertEqual(len(net_out), 4) self.assertIsNotNone(net_out["D"]) self.assertIsNotNone(net_out["F"]) self.assertIsNotNone(net_out["G"]) self.assertIsNotNone(net_out["E"])
def test_stride(self): input = Container("input", Memory((1, 1, 4, 4))) output = Pooling("output", "input", (2, 2), PoolingType.MAX, stride=(3, 3)) inp_array = np.arange(4 * 4).reshape((1, 1, 4, 4)) input.fill(inp_array) network = Network([output, input]) net_out = network.execute() self.assertEqual(len(net_out), 1) self.assertEqual(len(net_out["output"].shape), 4) out_reference = np.array([5]).reshape((1, 1, 1, 1)) self.assertTrue(np.array_equal(out_reference, net_out["output"]))
def test_generic_3d(self): shapes = [(3, 32, 32), (10, 1, 1), (1, 2, 3), (5, 224, 224)] for activ_func in ActivationFunctions: for input_shape in shapes: input = Container("input", Memory(input_shape)) output = Activation("output", "input", activ_func) input_values = np.random.uniform(-2, 2, input_shape) input.fill(input_values) network = Network([output, input]) net_out = network.execute() self.assertEqual(len(net_out), 1) self.assertIsNotNone(net_out["output"]) reference_output = self.get_reference(activ_func, input_values) real_output = net_out["output"] self.assertTrue(np.array_equal(reference_output, real_output))
def test_concat_feature(self): input0 = Container("input0", Memory((2, 4, 1, 2))) inp0_arr = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7]).reshape((2, 4, 1, 2)) input0.fill(inp0_arr) input1 = Container("input1", Memory((2, 1, 1, 2))) inp1_arr = np.array([13, 13, 24, 24]).reshape((2, 1, 1, 2)) input1.fill(inp1_arr) concat = Concatenation("concat", ["input0", "input1"], axis=1) layers = [input0, input1, concat] network = Network(layers) net_out = network.execute() real_output = net_out["concat"] self.assertEqual(len(net_out), 1) self.assertIsNotNone(real_output) ref_output = np.concatenate((inp0_arr, inp1_arr), axis=1) self.assertEqual(real_output.shape, (2, 5, 1, 2)) self.assertTrue(np.array_equal(ref_output, real_output))
from api.network import Network from api.linear import Linear from api.container import Container from api.memory import Memory import numpy as np # ---- TEST CONTAINER -------- input = Container("input", Memory((1, 10))) inp_array = np.array([1, 1, 1, 1, 1, 2, 2, 2, 2, 2]).reshape((1, 10)) input.fill(inp_array) weights = Container("weights", Memory((2, 10))) bias = Container("bias", Memory((2, ))) weights_array = np.arange(20).reshape((2, 10)) bias_array = np.zeros((2, )) weights.fill(weights_array) bias.fill(bias_array) fc = Linear("fc", "input", "weights", "bias") net2 = Network([input, weights, bias, fc], dump_graph=False) outputs = net2.execute() print(outputs) # ---- END -------- print("end tests")