def sem_segment(server_address, model_spec_name, image, label_map_path=None): """ Run a semantic segmentation inference on a TensorFlow Serving server. :param server_address: Address (hostname & port) of the TensorFlow Serving server. :param model_spec_name: The name of the model which you'd like the inference from. :param image: A PIL/Pillow image to inference on. :param label_map_path: The path to a label_map file, to be used to convert class ids to human-readable names. :return: A tuple of the lists of classes, labels, scores, boxes, and masks. Classes are integer ids, labels are text (or the entire list is `None` if no `label_map_path` was not specified), scores are floats on [0,1), boxes are lists of length 4 with bounds represented on [0,1]. The exact format of the masks isn't well-specified yet. """ numpy_ary = np.array([np.array(image, dtype=np.uint8)]) response = requests.post(f'http://{server_address}/v1/models/{model_spec_name}:predict', json={"inputs": numpy_ary.tolist()}) response.raise_for_status() outputs = response.json()['outputs'] if label_map_path: label_map = label_map_util.create_category_index_from_labelmap(label_map_path, use_display_name=True) else: label_map = None num_detections = int(outputs['num_detections'][0]) classes = [int(x) for x in outputs['detection_classes'][0][:num_detections]] labels = [label_map[x]['name'] for x in classes] if label_map else None scores = outputs['detection_scores'][0][:num_detections] boxes = outputs['detection_boxes'][0][:num_detections] if outputs['detection_boxes'] else None masks = outputs['detection_masks'][0][:num_detections] if outputs['detection_masks'] else None return classes, labels, scores, boxes, masks
def tf_init(self): detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(self.frz_graph, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') self.category_index = label_map_util.create_category_index_from_labelmap( self.labels, use_display_name=True) if not self.useGpu: config = tf.ConfigProto(device_count={'GPU': 0}) self.session = tf.Session(graph=detection_graph, config=config) else: self.session = tf.Session(graph=detection_graph) all_tensor_names = { output.name for op in detection_graph.get_operations() for output in op.outputs } tensor_dict = {} for key in [ 'num_detections', 'detection_boxes', 'detection_scores', 'detection_classes' ]: tensor_name = key + ':0' if tensor_name in all_tensor_names: tensor_dict[key] = detection_graph.get_tensor_by_name( tensor_name) self.image_tensor = detection_graph.get_tensor_by_name( 'image_tensor:0') self.tensor_dict = tensor_dict
def __init__(self, is_site): #TODO load classifier if is_site: MODEL_NAME = 'loop_inference_graph' else: MODEL_NAME = 'simulator_inference_graph' #MODEL_FILE = MODEL_NAME + '.tar.gz' #DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/' BASE_PATH = './light_classification/' # Path to frozen detection graph. This is the actual model that is used for the object detection. rospy.loginfo("path :: %s", os.getcwd()) PATH_TO_FROZEN_GRAPH = BASE_PATH + 'models/' + MODEL_NAME + '/frozen_inference_graph.pb' # List of the strings that is used to add correct label for each box. PATH_TO_LABELS = BASE_PATH + 'label_map.pbtxt' # Loading tensorflow model self.detection_graph = tf.Graph() with self.detection_graph.as_default(): self.od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid: self.serialized_graph = fid.read() self.od_graph_def.ParseFromString(self.serialized_graph) tf.import_graph_def(self.od_graph_def, name='') #Loading label map self.category_index = label_util.create_category_index_from_labelmap( PATH_TO_LABELS, use_display_name=True)
def read_category_map(self, pbtxt_file): label_dict = label_map_util.create_category_index_from_labelmap( pbtxt_file, use_display_name=True) labels = pd.DataFrame.from_dict(label_dict, orient='index') labels['value'] = '1' labels['type'] = 'boolean' labels['label_map'] = pbtxt_file labels.to_csv('categories.csv', index=False) return labels
def bbox_color(self, label): PATH_TO_LABELS = os.path.join(self.label_file) category_index = label_map_util.create_category_index_from_labelmap( PATH_TO_LABELS, use_display_name=True) labels = '' for i in range(1, len(category_index) + 1): labels += "'" + category_index[i]["name"] + "'," # if label in self.labels_to_highlight: if label in labels: return highlight_color else: return default_color
def sem_segment(server_address, model_spec_name, image, label_map_path=None): """ Run a semantic segmentation inference on a TensorFlow Serving server. :param server_address: Address (hostname & port) of the TensorFlow Serving server. :param model_spec_name: The name of the model which you'd like the inference from. :param image: A PIL/Pillow image to inference on. :param label_map_path: The path to a label_map file, to be used to convert class ids to human-readable names. :return: A tuple of the lists of classes, labels, scores, boxes, and masks. Classes are integer ids, labels are text (or the entire list is `None` if no `label_map_path` was not specified), scores are floats on [0,1), boxes are lists of length 4 with bounds represented on [0,1]. The exact format of the masks isn't well-specified yet. """ numpy_ary = np.array([np.array(image, dtype=np.uint8)]) stub = prediction_service_pb2_grpc.PredictionServiceStub( grpc.insecure_channel(server_address)) request = predict_pb2.PredictRequest() request.model_spec.name = model_spec_name request.inputs['inputs'].CopyFrom( tf.contrib.util.make_tensor_proto(numpy_ary)) result = stub.Predict(request, 10.0) outputs = result.outputs if label_map_path: label_map = label_map_util.create_category_index_from_labelmap( label_map_path, use_display_name=True) else: label_map = None num_detections = int(outputs['num_detections'].float_val[0]) classes = [ int(x) for x in outputs['detection_classes'].float_val[:num_detections] ] # TODO: Finish conversion labels = [label_map[x]['name'] for x in classes] if label_map else None scores = outputs['detection_scores'].float_val[:num_detections] boxes = outputs['detection_boxes'].float_val[:num_detections] if outputs[ 'detection_boxes'] else None masks = outputs['detection_masks'].float_val[:num_detections] if outputs[ 'detection_masks'] else None return classes, labels, scores, boxes, masks
# What model to download. MODEL_NAME = 'ssdlite_mobilenet_v2_coco' # Path to frozen detection graph. This is the actual model that is used for the object detection. PATH_TO_FROZEN_GRAPH = '../fine_tune_model/frozen_inference_graph.pb' # List of the strings that is used to add correct label for each box. PATH_TO_LABELS = '../annotations/label_map.pbtxt' detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') category_index = label_map_util.create_category_index_from_labelmap( PATH_TO_LABELS, use_display_name=True) with detection_graph.as_default(): with tf.Session() as sess: cap = cv2.VideoCapture('test.mp4') frame_number = 0 while (cap.isOpened()): ret, frame = cap.read() image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).astype(np.uint8) # Get handles to input and output tensors ops = tf.get_default_graph().get_operations() all_tensor_names = { output.name for op in ops for output in op.outputs } tensor_dict = {}