def __init__(self, net_model): labels_file = LABELS_DICT[net_model['Dataset'].lower()] label_map = label_map_util.load_labelmap( labels_file) # loads the labels map. categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=999999) category_index = label_map_util.create_category_index(categories) self.classes = {} # We build is as a dict because of gaps on the labels definitions for cat in category_index: self.classes[cat] = str(category_index[cat]['name']) # Frozen inference graph, written on the file CKPT = os.path.join('Net', 'Models', net_model['Model']) detection_graph = tf.compat.v1.Graph() # new graph instance. with detection_graph.as_default(): od_graph_def = tf.compat.v1.GraphDef() with tf.io.gfile.GFile(CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') # Set additional parameters for the TF session # Set params depending on the availability of a GPU if tf.test.is_gpu_available(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8) config = tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False) self.sess = tf.compat.v1.Session(graph=detection_graph, config=config) else: self.sess = tf.compat.v1.Session(graph=detection_graph) # Extract the placeholders self.image_tensor = detection_graph.get_tensor_by_name( 'image_tensor:0') # NCHW conversion. not possible #self.image_tensor = tf.transpose(self.image_tensor, [0, 3, 1, 2]) self.detection_boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') 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') self.boxes = [] self.scores = [] self.predictions = [] # Dummy initialization (otherwise it takes longer then) dummy_tensor = np.zeros((1, 1, 1, 3), dtype=np.int32) self.sess.run([ self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections ], feed_dict={self.image_tensor: dummy_tensor}) self.confidence_threshold = 0.5
def __init__(self, net_model): self.framework = "TensorFlow" labels_file = LABELS_DICT[net_model['Dataset'].lower()] label_map = label_map_util.load_labelmap( labels_file) # loads the labels map. categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=999999) category_index = label_map_util.create_category_index(categories) self.classes = {} # We build is as a dict because of gaps on the labels definitions for cat in category_index: self.classes[cat] = str(category_index[cat]['name']) # Frozen inference graph, written on the file CKPT = 'Net/TensorFlow/' + net_model['Model'] detection_graph = tf.Graph() # new graph instance. with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7) self.sess = tf.Session( graph=detection_graph, config=tf.ConfigProto(log_device_placement=False, #gpu_options=gpu_options)) )) self.image_tensor = detection_graph.get_tensor_by_name( 'image_tensor:0') # NCHW conversion. not possible #self.image_tensor = tf.transpose(self.image_tensor, [0, 3, 1, 2]) self.detection_boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') 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') self.boxes = [] self.scores = [] self.predictions = [] # Dummy initialization (otherwise it takes longer then) dummy_tensor = np.zeros((1, 1, 1, 3), dtype=np.int32) self.sess.run([ self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections ], feed_dict={self.image_tensor: dummy_tensor}) self.confidence_threshold = 0.5 print("Network ready!")
def __init__(self, net_model): SystemExit('Keras not supported yet (for now...)') self.framework = "Keras" # Parse the dataset to get which labels to yield labels_file = LABELS_DICT[net_model['Dataset'].lower()] label_map = label_map_util.load_labelmap( labels_file) # loads the labels map. categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=100000) category_index = label_map_util.create_category_index(categories) self.classes = {} # We build is as a dict because of gaps on the labels definitions for cat in category_index: self.classes[cat] = str(category_index[cat]['name']) MODEL_FILE = 'Net/Keras/' + net_model['Model'] file = h5py.File(MODEL_FILE, 'r') ssd_loss = SSDLoss(neg_pos_ratio=3, n_neg_min=0, alpha=1.0) K.clear_session() if str(file.items()[0][0]) == 'model_weights': print("Full model detected. Loading it...") try: self.model = load_model(MODEL_FILE, custom_objects={ 'AnchorBoxes': AnchorBoxes, 'L2Normalization': L2Normalization, 'DecodeDetections': DecodeDetections, 'compute_loss': ssd_loss.compute_loss }) except Exception as e: SystemExit(e) else: print( "Weights file detected. Creating a model and loading the weights into it..." ) print "Model file: ", MODEL_FILE self.model = create_model_from_weights.create_model( MODEL_FILE, ssd_loss, len(self.classes)) # the Keras network works on 300x300 images. Reference sizes: input_size = self.model.input.shape.as_list() self.img_height = input_size[1] self.img_width = input_size[2] # Output preallocation self.predictions = np.asarray([]) self.boxes = np.asarray([]) self.scores = np.asarray([]) dummy = np.zeros([1, self.img_height, self.img_width, 3]) self.model.predict(dummy) print("Network ready!")
def __init__(self, net_model): # attributes from dl-objecttracker architecture self.input_image = None self.output_image = None self.activated = True self.detection = None self.label = None self.colors = None self.frame = None # attributes set by yml config self.confidence_threshold = None # new necessary attributes from dl-objectdetector network architecture self.original_height = None self.original_width = None self.font = cv2.FONT_HERSHEY_SIMPLEX self.scale = 0.5 COLORS = label_map_util.COLORS self.framework = "Keras" self.net_has_masks = False self.log_network_results = [] self.fps_network_results = [] self.log_done = False self.logger_status = True self.image_scale = (None, None) # Parse the dataset to get which labels to yield labels_file = LABELS_DICT[net_model['Dataset'].lower()] label_map = label_map_util.load_labelmap( labels_file) # loads the labels map. categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=100000) category_index = label_map_util.create_category_index(categories) self.classes = {} # We build is as a dict because of gaps on the labels definitions for cat in category_index: self.classes[cat] = str(category_index[cat]['name']) # We create the color dictionary for the bounding boxes. self.colors = {} idx = 0 for _class in self.classes.values(): self.colors[_class] = COLORS[idx] idx = +1 MODEL_FILE = 'Net/Keras/' + net_model['Model'] file = h5py.File(MODEL_FILE, 'r') ssd_loss = SSDLoss(neg_pos_ratio=3, n_neg_min=0, alpha=1.0) K.clear_session() if str(file.items()[0][0]) == 'model_weights': print("Full model detected. Loading it...") try: self.model = load_model(MODEL_FILE, custom_objects={ 'AnchorBoxes': AnchorBoxes, 'L2Normalization': L2Normalization, 'DecodeDetections': DecodeDetections, 'compute_loss': ssd_loss.compute_loss }) except Exception as e: SystemExit(e) else: print( "Weights file detected. Creating a model and loading the weights into it..." ) print("Model file: ", MODEL_FILE) self.model = create_model_from_weights.create_model( MODEL_FILE, ssd_loss, len(self.classes)) # the Keras network works on 300x300 images. Reference sizes: input_size = self.model.input.shape.as_list() self.img_height = input_size[1] self.img_width = input_size[2] # Factors to rescale the output bounding boxes self.height_factor = np.true_divide(self.img_height, self.img_height) self.width_factor = np.true_divide(self.img_width, self.img_width) # Output preallocation self.predictions = np.asarray([]) self.boxes = np.asarray([]) self.scores = np.asarray([]) dummy = np.zeros([1, self.img_height, self.img_width, 3]) self.model.predict(dummy) print("Network ready!")
def __init__(self, net_model): # attributes from dl-objecttracker network architecture self.input_image = None self.output_image = None self.activated = True self.detection = None self.label = None self.colors = None self.frame = None # attributes set by yml config self.confidence_threshold = None # new necessary attributes from dl-objectdetector network architecture self.original_height = None self.original_width = None self.font = cv2.FONT_HERSHEY_SIMPLEX self.scale = 0.5 COLORS = label_map_util.COLORS self.framework = "TensorFlow" self.net_has_masks = False self.log_network_results = [] self.fps_network_results = [] self.log_done = False self.logger_status = True self.image_scale = (None, None) labels_file = LABELS_DICT[net_model['Dataset'].lower()] label_map = label_map_util.load_labelmap( labels_file) # loads the labels map. categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=999999) category_index = label_map_util.create_category_index(categories) self.classes = {} # We build is as a dict because of gaps on the labels definitions for cat in category_index: self.classes[cat] = str(category_index[cat]['name']) # We create the color dictionary for the bounding boxes. self.colors = {} idx = 0 for _class in self.classes.values(): self.colors[_class] = COLORS[idx] idx = +1 # Frozen inference graph, written on the file CKPT = 'Net/TensorFlow/' + net_model['Model'] detection_graph = tf.Graph() # new graph instance. with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') # Set additional parameters for the TF session gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8) config = tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False) self.sess = tf.Session(graph=detection_graph, config=config) self.image_tensor = detection_graph.get_tensor_by_name( 'image_tensor:0') self.detection_boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') 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') for op in detection_graph.get_operations(): if op.name == 'detection_masks': self.net_has_masks = True self.detection_masks = detection_graph.get_tensor_by_name( 'detection_masks:0') self.scores = [] self.predictions = [] # Dummy initialization (otherwise it takes longer then) dummy_tensor = np.zeros((1, 1, 1, 3), dtype=np.int32) if self.net_has_masks: self.sess.run([ self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections, self.detection_masks ], feed_dict={self.image_tensor: dummy_tensor}) else: self.sess.run([ self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections ], feed_dict={self.image_tensor: dummy_tensor}) print("Network ready!")