def test_set_batch_dimension(): model = create_test_model() model_param1 = model.get_parameters()[0] model_param2 = model.get_parameters()[1] # batch == 2 model_param1.set_layout(Layout("NC")) assert get_batch(model) == 2 # set batch to 1 set_batch(model, Dimension(1)) assert get_batch(model) == 1 # check if shape of param 1 has changed assert model_param1.get_output_shape(0) == PartialShape([1, 1]) # check if shape of param 2 has not changed assert model_param2.get_output_shape(0) == PartialShape([2, 1])
def set_input_to_blobs(request, input): model_inputs = request.model_inputs for layer_name, data in input.items(): found_tensor = False for model_input in model_inputs: if model_input.get_any_name() == layer_name: if PartialShape(data.shape) != model_input.get_partial_shape(): raise ValueError("Input data and input layer with name {0} has different shapes: \ {1} and {2}".format(layer_name, PartialShape(data.shape), model_input.get_partial_shape())) new_tensor = Tensor(data) request.set_tensor(model_input.get_any_name(), new_tensor) found_tensor = True if not found_tensor: raise ValueError("No input layer with name {}".format(layer_name))
def create_infer_requests(self, model, path, scales=None): if scales is not None: orig_shape = model.input('mel').shape requests = [] for i in range(scales): new_shape = (orig_shape[0], orig_shape[1], orig_shape[2] * (i + 1)) model.reshape({"mel": PartialShape([new_shape[0], new_shape[1], new_shape[2]])}) compiled_model = self.ie.compile_model(model, device_name=self.device) requests.append(compiled_model.create_infer_request()) model.reshape({"mel": PartialShape([orig_shape[0], orig_shape[1], orig_shape[2]])}) else: compiled_model = self.ie.compile_model(model, device_name=self.device) requests = compiled_model.create_infer_request() log.info('The MelGAN model {} is loaded to {}'.format(path, self.device)) return requests
def get_test_function(): param = ov.opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") param.get_output_tensor(0).set_names({"parameter"}) relu = ov.opset8.relu(param) res = ov.opset8.result(relu, name="result") res.get_output_tensor(0).set_names({"result"}) return Model([res], [param], "test")
def test_get_result_index(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu = ops.relu(param, name="relu") function = Model(relu, [param], "TestFunction") assert len(function.outputs) == 1 assert function.get_result_index(function.outputs[0]) == 0
def test_node_input(): shape = [2, 2] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") parameter_b = ops.parameter(shape, dtype=np.float32, name="B") model = parameter_a + parameter_b model_inputs = model.inputs() assert len(model_inputs) == 2 assert [input_node.get_index() for input_node in model_inputs] == [0, 1] assert np.equal( [input_node.get_element_type() for input_node in model_inputs], model.get_element_type(), ).all() assert np.equal([input_node.get_shape() for input_node in model_inputs], Shape(shape)).all() assert np.equal( [input_node.get_partial_shape() for input_node in model_inputs], PartialShape(shape), ).all() input0 = model.input(0) input1 = model.input(1) assert [input0.get_index(), input1.get_index()] == [0, 1]
def test_reshape_with_ports(): model = create_test_model() new_shape = PartialShape([1, 4]) for input in model.inputs: assert isinstance(input, Output) model.reshape({input: new_shape}) assert input.partial_shape == new_shape
def __init__(self, core, model_path, input_shape, device): assert not input_shape[ 2] % self.pad_to, f"{self.pad_to} must be a divisor of input_shape's third dimension" log.info('Reading model {}'.format(model_path)) model = core.read_model(model_path) if len(model.inputs) != 1: raise RuntimeError('QuartzNet must have one input') self.input_tensor_name = model.inputs[0].get_any_name() model_input_shape = model.inputs[0].shape if len(model_input_shape) != 3: raise RuntimeError('QuartzNet input must be 3-dimensional') if model_input_shape[1] != input_shape[1]: raise RuntimeError( "QuartzNet input second dimension can't be reshaped") if model_input_shape[2] % self.pad_to: raise RuntimeError( f'{self.pad_to} must be a divisor of QuartzNet input third dimension' ) if len(model.outputs) != 1: raise RuntimeError('QuartzNet must have one output') model_output_shape = model.outputs[0].shape if len(model_output_shape) != 3: raise RuntimeError('QuartzNet output must be 3-dimensional') if model_output_shape[2] != len( self.alphabet) + 1: # +1 for blank char raise RuntimeError( f'QuartzNet output third dimension size must be {len(self.alphabet) + 1}' ) model.reshape({self.input_tensor_name: PartialShape(input_shape)}) compiled_model = core.compile_model(model, device) self.output_tensor = compiled_model.outputs[0] self.infer_request = compiled_model.create_infer_request() log.info('The model {} is loaded to {}'.format(model_path, device))
def set_model(self, model, output_names=None, md_shapes=None): """ Set/reset model to instance of engine class :param model: NXModel instance for inference """ if model.is_cascade: raise Exception('Cascade models are not supported in current launcher') # save model in IR path = save_model(model, self._tmp_dir.name, 'tmp_model')[0] # load IR model ir_model = self._load_model(path) cast_friendly_names(ir_model.inputs + ir_model.outputs) if output_names is not None: output_names = [convert_to_outputs_name(output_name) for output_name in output_names] ir_model.add_outputs(output_names) if md_shapes is not None: ng_shapes = {} for key, shape in md_shapes.items(): ng_shapes[key] = PartialShape(shape) ir_model.reshape(ng_shapes) self.model = self._ie.compile_model(model=ir_model, device_name=self.device) self.infer_request = self.model.create_infer_request()
def test_input_shape(self, mock_argparse): main(argparse.ArgumentParser(), fem, 'ov_mock_mo_frontend') stat = get_model_statistic() # verify that 'set_partial_shape' was called assert stat.set_partial_shape == 1 assert stat.lastArgPartialShape == PartialShape([1, 2, 3, 4])
def __init__(self, model, ie, device='CPU', default_width=800): """ return class provided MelGAN inference. :param model: path to xml with MelGAN model of WaveRNN :param ie: instance of the IECore :param device: target device :return: """ self.device = device self.ie = ie self.scales = 4 self.hop_length = 256 self.model = self.load_network(model) if self.model.input('mel').shape[2] != default_width: orig_shape = self.model.input('mel').shape new_shape = (orig_shape[0], orig_shape[1], default_width) self.model.reshape({"mel": PartialShape([new_shape[0], new_shape[1], new_shape[2]])}) self.requests = self.create_infer_requests(self.model, model, self.scales) # fixed number of columns in mel-spectrogramm self.mel_len = self.model.input('mel').shape[2] self.widths = [self.mel_len * (i + 1) for i in range(self.scales)]
def test_ngraph_function_api(): shape = [2, 2] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") parameter_b = ops.parameter(shape, dtype=Type.f32, name="B") parameter_c = ops.parameter(shape, dtype=np.float32, name="C") model = (parameter_a + parameter_b) * parameter_c assert parameter_a.element_type == Type.f32 assert parameter_b.element_type == Type.f32 assert parameter_a.partial_shape == PartialShape([2, 2]) parameter_a.layout = ov.Layout("NC") assert parameter_a.layout == ov.Layout("NC") function = Model(model, [parameter_a, parameter_b, parameter_c], "TestFunction") function.get_parameters()[1].set_partial_shape(PartialShape([3, 4, 5])) ordered_ops = function.get_ordered_ops() op_types = [op.get_type_name() for op in ordered_ops] assert op_types == [ "Parameter", "Parameter", "Parameter", "Add", "Multiply", "Result" ] assert len(function.get_ops()) == 6 assert function.get_output_size() == 1 assert ["A", "B", "C" ] == [input.get_node().friendly_name for input in function.inputs] assert ["Result"] == [ output.get_node().get_type_name() for output in function.outputs ] assert function.input(0).get_node().friendly_name == "A" assert function.output(0).get_node().get_type_name() == "Result" assert function.input(tensor_name="A").get_node().friendly_name == "A" assert function.output().get_node().get_type_name() == "Result" assert function.get_output_op(0).get_type_name() == "Result" assert function.get_output_element_type( 0) == parameter_a.get_element_type() assert list(function.get_output_shape(0)) == [2, 2] assert (function.get_parameters()[1].get_partial_shape()) == PartialShape( [3, 4, 5]) assert len(function.get_parameters()) == 3 results = function.get_results() assert len(results) == 1 assert results[0].get_output_element_type(0) == Type.f32 assert results[0].get_output_partial_shape(0) == PartialShape([2, 2]) results[0].layout = ov.Layout("NC") assert results[0].layout.to_string() == ov.Layout("NC") assert function.get_friendly_name() == "TestFunction"
def __init__(self, core, device, i3d_path, mstcn_path): self.ActionTerms = [ "background", "noise_action", "remove_support_sleeve", "remove_pointer_sleeve", "adjust_rider", "adjust_nut", "adjust_balancing", "open_box", "close_box", "choose_weight", "put_left", "put_right", "take_left", "take_right", "install_support_sleeve", "install_pointer_sleeve", ] self.EmbedBufferTop = np.zeros((1024, 0)) self.EmbedBufferFront = np.zeros((1024, 0)) self.ImgSizeHeight = 224 self.ImgSizeWidth = 224 self.EmbedBatchSize = 1 self.SegBatchSize = 24 self.EmbedWindowLength = 16 self.EmbedWindowStride = 1 self.EmbedWindowAtrous = 3 self.TemporalLogits = np.zeros((0, len(self.ActionTerms))) net = core.read_model(i3d_path) net.reshape({net.inputs[0]: PartialShape( [self.EmbedBatchSize, self.EmbedWindowLength, self.ImgSizeHeight, self.ImgSizeWidth, 3])}) nodes = net.get_ops() net.add_outputs(nodes[13].output(0)) self.i3d = core.compile_model(model=net, device_name=device) self.mstcn_net = core.read_model(mstcn_path) self.mstcn = core.compile_model(model=self.mstcn_net, device_name=device) self.mstcn_input_keys = self.mstcn.inputs self.mstcn_output_key = self.mstcn.outputs self.mstcn_net.reshape({'input': PartialShape([1, 2048, 1])}) self.reshape_mstcn = core.compile_model(model=self.mstcn_net, device_name=device) file_path = Path(__file__).parent / 'init_his.npz' init_his_feature = np.load(file_path) self.his_fea = {f'fhis_in_{i}': init_his_feature[f'arr_{i}'] for i in range(4)}
def test_info_update(): info1 = VariableInfo() info1.data_shape = PartialShape([1]) info1.data_type = Type.f32 info1.variable_id = "test_id" variable = Variable(info1) info2 = VariableInfo() info2.data_shape = PartialShape([2, 1]) info2.data_type = Type.i64 info2.variable_id = "test_id2" variable.update(info2) assert variable.info.data_shape == info2.data_shape assert variable.info.data_type == info2.data_type assert variable.info.variable_id == info2.variable_id
def test_input_get_partial_shape(device): core = Core() func = core.read_model(model=test_net_xml, weights=test_net_bin) exec_net = core.compile_model(func, device) input = exec_net.output(0) input_node = input.get_node().inputs()[0] expected_partial_shape = PartialShape([1, 10]) assert input_node.get_partial_shape() == expected_partial_shape
def test_const_output_get_partial_shape(device): core = Core() func = core.read_model(model=test_net_xml, weights=test_net_bin) exec_net = core.compile_model(func, device) node = exec_net.input("data") expected_partial_shape = PartialShape([1, 3, 32, 32]) assert node.get_partial_shape() == expected_partial_shape assert node.partial_shape == expected_partial_shape
def reshape_network(network, shapes): partial_shapes = {} for name, shape in shapes.items(): p_shape = PartialShape( [Dimension(d) if not isinstance(d, tuple) else Dimension(d[0], d[1]) for d in shape]) partial_shapes[name] = p_shape network.reshape(partial_shapes) return network
def get_model(): param = opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") param.get_output_tensor(0).set_names({"parameter"}) relu = opset8.relu(param) reshape = opset8.reshape(relu, opset8.shape_of(relu), False) res = opset8.result(reshape, name="result") res.get_output_tensor(0).set_names({"result"}) return Model([res], [param], "test")
def test_info_as_property(): info = VariableInfo() info.data_shape = PartialShape([1]) info.data_type = Type.f32 info.variable_id = "test_id" variable = Variable(info) assert variable.info.data_shape == info.data_shape assert variable.info.data_type == info.data_type assert variable.info.variable_id == info.variable_id
def reshape_model(self, new_shape): new_shape = { name: PartialShape([ Dimension(dim) if not isinstance(dim, tuple) else Dimension( dim[0], dim[1]) for dim in shape ]) for name, shape in new_shape.items() } self.model.reshape(new_shape)
def test_replace_output_update_name(): param = opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") relu = opset8.relu(param.output(0)) exp = opset8.exp(relu.output(0)) res = opset8.result(exp.output(0), name="result") replace_output_update_name(exp.output(0), exp.input_value(0)) assert res.input_value(0).get_node() == exp
def test_model_set_partial_shape(): model = init_model() place = model.get_place_by_tensor_name(tensor_name="") test_shape = PartialShape([1, 2, 3, 4]) model.set_partial_shape(place=place, shape=test_shape) stat = get_mdl_stat() assert stat.set_partial_shape == 1 assert stat.lastArgPlace == place assert stat.lastArgPartialShape == test_shape
def check_mean_constant(self, node, expected, shape=None): const_node = node.input(1).get_source_output().get_node() self.assertEqual(const_node.get_type_name(), 'Constant') if node.get_type_name() == 'Subtract': self.assertTrue(np.allclose(const_node.get_vector(), expected)) else: self.assertTrue( np.allclose(const_node.get_vector(), -expected.toList())) if shape: self.assertEqual(const_node.shape, PartialShape(shape))
def test_non_max_suppression(): boxes_shape = [1, 1000, 4] scores_shape = [1, 1, 1000] boxes_parameter = ov.parameter(boxes_shape, name="Boxes", dtype=np.float32) scores_parameter = ov.parameter(scores_shape, name="Scores", dtype=np.float32) node = ov.non_max_suppression(boxes_parameter, scores_parameter, make_constant_node(1000, np.int64)) assert node.get_type_name() == "NonMaxSuppression" assert node.get_output_size() == 3 assert node.get_output_partial_shape(0) == PartialShape( [Dimension(0, 1000), Dimension(3)]) assert node.get_output_partial_shape(1) == PartialShape( [Dimension(0, 1000), Dimension(3)]) assert list(node.get_output_shape(2)) == [1]
def test_runtime_info(): test_shape = PartialShape([1, 1, 1, 1]) test_type = Type.f32 test_param = Parameter(test_type, test_shape) relu_node = ops.relu(test_param) runtime_info = relu_node.get_rt_info() runtime_info["affinity"] = "test_affinity" relu_node.set_friendly_name("testReLU") runtime_info_after = relu_node.get_rt_info() assert runtime_info_after["affinity"] == "test_affinity"
def test_add_outputs_incorrect_outputs_list(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu1 = ops.relu(param, name="relu1") relu1.get_output_tensor(0).set_names({"relu_t1"}) function = Model(relu1, [param], "TestFunction") assert len(function.get_results()) == 1 with pytest.raises(TypeError) as e: function.add_outputs([0, 0]) assert "Incorrect type of a value to add as output at index 0" in str(e.value)
def check_scale_constant(self, node, expected, shape=None): const_node = node.input(1).get_source_output().get_node() self.assertEqual(const_node.get_type_name(), 'Constant') if node.get_type_name() == 'Divide': self.assertTrue(np.allclose(const_node.get_vector(), expected)) else: self.assertTrue(np.allclose(const_node.get_vector(), 1. / expected)) if shape: assert const_node.shape == PartialShape(shape)
def partial_shape_from_tuple(shape: tuple): new_shape = [] for dim in shape: if isinstance(dim, tuple): assert len(dim) == 2, "Incorrect boundaries of dimension {} in shape {}".format(dim, shape) assert dim[0] >= 0, "Incorrect min value of dimension {} in shape".format(dim, shape) new_shape.append(Dimension(dim[0], dim[1])) else: assert isinstance(dim, np.int64), "Incorrect type of dimension {} in shape".format(dim, shape) new_shape.append(Dimension(dim)) return PartialShape(new_shape)
def test_replace_source_output(): param = opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") relu = opset8.relu(param.output(0)) res = opset8.result(relu.output(0), name="result") exp = opset8.exp(param.output(0)) res.input(0).replace_source_output(exp.output(0)) assert len(exp.output(0).get_target_inputs()) == 1 assert len(relu.output(0).get_target_inputs()) == 0 assert next(iter(exp.output(0).get_target_inputs())).get_node() == res
def test_function_add_output_incorrect_tensor_name(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu1 = ops.relu(param, name="relu1") relu1.get_output_tensor(0).set_names({"relu_t1"}) relu2 = ops.relu(relu1, name="relu2") function = Model(relu2, [param], "TestFunction") assert len(function.get_results()) == 1 with pytest.raises(RuntimeError) as e: function.add_outputs("relu_t") assert "Tensor name relu_t was not found." in str(e.value)