def load_model(self): pipeline_config = os.path.join(self.model_ckp_path, "pipeline.config") model_dir = os.path.join(self.model_ckp_path, "checkpoint") label_map_path = os.path.join(self.model_ckp_path, "label_map.pbtxt") configs = config_util.get_configs_from_pipeline_file(pipeline_config) model_config = configs['model'] detection_model = model_builder.build(model_config=model_config, is_training=False) ckpt = tf.compat.v2.train.Checkpoint(model=detection_model) ckpt.restore(os.path.join(model_dir, 'ckpt-0')).expect_partial() msg = "Model was restored: {0}".format(model_dir) logging.info(msg) self.detect_fn = self.get_model_detection_function(detection_model) label_map = label_map_util.load_labelmap(label_map_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) self.category_index = label_map_util.create_category_index(categories) label_map_dict = label_map_util.get_label_map_dict( label_map, use_display_name=True) msg = "Label map was loaded: {0}".format(label_map_path) logging.info(msg) self.assign_color_to_classes()
def InferenceTest(): print('Sample Inference model.') labels_file_path = '/home/gowtham/Desktop/Unity3d/Kerasbg/pretrained/label_map.pbtxt' model_dir = "/home/gowtham/Desktop/Unity3d/Kerasbg/weights/" pipeline_config = '/home/gowtham/Desktop/Unity3d/Kerasbg/pretrained/' + 'pipeline.config' checkpoint_path = model_dir + 'ckpt18800-1' configs = config_util.get_configs_from_pipeline_file(pipeline_config) model_config = configs['model'] detection_model = model_builder.build(model_config=model_config, is_training=False) ckpt = tf.compat.v2.train.Checkpoint(model=detection_model) ckpt.restore(checkpoint_path) test_Images_NP = helper.load_image_into_numpy_array(train_Image_Dir + "/Test") label_id_offset = 1 label_map = label_map_util.load_labelmap(labels_file_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) category_index = label_map_util.create_category_index(categories) return test_Images_NP, category_index, detection_model
def set_model_params(self, exported_model_dir="export_dir"): pipeline_config = exported_model_dir + "/pipeline.config"; model_dir = exported_model_dir + '/checkpoint/' # Load pipeline config and build a detection model configs = config_util.get_configs_from_pipeline_file(pipeline_config) model_config = configs['model'] detection_model = model_builder.build( model_config=model_config, is_training=False) # Restore checkpoint ckpt = tf.compat.v2.train.Checkpoint( model=detection_model) ckpt.restore(os.path.join(model_dir, 'ckpt-0')).expect_partial() self.system_dict["detect_fn"] = self.get_model_detection_function(detection_model) label_map_path = configs['eval_input_config'].label_map_path label_map = label_map_util.load_labelmap(label_map_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) self.system_dict["category_index"] = label_map_util.create_category_index(categories) self.system_dict["label_map_dict"] = label_map_util.get_label_map_dict(label_map, use_display_name=True)
def load(self): with open(os.path.join(self.model_path, 'config.json')) as f: data = json.load(f) try: self.validate_json_configuration(data) self.set_model_configuration(data) except ApplicationError as e: raise e self.label_path = os.path.join(self.model_path, 'object-detection.pbtxt') self.label_map = label_map_util.load_labelmap(self.label_path) self.categories = label_map_util.convert_label_map_to_categories( self.label_map, max_num_classes=label_map_util.get_max_label_map_index( self.label_map), use_display_name=True) self.category_index = label_map_util.create_category_index( self.categories) self.label_map_dict = label_map_util.get_label_map_dict( self.label_map, use_display_name=True) self.labels = [label for label in self.label_map_dict] # allow memory growth [ tf.config.experimental.set_memory_growth(gpu, True) for gpu in tf.config.experimental.list_physical_devices('GPU') ] self.detect_fn = tf.saved_model.load(self.model_path)
def get_labels(): label_map_path = 'object_detection/data/mscoco_label_map.pbtxt' label_map = label_map_util.load_labelmap(label_map_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) category_index = label_map_util.create_category_index(categories) label_map_dict = label_map_util.get_label_map_dict(label_map, use_display_name=True) return label_map_dict
def __init__(self, model_name, models_dir=None, label_map_path=None, score_thres=0.45): # models_dir arg validation if models_dir is None: models_dir = path.join(HumanLocalizer.MODULE_DIR, "NNs") if not path.exists(models_dir) or not path.isdir(models_dir): raise ValueError("The specified models_dir: {} does not exist, or is not a directory".format(models_dir)) self.models_dir = models_dir # model_name arg validation models = [d for d in listdir(self.models_dir) if path.isdir(path.join(self.models_dir, d))] if model_name not in models: raise ValueError("The requested model {} does not exist! Please download to the specified models_dir".format(model_name)) self.model_name = model_name # label_map_path arg validation if label_map_path is None: label_map_path = path.join(self.models_dir, 'mscoco_label_map.pbtxt') if not path.exists(label_map_path) or not path.isfile(label_map_path): raise ValueError("The specified label_map_path: {} does not exist, or is not a file".format(label_map_path)) self.label_map_path = label_map_path path_to_ckpt = os.path.join(self.models_dir, self.model_name, "checkpoint", "ckpt-0") path_to_cfg = os.path.join(self.models_dir, self.model_name, "pipeline.config") #recover saved model self.configs = config_util.get_configs_from_pipeline_file(path_to_cfg) model_config = self.configs['model'] self.detection_model = model_builder.build(model_config=model_config, is_training=False) # Restore checkpoint self.ckpt = tf.compat.v2.train.Checkpoint(model=self.detection_model) self.ckpt.restore(path_to_ckpt) # load map labels for inference decoding label_map = label_map_util.load_labelmap(self.label_map_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) self.category_index = label_map_util.create_category_index(categories) for k, v in self.category_index.items(): if v['name'] == 'person': self.person_category_index = v['id'] self.label_id_offset = 1 self.detect_fn = self.get_model_detection_function(self.detection_model) self.score_thres = score_thres
def load_detection_graph_and_category_index(self): self.detection_graph = tf.Graph() with self.detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(self.path_to_the_inference_graph, '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(self.path_to_label_map_file) self.num_classes = label_map_util.get_max_label_map_index(label_map) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=self.num_classes, use_display_name=True) self.category_index = label_map_util.create_category_index(categories)
def get_label_map(): label_map_path = os.path.join(root_file_path, "..", "checkpoints", "label_map.txt") label_map = label_map_util.load_labelmap(label_map_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True, ) category_index = label_map_util.create_category_index(categories) label_map_dict = label_map_util.get_label_map_dict(label_map, use_display_name=False) return categories, category_index, label_map_dict
def load_label_map(configs): label_map_path = configs['eval_input_config'].label_map_path label_map = label_map_util.load_labelmap( 'models/research/object_detection/data/mscoco_complete_label_map.pbtxt' ) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) category_index = label_map_util.create_category_index(categories) label_map_dict = label_map_util.get_label_map_dict(label_map, use_display_name=True) return label_map_dict, label_map_path, category_index
def create_label_map(self): configs = config_util.get_configs_from_pipeline_file( self.pipeline_config) label_map_path = configs['eval_input_config'].label_map_path label_map = label_map_util.load_labelmap(label_map_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) category_index = label_map_util.create_category_index(categories) label_map_dict = label_map_util.get_label_map_dict( label_map, use_display_name=True) return label_map_dict
def run_model(filename): model = tf.saved_model.load(f'saved_model') # pipeline_config = 'pipeline_file.config' # configs = config_util.get_configs_from_pipeline_file(pipeline_config) # locc = 'Food_label_map.pbtxt' # map labels for inference decoding label_map_path = 'Food_label_map.pbtxt' label_map = label_map_util.load_labelmap(label_map_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) category_index = label_map_util.create_category_index(categories) label_map_dict = label_map_util.get_label_map_dict(label_map, use_display_name=True) print(list(label_map_dict.keys())[list( label_map_dict.values()).index(1)]) # Prints george output_score = [] output_item = [] for image_path in glob.glob(filename): image_np = load_image_into_numpy_array(image_path) output_dict = run_inference_for_single_image(model, image_np) viz_utils.visualize_boxes_and_labels_on_image_array( image_np, output_dict['detection_boxes'], output_dict['detection_classes'], output_dict['detection_scores'], category_index, instance_masks=output_dict.get('detection_masks_reframed', None), use_normalized_coordinates=True, line_thickness=8) display(Image.fromarray(image_np)) for i in range(len(output_dict['detection_scores'])): if output_dict['detection_scores'][i] > 0.5: output_score.append(output_dict['detection_scores'][i]) output_item.append((list(label_map_dict.keys())[list( label_map_dict.values()).index( output_dict['detection_classes'][i])])) print(output_item) print(output_score) new_dict = dict(zip(output_item, output_score)) Image.fromarray(image_np).save('out_' + filename) return new_dict
def __init__(self, args): self.args = args #self.FULL_LABEL_CLASSES=args.FULL_LABEL_CLASSES self.threshold = args.threshold tf.keras.backend.clear_session() self.detect_fn = tf.saved_model.load(args.modelbasefolder) label_map_path=args.labelmappath #'./models/research/object_detection/data/mscoco_label_map.pbtxt' label_map = label_map_util.load_labelmap(label_map_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) self.category_index = label_map_util.create_category_index(categories) label_map_dict = label_map_util.get_label_map_dict(label_map, use_display_name=True) self.FULL_LABEL_CLASSES=list(label_map_dict.keys())
def get_detection_graph_and_category_index( self, model_name="ssd_inception_v2_coco_11_06_2017", path_to_the_model_database="/TFM/model_database"): self.look_for_the_model(model_name, path_to_the_model_database) self.detection_graph = tf.Graph() with self.detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(self.path_to_the_inference_graph, '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(self.path_to_label_map_file) self.num_classes = label_map_util.get_max_label_map_index(label_map) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=self.num_classes, use_display_name=True) self.category_index = label_map_util.create_category_index(categories) return detection_graph, category_index
def getLabels(self, image): #load image into np array (im_width, im_height) = image.size image_np = np.array(image.getdata()).reshape( (im_height, im_width, 3)).astype(np.uint8) input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32) detections, predictions_dict, shapes = self.detect_fn(input_tensor) #TODO (Henry): visualization code for now configs = config_util.get_configs_from_pipeline_file( self.pipeline_config) #map labels for inference decoding label_map_path = configs['eval_input_config'].label_map_path label_map = label_map_util.load_labelmap( os.path.join(os.path.dirname(__file__), label_map_path)) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) category_index = label_map_util.create_category_index(categories) # label_id_offset = 1 # image_np_with_detections = image_np.copy() # viz_utils.visualize_boxes_and_labels_on_image_array( # image_np_with_detections, # detections['detection_boxes'][0].numpy(), # (detections['detection_classes'][0].numpy() + label_id_offset).astype(int), # detections['detection_scores'][0].numpy(), # category_index, # use_normalized_coordinates=True, # max_boxes_to_draw=8, # min_score_thresh=0.4, # agnostic_mode=False, # ) # plt.figure(figsize=(12,16)) # plt.imshow(image_np_with_detections) # plt.savefig('res.png') return detections, category_index
def set_model_params(self, exported_model_dir="export_dir", model_type="saved_model"): saved_model_dir = exported_model_dir + '/saved_model' self.system_dict["saved_model_loaded"] = tf.saved_model.load( saved_model_dir, tags=[tag_constants.SERVING]) self.system_dict["signature_keys"] = list( self.system_dict["saved_model_loaded"].signatures.keys()) pipeline_config = exported_model_dir + '/pipeline.config' configs = config_util.get_configs_from_pipeline_file(pipeline_config) label_map_path = configs['eval_input_config'].label_map_path label_map = label_map_util.load_labelmap(label_map_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) self.system_dict[ "category_index"] = label_map_util.create_category_index( categories) self.system_dict["label_map_dict"] = label_map_util.get_label_map_dict( label_map, use_display_name=True)
def make_detect_function(model_dir, labelmap_path): configs = config_util.get_configs_from_pipeline_file(str(Path(model_dir, 'pipeline.config'))) model = model_builder.build(model_config=configs['model'], is_training=False) ckpt = tf.compat.v2.train.Checkpoint(model=model) ckpt.restore(str(Path(model_dir, 'checkpoint', 'ckpt-0'))).expect_partial() @tf.function def func(image): """Detect objects in image.""" image, shapes = model.preprocess(image) prediction_dict = model.predict(image, shapes) detections = model.postprocess(prediction_dict, shapes) return detections, prediction_dict, tf.reshape(shapes, [-1]) label_map = label_map_util.load_labelmap(str(Path(labelmap_path))) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True, ) category_index = label_map_util.create_category_index(categories) return func, category_index
image, shapes = model.preprocess(image) prediction_dict = model.predict(image, shapes) detections = model.postprocess(prediction_dict, shapes) return detections, prediction_dict, tf.reshape(shapes, [-1]) return detect_fn detect_fn = get_model_detection_function(detection_model) label_map_path = "models/research/object_detection/data/mscoco_label_map.pbtxt" label_map = label_map_util.load_labelmap(label_map_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True, ) category_index = label_map_util.create_category_index(categories) label_map_dict = label_map_util.get_label_map_dict(label_map, use_display_name=True) image_dir = output_dir + "/sample_01" import json def load_zone_anno(json_filename): """ Load the json with ROI and MOI annotation. """
def test_get_max_label_map_index(self): num_classes = 4 label_map_proto = self._generate_label_map(num_classes=num_classes) max_index = label_map_util.get_max_label_map_index(label_map_proto) self.assertEqual(num_classes, max_index)
def test_get_max_label_map_index(self): num_classes = 4 label_map_proto = self._generate_label_map(num_classes=num_classes) max_index = label_map_util.get_max_label_map_index(label_map_proto) self.assertEqual(num_classes, max_index)
def test(pipeline_config_path, model_dir, label_map_path, test_data_dir, inference_dir): Path(inference_dir).mkdir(parents=True, exist_ok=True) configs = config_util.get_configs_from_pipeline_file(pipeline_config_path) model_config = configs['model'] detection_model = model_builder.build(model_config=model_config, is_training=False) ckpt = tf.compat.v2.train.Checkpoint(model=detection_model) ckpt.restore(model_dir) # detect_fn = get_model_detection_function(detection_model) label_map = label_map_util.load_labelmap(label_map_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) category_index = label_map_util.create_category_index(categories) all_image = get_all_image_files(test_data_dir) for image_path in all_image: file_name = os.path.basename(image_path) name, image_format = os.path.splitext(image_path) try: image_np = load_image_into_numpy_array(image_path) input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32) detections, predictions_dict, shapes = detect_fn( detection_model, input_tensor) width = shapes.numpy()[1] height = shapes.numpy()[0] boxes = detections['detection_boxes'][0].numpy() classes = (detections['detection_classes'][0].numpy() + 1).astype(int) scores = detections['detection_scores'][0].numpy() display_str = f'image : {file_name}' for i in range(boxes.shape[0]): score = round(100 * scores[i]) if score >= 25: # print(boxes[i]) display_str = f'{display_str} / {category_index[classes[i]]["name"]}: {str(round(100 * scores[i]))}% ' \ f'({str(boxes[i])})' ## xmin, ymin, xmax, ymax print(display_str) label_id_offset = 1 image_np_with_detections = image_np.copy() viz_utils.visualize_boxes_and_labels_on_image_array( image_np_with_detections, detections['detection_boxes'][0].numpy(), (detections['detection_classes'][0].numpy() + label_id_offset).astype(int), detections['detection_scores'][0].numpy(), category_index, use_normalized_coordinates=True, max_boxes_to_draw=200, min_score_thresh=.25, agnostic_mode=False, ) save_img = cv2.cvtColor(image_np_with_detections, cv2.COLOR_BGR2RGB) class_dir = os.path.dirname(image_path).split('/')[-1] Path(os.path.join(inference_dir, class_dir)).mkdir(parents=True, exist_ok=True) shutil.copy( f'{name}-o.csv', os.path.join(inference_dir, class_dir, f'{os.path.basename(name)}-o.csv')) cv2.imwrite(os.path.join(inference_dir, class_dir, file_name), save_img) except Exception as e: print(f'### Exception : {file_name} - {str(e)}')
def main(input_path, output_path, config_path, ckpt_path): # we recover our saved model here cwd = os.path.abspath(os.getcwd()) # gets the last ckpt from the ckpt folder automatically, # gets full paths for ckpt and pipeline files ckpt_name = sorted(os.listdir(ckpt_path))[1].split(".")[0] model_dir = ckpt_path + ckpt_name config_path = cwd + "/" + config_path model_dir = cwd + "/" + model_dir print("[INFO]: Last checkpoint is:", model_dir) print() print("[INFO]: Config path is:", config_path) print() configs = config_util.get_configs_from_pipeline_file(config_path) print(configs) print() model_config = configs["model"] detection_model = model_builder.build(model_config=model_config, is_training=False) # Restore checkpoint ckpt = tf.compat.v2.train.Checkpoint(model=detection_model) ckpt.restore(model_dir) print("[INFO]: Done restoring model...") detect_fn = get_model_detection_function(detection_model) #map labels for inference decoding label_map_path = configs['eval_input_config'].label_map_path label_map = label_map_util.load_labelmap(label_map_path) print("[INFO]: Done") categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=label_map_util.get_max_label_map_index(label_map), use_display_name=True) category_index = label_map_util.create_category_index(categories) label_map_dict = label_map_util.get_label_map_dict(label_map, use_display_name=True) #run detector on test image #it takes a little longer on the first run and then runs at normal speed. print("[INFO]: Loaded labels...") print() #input video for object detection inference if not isinstance(input_path, int): vid = WebcamVideoStream(src=0).start() # run another while function else: vid = FileVideoStream( input_path).start() # run another while in a function time.sleep(1.0) #output video name if output_path != None: fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') videoOut = cv2.VideoWriter(output_path, fourcc, 30.0, (im.shape[1], im.shape[0])) print("[INFO] loading model...") print("[INFO] starting video play...") fps = FPS().start() while True: frame = vid.read() frame = imutils.resize(frame, width=450) (im_width, im_height) = (frame.shape[1], frame.shape[0]) image_np = np.array(frame).reshape( (im_height, im_width, 3)).astype(np.uint8) input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32) detections, predictions_dict, shapes = detect_fn(input_tensor) label_id_offset = 1 image_np_with_detections = image_np.copy() viz_utils.visualize_boxes_and_labels_on_image_array( image_np_with_detections, detections['detection_boxes'][0].numpy(), (detections['detection_classes'][0].numpy() + label_id_offset).astype(int), detections['detection_scores'][0].numpy(), category_index, use_normalized_coordinates=True, max_boxes_to_draw=100, min_score_thresh=.5, agnostic_mode=False, ) cv2.imshow("frame", image_np_with_detections) if cv2.waitKey(1) & 0xFF == ord('q'): break if output_path != None: videoOut.write(image_np_with_detections) fps.update() fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows() vid.stop() if output_path != None: videoOut.release()
def load_label_map(path_to_label_map): label_map = label_map_util.load_labelmap(path_to_label_map) max_num_classes = label_map_util.get_max_label_map_index(label_map) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=max_num_classes, use_display_name=True) return label_map_util.create_category_index(categories)