def insert_pre_processing(graph: Graph, input_node: Node, node_mean_scale_values: np.array, preprocessing_name: str): assert preprocessing_name in ['scale', 'mean'] if node_mean_scale_values.get(preprocessing_name) is None: return user_value = node_mean_scale_values[preprocessing_name] value = 1 / user_value if preprocessing_name == 'scale' else user_value * (-1) optimize_value = int(preprocessing_name == 'scale') op = Mul if preprocessing_name == 'scale' else Add if all([x == optimize_value for x in value]): return assert input_node.has_valid('shape') features_dim_idx = get_features_dim(graph.graph['layout'], len(input_node.shape)) assert value.size == input_node.shape[features_dim_idx] or value.size == 1 shape = np.ones(len(input_node.shape), dtype=np.int64) shape[features_dim_idx] = value.size value = value.reshape(shape) name = input_node.soft_get('name', input_node.id) + '/' + preprocessing_name preprocessing = create_op_with_const_inputs(graph, op=op, port_value_dict={1: value}, op_attrs={'name': name}) for dst in input_node.out_port(0).get_destinations(): if dst.node.soft_get('type') != 'ShapeOf': dst.get_connection().set_source(preprocessing.out_port(0)) input_node.out_port(0).connect(preprocessing.in_port(0))
def insert_pre_processing(graph: Graph, input_node: Node, node_mean_scale_values: np.array, preprocessing_name: str): assert preprocessing_name in ['scale', 'mean'] if node_mean_scale_values.get(preprocessing_name) is None: return user_value = node_mean_scale_values[preprocessing_name] value = 1 / user_value if preprocessing_name == 'scale' else user_value * (-1) optimize_value = int(preprocessing_name == 'scale') op = Mul if preprocessing_name == 'scale' else Add if all([x == optimize_value for x in value]): return assert input_node.has_valid('shape') features_dim_idx = get_features_dim(graph.graph['layout'], len(input_node.shape)) assert value.size == input_node.shape[features_dim_idx] or value.size == 1 shape = np.ones(len(input_node.shape), dtype=np.int64) shape[features_dim_idx] = value.size value = value.reshape(shape) name = input_node.soft_get('name', input_node.id) + '/' + preprocessing_name preprocessing = create_op_with_const_inputs(graph, op=op, port_value_dict={1: value}, op_attrs={'name': name}) for dst in input_node.out_port(0).get_destinations(): if dst.node.soft_get('type') != 'ShapeOf': # After the insertion of additional operations model optimizer # should keep the link to the input layer. Parameter node in framework # should map to parameter node in IR. # For this reason 'fw_tensor_debug_info' should be kept in data node. dst.get_connection().set_source(preprocessing.out_port(0), "source") input_node.out_port(0).connect(preprocessing.in_port(0))
def insert_pre_processing(graph: Graph, input_node: Node, node_mean_scale_values: np.array, preprocessing_name: str): assert preprocessing_name in ['scale', 'mean'] if node_mean_scale_values.get(preprocessing_name) is None: return user_value = node_mean_scale_values[preprocessing_name] value = 1 / user_value if preprocessing_name == 'scale' else user_value * ( -1) optimize_value = int(preprocessing_name == 'scale') op = Mul if preprocessing_name == 'scale' else Add if all([x == optimize_value for x in value]): return assert input_node.has_valid('shape') features_dim_idx = get_features_dim(graph.graph['layout'], len(input_node.shape)) assert compatible_dims( value.size, input_node.shape[features_dim_idx]) or value.size == 1 shape = np.ones(len(input_node.shape), dtype=np.int64) shape[features_dim_idx] = value.size value = value.reshape(shape) name = input_node.soft_get('name', input_node.id) + '/' + preprocessing_name preprocessing = create_op_with_const_inputs(graph, op=op, port_value_dict={1: value}, op_attrs={'name': name}) if input_node.is_out_port_connected(0) and len( input_node.out_port(0).get_destinations()) == 1: # There are models with pattern Parameter(uint8) -> Convert(float). # Adding mean/scale leads to the following: # Parameter(uint8) -> Mean/Scale -> Convert(float) which is incorrect. # To fix this mean and scale preprocessing node is inserted after Convert(float) node. out_node = input_node.out_port(0).get_destination().node convert_type = out_node.soft_get('dst_type') if out_node.soft_get('type') == "Convert" and (convert_type in [ np.float32, np.float16 ]): input_node = out_node if convert_type != value.dtype: new_value = value.astype(convert_type) const_node = preprocessing.in_port( 1).get_connection().get_source().node const_node['value'] = new_value for dst in input_node.out_port(0).get_destinations(): if dst.node.soft_get('type') != 'ShapeOf': # After the insertion of additional operations model optimizer # should keep the link to the input layer. Parameter node in framework # should map to parameter node in IR. # For this reason 'fw_tensor_debug_info' should be kept in data node. dst.get_connection().set_source(preprocessing.out_port(0), "source") input_node.out_port(0).connect(preprocessing.in_port(0))
def draw_boxes(image: np.array, bboxes: list, ids: list = None, box_type: str = 'detector'): ''' Draw the bounding boxes on the image, return image with boxes :param image: `np.array` with shape (H, W, 3) in RGB format :param bboxes: list of `np.array`s, each np.array contains coordinates of BB in tlbr format (see `convert_bbox`) :param ids: if not None, put ID from 'ids' on top of each bounding box :return image: image with the drawn boxes ''' if box_type == 'detector': ids = range(len(bboxes)) # just in bboxes order bboxes_for_draw = bboxes if box_type == 'tracker': ids = [tr_id for bbox, tr_id in bboxes] bboxes_for_draw = [bbox for bbox, tr_id in bboxes] if box_type == 'vis_to_file': ids = ids bboxes_for_draw = bboxes # print('bboxes=', bboxes) # print('bboxes_for_draw=', bboxes_for_draw) digit_height = 35 digit_width = 23 for ID, bbox in zip(ids, bboxes_for_draw): # print('ID, bbox, box_type==', ID, bbox, box_type) image = cv2.rectangle( image, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2, ) if box_type == 'detector': note = 'det=' + str(ID) note_coords = (int(bbox[0]), int(bbox[1]) + digit_height) note_color = (255, 0, 0) elif box_type == 'tracker' or 'vis_to_file': note = str(ID) #'tr_id=' + str(ID) note_coords = (int(bbox[0]), int(bbox[1]) + digit_height) note_color = (0, 0, 0) else: raise ValueError image = cv2.rectangle(image, (int(bbox[0]), int(bbox[1])), (int(bbox[0]) + digit_width * len(str(note)), int(bbox[1]) + digit_height), (255, 255, 255), cv2.FILLED) image = cv2.putText( image, text=note, org=note_coords, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=note_color, thickness=2, lineType=cv2.LINE_AA, ) return image if type(image) == type(np.array([])) else image.get()