def test_list_not_type_no_shape(self): pb_tensor = PB( dict( dtype=3, tensor_content= b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00' )) tf_dtype = pb_tensor.dtype shape = np.array([5]) ref = [1, 2, 3, 4, 5] res = tf_tensor_content(tf_dtype, shape, pb_tensor) self.assertTrue(np.all(res == ref))
def test_list_not_type_shape(self): pb_tensor = PB({ 'dtype': 3, 'tensor_content': b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00' }) tf_dtype = pb_tensor.dtype shape = np.array([10]) ref = [1, 2, 3, 4, 5, 5, 5, 5, 5, 5] res = tf_tensor_content(tf_dtype, shape, pb_tensor) self.assertTrue(np.all(res == ref))
def test_str_decode_list(self): pb_tensor = PB({ 'dtype': 7, 'string_val': [b'\377\330\377\377\330\377'], }) shape = int64_array([]) warning_message = 'ERROR:root:Failed to parse a tensor with Unicode characters. Note that Inference Engine ' \ 'does not support string literals, so the string constant should be eliminated from the ' \ 'graph.' with self.assertLogs(log.getLogger(), level="ERROR") as cm: result = tf_tensor_content(pb_tensor.dtype, shape, pb_tensor) self.assertEqual([warning_message, warning_message], cm.output)
def test_nd_not_type_no_shape(self): pb_tensor = PB({ 'dtype': 3, 'tensor_content': b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00', }) tf_dtype = pb_tensor.dtype shape = np.array([2, 3]) ref = [[1, 2, 3], [4, 5, 6]] res = tf_tensor_content(tf_dtype, shape, pb_tensor) self.assertTrue(np.all(res == ref))
def test_crop_ext(self): params = {'attrs': {'offset': '(5, 5)', 'num_args': 2}} node = PB({'symbol_dict': params}) CropFrontExtractor.extract(node) exp_res = { 'axis': 2, 'offset': [5, 5], 'dim': None, 'infer': Crop.infer, 'type': 'Crop' } for key in exp_res.keys(): self.assertEqual(node[key], exp_res[key])
def _create_node(across_spatial=None, channel_shared=None, eps=None): if across_spatial is None: across_spatial = 0 if channel_shared is None: channel_shared = 0 if eps is None: eps = 0.1 pb = onnx.helper.make_node('Normalize', across_spatial=across_spatial, channel_shared=channel_shared, eps=eps, inputs=['a'], outputs=['b']) node = PB({'pb': pb}) return node
def _create_node(**attrs): pb = onnx.helper.make_node( 'RNN', inputs=[ 'X', 'W', 'R', 'B', ], outputs=['Y', 'Y_h', 'Y_c'], hidden_size=128, **attrs, ) node = PB({'pb': pb}) return node
def test_str_decode(self): pb_tensor = PB({ 'dtype': 7, 'string_val': [b"\037\000\036\000\002\000\303\237\035\000\002"] }) tf_dtype = pb_tensor.dtype shape = int64_array([1]) warning_message = 'ERROR:root:Failed to parse a tensor with Unicode characters. Note that Inference Engine ' \ 'does not support string literals, so the string constant should be eliminated from the ' \ 'graph.' ref_val = np.array([b'\x1f\x00\x1e\x00\x02\x00\xc3\x9f\x1d\x00\x02']) with self.assertLogs(log.getLogger(), level="ERROR") as cm: result = tf_tensor_content(tf_dtype, shape, pb_tensor) self.assertEqual([warning_message], cm.output) self.assertEqual(ref_val, result)
def _create_transpose_node(order: list): if order is None: # Default transpose pb = onnx.helper.make_node( 'Transpose', inputs=['data'], outputs=['transposed'], ) else: # Transpose with order pb = onnx.helper.make_node('Transpose', inputs=['data'], outputs=['transposed'], perm=order) node = PB({'pb': pb}) return node
def _create_squeeze_node(axes): if axes is None: pb = onnx.helper.make_node( 'Squeeze', inputs=['x'], outputs=['y'], ) else: pb = onnx.helper.make_node( 'Squeeze', inputs=['x'], outputs=['y'], axes=axes, ) node = PB({'pb': pb}) return node
def _create_node(pads=None, value=None, mode=None): if pads is None: pads = [1, 2, 3, 4] if value is None: value = 0.0 if mode is None: mode = 'constant' pb = onnx.helper.make_node('Pad', pads=pads, mode=mode, value=value, inputs=['a'], outputs=['b']) graph = Graph() node = PB({'pb': pb, 'graph': graph}) return node
def test_conv_2d_defaults(self): node = PB({ 'pb': PB({ 'attr': { 'data_format': PB({'s': b"NHWC"}), 'strides': PB({'list': PB({"i": self.strides})}), 'padding': PB({'s': b'VALID'}), 'dilations': PB({'list': PB({"i": [1, 1, 1, 1]})}) } }) }) self.expected = { 'bias_addable': True, 'dilation': np.array([1, 1, 1, 1], dtype=np.int8), 'type': 'Convolution', 'layout': 'NHWC', } Conv2DFrontExtractor.extract(node) self.res = node self.expected_call_args = (None, False) self.compare()
def test_conv_ext_empty_numbers(self, weights_biases_mock, layout_attrs_mock): weights_biases_mock.return_value = {} layout_attrs_mock.return_value = {} params = { 'pad': None, 'kernel_size': None, 'stride': None, 'dilation': None, 'group': 14, 'num_output': 15, 'bias_term': True, 'pad_w': 3, 'pad_h': 4, 'kernel_w': 5, 'kernel_h': 6, 'stride_h': 3, 'stride_w': 2, } node = PB({'pb': FakeConvProtoLayer(FakeMultiParam(params))}) ConvFrontExtractor.extract(node) res = node exp_res = { 'op': 'Conv2D', 'pad': np.array([[0, 0], [0, 0], [4, 4], [3, 3]]), 'pad_spatial_shape': np.array([[4, 4], [3, 3]]), 'stride': np.array([1, 1, 3, 2]), 'kernel_spatial': np.array([6, 5]), 'dilation': np.array([1, 1, 1, 1]), 'group': 14, 'bias_addable': True, 'bias_term': True, } self.assertTrue(weights_biases_mock.called) self.assertTrue(layout_attrs_mock.called) for key in exp_res.keys(): if key in ('pad', 'pad_spatial_shape', 'stride', 'kernel_spatial', 'dilation'): np.testing.assert_equal(res[key], exp_res[key]) else: self.assertEqual(res[key], exp_res[key])
def _create_do_node(num_classes=0, share_location=0, background_label_id=0, code_type="", variance_encoded_in_target=0, keep_top_k=0, confidence_threshold=0, nms_threshold=0, top_k=0, eta=0): pb = onnx.helper.make_node( 'DetectionOutput', inputs=['x'], outputs=['y'], num_classes=num_classes, share_location=share_location, background_label_id=background_label_id, code_type=code_type, variance_encoded_in_target=variance_encoded_in_target, keep_top_k=keep_top_k, confidence_threshold=confidence_threshold, # nms_param nms_threshold=nms_threshold, top_k=top_k, eta=eta, ) node = PB({'pb': pb}) return node
def _create_priorbox_clustered_node(width=np.array([]), height=np.array([]), flip=False, clip=False, variance=None, img_size=0, img_h=0, img_w=0, step=0, step_h=0, step_w=0, offset=0): pb = onnx.helper.make_node( 'PriorBoxClustered', inputs=['x'], outputs=['y'], width=width, height=height, flip=flip, clip=clip, variance=variance, img_size=img_size, img_h=img_h, img_w=img_w, step=step, step_h=step_h, step_w=step_w, offset=offset, ) node = PB({'pb': pb}) return node
def test_deconv_ext_target_shape_with_output_pad(self): params = { 'attrs': { "kernel": "(4, 4)", "no_bias": "True", "num_filter": "21", "num_group": "14", "pad": "(4, 4)", "stride": "(2, 2)", "dilate": "(3, 3)", "workspace": "1536", "target_shape": "(120, 120)", "adj": "(1, 1)" } } node = PB({'symbol_dict': params}) DeconvFrontExtractor.extract(node) exp_res = { 'op': 'Deconvolution', 'pad': np.array([[0, 0], [0, 0], [4, 4], [4, 4]]), 'pad_spatial_shape': np.array([[4, 4], [4, 4]]), 'stride': np.array([1, 1, 2, 2]), 'kernel_spatial': np.array([4, 4]), 'dilation': np.array([1, 1, 3, 3]), 'group': 14, 'output': 21, 'bias_addable': True, 'bias_term': False, 'output_spatial_shape': np.array([120, 120]), } for key in exp_res.keys(): if key in ('pad', 'pad_spatial_shape', 'stride', 'kernel_spatial', 'dilation', 'output_spatial_shape'): np.testing.assert_equal(node[key], exp_res[key]) else: self.assertEqual(node[key], exp_res[key])
def test_deconv2d_nchw(self): node = PB({ 'pb': PB({ 'attr': { 'data_format': PB({'s': b"NCHW"}), 'strides': PB({'list': PB({"i": self.strides})}), 'padding': PB({'s': b'VALID'}) } }) }) self.expected = { "spatial_dims": [2, 3], "channel_dims": [1], "batch_dims": [0], 'stride': np.array(self.strides, dtype=np.int8), } Conv2DBackpropInputFrontExtractor.extract(node) self.res = node self.expected_call_args = (None, False) self.compare()
def test_deconv2d_defaults(self): node = PB({ 'pb': PB({ 'attr': { 'data_format': PB({'s': b"NHWC"}), 'strides': PB({'list': PB({"i": self.strides})}), 'padding': PB({'s': b'VALID'}) } }) }) self.expected = { 'bias_addable': True, 'pad': None, # will be inferred when input shape is known 'pad_spatial_shape': None, 'output_spatial_shape': None, 'output_shape': None, 'group': None, } Conv2DBackpropInputFrontExtractor.extract(node) self.res = node self.expected_call_args = (None, False) self.compare()
def test_conv_no_pb_no_ml(self): node = PB({'pb': None}) self.assertRaises(Error, ConvFrontExtractor.extract, node)
def _create_node(**attrs): params = {'attrs': {**attrs}} node = PB({'symbol_dict': params}) return node