def generate(self): model_path = os.path.expanduser(self.model_path) assert model_path.endswith( '.h5'), 'Keras model or weights must be a .h5 file.' # Load model, or construct model and load weights. num_anchors = len(self.anchors) num_classes = len(self.class_names) my_input = Input(shape=[], dtype=tf.string) self.base_input = my_input my_input = Lambda(get_inputs, output_shape=(416, 416, 3))(my_input) my_input = Input(tensor=my_input) self.yolo_model = yolo_body(my_input, num_anchors // 3, num_classes) self.yolo_model.load_weights( self.model_path) # make sure model, anchors and classes match print( 'Detection model, {} model, {} anchors, and {} classes load success!.' .format(model_path, num_anchors, num_classes)) self.input_image_shape = K.placeholder(shape=(2, )) boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors, len(self.class_names), self.input_image_shape, score_threshold=self.score, iou_threshold=self.iou) return boxes, scores, classes
def generate(self): model_path = os.path.expanduser(self.model_path) assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.' # Load model, or construct model and load weights. num_anchors = len(self.anchors) num_classes = len(self.class_names) is_tiny_version = num_anchors==6 # default setting try: self.yolo_model = load_model(model_path, compile=False) except: self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) \ if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes) self.yolo_model.load_weights(self.model_path) # make sure model, anchors and classes match else: assert self.yolo_model.layers[-1].output_shape[-1] == \ num_anchors/len(self.yolo_model.output) * (num_classes + 5), \ 'Mismatch between model and given anchor and class sizes' print('{} model, anchors, and classes loaded.'.format(model_path)) # Generate output tensor targets for filtered bounding boxes. self.input_image_shape = K.placeholder(shape=(2, )) if self.gpu_num>=2: self.yolo_model = multi_gpu_model(self.yolo_model, gpus=self.gpu_num) boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors, len(self.class_names), self.input_image_shape, score_threshold=self.score, iou_threshold=self.iou) return boxes, scores, classes
def predict(data, image_shape, model, model_param, score_threshold = .6, iou_threshold = .5, max_boxes = 20): """ yolo_eval: Evaluate YOLO model on given input and return filtered boxes. """ class_names, num_classes, anchors, input_shape = model_param # data shape = [(1, 13, 13, 75), (1, 26, 26, 75), (1, 52, 52, 75)] grid1, grid2, grid3 = np.array(data[0]), np.array(data[1]), np.array(data[2]) print(grid1.shape, grid2.shape, grid3.shape) # print(grid1[0, 7, 5, :25]) # predict boxes, scores, classes = yolo_eval(data, anchors, num_classes, image_shape, score_threshold=score_threshold, iou_threshold=iou_threshold) result = [boxes, scores, classes] return result
def generate(self): # Load model, or construct model and load weights. num_anchors = len(self.anchors) num_classes = len(self.class_names) is_tiny_version = num_anchors == 6 # default setting # Generate colors for drawing bounding boxes. hsv_tuples = [(x / len(self.class_names), 1., 1.) for x in range(len(self.class_names))] self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) self.colors = list( map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors)) np.random.seed(10101) # Fixed seed for consistent colors across runs. np.random.shuffle( self.colors) # Shuffle colors to decorrelate adjacent classes. np.random.seed(None) # Reset seed to default. # Generate output tensor targets for filtered bounding boxes. # self.input_image_shape = K.placeholder(shape=(2, )) self.input_image_shape = tf.placeholder(dtype=tf.float32, shape=(2, )) output = [ self.graph.get_tensor_by_name("conv2d_10/BiasAdd:0"), self.graph.get_tensor_by_name("conv2d_13/BiasAdd:0") ] boxes, scores, classes = yolo_eval(output, self.anchors, len(self.class_names), self.input_image_shape, score_threshold=self.score, iou_threshold=self.iou) return boxes, scores, classes
def compute_output(cls, image_data, image_shape): input_image_shape = tf.constant(image_shape) class_names = cls._get_class() anchors = cls._get_anchors() iou = 0.4 #Adjust param score = 0.7 #Adjust param boxes, scores, classes = yolo_eval(cls.yolo_model(image_data), anchors, len(class_names), input_image_shape, score_threshold=score, iou_threshold=iou) return boxes, scores, classes
def generate(self): # 加载模型 model_path = os.path.expanduser(self.model_path) assert model_path.endswith( '.h5'), 'Keras model or weights must be a .h5 file.' # Load model, or construct model and load weights. num_anchors = len(self.anchors) num_classes = len(self.class_names) is_tiny_version = num_anchors == 6 # default setting try: self.yolo_model = load_model(model_path, compile=False) except: self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) \ if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes) self.yolo_model.load_weights( self.model_path) # make sure model, anchors and classes match else: assert self.yolo_model.layers[-1].output_shape[-1] == \ num_anchors/len(self.yolo_model.output) * (num_classes + 5), \ 'Mismatch between model and given anchor and class sizes' print('{} model, anchors, and classes loaded.'.format(model_path)) # Generate colors for drawing bounding boxes. hsv_tuples = [(x / len(self.class_names), 1., 1.) for x in range(len(self.class_names))] self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) self.colors = list( map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors)) np.random.seed(10101) # Fixed seed for consistent colors across runs. np.random.shuffle( self.colors) # Shuffle colors to decorrelate adjacent classes. np.random.seed(None) # Reset seed to default. # Generate output tensor targets for filtered bounding boxes. self.input_image_shape = K.placeholder(shape=(2, )) if self.gpu_num >= 2: self.yolo_model = multi_gpu_model(self.yolo_model, gpus=self.gpu_num) boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors, len(self.class_names), self.input_image_shape, score_threshold=self.score, iou_threshold=self.iou) return boxes, scores, classes
def __init__(self): # Input parameters anchors_path = 'model_data/tiny_yolo_anchors.txt' classes_path = 'model_data/coco_classes.txt' model_path = 'model_data/yolov3-tiny.h5' score_threshold = 0.3 iou_threshold = 0.45 self.SELECTED_CLASS = 9 # Start session self.sess = K.get_session() # Preprare model anchors = get_anchors(anchors_path) classes = get_class(classes_path) self.model= tiny_yolo_body(Input(shape=(None,None,3)), len(anchors)//2, len(classes)) self.model.load_weights(model_path) # Prepare placeholders self.input_image_shape = K.placeholder(shape=(2, )) self.ph_boxes, self.ph_scores, self.ph_classes = yolo_eval(self.model.output, anchors, len(classes), (416,416), score_threshold=score_threshold, iou_threshold=iou_threshold)
def __init__(self, score_threshold=0.3, iou_threshold=0.45): # Input parameters anchors_path = './data/tiny_yolo_anchors.txt' classes_path = './data/coco_classes.txt' model_path = './data/yolov3-tiny.h5' self.SELECTED_CLASS = 9 # Start session config = tf.ConfigProto(gpu_options=tf.GPUOptions( per_process_gpu_memory_fraction=0.5)) config.gpu_options.allow_growth = True self.graph = Graph() with self.graph.as_default(): self.session = tf.Session(config=config) with self.session.as_default(): self.sess = K.get_session() self.K_learning_phase = K.learning_phase() # Preprare model anchors = get_anchors(anchors_path) classes = get_class(classes_path) self.model = tiny_yolo_body(Input(shape=(None, None, 3)), len(anchors) // 2, len(classes)) self.model.load_weights(model_path) # Prepare placeholders self.input_image_shape = K.placeholder(shape=(2, )) self.ph_boxes, self.ph_scores, self.ph_classes = yolo_eval( self.model.output, anchors, len(classes), (416, 416), score_threshold=score_threshold, iou_threshold=iou_threshold)
def load_yolo(self): model_path = os.path.expanduser(self.model_path) assert model_path.endswith( '.h5'), 'Keras model or weights must be a .h5 file.' self.class_names = self.get_class() self.anchors = self.get_anchors() num_anchors = len(self.anchors) num_classes = len(self.class_names) # Generate colors for drawing bounding boxes. hsv_tuples = [(x / len(self.class_names), 1., 1.) for x in range(len(self.class_names))] self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) self.colors = list( map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors)) self.sess = K.get_session() # Load model, or construct model and load weights. self.yolo4_model = yolo4_body(Input(shape=(608, 608, 3)), num_anchors // 3, num_classes) # Read and convert darknet weight print('Loading weights.') weights_file = open(self.weights_path, 'rb') major, minor, revision = np.ndarray(shape=(3, ), dtype='int32', buffer=weights_file.read(12)) if (major * 10 + minor) >= 2 and major < 1000 and minor < 1000: seen = np.ndarray(shape=(1, ), dtype='int64', buffer=weights_file.read(8)) else: seen = np.ndarray(shape=(1, ), dtype='int32', buffer=weights_file.read(4)) print('Weights Header: ', major, minor, revision, seen) convs_to_load = [] bns_to_load = [] for i in range(len(self.yolo4_model.layers)): layer_name = self.yolo4_model.layers[i].name if layer_name.startswith('conv2d_'): convs_to_load.append((int(layer_name[7:]), i)) if layer_name.startswith('batch_normalization_'): bns_to_load.append((int(layer_name[20:]), i)) convs_sorted = sorted(convs_to_load, key=itemgetter(0)) bns_sorted = sorted(bns_to_load, key=itemgetter(0)) bn_index = 0 for i in range(len(convs_sorted)): print('Converting ', i) if i == 93 or i == 101 or i == 109: #no bn, with bias weights_shape = self.yolo4_model.layers[ convs_sorted[i][1]].get_weights()[0].shape bias_shape = self.yolo4_model.layers[ convs_sorted[i][1]].get_weights()[0].shape[3] filters = bias_shape size = weights_shape[0] darknet_w_shape = (filters, weights_shape[2], size, size) weights_size = np.product(weights_shape) conv_bias = np.ndarray(shape=(filters, ), dtype='float32', buffer=weights_file.read(filters * 4)) conv_weights = np.ndarray(shape=darknet_w_shape, dtype='float32', buffer=weights_file.read( weights_size * 4)) conv_weights = np.transpose(conv_weights, [2, 3, 1, 0]) self.yolo4_model.layers[convs_sorted[i][1]].set_weights( [conv_weights, conv_bias]) else: #with bn, no bias weights_shape = self.yolo4_model.layers[ convs_sorted[i][1]].get_weights()[0].shape size = weights_shape[0] bn_shape = self.yolo4_model.layers[bns_sorted[bn_index] [1]].get_weights()[0].shape filters = bn_shape[0] darknet_w_shape = (filters, weights_shape[2], size, size) weights_size = np.product(weights_shape) conv_bias = np.ndarray(shape=(filters, ), dtype='float32', buffer=weights_file.read(filters * 4)) bn_weights = np.ndarray(shape=(3, filters), dtype='float32', buffer=weights_file.read(filters * 12)) bn_weight_list = [ bn_weights[0], # scale gamma conv_bias, # shift beta bn_weights[1], # running mean bn_weights[2] # running var ] self.yolo4_model.layers[bns_sorted[bn_index][1]].set_weights( bn_weight_list) conv_weights = np.ndarray(shape=darknet_w_shape, dtype='float32', buffer=weights_file.read( weights_size * 4)) conv_weights = np.transpose(conv_weights, [2, 3, 1, 0]) self.yolo4_model.layers[convs_sorted[i][1]].set_weights( [conv_weights]) bn_index += 1 weights_file.close() self.yolo4_model.save(self.model_path) if self.gpu_num >= 2: self.yolo4_model = multi_gpu_model(self.yolo4_model, gpus=self.gpu_num) self.input_image_shape = K.placeholder(shape=(2, )) self.boxes, self.scores, self.classes = yolo_eval( self.yolo4_model.output, self.anchors, len(self.class_names), self.input_image_shape, score_threshold=self.score)