def test_any_input_predicate(): param = opset8.parameter(PartialShape([1, 3, 22, 22])) slope = opset8.parameter(PartialShape([])) m = Matcher(AnyInput(lambda output: len(output.get_shape()) == 4), "FindActivation") assert m.match(param) assert not m.match(slope)
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.core.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.core.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_data_shapes_map(data_shape_string, input_names): # Parse parameter string like "input0[shape1][shape2],input1[shape1]" or "[shape1][shape2]" (applied to all inputs) return_value = {} if data_shape_string: data_shape_string += ',' matches = re.findall(r'(.*?\[.*?\]),', data_shape_string) if matches: for match in matches: input_name = match[:match.find('[')] shapes = re.findall(r'\[(.*?)\]', match[len(input_name):]) if input_name: return_value[input_name] = list( PartialShape(shape_str) for shape_str in shapes) else: data_shapes = list( PartialShape(shape_str) for shape_str in shapes) num_inputs, num_shapes = len(input_names), len(data_shapes) if num_shapes != 1 and num_shapes % num_inputs != 0: raise Exception( f"Number of provided data_shapes is not a multiple of the number of model inputs!" ) return_value = defaultdict(list) for i in range(max(num_shapes, num_inputs)): return_value[input_names[i % num_inputs]].append( data_shapes[i % num_shapes]) return return_value else: raise Exception( f"Can't parse input parameter: {data_shape_string}") return return_value
def test_all_predicates(): static_param = opset8.parameter(PartialShape([1, 3, 22, 22]), np.float32) dynamic_param = opset8.parameter(PartialShape([-1, 6]), np.long) fully_dynamic_param = opset8.parameter(PartialShape.dynamic()) assert Matcher(WrapType("opset8.Parameter", consumers_count(0)), "Test").match(static_param) assert not Matcher(WrapType("opset8.Parameter", consumers_count(1)), "Test").match(static_param) assert Matcher(WrapType("opset8.Parameter", has_static_dim(1)), "Test").match(static_param) assert not Matcher(WrapType("opset8.Parameter", has_static_dim(0)), "Test").match(dynamic_param) assert Matcher(WrapType("opset8.Parameter", has_static_dims([0, 3])), "Test").match(static_param) assert not Matcher(WrapType("opset8.Parameter", has_static_dims([0, 1])), "Test").match(dynamic_param) assert Matcher(WrapType("opset8.Parameter", has_static_shape()), "Test").match(static_param) assert not Matcher(WrapType("opset8.Parameter", has_static_shape()), "Test").match(dynamic_param) assert Matcher(WrapType("opset8.Parameter", has_static_rank()), "Test").match(dynamic_param) assert not Matcher(WrapType("opset8.Parameter", has_static_rank()), "Test").match(fully_dynamic_param) assert Matcher(WrapType("opset8.Parameter", type_matches(get_element_type(np.float32))), "Test").match(static_param) assert not Matcher(WrapType("opset8.Parameter", type_matches(get_element_type(np.float32))), "Test").match(dynamic_param) assert Matcher(WrapType("opset8.Parameter", type_matches_any([get_element_type(np.float32), get_element_type(np.long)])), "Test").match(static_param) assert Matcher(WrapType("opset8.Parameter", type_matches_any([get_element_type(np.float32), get_element_type(np.long)])), "Test").match(dynamic_param)
def __init__(self, core, model_path, input_shape, device, vocab_file, dynamic_flag): log.info('Reading model {}'.format(model_path)) model = core.read_model(model_path) if len(model.inputs) != 1: raise RuntimeError('Wav2Vec must have one input') self.input_tensor_name = model.inputs[0].get_any_name() model_input_shape = model.inputs[0].partial_shape if len(model_input_shape) != 2: raise RuntimeError('Wav2Vec input must be 2-dimensional') if len(model.outputs) != 1: raise RuntimeError('Wav2Vec must have one output') model_output_shape = model.outputs[0].partial_shape if len(model_output_shape) != 3: raise RuntimeError('Wav2Vec output must be 3-dimensional') if model_output_shape[2] != len(self.alphabet): raise RuntimeError( f'Wav2Vec output third dimension size must be {len(self.alphabet)}' ) if not dynamic_flag: model.reshape({self.input_tensor_name: PartialShape(input_shape)}) elif not model.is_dynamic(): model.reshape({self.input_tensor_name: PartialShape((-1, -1))}) 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)) self._init_vocab(vocab_file)
def __call__(self, *input_values: NumericData) -> List[NumericData]: """Run computation on input values and return result.""" # Input validation if len(input_values) < len(self.parameters): raise UserInputError( "Expected %s params, received not enough %s values.", len(self.parameters), len(input_values)) param_names = [param.friendly_name for param in self.parameters] input_shapes = [get_shape(input_value) for input_value in input_values] if self.network_cache.get(str(input_shapes)) is None: function = self.function if self.function.is_dynamic(): function.reshape( dict( zip(param_names, [PartialShape(i) for i in input_shapes]))) self.network_cache[str(input_shapes)] = function else: function = self.network_cache[str(input_shapes)] executable_network = self.runtime.backend.compile_model( function, self.runtime.backend_name) for parameter, input in zip(self.parameters, input_values): parameter_shape = parameter.get_output_partial_shape(0) input_shape = PartialShape([]) if isinstance( input, (int, float)) else PartialShape(input.shape) if not parameter_shape.compatible(input_shape): raise UserInputError( "Provided tensor's shape: %s does not match the expected: %s.", input_shape, parameter_shape, ) is_bfloat16 = any( parameter.get_output_element_type(0) == Type.bf16 for parameter in self.parameters) if is_bfloat16: input_values = self.convert_to_tensors(input_values) request = executable_network.create_infer_request() result_buffers = request.infer(dict(zip(param_names, input_values))) # # Note: other methods to get result_buffers from request # # First call infer with no return value: # request.infer(dict(zip(param_names, input_values))) # # Now use any of following options: # result_buffers = [request.get_tensor(n).data for n in request.outputs] # result_buffers = [request.get_output_tensor(i).data for i in range(len(request.outputs))] # result_buffers = [t.data for t in request.output_tensors] # # Since OV overwrite result data type we have to convert results to the original one. original_dtypes = [ get_dtype(result.get_output_element_type(0)) for result in self.results ] converted_buffers = self.convert_buffers(result_buffers, original_dtypes) return converted_buffers
def test_set_name_for_dimension(): skip_if_onnx_frontend_is_disabled() fe = fem.load_by_framework(framework=ONNX_FRONTEND_NAME) model = fe.load("test_place_names.onnx") dim_name = "batch_size" input1 = model.get_place_by_tensor_name(tensorName="in1") model.set_name_for_dimension(input1, 0, dim_name) assert model.get_partial_shape(input1) == PartialShape([-1, 2]) output1 = model.get_place_by_tensor_name(tensorName="out1") model.set_name_for_dimension(output1, 1, dim_name) assert model.get_partial_shape(output1) == PartialShape([1, -1]) # sub_output rank is 2 so setting dim_name at index 3 extends its rank to 4 sub_output = model.get_place_by_tensor_name(tensorName="sub_out") model.set_name_for_dimension(sub_output, 3, dim_name) assert model.get_partial_shape(sub_output) == PartialShape([2, 2, -1, -1]) with pytest.raises(Exception) as e: model.set_name_for_dimension(input1, 0, "") assert "name must not be empty" in str(e) one_const = model.get_place_by_tensor_name(tensorName="one_const") with pytest.raises(Exception) as e: model.set_name_for_dimension(one_const, 0, dim_name) assert "ONNX initializer shape dimension cannot be dynamic." in str(e)
def test_parameter_index_invalid(): shape1 = PartialShape([1]) param1 = ops.parameter(shape1, dtype=np.float32, name="data1") relu = ops.relu(param1, name="relu") function = Model(relu, [param1], "TestFunction") shape2 = PartialShape([2]) param2 = ops.parameter(shape2, dtype=np.float32, name="data2") assert function.get_parameter_index(param2) == -1
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[11].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_any_input(): param = opset8.parameter(PartialShape([1, 3, 22, 22])) relu = opset8.relu(param.output(0)) slope = opset8.parameter(PartialShape([])) prelu = opset8.prelu(param.output(0), slope.output(0)) m = Matcher(WrapType("opset8.PRelu", [AnyInput(), AnyInput()]), "FindActivation") assert not m.match(relu) assert m.match(prelu)
def test_or(): param = opset8.parameter(PartialShape([1, 3, 22, 22])) relu = opset8.relu(param.output(0)) slope = opset8.parameter(PartialShape([])) prelu = opset8.prelu(param.output(0), slope.output(0)) m = Matcher(Or([WrapType("opset8.Relu"), WrapType("opset8.PRelu")]), "FindActivation") assert m.match(relu) assert m.match(prelu)
def test_set_batch_size(self, mock_argparse): mock_return_partial_shape(PartialShape([-1, 2, 3, 4])) main(argparse.ArgumentParser(), fem, 'ov_mock_mo_frontend') stat = get_model_statistic() # verify that 'set_element_type' was called # 2 is because mock model has 2 inputs assert stat.get_partial_shape == 2 assert stat.set_partial_shape == 2 assert stat.lastArgPartialShape == PartialShape([123, 2, 3, 4])
def test_test_descriptor_tensor(): 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"}) td = relu1.get_output_tensor(0) assert "relu_t1" in td.names assert td.element_type == Type.f32 assert td.partial_shape == PartialShape([1]) assert repr(td.shape) == "<Shape: {1}>" assert td.size == 4 assert td.any_name == "relu_t1"
def test_partial_shape_equals(): ps1 = PartialShape.dynamic() ps2 = PartialShape.dynamic() assert ps1 == ps2 ps1 = PartialShape([1, 2, 3]) ps2 = PartialShape([1, 2, 3]) assert ps1 == ps2 shape = Shape([1, 2, 3]) ps = PartialShape([1, 2, 3]) assert shape == ps
def test_replace_parameter(): shape1 = PartialShape([1]) param1 = ops.parameter(shape1, dtype=np.float32, name="data") shape2 = PartialShape([2]) param2 = ops.parameter(shape2, dtype=np.float32, name="data") relu = ops.relu(param1, name="relu") function = Model(relu, [param1], "TestFunction") param_index = function.get_parameter_index(param1) function.replace_parameter(param_index, param2) assert function.get_parameter_index(param2) == param_index assert function.get_parameter_index(param1) == -1
def test_get_result_index_invalid(): shape1 = PartialShape([1]) param1 = ops.parameter(shape1, dtype=np.float32, name="data1") relu1 = ops.relu(param1, name="relu1") function = Model(relu1, [param1], "TestFunction") shape2 = PartialShape([2]) param2 = ops.parameter(shape2, dtype=np.float32, name="data2") relu2 = ops.relu(param2, name="relu2") invalid_output = relu2.outputs()[0] assert len(function.outputs) == 1 assert function.get_result_index(invalid_output) == -1
def test_add_extension_template_extension(device): core, model = get_model_with_template_extension() assert isinstance(model, Model) before_reshape = PartialShape([1, 3, 22, 22]) after_reshape = PartialShape([8, 9, 33, 66]) new_shapes = {"in_data": after_reshape} assert model.input().partial_shape == before_reshape model.reshape(new_shapes) # compile to check objects can be destroyed # in order core -> model -> compiled compiled = core.compile_model(model, device) assert compiled.input().partial_shape == after_reshape
def test_wrap_type_ctors(): param = opset8.parameter(PartialShape([1, 3, 22, 22])) relu = opset8.relu(param.output(0)) slope = opset8.parameter(PartialShape([])) prelu = opset8.prelu(param.output(0), slope.output(0)) m = Matcher(WrapType(["opset8.Relu", "opset8.PRelu"]), "FindActivation") assert m.match(relu) assert m.match(prelu) m = Matcher(WrapType(["opset8.Relu", "opset8.PRelu"], WrapType("opset8.Parameter").output(0)), "FindActivation") assert m.match(relu)
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_set_batch_int(): 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, 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 __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_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 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_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 __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 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 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_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"