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 assert shape == ps.to_shape()
def get_image_tensors(image_paths, info, batch_sizes): processed_frames = 0 widthes = info.widthes if info.is_dynamic else [info.width] heights = info.heights if info.is_dynamic else [info.height] tensors = [] process_with_original_shapes = False num_shapes = len(info.shapes) if num_shapes == 0: process_with_original_shapes = True num_images = len(image_paths) niter = max(num_shapes, num_images) for i in range(niter): shape = list(info.shapes[i % num_shapes]) if num_shapes else [] dtype = get_dtype(info.element_type.get_type_name())[0] images = np.ndarray(shape=shape, dtype=dtype) image_index = processed_frames current_batch_size = 1 if process_with_original_shapes else batch_sizes[ i % num_shapes] for b in range(current_batch_size): image_index %= num_images image_filename = image_paths[image_index] logger.info(f'Prepare image {image_filename}') image = cv2.imread(image_filename) if process_with_original_shapes: logger.info( f'Image will be processed with original shape - {image.shape[:-1]}' ) elif info.layout.has_name('H') and info.layout.has_name('W'): new_im_size = (widthes[i % num_shapes], heights[i % num_shapes]) if image.shape[:-1] != new_im_size: logger.warning( f"Image is resized from ({image.shape[:-1]}) to ({new_im_size})" ) image = cv2.resize(image, new_im_size) if info.scale.size or info.mean.size: blue, green, red = cv2.split(image) if info.mean.size: blue = np.subtract(blue, info.mean[0]) green = np.subtract(green, info.mean[1]) red = np.subtract(red, info.mean[2]) if info.scale.size: blue = np.divide(blue, info.scale[0]) green = np.divide(green, info.scale[1]) red = np.divide(red, info.scale[2]) image = cv2.merge([blue, green, red]) if str(info.layout) in ['[N,C,H,W]', '[C,H,W]']: image = image.transpose((2, 0, 1)) if process_with_original_shapes: if len(info.partial_shape) == 4: image = np.expand_dims(image, 0) p_shape = PartialShape(image.shape) if info.partial_shape.compatible(p_shape): info.data_shapes.append(p_shape.to_shape()) else: raise Exception( f"Data shape '{str(p_shape)}' provided for input '{info.name}' " f"is not compatible with partial shape '{str(info.partial_shape)}' for this input." ) tensors.append(Tensor(image.astype(dtype))) else: try: images[b] = image except ValueError: #raise Exception(f"Image shape {image.shape} is not compatible with input shape {shape}. " #f"Try to provide layout for input '{info.name}'.") # backward compatibility # will be removed logger.warning( f"Image shape {image.shape} is not compatible with input shape {shape}. " f"Input '{info.name}' will be filled with random values!" ) return fill_tensors_with_random(info) image_index += 1 processed_frames += current_batch_size if not process_with_original_shapes: tensors.append(Tensor(images)) return tensors
def test_partial_shape(): ps = PartialShape([1, 2, 3, 4]) assert ps.is_static assert not ps.is_dynamic assert ps.rank == 4 assert repr(ps) == "<PartialShape: {1,2,3,4}>" assert ps.get_dimension(0) == Dimension(1) assert ps.get_dimension(1) == Dimension(2) assert ps.get_dimension(2) == Dimension(3) assert ps.get_dimension(3) == Dimension(4) shape = Shape([1, 2, 3]) ps = PartialShape(shape) assert ps.is_static assert not ps.is_dynamic assert ps.all_non_negative assert ps.rank == 3 assert list(ps.get_shape()) == [1, 2, 3] assert list(ps.get_max_shape()) == [1, 2, 3] assert list(ps.get_min_shape()) == [1, 2, 3] assert list(ps.to_shape()) == [1, 2, 3] assert repr(shape) == "<Shape: {1, 2, 3}>" assert repr(ps) == "<PartialShape: {1,2,3}>" ps = PartialShape( [Dimension(1), Dimension(2), Dimension(3), Dimension.dynamic()]) assert not ps.is_static assert ps.is_dynamic assert ps.all_non_negative assert ps.rank == 4 assert list(ps.get_min_shape()) == [1, 2, 3, 0] assert list(ps.get_max_shape())[3] > 1000000000 assert repr(ps) == "<PartialShape: {1,2,3,?}>" assert ps.get_dimension(0) == Dimension(1) assert ps.get_dimension(1) == Dimension(2) assert ps.get_dimension(2) == Dimension(3) assert ps.get_dimension(3) == Dimension.dynamic() ps = PartialShape([1, 2, 3, -1]) assert not ps.is_static assert ps.is_dynamic assert ps.all_non_negative assert ps.rank == 4 assert list(ps.get_min_shape()) == [1, 2, 3, 0] assert list(ps.get_max_shape())[3] > 1000000000 assert repr(ps) == "<PartialShape: {1,2,3,?}>" ps = PartialShape.dynamic() assert not ps.is_static assert ps.is_dynamic assert ps.rank == Dimension.dynamic() assert list(ps.get_min_shape()) == [] assert list(ps.get_max_shape()) == [] assert repr(ps) == "<PartialShape: ...>" ps = PartialShape.dynamic(rank=Dimension(2)) assert not ps.is_static assert ps.is_dynamic assert ps.rank == 2 assert 2 == ps.rank assert list(ps.get_min_shape()) == [0, 0] assert list(ps.get_max_shape())[0] > 1000000000 assert repr(ps) == "<PartialShape: {?,?}>"
def test_partial_shape(): ps = PartialShape([1, 2, 3, 4]) assert ps.is_static assert not ps.is_dynamic assert ps.rank == 4 assert repr(ps) == "<PartialShape: {1,2,3,4}>" assert ps.get_dimension(0) == Dimension(1) assert ps.get_dimension(1) == Dimension(2) assert ps.get_dimension(2) == Dimension(3) assert ps.get_dimension(3) == Dimension(4) shape = Shape([1, 2, 3]) ps = PartialShape(shape) assert ps.is_static assert not ps.is_dynamic assert ps.all_non_negative assert ps.rank == 3 assert list(ps.get_shape()) == [1, 2, 3] assert list(ps.get_max_shape()) == [1, 2, 3] assert list(ps.get_min_shape()) == [1, 2, 3] assert list(ps.to_shape()) == [1, 2, 3] assert repr(shape) == "<Shape: {1, 2, 3}>" assert repr(ps) == "<PartialShape: {1,2,3}>" ps = PartialShape( [Dimension(1), Dimension(2), Dimension(3), Dimension.dynamic()]) assert not ps.is_static assert ps.is_dynamic assert ps.all_non_negative assert ps.rank == 4 assert list(ps.get_min_shape()) == [1, 2, 3, 0] assert list(ps.get_max_shape())[3] > 1000000000 assert repr(ps) == "<PartialShape: {1,2,3,?}>" assert ps.get_dimension(0) == Dimension(1) assert ps.get_dimension(1) == Dimension(2) assert ps.get_dimension(2) == Dimension(3) assert ps.get_dimension(3) == Dimension.dynamic() ps = PartialShape([1, 2, 3, -1]) assert not ps.is_static assert ps.is_dynamic assert ps.all_non_negative assert ps.rank == 4 assert list(ps.get_min_shape()) == [1, 2, 3, 0] assert list(ps.get_max_shape())[3] > 1000000000 assert repr(ps) == "<PartialShape: {1,2,3,?}>" ps = PartialShape.dynamic() assert not ps.is_static assert ps.is_dynamic assert ps.rank == Dimension.dynamic() assert list(ps.get_min_shape()) == [] assert list(ps.get_max_shape()) == [] assert repr(ps) == "<PartialShape: ...>" ps = PartialShape.dynamic(rank=Dimension(2)) assert not ps.is_static assert ps.is_dynamic assert ps.rank == 2 assert 2 == ps.rank assert list(ps.get_min_shape()) == [0, 0] assert list(ps.get_max_shape())[0] > 1000000000 assert repr(ps) == "<PartialShape: {?,?}>" shape_list = [(1, 10), [2, 5], 4, Dimension(2), "..10"] ref_ps = PartialShape([ Dimension(1, 10), Dimension(2, 5), Dimension(4), Dimension(2), Dimension(-1, 10) ]) assert PartialShape(shape_list) == ref_ps assert PartialShape(tuple(shape_list)) == ref_ps with pytest.raises(TypeError) as e: PartialShape([(1, 2, 3)]) assert "Two elements are expected in tuple(lower, upper) " \ "for dynamic dimension, but 3 elements were given." in str(e.value) with pytest.raises(TypeError) as e: PartialShape([("?", "?")]) assert "Incorrect pair of types (<class 'str'>, <class 'str'>) " \ "for dynamic dimension, ints are expected." in str(e.value) with pytest.raises(TypeError) as e: PartialShape([range(10)]) assert "Incorrect type <class 'range'> for dimension. Expected types are: " \ "int, str, openvino.runtime.Dimension, list/tuple with lower " \ "and upper values for dynamic dimension." in str(e.value) ps = PartialShape("...") assert ps == PartialShape.dynamic() ps = PartialShape("?, 3, ..224, 28..224") assert ps == PartialShape( [Dimension(-1), Dimension(3), Dimension(-1, 224), Dimension(28, 224)]) with pytest.raises(RuntimeError) as e: ps = PartialShape("?,,3") assert 'Cannot get vector of dimensions! "?,,3" is incorrect' in str( e.value) shape = Shape() assert len(shape) == 0