def test_split_tuple_get_item(self): xlayers = [ xlayer.XLayer( name='in1', type=['Input'], shapes=[1, 5, 1, 1], sizes=[5], bottoms=[], tops=['split1'], attrs={}, targets=[] ), xlayer.XLayer( type=['Split'], name='split1', shapes=[[1, 1, 1, 1], [1, 3, 1, 1], [1, 1, 1, 1]], sizes=[1, 3, 1], bottoms=['in1'], tops=[], targets=[], attrs={'axis': 1, 'indices': [1, 4]} ), xlayer.XLayer( type=['TupleGetItem'], name='tgi1', shapes=[1, 3, 1, 1], sizes=[1], bottoms=['split1'], tops=[], targets=[], attrs={'index': 1} ), xlayer.XLayer( type=['TupleGetItem'], name='tgi2', shapes=[1, 1, 1, 1], sizes=[1], bottoms=['split1'], tops=[], targets=[], attrs={'index': 2} ) ] params = {} runtime_tf = RuntimeTF('test', xlayers, params) inputs = { 'in1': np.array([1, 2, 3, 4, 5], dtype=np.float32) .reshape(1, 5, 1, 1) } outpt_1, outpt_2 = runtime_tf.run(inputs, ['tgi1', 'tgi2']) expected_outpt_1 = np.array([2, 3, 4], dtype=np.float32)\ .reshape(1, 3, 1, 1) expected_outpt_2 = np.array([5], dtype=np.float32).reshape(1, 1, 1, 1) np.testing.assert_array_almost_equal(outpt_1, expected_outpt_1) np.testing.assert_array_almost_equal(outpt_2, expected_outpt_2)
def test_batch_norm(self): M = np.array([0.5, 1.2], dtype=np.float32) V = np.array([0.1, 0.05], dtype=np.float32) G = np.array([1.0, 1.0], dtype=np.float32) B = np.array([0., 0.], dtype=np.float32) xlayers = [xlayer.XLayer( name='input', type=['Input'], shapes=[1, 2, 1, 1], sizes=[2], bottoms=[], tops=[], attrs={}, targets=[] ), xlayer.XLayer( name='bn', type=['BatchNorm'], shapes=[1, 2, 1, 1], sizes=[2], bottoms=['input'], tops=[], data=xlayer.BatchData(M, V, G, B), attrs={ 'axis': 1, 'epsilon': 0.000001 }, targets=[] )] params = { 'bn_mu': M, 'bn_variance': V, 'bn_gamma': G, 'bn_beta': B } runtime_tf = RuntimeTF('test', xlayers, params) inputs = { 'input': np.array([1, 1], dtype=np.float32).reshape(1, 2, 1, 1) } outpt = runtime_tf.run(inputs)[0] expected_outpt = (inputs['input'] - np.reshape(M, (1, 2, 1, 1))) /\ np.sqrt(np.reshape(V, (1, 2, 1, 1)) + 0.000001) np.testing.assert_array_almost_equal(outpt, expected_outpt)
def test_upsampling2d_nearest_neighbor(self): X = xlayer.XLayer(type=['Upsampling2D'], name='ups1', shapes=[1, 1, 4, 6], sizes=[32], bottoms=['in1'], tops=[], targets=[], attrs={ 'scale_h': 2, 'scale_w': 3, 'data_layout': 'NCHW', 'method': 'nearest_neighbor', 'align_corners': False }) input_shapes = {'in1': TensorShape([1, 1, 2, 2])} layers = X_2_TF['Upsampling2D'](X, input_shapes, {}) assert len(layers) == 1 inpt = np.array([[1, 2], [3, 4]]).reshape((1, 1, 2, 2)) inputs = [inpt] expected_outpt = np.array([[1, 1, 1, 2, 2, 2], [1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4], [3, 3, 3, 4, 4, 4]]).reshape((1, 1, 4, 6)) out = layers[0].forward_exec(inputs) assert out.shape == (1, 1, 4, 6) np.testing.assert_array_equal(out, expected_outpt)
def test_bias_add(self): B = np.array([0.1, 0.05], dtype=np.float32) X = xlayer.XLayer(name='bias_add', type=['BiasAdd'], shapes=[1, 2, 1, 1], sizes=[2], bottoms=['input'], tops=[], data=[B], attrs={'axis': 1}, targets=[]) input_shapes = {'input': TensorShape([1, 2, 1, 1])} inputs = { 'input': np.array([1, 1], dtype=np.float32).reshape(1, 2, 1, 1) } params = {'bias_add_bias': B} layers = base.get_bias_add_layer(BiasAddLayer, ConstantLayer)(X, input_shapes, params) assert (len(layers) == 2) inputs.update(params) for layer in layers: inpts = [inputs[name] for name in layer.inputs] outpt = layer.forward_exec(inpts) expected_outpt = np.array([1.1, 1.05], dtype=np.float32)\ .reshape(1, 2, 1, 1) np.testing.assert_array_equal(outpt, expected_outpt)
def test_mean(self): X = xlayer.XLayer( type=['Mean'], name='mean1', shapes=[-1, 1, 1, 4], sizes=[4], bottoms=['in1'], tops=[], targets=[], attrs={ 'axes': [1, 2], 'keepdims': True } ) input_shapes = {'in1': TensorShape([1, 3, 3, 4])} layers = X_2_TF['Mean'](X, input_shapes, {}) assert len(layers) == 1 inpt = np.ones((1, 3, 3, 4)) inputs = [inpt] out = layers[0].forward_exec(inputs) assert isinstance(out, np.ndarray) assert len(out) == 1 assert out.shape == (1, 1, 1, 4)
def test_tuple_get_item_transpose(self): A = np.ones((1, 4, 4, 3), dtype=np.float32) B = np.ones((1, 4, 4, 3), dtype=np.float32) X = xlayer.XLayer(name='tgi', type=['TupleGetItem'], shapes=[1, 3, 4, 4], sizes=[48], bottoms=['in'], tops=[], attrs={ 'index': 1, 'transpose': True, 'axes': [0, 3, 1, 2] }, targets=[]) input_shapes = { 'in': TupleShape([TensorShape([1, 4, 4, 3]), TensorShape([1, 4, 4, 3])]) } params = {} layers = X_2_TF['TupleGetItem'](X, input_shapes, params) assert len(layers) == 1 outpt = layers[0].forward_exec([A, B]) assert outpt.shape == (1, 3, 4, 4)
def test_split_tuple(self): X = xlayer.XLayer(type=['Split'], name='split1', shapes=[[-1, 1, 4, 4], [-1, 3, 4, 4], [-1, 1, 4, 4]], sizes=[32, 32, 32], bottoms=['in1'], tops=[], targets=[], attrs={ 'axis': 1, 'indices': [1, 4] }) input_shapes = {'in1': TensorShape([1, 5, 4, 4])} layers = X_2_TF['Split'](X, input_shapes, {}) assert len(layers) == 1 inpt = np.ones((1, 5, 4, 4)) inputs = [inpt] out = layers[0].forward_exec(inputs) assert isinstance(out, list) assert len(out) == 3 assert out[0].shape == (1, 1, 4, 4) assert out[1].shape == (1, 3, 4, 4) assert out[2].shape == (1, 1, 4, 4)
def test_variable(self): V = np.array([0.1, 0.05], dtype=np.float32) X = xlayer.XLayer( name='var', type=['Variable'], shapes=[2], sizes=[2], bottoms=[], tops=[], attrs={ # 'init_value': V, 'dtype': 'float32' }, data=[V], targets=[] ) input_shapes = {} params = {} layers = X_2_TF['Variable'](X, input_shapes, params) assert(len(layers) == 1) inputs = {} for layer in layers: inpts = [inputs[name] for name in layer.inputs] outpt = layer.forward_exec(inpts) np.testing.assert_array_almost_equal(outpt, V)
def test_take(self): X = xlayer.XLayer(type=['Take'], name='take1', shapes=[-1, 1, 4], sizes=[4], bottoms=['in1', 'indices'], tops=[], targets=[], attrs={ 'axis': 1, 'mode': 'clip' }) input_shapes = { 'in1': TensorShape([1, 3, 4]), 'indices': TensorShape([]) } layers = X_2_TF['Take'](X, input_shapes, {}) assert len(layers) == 1 inpt = np.reshape( np.array([[[1, 1], [1, 1]], [[2, 2], [2, 2]], [[2, 2], [2, 2]]], dtype=np.float32), (1, 3, 4)) indices = np.array(0, np.int32) inputs = [inpt, indices] out = layers[0].forward_exec(inputs) assert (out.shape) == (1, 4) np.testing.assert_array_equal( out, np.array([[1, 1, 1, 1]], dtype=np.float32))
def test_expand_dims(self): X = xlayer.XLayer(type=['ExpandDims'], name='ed1', shapes=[-1, 1, 1, 4], sizes=[4], bottoms=['in1'], tops=[], targets=[], attrs={ 'axis': 1, 'num_newaxis': 2 }) input_shapes = {'in1': TensorShape([1, 4])} layers = X_2_TF['ExpandDims'](X, input_shapes, {}) assert (len(layers) == 1) inputs = [ np.reshape(np.array([[1, 1], [5, -1]], dtype=np.float32), (1, 4)) ] outpt = layers[0].forward_exec(inputs) assert (outpt.shape) == (1, 1, 1, 4)
def test_add_same_shape(self): left = np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 1, 1], [1, 1, 1], [1, 1, 1]]]], dtype=np.float32) right = np.array([[[[-1, -2, -3], [-4, -5, -6], [-7, -8, -9]], [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]]], dtype=np.float32) X = xlayer.XLayer(name='add', type=['Add'], shapes=[1, 2, 3, 3], sizes=[18], bottoms=['left', 'right'], tops=[], attrs={}, targets=[]) input_shapes = { 'left': TensorShape([1, 2, 3, 3]), 'right': TensorShape([1, 2, 3, 3]) } inputs = {'left': left, 'right': right} layers = X_2_TF['Add'](X, input_shapes, {}) assert len(layers) == 1 for layer in layers: inpts = [inputs[name] for name in layer.inputs] outpt = layer.forward_exec(inpts) expected_outpt = np.zeros((1, 2, 3, 3), dtype=np.float32) np.testing.assert_array_equal(outpt, expected_outpt)
def test_conv2d_transpose(self): tf.compat.v1.reset_default_graph() K = np.reshape(np.array([[[1, 1, 1], [1, 2, 1], [1, 1, 1]]], dtype=np.float32), (1, 1, 3, 3)) B = np.array([1], dtype=np.float32) X = xlayer.XLayer( name='tconv', type=['TransposeConv2D'], shapes=[1, 1, 6, 6], sizes=[25], bottoms=['input'], tops=[], data=xlayer.ConvData(K, B), attrs={ 'data_layout': 'NCHW', 'kernel_layout': 'OIHW', 'padding': [[0, 0], [0, 0], [0, 0], [0, 0]], 'strides': [2, 2], 'dilation': [1, 1], 'groups': 1 }, targets=[] ) input_shapes = { 'input': TensorShape([1, 1, 3, 3]) } data = np.reshape(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32), (1, 1, 3, 3)) inputs = { 'input': data } params = { 'tconv_kernel': K, 'tconv_biases': B } layers = base.get_conv2d_transpose_layer( Conv2DTransposeLayer, ConstantLayer)(X, input_shapes, params) assert(len(layers) == 3) inputs.update(params) for layer in layers: inpts = [inputs[name] for name in layer.inputs] outpt = layer.forward_exec(inpts) expected_outpt = np.array([1], dtype=np.float32) +\ np.array([[[[1, 1, 3, 2, 5, 3], [1, 2, 3, 4, 5, 6], [5, 5, 12, 7, 16, 9], [4, 8, 9, 10, 11, 12], [11, 11, 24, 13, 28, 15], [7, 14, 15, 16, 17, 18]]]], dtype=np.float32) np.testing.assert_array_almost_equal(outpt, expected_outpt)
def test_conv2d(self): tf.compat.v1.reset_default_graph() K = np.reshape( np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=np.float32), (2, 1, 2, 2)) B = np.array([0, 0], dtype=np.float32) X = xlayer.XLayer(name='test_conv2d', type=['Convolution'], shapes=[1, 2, 3, 3], sizes=[18], bottoms=['input'], tops=[], data=xlayer.ConvData(K, B), attrs={ 'data_layout': 'NCHW', 'kernel_layout': 'OIHW', 'padding': [[0, 0], [0, 0], [0, 0], [0, 0]], 'strides': [1, 1], 'dilation': [1, 1], 'groups': 1 }, targets=[]) input_shapes = {'input': TensorShape([1, 1, 4, 4])} inputs = {'input': np.ones((1, 1, 4, 4), dtype=np.float32)} params = { 'test_conv2d_kernel': np.reshape( np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=np.float32), (2, 1, 2, 2)), 'test_conv2d_biases': np.array([0, 0], dtype=np.float32) } layers = base.get_conv2d_layer(ConvLayer, ConstantLayer)(X, input_shapes, params) assert (len(layers) == 3) inputs.update(params) for layer in layers: # print("-----------------------") # print("Run layer: {}".format(layer.name)) inpts = [inputs[name] for name in layer.inputs] outpt = layer.forward_exec(inpts) # print("Output:", outpt.shape, outpt) inputs[layer.name] = outpt expected_outpt = np.array([[[[10., 10., 10.], [10., 10., 10.], [10., 10., 10.]], [[26., 26., 26.], [26., 26., 26.], [26., 26., 26.]]]]) np.testing.assert_array_equal(outpt, expected_outpt)
def test_batch_norm(self): M = np.array([0.5, 1.2], dtype=np.float32) V = np.array([0.1, 0.05], dtype=np.float32) G = np.array([1.0, 1.0], dtype=np.float32) B = np.array([0., 0.], dtype=np.float32) xlayers = [xlayer.XLayer( name='input', type=['Input'], shapes=[1, 2, 1, 1], sizes=[2], bottoms=[], tops=[], attrs={}, targets=[] ), xlayer.XLayer( name='bn', type=['BatchNorm'], shapes=[1, 2, 1, 1], sizes=[2], bottoms=['input'], tops=[], data=xlayer.BatchData(M, V, G, B), attrs={ 'axis': 1, 'epsilon': 0.000001 }, targets=[] )] xgraph = XGraphFactory().build_from_xlayer(xlayers) runtime_tf = RuntimeTF('test', xgraph) inputs = { 'input': np.array([1, 1], dtype=np.float32).reshape(1, 2, 1, 1) } outpt = runtime_tf.run(inputs)[0] expected_outpt = (inputs['input'] - np.reshape(M, (1, 2, 1, 1))) /\ np.sqrt(np.reshape(V, (1, 2, 1, 1)) + 0.000001) np.testing.assert_array_almost_equal(outpt, expected_outpt)
def test_constant(self): C = np.array([0.1, 0.05], dtype=np.float32) X = xlayer.XLayer(name='c1', type=['Constant'], shapes=[2], sizes=[2], bottoms=[], data=[C], tops=[], attrs={}, targets=[]) input_shapes = {} params = {} layers = X_2_TF['Constant'](X, input_shapes, params) assert len(layers) == 1 outpt = layers[0].forward_exec([]) np.testing.assert_array_almost_equal(outpt, C)
def test_tuple_get_item(self): A = np.array([0.1, 0.05], dtype=np.float32) B = np.array([0.1, 0.05, 0.1], dtype=np.float32) X = xlayer.XLayer(name='tgi', type=['TupleGetItem'], shapes=[3], sizes=[3], bottoms=['in'], tops=[], attrs={'index': 1}, targets=[]) input_shapes = {'in': TupleShape([TensorShape([2]), TensorShape([3])])} params = {} layers = X_2_TF['TupleGetItem'](X, input_shapes, params) assert len(layers) == 1 outpt = layers[0].forward_exec([A, B]) np.testing.assert_array_almost_equal(outpt, B)
def test_expand_dims(self): X = xlayer.XLayer(type=['Sigmoid'], name='s1', shapes=[-1, 1, 2, 2], sizes=[4], bottoms=['in1'], tops=[], targets=[], attrs={}) input_shapes = {'in1': TensorShape([1, 1, 2, 2])} layers = X_2_TF['Sigmoid'](X, input_shapes, {}) assert len(layers) == 1 inpt = np.array([[1, 10], [5, -1]], dtype=np.float32)\ .reshape(1, 1, 2, 2) expected_outpt = 1 / (1 + np.exp(-inpt)) outpt = layers[0].forward_exec([inpt]) assert (outpt.shape) == (1, 1, 2, 2) np.testing.assert_array_almost_equal(outpt, expected_outpt)