def test_eran_unimplemented(): """Tests loading a convolutional Network from ERAN format. """ path = "eran_unimplemented.eran" with open(path, "w") as netfile: netfile.write("Sin\n") netfile.write("[[1, 2], [3, 4]]") try: network = Network.from_file(path) assert False except NotImplementedError: assert True with open(path, "w") as netfile: netfile.write("Conv2D\nSin, filters=1, kernel_size=[1, 1], ") netfile.write("input_shape=[1, 1, 2], stride=[1, 1], padding=0\n") netfile.write("[[[[1], [2]]]]\n") netfile.write("[-10]\n") try: network = Network.from_file(path) assert False except NotImplementedError: assert True
def test_conv_from_eran(): """Tests loading a convolutional Network from ERAN format. """ path = "conv_test.eran" with open(path, "w") as netfile: netfile.write("Conv2D\nReLU, filters=2, kernel_size=[2, 2], ") netfile.write("input_shape=[16, 16, 2], stride=[10, 10], padding=2\n") netfile.write("[[[[1, 2], [3, 4]], [[5, 6], [7, 8]]],") netfile.write(" [[[8, 7], [6, 5]], [[4, 3], [2, 1]]]]\n") netfile.write("[-1, -2]\n") netfile.write("Affine\n[[1, 2, 3, 4, 5, 6, 7, 8], ") netfile.write("[5, 6, 7, 8, 9, 10, 11, 12]]\n[-1, -2]\n") netfile.write("Conv2D\nHardTanh, filters=1, kernel_size=[1, 1], ") netfile.write("input_shape=[1, 1, 2], stride=[1, 1], padding=0\n") netfile.write("[[[[1], [2]]]]\n") netfile.write("[-10]\n") network = Network.from_file(path) assert len(network.layers) == 5 assert isinstance(network.layers[0], Conv2DLayer) assert np.allclose( network.layers[0].filter_weights, np.array([[[[1, 2], [3, 4]], [[5, 6], [7, 8]]], [[[8, 7], [6, 5]], [[4, 3], [2, 1]]]])) assert np.allclose(network.layers[0].biases, np.array([-1, -2])) assert network.layers[0].window_data.input_shape == (16, 16, 2) assert network.layers[0].window_data.window_shape == (2, 2) assert network.layers[0].window_data.strides == (10, 10) assert network.layers[0].window_data.padding == (2, 2) assert network.layers[0].window_data.out_channels == 2 assert isinstance(network.layers[1], ReluLayer) assert isinstance(network.layers[2], FullyConnectedLayer) assert np.allclose( network.layers[2].weights, np.array([[1, 5, 2, 6, 3, 7, 4, 8], [5, 9, 6, 10, 7, 11, 8, 12]]).T) assert np.allclose(network.layers[2].biases, np.array([[-1, -2]])) assert isinstance(network.layers[3], Conv2DLayer) assert np.allclose(network.layers[3].filter_weights, np.array([[[[1], [2]]]])) assert np.allclose(network.layers[3].biases, np.array([-10])) assert network.layers[3].window_data.input_shape == (1, 1, 2) assert network.layers[3].window_data.window_shape == (1, 1) assert network.layers[3].window_data.strides == (1, 1) assert network.layers[3].window_data.padding == (0, 0) assert network.layers[3].window_data.out_channels == 1 assert isinstance(network.layers[4], HardTanhLayer)
def test_fcn_from_eran(): """Tests loading a fully-connected Network from ERAN format. """ path = "fcn_test.eran" with open(path, "w") as netfile: netfile.write("ReLU\n[[-1, 2, -3], [-4, 5, -6]]\n[7, 8]\n") netfile.write("Normalize mean=[1, 2] std=[3, 4]\n") netfile.write("HardTanh\n[[-8, 7, -6], [-5, 4, -3]]\n[2, 1]\n") network = Network.from_file(path) assert len(network.layers) == 5 assert isinstance(network.layers[0], FullyConnectedLayer) assert np.allclose(network.layers[0].weights, np.array([[-1, -4], [2, 5], [-3, -6]])) assert np.allclose(network.layers[0].biases, np.array([[7, 8]])) assert isinstance(network.layers[1], ReluLayer) assert isinstance(network.layers[2], NormalizeLayer) assert np.allclose(network.layers[2].means, np.array([[1, 2]])) assert np.allclose(network.layers[2].standard_deviations, np.array([[3, 4]])) assert isinstance(network.layers[3], FullyConnectedLayer) assert np.allclose(network.layers[3].weights, np.array([[-8, -5], [7, 4], [-6, -3]])) assert np.allclose(network.layers[3].biases, np.array([[2, 1]])) assert isinstance(network.layers[4], HardTanhLayer)
def test_squeezenet_from_onnx(): """Tests loading a SqueezeNet Network from ONNX format. """ network = Network.from_file("external/onnx_squeezenet/squeezenet1.1.onnx") assert len(network.layers) == 40