def __init__(self): #TODO load classifier # relative to file path, otherwise we have got problems with launch-type specific working directory base_path = os.path.dirname(os.path.abspath(__file__)) graph_pth = os.path.join(base_path, 'fine_tuned_model_real_mobilenet', 'frozen_inference_graph.pb') label_pth = os.path.join(base_path, 'fine_tuned_model_real_mobilenet', 'labels_map.pbtxt') self.graph_pth = graph_pth self.detection_graph = self.load_tf_graph(self.graph_pth) self.label_pth = label_pth self.label_map = label_map_util.load_labelmap(label_pth) self.categories = label_map_util.convert_label_map_to_categories(self.label_map, max_num_classes=3, use_display_name=True) self.category_index = label_map_util.create_category_index(self.categories) self.class_map = { 1: TrafficLight.GREEN, 2: TrafficLight.YELLOW, 3: TrafficLight.RED }
def __init__(self): # get our label map label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=3, use_display_name=True) self.category_index = label_map_util.create_category_index(categories) # other settings self.max_objects = 5 self.min_score_thresh = 0.99 self.line_thickness = 5 # Load the Tensorflow model into memory. detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') # get our graph and save to class self.sess = tf.Session(graph=detection_graph) # Define input and output tensors for the object detection classifier self.it = detection_graph.get_tensor_by_name('image_tensor:0') self.db = detection_graph.get_tensor_by_name('detection_boxes:0') self.ds = detection_graph.get_tensor_by_name('detection_scores:0') self.dc = detection_graph.get_tensor_by_name('detection_classes:0') self.nd = detection_graph.get_tensor_by_name('num_detections:0')
def Open(self): #Loading label map self.label_map = label_map_util.load_labelmap(self.PATH_TO_LABELS) self.categories = label_map_util.convert_label_map_to_categories(self.label_map, max_num_classes=self.NUM_CLASSES, use_display_name=True) self.category_index = label_map_util.create_category_index(self.categories) #Load a (frozen) Tensorflow model into memory. detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(self.PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') config = tf.ConfigProto( device_count = {'GPU': 0} ) detection_graph.as_default() self.detector = tf.Session(graph=detection_graph, config=config) # Definite input and output Tensors for detection_graph self.image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. self.detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. self.detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') self.detection_classes = detection_graph.get_tensor_by_name('detection_classes:0') self.num_detections = detection_graph.get_tensor_by_name('num_detections:0')
def _load_object_annotations(object_annotations_file): tf.logging.info('Building object index.') with tf.gfile.GFile(object_annotations_file, 'r') as fid: img_to_obj_annotation = json.load(fid) category_index = label_map_util.create_category_index() images = [] for patientId in img_to_obj_annotation: if patientId.endswith("1"): directory = "shift_image" elif patientId.endswith("2"): directory = "shift_bbox" elif patientId.endswith("3"): directory = "scale_bbox" elif patientId.endswith("4"): directory = "scale_image" elif patientId.endswith("5"): directory = "scale_shift_bbox" elif patientId.endswith("6"): directory = "shift_image_shift_bbox" else: directory = "scale_image_scale_shift_bbox" images.append({'height': 1024, 'width': 1024, 'id': patientId, 'file_name': "{}/{}.png".format(directory, patientId)}) return images, img_to_obj_annotation, category_index
def __init__(self): # Get paths TFWD_PATH = os.path.join(os.path.dirname(__file__), 'tf') PATH_TO_CKPT = os.path.join(TFWD_PATH,'frozen_inference_graph.pb') PATH_TO_LABELS = os.path.join(TFWD_PATH,'annotation.pbtxt') # get our label map label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=3, use_display_name=True) self.category_index = label_map_util.create_category_index(categories) # Load the Tensorflow model into memory. detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') # get our graph and save to class self.sess = tf.Session(graph=detection_graph) # Define input and output tensors for the object detection classifier self.it = detection_graph.get_tensor_by_name('image_tensor:0') self.db = detection_graph.get_tensor_by_name('detection_boxes:0') self.ds = detection_graph.get_tensor_by_name('detection_scores:0') self.dc = detection_graph.get_tensor_by_name('detection_classes:0') self.nd = detection_graph.get_tensor_by_name('num_detections:0')
def set_model(model_name): for file in glob.glob("*"): if (file == model_name): pass # Path to frozen detection graph. This is the actual model that is used for the object detection. path_to_ckpt = model_name + '/frozen_inference_graph.pb' # List of the strings that is used to add correct label for each box. path_to_labels = os.path.join('data', 'mscoco_label_map.pbtxt') num_classes = 90 # Load a (frozen) Tensorflow model into memory. detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(path_to_ckpt, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') # Loading label map # Label maps map indices to category names, so that when our convolution network predicts 5, we know that this corresponds to airplane. Here I use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine label_map = label_map_util.load_labelmap(path_to_labels) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=num_classes, use_display_name=True) category_index = label_map_util.create_category_index(categories) return detection_graph, category_index
def _load_mapphx(self): label_map = label_map_util.load_labelmap(self.PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=self.NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) return category_index
def __init__(self): #TODO load classifier sess, _ = load_graph('models/sim_model.pb') self.sess = sess self.sess_graph = self.sess.graph # Definite input and output Tensors for sess self.image_tensor = self.sess_graph.get_tensor_by_name( 'image_tensor:0') # Each box represents a part of the image where a particular object was detected. self.detection_boxes = self.sess_graph.get_tensor_by_name( 'detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. self.detection_scores = self.sess_graph.get_tensor_by_name( 'detection_scores:0') self.detection_classes = self.sess_graph.get_tensor_by_name( 'detection_classes:0') self.num_classes = 3 self.label_map = label_map_util.load_labelmap("./labelmap.pbtxt") self.categories = label_map_util.convert_label_map_to_categories( self.label_map, max_num_classes=self.num_classes, use_display_name=True) self.category_index = label_map_util.create_category_index( self.categories) self.pub_tl_clssifier_monitor = None self.bridge = None if SHOW_MONITOR_IMAGE: self.pub_tl_clssifier_monitor = rospy.Publisher( '/clssifier_monitor_image', Image_msg, queue_size=2) self.bridge = CvBridge()
def load_model(): # 添加模型路径: CWD_PATH = os.getcwd() # os.getcwd() 方法用于返回当前工作目录。 PATH_TO_CKPT = os.path.join(CWD_PATH, '../ssd_mobilenet_v1_coco_2018_01_28', 'frozen_inference_graph.pb') # List of the strings that is used to add correct label for each box. PATH_TO_LABELS = os.path.join(CWD_PATH, 'data', 'mscoco_label_map.pbtxt') NUM_CLASSES = 90 # 加载模型 detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') # 加载lable map label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) print('models have already loaded.') return detection_graph, category_index
def __displayDetectedImage(self, item): dcmFileName = item.data(0, Qt.UserRole) self.ui.statusBar.showMessage(dcmFileName) #预处理 dcmFile = pydicom.read_file(dcmFileName) origin = dcmFile.pixel_array # type:numpy.ndarray intercept = dcmFile.RescaleIntercept slope = dcmFile.RescaleSlope origin = origin * slope + intercept origin[origin < -100] = -100 # 去除低亮部分 origin[origin > 750] = 750 # 去除高亮部分 # 归一化到0-255 origin = cv2.normalize(origin, None, 0, 255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC3) image_np = np.expand_dims(origin, -1) # 将origin增加一个维度 image_np = np.repeat(image_np, 3, 2) # 将二维矩阵重复三次 #plt.imsave("output/undetected.jpg", image_np) NUM_CLASSES = 6 PATH_TO_LABELS = 'label_map.pbtxt' label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) result = item.data(1, Qt.UserRole) boxes = result[1] scores = result[2] classes = result[3] vis_util.visualize_boxes_and_labels_on_image_array( image_np, boxes, classes, scores, category_index, use_normalized_coordinates=True, line_thickness=1) #plt.imsave("output/detected.jpg", image_np) image = QImage(image_np, image_np.shape[1], image_np.shape[0], QImage.Format_RGB888) self.curPixmap = QPixmap(image) self.on_actZoomFitH_triggered() self.ui.actZoomIn.setEnabled(True) self.ui.actZoomOut.setEnabled(True) self.ui.actZoomRealSize.setEnabled(True) self.ui.actZoomFitW.setEnabled(True) self.ui.actZoomFitH.setEnabled(True)
def __init__(self, graph: tf.Graph, label_map): self.graph: tf.Graph = graph self.label_map = label_map self.categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=1000) self.category_index: CategoryIndex = ( label_map_util.create_category_index(self.categories))
def _load_labels(self): self.label_map = label_map_util.load_labelmap(self.labels_fp) self.categories = label_map_util.convert_label_map_to_categories( self.label_map, max_num_classes=self.num_classes, use_display_name=True) self.category_index = label_map_util.create_category_index( self.categories)
def __init__(self): self.saved_image_limit = 500 self.saved_image_counter = 1 self.save_images = rospy.get_param("~save_image", False) self.current_light = TrafficLight.UNKNOWN self.model_path = rospy.get_param('~model') self.readsize = 1024 # Build the model self.detection_graph = tf.Graph() # create config config = tf.ConfigProto() # reassemble the model from chunks. credit goes to team vulture for this idea if not os.path.exists(self.model_path): if not os.path.exists(self.model_path+'/chunks'): output = open(self.model_path, 'wb') frozen_model_path = os.path.dirname(self.model_path)+'/frozen_model_chunks' chunks = os.listdir(frozen_model_path) chunks.sort() for filename in chunks: filepath = os.path.join(frozen_model_path, filename) with open(filepath, 'rb') as fileobj: for chunk in iter(partial(fileobj.read, self.readsize), ''): output.write(chunk) output.close() # Create the graph with self.detection_graph.as_default(): graph_def = tf.GraphDef() with tf.gfile.GFile(self.model_path, 'rb') as fid: serialized_graph = fid.read() graph_def.ParseFromString(serialized_graph) tf.import_graph_def(graph_def, name='') rospy.loginfo('Loaded frozen tensorflow model: %s', self.model_path) # Create a reusable sesion attribute self.sess = tf.Session(graph=self.detection_graph, config=config) self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0') self.detection_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0') self.detection_scores = self.detection_graph.get_tensor_by_name('detection_scores:0') self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0') self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0') self.path = './light_classification/UTL_label_map.pbtxt' print(self.path) PATH_TO_LABELS = self.path NUM_CLASSES = 3 label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True) self.category_index = label_map_util.create_category_index(categories) self.count = 1
def main(_): # step 2: send a request options = [('grpc.max_send_message_length', 1000 * 1024 * 1024), ('grpc.max_receive_message_length', 1000 * 1024 * 1024)] channel = grpc.insecure_channel(FLAGS.server, options=options) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) request = predict_pb2.PredictRequest() request.model_spec.name = 'model' request.model_spec.signature_name = 'serving_default' # step 1: prepare input img = cv2.imread(FLAGS.image) h, w = img.shape[:2] if compress: ratio = w / h h1 = height w1 = round(h1 * ratio) scaled_img = cv2.resize(img, (w1, h1), interpolation=cv2.INTER_AREA) tensor = tf.contrib.util.make_tensor_proto(scaled_img, shape=[1] + list(scaled_img.shape)) else: tensor = tf.contrib.util.make_tensor_proto(img, shape=[1] + list(img.shape)) request.inputs['inputs'].CopyFrom(tensor) start = time.time() # step 3: get the results result_future = stub.Predict.future(request, 10.0) # 10 secs timeout result = result_future.result() stop = time.time() print('time is ', stop - start) NUM_CLASSES = 30 label_map = label_map_util.load_labelmap('annotations/label_map.pbtxt') categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) boxes = result.outputs['detection_boxes'].float_val classes = result.outputs['detection_classes'].float_val scores = result.outputs['detection_scores'].float_val result = vis_util.visualize_boxes_and_labels_on_image_array( img, np.reshape(boxes, [100, 4]), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=8) cv2.imwrite('result.jpg', result)
def load_labels_mapping(labels_path, n_classes=7): label_map = label_map_util.load_labelmap(labels_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=n_classes, use_display_name=True) category_index = label_map_util.create_category_index(categories) labels_mapping = {} for idx in category_index: labels_mapping[category_index[idx]['name']] = idx return labels_mapping
def get_labeled_image(image_path, path_to_labels, num_classes, boxes, classes, scores): label_map = label_map_util.load_labelmap(path_to_labels) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=num_classes, use_display_name=True) category_index = label_map_util.create_category_index(categories) image = Image.open(image_path) image_np = load_image_into_numpy_array(image) image_process = vis_util.visualize_boxes_and_labels_on_image_array( image_np, boxes, classes, scores, category_index) return image_process
def get_category_index(self): """ Transforms label map into category index for visualization. Returns: category_index: The category index corresponding to the given label map. """ label_map = label_map_util.load_labelmap(self.label_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=1, use_display_name=True) category_index = label_map_util.create_category_index(categories) return category_index
def _create_tf_record_from_coco_annotations(annotations_file, image_dir, output_path, include_masks, num_shards): """Loads COCO annotation json files and converts to tf.Record format. Args: annotations_file: JSON file containing bounding box annotations. image_dir: Directory containing the image files. output_path: Path to output tf.Record file. include_masks: Whether to include instance segmentations masks (PNG encoded) in the result. default: False. num_shards: number of output file shards. """ with contextlib2.ExitStack() as tf_record_close_stack, \ tf.io.gfile.GFile(annotations_file, 'r') as fid: output_tfrecords = tf_record_creation_util.open_sharded_output_tfrecords( tf_record_close_stack, output_path, num_shards) groundtruth_data = json.load(fid) images = groundtruth_data['images'] category_index = label_map_util.create_category_index( groundtruth_data['categories']) annotations_index = {} if 'annotations' in groundtruth_data: logging.info( 'Found groundtruth annotations. Building annotations index.') for annotation in groundtruth_data['annotations']: image_id = annotation['image_id'] if image_id not in annotations_index: annotations_index[image_id] = [] annotations_index[image_id].append(annotation) missing_annotation_count = 0 for image in images: image_id = image['id'] if image_id not in annotations_index: missing_annotation_count += 1 annotations_index[image_id] = [] logging.info('%d images are missing annotations.', missing_annotation_count) total_num_annotations_skipped = 0 for idx, image in enumerate(images): if idx % 100 == 0: logging.info('On image %d of %d', idx, len(images)) annotations_list = annotations_index[image['id']] _, tf_example, num_annotations_skipped = create_tf_example( image, annotations_list, image_dir, category_index, include_masks) total_num_annotations_skipped += num_annotations_skipped shard_idx = idx % num_shards output_tfrecords[shard_idx].write(tf_example.SerializeToString()) logging.info('Finished writing, skipped %d annotations.', total_num_annotations_skipped)
def __init__(self, face_crop_size=160, face_crop_margin=32): self.label_map = label_map_util.load_labelmap(self.PATH_TO_LABELS) self.categories = label_map_util.convert_label_map_to_categories( self.label_map, max_num_classes=self.NUM_CLASSES, use_display_name=True) self.category_index = label_map_util.create_category_index( self.categories) self.detection_graph, self.detection_graph_sess = self._setup_mobilenet( ) self.face_crop_size = face_crop_size self.face_crop_margin = face_crop_margin
def detect(image, threshold=0.5): PATH_TO_CKPT = os.path.join('data', 'model', 'frozen_inference_graph.pb') PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt') NUM_CLASSES = 2 objects = [] detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') detection_boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') detection_scores = detection_graph.get_tensor_by_name( 'detection_scores:0') detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') num_detections = detection_graph.get_tensor_by_name( 'num_detections:0') image_np_expanded = np.expand_dims(image, axis=0) (boxes, scores, classes, num) = sess.run([ detection_boxes, detection_scores, detection_classes, num_detections ], feed_dict={image_tensor: image_np_expanded}) for i in range(min(20, np.squeeze(boxes).shape[0])): class_value = np.squeeze(classes).astype(np.int32)[i] score_value = np.squeeze(scores)[i] if class_value in category_index.keys(): ymin, xmin, ymax, xmax = tuple( np.squeeze(boxes)[i].tolist()) class_name = category_index[class_value]['name'] if score_value > threshold: objects.append([class_name, (xmin, xmax, ymin, ymax)]) return objects
def _init_category_index(label_map_path): """Creates category index from class indexes to name of the classes. Args: label_map_path: path to the mapping. Returns: A map for mapping int keys to string categories. """ label_map = label_map_util.load_labelmap(label_map_path) num_classes = np.max(x.id for x in label_map.item) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=num_classes, use_display_name=True) category_index = label_map_util.create_category_index(categories) return category_index
def load(self): config = tf.ConfigProto() config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1 self.detection_graph = tf.Graph() with tf.Session(graph=self.detection_graph, config=config) as sess: self.session = sess od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True) self.category_index = label_map_util.create_category_index(categories)
def __init__(self, model_path, label_path): #TODO load classifier self.label_map = label_map_util.load_labelmap(label_path) self.categories = label_map_util.convert_label_map_to_categories( self.label_map, max_num_classes=NUM_CLASSES, use_display_name=True) self.category_index = label_map_util.create_category_index( self.categories) self.model_path = model_path self.detection_graph = None self.tf_session = None self.image_tensor = None self.detection_boxes = None self.detection_scores = None self.detection_classes = None self.num_detections = None
def init_hook(**params): threshold = params.get('threshold') skip_scores = params.get('skip_scores') skip_labels = params.get('skip_labels') thickness = params.get('line_thickness') max_boxes = params.get('max_boxes') input_type = params.get('input_type') output_type = params.get('output_type') if input_type and input_type in ['encoded_image_string', 'image_tensor']: LOG.info('Set input type to "%s"' % input_type) PARAMS['input_type'] = input_type if output_type and output_type in ['image', 'boxes']: LOG.info('Set output type to "%s"' % output_type) PARAMS['output_type'] = output_type if skip_labels: PARAMS['skip_labels'] = boolean_string(skip_labels) if skip_scores: PARAMS['skip_scores'] = boolean_string(skip_labels) if threshold: PARAMS['threshold'] = float(threshold) if thickness: PARAMS['line_thickness'] = int(thickness) if max_boxes: PARAMS['max_boxes'] = int(max_boxes) label_map_path = params.get('label_map') if not label_map_path: raise RuntimeError('Label map required. Provide path to label_map via' ' -o label_map=<label_map.pbtxt>') LOG.info('Loading label map from %s...' % label_map_path) label_map = label_map_util.load_labelmap(label_map_path) max_num_classes = max([item.id for item in label_map.item]) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes) global category_index category_index = label_map_util.create_category_index(categories) LOG.info('Loaded.') LOG.info('Initialized with params: %s', PARAMS)
def __init__(self): self.modelpath = rospy.get_param('~model_path') # PATH_TO_MODEL = 'models/trial19_ssd_inception_sim_frozen_inference_graph.pb' # model resnet-udacity-sim-large-10-regions PATH_TO_MODEL = self.modelpath self.saved_image_limit = 500 self.saved_image_counter = 1 self.save_images = False self.readsize = 1024 # Build the model self.detection_graph = tf.Graph() # Create the graph with self.detection_graph.as_default(): graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_MODEL, 'rb') as fid: serialized_graph = fid.read() graph_def.ParseFromString(serialized_graph) tf.import_graph_def(graph_def, name='') self.image_tensor = self.detection_graph.get_tensor_by_name( 'image_tensor:0') self.detection_boxes = self.detection_graph.get_tensor_by_name( 'detection_boxes:0') self.detection_scores = self.detection_graph.get_tensor_by_name( 'detection_scores:0') self.detection_classes = self.detection_graph.get_tensor_by_name( 'detection_classes:0') self.num_detections = self.detection_graph.get_tensor_by_name( 'num_detections:0') self.path = './light_classification/Conversion_label_map.pbtxt' # Create a reusable sesion attribute self.sess = tf.Session(graph=self.detection_graph) PATH_TO_LABELS = self.path NUM_CLASSES = 4 label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) self.category_index = label_map_util.create_category_index(categories) self.count = 1
def __init__(self, num_class): threading.Thread.__init__(self) self.map = {1: "bottle", 2: "speaker", 3: "scissors"} self.current_Path = os.getcwd() self.Load_from_location = os.path.join(self.current_Path, 'images') self.Store_Search = store_Search( os.path.join(self.current_Path, 'static')) self.NUM_CLASSES = num_class self.PATH_TO_LABELS = os.path.join(self.current_Path, 'labelmap.pbtxt') self.label_map = label_map_util.load_labelmap(self.PATH_TO_LABELS) self.PATH_TO_CKPT = os.path.join(self.current_Path, 'frozen_inference_graph.pb') self.categories = label_map_util.convert_label_map_to_categories( self.label_map, max_num_classes=self.NUM_CLASSES, use_display_name=True) self.category_index = label_map_util.create_category_index( self.categories)
def evaluate(self): """Compute evaluation result. Returns: A dictionary of metrics with the following fields - 1. summary_metrics: 'Precision/mAP@<matching_iou_threshold>IOU': mean average precision at the specified IOU threshold. 2. per_category_ap: category specific results with keys of the form 'PerformanceByCategory/mAP@<matching_iou_threshold>IOU/category'. """ (per_class_ap, mean_ap, _, _, per_class_corloc, mean_corloc) = (self._evaluation.evaluate()) pascal_metrics = { self._metric_prefix + 'Precision/mAP@{}IOU'.format(self._matching_iou_threshold): mean_ap } if self._evaluate_corlocs: pascal_metrics[self._metric_prefix + 'Precision/meanCorLoc@{}IOU'.format( self._matching_iou_threshold)] = mean_corloc category_index = label_map_util.create_category_index(self._categories) for idx in range(per_class_ap.size): if idx + self._label_id_offset in category_index: display_name = ( self._metric_prefix + 'PerformanceByCategory/AP@{}IOU/{}'.format( self._matching_iou_threshold, category_index[idx + self._label_id_offset]['name'])) pascal_metrics[display_name] = per_class_ap[idx] # Optionally add CorLoc metrics.classes if self._evaluate_corlocs: display_name = ( self._metric_prefix + 'PerformanceByCategory/CorLoc@{}IOU/{}'.format( self._matching_iou_threshold, category_index[idx + self._label_id_offset]['name'])) pascal_metrics[display_name] = per_class_corloc[idx] return pascal_metrics
def __init__(self): CWD_PATH = os.getcwd() MODEL_FOLDER = 'models' MODEL_NAME = 'ssd_mobilenet_v1_model' PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_FOLDER, MODEL_NAME, 'frozen_inference_graph.pb') PATH_TO_LABELS = os.path.join(CWD_PATH, MODEL_FOLDER, MODEL_NAME, 'label_map.pbtxt') # 1 - person, 2 - dog, 3 - cat NUM_CLASSES = 3 # ssd engine ready signal self.ready = False # begin load models try: self.detection_graph = tf.Graph() with self.detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') self.detection_graph.as_default() except: print('Warning! Failed to load ' + MODEL_NAME + ' frozen graph file (.pb), object detection disabled') try: label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) self.category_index = label_map_util.create_category_index( categories) self.sess = tf.Session(graph=self.detection_graph) self.ready = True except: print('Warning! Failed to load ' + MODEL_NAME + ' label map (.pbtxt), object detection disabled')
def __init__(self, parent=None): # Load a (frozen) Tensorflow model into memory. self.detection_graph = tf.Graph() with self.detection_graph.as_default(): od_graph_def = tf.compat.v1.GraphDef() with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') # Loading label map label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) self.category_index = label_map_util.create_category_index(categories) self.count = 0 self.detection_graph.as_default() self.sess = tf.compat.v1.Session(graph=self.detection_graph)
def evaluate(self): """Compute evaluation result. Returns: A dictionary of metrics with the following fields - 1. summary_metrics: 'Precision/mAP@<matching_iou_threshold>IOU': mean average precision at the specified IOU threshold. 2. per_category_ap: category specific results with keys of the form 'PerformanceByCategory/mAP@<matching_iou_threshold>IOU/category'. """ (per_class_ap, mean_ap, _, _, per_class_corloc, mean_corloc) = ( self._evaluation.evaluate()) pascal_metrics = { self._metric_prefix + 'Precision/mAP@{}IOU'.format(self._matching_iou_threshold): mean_ap } if self._evaluate_corlocs: pascal_metrics[self._metric_prefix + 'Precision/meanCorLoc@{}IOU'.format( self._matching_iou_threshold)] = mean_corloc category_index = label_map_util.create_category_index(self._categories) for idx in range(per_class_ap.size): if idx + self._label_id_offset in category_index: display_name = ( self._metric_prefix + 'PerformanceByCategory/AP@{}IOU/{}'.format( self._matching_iou_threshold, category_index[idx + self._label_id_offset]['name'])) pascal_metrics[display_name] = per_class_ap[idx] # Optionally add CorLoc metrics.classes if self._evaluate_corlocs: display_name = ( self._metric_prefix + 'PerformanceByCategory/CorLoc@{}IOU/{}' .format(self._matching_iou_threshold, category_index[idx + self._label_id_offset]['name'])) pascal_metrics[display_name] = per_class_corloc[idx] return pascal_metrics
def load_model(self): # change params here NUM_CLASSES = 37 sess = tf.Session() with sess.as_default(): # Load the model od_graph_def = tf.GraphDef() with tf.gfile.GFile( os.path.join(self.model_dir, "frozen_inference_graph.pb"), 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') # Get handles to input and output tensors ops = tf.get_default_graph().get_operations() self._sess = sess all_tensor_names = {output.name for op in ops 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] = tf.get_default_graph().get_tensor_by_name( tensor_name) self._tsdict = tensor_dict # now we have a dictionary named category_index, with key as the index and the value the pet cato. name label_map = label_map_util.load_labelmap( os.path.join(self.model_dir, "label_map.pbtxt")) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) self._ct_index = label_map_util.create_category_index(categories)