def test_write_to_buffer_uint16(): tensor_desc = TensorDesc("U16", [1, 3, 127, 127], "NCHW") array = np.zeros(shape=(1, 3, 127, 127), dtype=np.uint16) blob = Blob(tensor_desc, array) ones_arr = np.ones(shape=(1, 3, 127, 127), dtype=np.uint16) blob.buffer[:] = ones_arr assert np.array_equal(blob.buffer, ones_arr)
def test_cannot_set_shape_preallocated_memory(): tensor_desc = TensorDesc("FP32", [1, 3, 127, 127], "NHWC") array = np.ones([1, 3, 127, 127], dtype=np.float32) blob = Blob(tensor_desc, array) with pytest.raises(RuntimeError) as e: blob.set_shape([1, 4, 128, 128]) assert "Cannot call setShape for Blobs created on top of preallocated memory" in str(e.value)
def test_write_numpy_scalar_int64(): tensor_desc = TensorDesc("I64", [], "SCALAR") scalar = np.array(0, dtype=np.int64) blob = Blob(tensor_desc, scalar) scalar_to_write = np.array(1, dtype=np.int64) blob.buffer[:] = scalar_to_write assert np.array_equal(blob.buffer, np.atleast_1d(scalar_to_write))
def test_init_with_numpy(shape, layout): tensor_desc = TensorDesc("FP32", shape, layout) array = np.ones(shape=shape, dtype=np.float32) blob = Blob(tensor_desc, array) assert isinstance(blob.buffer, np.ndarray) assert np.shares_memory(blob.buffer, array) assert blob.tensor_desc == tensor_desc
def test_incompatible_array_and_td(): tensor_desc = TensorDesc("FP32", [1, 3, 127, 127], "NCHW") array = np.zeros(shape=(1, 2, 3, 4), dtype=np.float32) with pytest.raises(AttributeError) as e: Blob(tensor_desc, array) assert "Number of elements in provided numpy array 24 and " \ "required by TensorDesc 48387 are not equal" in str(e.value)
def test_write_to_buffer_fp32(): tensor_desc = TensorDesc("FP32", [1, 3, 127, 127], "NCHW") array = np.zeros(shape=(1, 3, 127, 127), dtype=np.float32) blob = Blob(tensor_desc, array) ones_arr = np.ones(shape=(1, 3, 127, 127), dtype=np.float32) blob.buffer[:] = ones_arr assert np.array_equal(blob.buffer, ones_arr)
def test_write_to_buffer(precision, numpy_precision): tensor_desc = TensorDesc(precision, [1, 3, 127, 127], "NCHW") array = np.zeros(shape=(1, 3, 127, 127), dtype=numpy_precision) blob = Blob(tensor_desc, array) ones_arr = np.ones(shape=(1, 3, 127, 127), dtype=numpy_precision) blob.buffer[:] = ones_arr assert np.array_equal(blob.buffer, ones_arr)
def test_set_mean_image(): ie_core = IECore() net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) tensor_desc = TensorDesc("FP32", [0, 127, 127], "CHW") mean_image_blob = Blob(tensor_desc) preprocess_info = net.input_info["data"].preprocess_info preprocess_info.set_mean_image(mean_image_blob) assert preprocess_info.mean_variant == MeanVariant.MEAN_IMAGE
def predict(self, inputs, metadata=None, **kwargs): """ Args: inputs: dictionary where keys are input layers names and values are data for them. metadata: metadata of input representations Returns: raw data from network. """ if self._lstm_inputs: return self._predict_sequential(inputs, metadata) results = [] for infer_inputs in inputs: if self._do_reshape: input_shapes = { layer_name: data.shape for layer_name, data in infer_inputs.items() } self._reshape_input(input_shapes) if self._use_set_blob: has_info = hasattr(self.exec_network, 'input_info') for key, input_data in infer_inputs.items(): if has_info: ie_input_info = OrderedDict([ (name, data.input_data) for name, data in self.exec_network.input_info.items() ]) else: ie_input_info = self.exec_network.inputs layout = self._target_layout_mapping.get( key, ie_input_info[key].layout) tensor_desc = TensorDesc(ie_input_info[key].precision, input_data.shape, layout) preprocess_info = self._preprocess_info.get(key) if preprocess_info is not None: self.exec_network.requests[0].set_blob( key, Blob(tensor_desc, input_data), preprocess_info) else: self.exec_network.requests[0].set_blob( key, Blob(tensor_desc, input_data)) result = self.exec_network.infer( infer_inputs ) if not self._use_set_blob else self.exec_network.infer() results.append(result) if metadata is not None: for meta_ in metadata: meta_['input_shape'] = self.inputs_info_for_meta() if self._output_layouts: meta_['output_layout'] = self._output_layouts self._do_reshape = False self._use_set_blob = self.disable_resize_to_input return results
def test_set_shape(): tensor_desc = TensorDesc("FP32", [1, 3, 127, 127], "NHWC") blob = Blob(tensor_desc) blob.set_shape([1, 4, 128, 128]) assert blob.tensor_desc.dims == [1, 4, 128, 128] assert blob.buffer.shape == (1, 4, 128, 128) array = np.ones([1, 3, 127, 127], dtype=np.float32) blob = Blob(tensor_desc, array) blob.set_shape([1, 4, 128, 128]) assert blob.tensor_desc.dims == [1, 4, 128, 128] assert blob.buffer.shape == (1, 4, 128, 128)
def test_set_mean_image_for_channel(): ie_core = IECore() net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) tensor_desc = TensorDesc("FP32", [127, 127], "HW") mean_image_blob = Blob(tensor_desc) preprocess_info = net.input_info["data"].preprocess_info preprocess_info.init(1) preprocess_info.set_mean_image_for_channel(mean_image_blob, 0) pre_process_channel = preprocess_info[0] assert isinstance(pre_process_channel.mean_data, Blob) assert pre_process_channel.mean_data.tensor_desc.dims == [127, 127] assert preprocess_info.mean_variant == MeanVariant.MEAN_IMAGE
def predict(self, inputs, metadata=None, **kwargs): if self._lstm_inputs: return self._predict_sequential(inputs, metadata) results = [] for infer_inputs in inputs: if self._do_reshape: input_shapes = { layer_name: data.shape for layer_name, data in infer_inputs.items() } self._reshape_input(input_shapes) if self._use_set_blob: has_info = hasattr(self.exec_network, 'input_info') for key, input_data in infer_inputs.items(): if has_info: ie_input_info = OrderedDict([ (name, data.input_data) for name, data in self.exec_network.input_info.items() ]) else: ie_input_info = self.exec_network.inputs layout = self._target_layout_mapping.get( key, ie_input_info[key].layout) tensor_desc = TensorDesc(ie_input_info[key].precision, input_data.shape, layout) preprocess_info = self._preprocess_info.get(key) if preprocess_info is not None: self.exec_network.requests[0].set_blob( key, Blob(tensor_desc, input_data), preprocess_info) else: self.exec_network.requests[0].set_blob( key, Blob(tensor_desc, input_data)) result = self.exec_network.infer( infer_inputs ) if not self._use_set_blob else self.exec_network.infer() results.append(result) if self.reset_memory_state: for state in self.exec_network.requests[0].query_state(): state.reset() if metadata is not None: self._fill_meta(metadata, None if not self.dyn_input_layers else inputs[-1]) self._do_reshape = False self._use_set_blob = self.disable_resize_to_input return results
def test_incompatible_input_precision(): import cv2 n, c, h, w = (1, 3, 32, 32) image = cv2.imread(path_to_image) if image is None: raise FileNotFoundError("Input image not found") image = cv2.resize(image, (h, w)) / 255 image = image.transpose((2, 0, 1)) image = image.reshape((n, c, h, w)) tensor_desc = TensorDesc("FP32", [1, 3, 32, 32], "NCHW") with pytest.raises(ValueError) as e: Blob(tensor_desc, image) assert "Data type float64 of provided numpy array " \ "doesn't match to TensorDesc precision FP32" in str(e.value)
def test_query_state_write_buffer(device, input_shape, data_type, mode): ie_core = ie.IECore() if device == "CPU": if ie_core.get_metric(device, "FULL_DEVICE_NAME") == "arm_compute::NEON": pytest.skip("Can't run on ARM plugin") layout = ["C", "HW", "CHW", "NCHW"] np_data_type = {"FP32": np.float32, "FP16": np.float16, "I32": np.int32} from openvino.inference_engine import TensorDesc, Blob net = ie.IENetwork(create_function_with_memory(input_shape, np_data_type[data_type])) ie_core = ie.IECore() exec_net = ie_core.load_network(network=net, device_name=device, num_requests=1) request = exec_net.requests[0] mem_states = request.query_state() mem_state = mem_states[0] assert mem_state.name == 'var_id_667' # todo: Uncomment after fix 45611, # CPU plugin returns outputs and memory state in FP32 in case of FP16 original precision #assert mem_state.state.tensor_desc.precision == data_type for i in range(1, 10): if mode == "set_init_memory_state": # create initial value const_init = 5 init_array = np.full(input_shape, const_init, dtype=np_data_type[mem_state.state.tensor_desc.precision]) tensor_desc = TensorDesc(mem_state.state.tensor_desc.precision, input_shape, layout[len(input_shape) - 1]) blob = Blob(tensor_desc, init_array) mem_state.state = blob res = exec_net.infer({"input_data": np.full(input_shape, 1, dtype=np_data_type[data_type])}) expected_res = np.full(input_shape, 1 + const_init, dtype=np_data_type[data_type]) elif mode == "reset_memory_state": # reset initial state of ReadValue to zero mem_state.reset() res = exec_net.infer({"input_data": np.full(input_shape, 1, dtype=np_data_type[data_type])}) # always ones expected_res = np.full(input_shape, 1, dtype=np_data_type[data_type]) else: res = exec_net.infer({"input_data": np.full(input_shape, 1, dtype=np_data_type[data_type])}) expected_res = np.full(input_shape, i, dtype=np_data_type[data_type]) assert np.allclose(res['MemoryAdd'], expected_res, atol=1e-6), \ "Expected values: {} \n Actual values: {} \n".format(expected_res, res)
def test_get_buffer(): tensor_desc = TensorDesc("FP32", [1, 3, 127, 127], "NCHW") array = np.ones(shape=(1, 3, 127, 127), dtype=np.float32) blob = Blob(tensor_desc, array) assert np.array_equal(blob.buffer, array)
def test_get_tensor_desc(): tensor_desc = TensorDesc("FP32", [1, 127, 127, 3], "NHWC") blob = Blob(tensor_desc) assert blob.tensor_desc == tensor_desc
def test_init_with_numpy(): tensor_desc = TensorDesc("FP32", [1, 3, 127, 127], "NCHW") array = np.ones(shape=(1, 3, 127, 127), dtype=np.float32) blob = Blob(tensor_desc, array) assert isinstance(blob.buffer, np.ndarray) assert blob.tensor_desc == tensor_desc
def test_incorrect_precision_setter(): tensor_desc = TensorDesc("FP32", [1, 127, 127, 3], "NHWC") with pytest.raises(ValueError) as e: tensor_desc.precision = "123" assert "Unsupported precision 123! List of supported precisions:" in str( e.value)
def test_init_with_tensor_desc(): tensor_desc = TensorDesc("FP32", [1, 3, 127, 127], "NHWC") blob = Blob(tensor_desc) assert isinstance(blob.buffer, np.ndarray) assert blob.tensor_desc == tensor_desc
def test_set_shape(): tensor_desc = TensorDesc("FP32", [1, 3, 127, 127], "NHWC") blob = Blob(tensor_desc) blob.set_shape([1, 4, 128, 128]) assert blob.tensor_desc.dims == [1, 4, 128, 128] assert blob.buffer.shape == (1, 4, 128, 128)
def test_incorrect_layout_setter(): tensor_desc = TensorDesc("FP32", [1, 127, 127, 3], "NHWC") with pytest.raises(ValueError) as e: tensor_desc.layout = "123" assert "Unsupported layout 123! List of supported layouts: " in str( e.value)
def test_init_incorrect_precision(): with pytest.raises(ValueError) as e: TensorDesc("123", [1, 127, 127, 3], "NHWC") assert "Unsupported precision 123! List of supported precisions: " in str( e.value)
def test_layout(): tensor_desc = TensorDesc("FP32", [1, 127, 127, 3], "NHWC") assert tensor_desc.layout == "NHWC"
def test_ne_operator(): tensor_desc = TensorDesc("FP32", [1, 3, 127, 127], "NHWC") tensor_desc_2 = TensorDesc("FP32", [1, 3, 127, 127], "NCHW") assert tensor_desc != tensor_desc_2
def test_dims(): tensor_desc = TensorDesc("FP32", [1, 127, 127, 3], "NHWC") assert tensor_desc.dims == [1, 127, 127, 3]
def test_precision(): tensor_desc = TensorDesc("FP32", [1, 127, 127, 3], "NHWC") assert tensor_desc.precision == "FP32"
def test_init(): tensor_desc = TensorDesc("FP32", [1, 127, 127, 3], "NHWC") assert isinstance(tensor_desc, TensorDesc)