Esempio n. 1
0
    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
Esempio n. 2
0
    def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        #self.yolo_model = load_model(model_path, compile=False)
        num_classes = len(self.class_names)
        num_anchors = len(self.anchors)
          # default setting

        coco_yolo_model = load_model(model_path, compile=False)

        self.yolo_model = 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
        index = [0, 1, 2, 3, 4] + [self.coco_names.index(i) + 5 for i in self.class_names]
        w = [[item[..., index + [i + 85 for i in index] + [i + 170 for i in index]]
                for item in coco_yolo_model.layers[i].get_weights()] for i in range(-3, 0)]

        for i in range(len(coco_yolo_model.layers) - 3):
            self.yolo_model.layers[i].set_weights(coco_yolo_model.layers[i].get_weights())
        for i, j in enumerate(range(-3, 0)):
            self.yolo_model.layers[j].set_weights(w[i])
        #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))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        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
Esempio n. 3
0
    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.'

        # 读取 model路径 anchorbox,cococ类别 加载模型
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        is_tiny_version = num_anchors == 6  # 默认设置
        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))

        # 生成绘制边框的颜色
        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)  # 固定种子为一致的进行运行
        np.random.shuffle(self.colors)  # 将颜色转换为相邻类
        np.random.seed(None)  # 将种子重置为默认

        # 为滤波后的包围盒生成输出张量目标
        self.input_image_shape = K.placeholder(shape=(2, ))
        if gpu_num >= 2:
            self.yolo_model = multi_gpu_model(self.yolo_model, gpus=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
Esempio n. 4
0
    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.'

        num_anchors = len(self.anchors)  # anchors的数量
        num_classes = len(self.class_names)  # 类别数

        self.yolo_model = yolo_body(Input(shape=(416, 416, 3)), 3, num_classes)
        self.yolo_model.load_weights(model_path)  # 加载模型参数

        print('{} model, {} anchors, and {} classes loaded.'.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
Esempio n. 5
0
    def generate(self):
        model_path = os.path.expanduser(self.weight_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)

        self.yolo_model = yolo_body(Input(shape=(None, None, 3)), self.num_seen, num_anchors // 3)
        self.yolo_model.load_weights(self.weight_path, by_name=True)
        print('{} model, anchors and classes loaded.'.format(model_path))

        # Generate output tensor targets for filtered bounding boxes.
        attribute = np.load(self.attribute_path)
        self.input_image_shape = K.placeholder(shape=(2,))
        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors, self.num_seen,
                                           attribute, self.input_image_shape,
                                           score_threshold=self.score, iou_threshold=self.iou)
        return boxes, scores, classes
Esempio n. 6
0
def build_model():

    global yolo_anchors
    yolo_anchors = get_anchors()

    global yolo_classes
    yolo_classes = get_class()

    global yolo_score
    yolo_score = 0.3

    global yolo_iou
    yolo_iou = 0.45

    num_anchors = len(yolo_anchors)
    num_classes = len(yolo_classes)

    global graph
    graph = tf.get_default_graph()

    global sess
    sess = K.get_session()

    global model

    global input_image_shape
    global boxes
    global scores
    global classes

    model = yolo_body(Input(shape=(None, None, 3)), num_anchors // 3,
                      num_classes)
    model.load_weights(FLAGS.model_path)
    print('{} model, anchors, and classes loaded.'.format(FLAGS.model_path))

    input_image_shape = K.placeholder(shape=(2, ))

    boxes, scores, classes = yolo_eval(model.output,
                                       yolo_anchors,
                                       num_classes,
                                       input_image_shape,
                                       score_threshold=yolo_score,
                                       iou_threshold=yolo_iou)
Esempio n. 7
0
    def __init__(self, model_path, classes_path, anchors_path, score_threshold, iou_threshold, size):

        """
        :param model_path:
        :param classes_path:
        :param anchors_path:
        :param score_threshold:
        :param iou_threshold:
        :param size

        """
        self.model_path = model_path
        self.iou = iou_threshold
        self.score = score_threshold
        self.model_image_size = size

        self.class_names = _get_class(classes_path)
        self.anchors = _get_anchors(anchors_path)
        self.sess = K.get_session()

        # 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(self.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(self.model_path))

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        self.boxes, self.scores, self.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)
        print('Finished generating output tensor targets.')
 def generate(self):
     # 在Keras中,如果模型训练完成后只保存了权重,那么需要先构建网络,再加载权重文件
     num_anchors = len(self.anchor_ndarray)
     num_classes = len(self.category_list)
     self.yolo_model = yolo_body(Input(shape=(None, None, 3)),
                                 num_anchors // 3, num_classes)
     self.yolo_model.load_weights(self.weights_h5FilePath)
     # 给不同类别的物体准备不同颜色的方框
     category_quantity = len(self.category_list)
     self.color_list = get_colorList(category_quantity)
     # 目标检测的输出:方框box,得分score,类别class
     self.input_image_size = K.placeholder(shape=(2, ))
     boxes, scores, classes = yolo_eval(self.yolo_model.output,
                                        self.anchor_ndarray,
                                        category_quantity,
                                        self.input_image_size,
                                        score_threshold=self.score,
                                        iou_threshold=self.iou)
     return boxes, scores, classes
def generate():
    global input_image_shape
    hsv_tuples = [(x / len(class_names), 1., 1.)
                  for x in range(len(class_names))]
    colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
    colors = list(
        map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
            colors))
    random.seed(10101)
    random.shuffle(colors)
    random.seed(None)
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_model.output,
                                       anchors,
                                       len(class_names),
                                       input_image_shape,
                                       score_threshold=score,
                                       iou_threshold=iou)
    return boxes, scores, classes
Esempio n. 10
0
    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.'

		#加载已经训练好模型或者构造模型并加载保存好的weight
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        is_tiny_version = num_anchors==6 
        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))

		#生成不同的颜色用来绘制不同box框
        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)  
        np.random.shuffle(self.colors)  
        np.random.seed(None)

        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)
		#yolo_eval方法根据score和iou的阈值进行非极大值抑制,筛选出符合条件的预测box
        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)
		#boxes,scores,classes都是按照类别划分的,boxes是每个类别对应最佳检测结果,scores记录每个类别符合条件的分数,classes记录每个类别的id
        return boxes, scores, classes
Esempio n. 11
0
    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 gpu_num>=2:
            self.yolo_model = multi_gpu_model(self.yolo_model, gpus=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
Esempio n. 12
0
    def generate(self):
        # 准备已经训练好的模型
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model or weights must b a .h5 file.'

        # 加载已经训练好的模型,如果加载失败,则创建模型,并加载训练好的weights
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        is_tiny_version = num_anchors==6
        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)
        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))

        # 为描绘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)
        np.random.shuffle(self.colors)
        np.random.seed(None)

        # 输入值的占位
        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)
        # 调用yolo_eval,计算出boxes,scores,classes
        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
Esempio n. 13
0
    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 == 12
        # print(num_anchors, is_tiny_version)

        self.yolo_model = yolo_body(Input(shape=(None, None, 3)),
                                    num_anchors // 4, num_classes)
        self.yolo_model.load_weights(
            self.model_path)  # make sure model, anchors and classes match

        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.
        # 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
Esempio n. 14
0
    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)
        self.yolo4_model.load_weights(model_path)

        print('{} model, anchors, and classes loaded.'.format(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)
Esempio n. 15
0
    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:
            print(self.yolo_model.layers[-1].output_shape[-1])
            print(num_anchors)
            print(len(self.yolo_model.output))
            print(num_classes)
            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
Esempio n. 16
0
 def generate(self):
     # 在Keras中,如果模型训练完成后只保存了权重,那么需要先构建网络,再加载权重
     num_anchors = len(self.anchor_ndarray)
     num_classes = len(self.className_list)
     self.yolo_model = yolo_body(Input(shape=(None, None, 3)),
                                 num_anchors // 3, num_classes)
     self.yolo_model.load_weights(self.modelFilePath)
     # 给不同类别的物体准备不同颜色的方框
     hsvTuple_list = [(x / len(self.className_list), 1., 1.)
                      for x in range(len(self.className_list))]
     color_list = [colorsys.hsv_to_rgb(*k) for k in hsvTuple_list]
     color_ndarray = (np.array(color_list) * 255).astype('int')
     self.color_list = [(k[0], k[1], k[2]) for k in color_ndarray]
     # 目标检测的输出:方框box,得分score,类别class
     self.input_image_size = K.placeholder(shape=(2, ))
     boxes, scores, classes = yolo_eval(self.yolo_model.output,
                                        self.anchor_ndarray,
                                        len(self.className_list),
                                        self.input_image_size,
                                        score_threshold=self.score,
                                        iou_threshold=self.iou)
     return boxes, scores, classes
    def generate(self):
        model_path = os.path.expanduser('model_data/yolov3.h5')
        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)

        try:
            # 加载模型
            self.yolo_model = load_model(model_path, compile=False)
        except:
            # 如果模型文件有问题,通过构建模型并加入参数的方式进行加载
            self.yolo_model = yolo_body(Input(shape=(None, None, 3)),
                                        num_anchors // 3, num_classes)
            self.yolo_model.load_weights(
                'model_data/yolov3.h5'
            )  # 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 gpu_num >= 2:
            self.yolo_model = multi_gpu_model(self.yolo_model, gpus=gpu_num)
        # 求出input_image中的boxes, scores和classes
        boxes, scores, classes = yolo_eval(self.yolo_model.output,
                                           self.anchors,
                                           len(self.class_names),
                                           self.input_image_shape,
                                           score_threshold=score_thres,
                                           iou_threshold=iou_thres)
        return boxes, scores, classes
    def generate(self, model_path, anchors, score):
        print(model_path)
        assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.'

        num_anchors = len(anchors)
        self.coco_classes = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus",
                   "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign",
                   "parking meter", "bench", "bird", "cat", "dog", "horse",
                   "sheep", "cow", "elephant", "bear", "zebra", "giraffe",
                   "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
                   "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove",
                   "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
                   "fork", "knife", "spoon", "bowl", "banana", "apple",
                   "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza",
                   "donut", "cake", "chair", "sofa", "pottedplant", "bed",
                   "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard",
                   "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator",
                   "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"]

        #num_classes = 1
        num_classes = len(self.coco_classes)

        model = None
        try:
            model = load_model(model_path, compile=False)
        except:
            model = yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes)
            model.load_weights(model_path)
        
        
        if self.gpu_num >=2:
            model = multi_gpu_model(model, gpus=self.gpu_num)
        boxes, scores, classes = yolo_eval(model.output, anchors,
                num_classes, self.input_image_shape,
                score_threshold=score, iou_threshold=self.iou)

        return {'model':model, 'boxes':boxes, 'scores': scores, 'classes': classes}
Esempio n. 19
0
    def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        if self.weights_only:
            image_input = Input(shape=(None, None, 3))
            num_anchors = len(self.anchors) // 3
            num_classes = len(self.class_names)
            self.yolo_model = yolo_body(image_input, num_anchors, num_classes)
            self.yolo_model.load_weights(self.model_path,
                                         by_name=True,
                                         skip_mismatch=True)
        else:
            self.yolo_model = load_model(self.model_path, compile=False)
        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))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(
            self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        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
Esempio n. 20
0
    def generate(self):
        self.set_class_size()
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        self.yolo_model = load_model(model_path, compile=False)
        self.distance_model = load_model(self.distance_model_path, compile=False)

        hsv_tuples = [(float(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))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        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
Esempio n. 21
0
    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.'

        num_anchors = len(self.anchors)  # anchors的数量
        num_classes = len(self.class_names)  # 类别数

        # 加载模型参数
        self.yolo_model = yolo_body(Input(shape=(None, None, 3)), 3,
                                    num_classes)
        self.yolo_model.load_weights(model_path)

        print('{} model, {} anchors, and {} classes loaded.'.format(
            model_path, num_anchors, num_classes))

        # 不同的框,不同的颜色
        hsv_tuples = [(float(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))  # RGB
        np.random.seed(10101)
        np.random.shuffle(self.colors)
        np.random.seed(None)

        # 根据检测参数,过滤框
        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
Esempio n. 22
0
def load(model_path, anchors_path, class_path):
    """Load the trained model.

    Args:
      model_path: absolute path to the model.
      anchors_path: the absolute path of the file which contains all the anchors
                    used for the network.
      class_path:  the absolute path of the file which contains the name of categories.

    Returns:
      The loaded yolo/tiny-yolo model.
    """
    class_names = get_classes(class_path)
    anchors = get_anchors(anchors_path)
    model_path = os.path.expanduser(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(anchors)
    num_classes = len(class_names)
    is_tiny_version = num_anchors==6 # default setting
    try:
        yolo_model = load_model(model_path, compile=False)
    except:
        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)
        yolo_model.load_weights(model_path) # make sure model, anchors and classes match
    else:
        assert yolo_model.layers[-1].output_shape[-1] == \
            num_anchors/len(yolo_model.output) * (num_classes + 5), \
            'Mismatch between model and given anchor and class sizes'
    input_image_shape = tf.constant([416, 416], shape=(2,))
    boxes, scores, classes = yolo_eval(yolo_model.output, anchors, len(class_names), input_image_shape,
                                       score_threshold=0.3, iou_threshold=0.45)
    print('{} model, anchors, and classes loaded.'.format(model_path))
    return yolo_model, is_tiny_version
Esempio n. 23
0
    def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        self.yolo_model = load_model(model_path, compile=False)
        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))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        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
Esempio n. 24
0
    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:
            from yolo3.model import yolo_head
            print("Loading json model")

            with open('logs/002/model_in_json.json', 'r') as f:
                model_json = json.load(f)

            model = model_from_json(model_json,
                                    custom_objects={"yolo_head": yolo_head})
            model.load_weights('logs/002/trained_weights_final.h5')
        except:
            # This is not working:
            # self.yolo_model = load_model(model_path, compile=False)
            # We create a new body:
            print("Is tiny model: {} ".format(is_tiny_version))
            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 {}  -> {}'.format(
                        self.yolo_model.layers[-1].output_shape[-1], num_anchors, num_classes,
                num_anchors/len(self.yolo_model.output) * (num_classes + 5))

        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
Esempio n. 25
0
    def calculate_aps(self):
        test_dataset_builder = Dataset(self.glob_path,
                                       self.batch_size,
                                       input_shapes=self.input_shape,
                                       mode=DATASET_MODE.TEST)
        bind(test_dataset_builder, self.parse_tfrecord)
        bind(test_dataset_builder, self.parse_text)
        test_dataset, test_num = test_dataset_builder.build()
        true_res = {}
        pred_res = []
        idx = 0
        APs = {}
        start = timer()
        for image, bbox in test_dataset:
            if self.input_shape != (None, None):
                boxed_image, resized_image_shape = letterbox_image(
                    image, tuple(self.input_shape))
            else:
                _, height, width, _ = tf.shape(image)
                new_image_size = (height - (height % 32), width - (width % 32))
                boxed_image, resized_image_shape = letterbox_image(
                    image, new_image_size)
            output = self.model.predict(boxed_image.numpy())
            out_boxes, out_scores, out_classes = yolo_eval(
                output,
                self.anchors,
                self.num_classes,
                image.shape[1:3],
                score_threshold=self.score,
                iou_threshold=self.nms)
            if len(out_classes) > 0:
                for i in range(len(out_classes)):
                    top, left, bottom, right = out_boxes[i]
                    pred_res.append([
                        idx, out_classes[i].numpy(), out_scores[i].numpy(),
                        left, top, right, bottom
                    ])
            true_res[idx] = tf.transpose(bbox[0]).numpy()
            idx += 1
        end = timer()
        print((end - start) / test_num)
        for cls in range(self.num_classes):
            pred_res_cls = [x for x in pred_res if x[1] == cls]
            if len(pred_res_cls) == 0:
                continue
            true_res_cls = {}
            npos = 0
            for index in true_res:
                objs = [obj for obj in true_res[index] if obj[4] == cls]
                npos += len(objs)
                BBGT = np.array([x[:4] for x in objs])
                true_res_cls[index] = {
                    'bbox': BBGT,
                    'difficult': [False] * len(objs),
                    'det': [False] * len(objs)
                }
            ids = [x[0] for x in pred_res_cls]
            scores = np.array([x[2] for x in pred_res_cls])
            bboxs = np.array([x[3:] for x in pred_res_cls])
            sorted_ind = np.argsort(-scores)
            bboxs = bboxs[sorted_ind, :]
            ids = [ids[x] for x in sorted_ind]

            nd = len(ids)
            tp = np.zeros(nd)
            fp = np.zeros(nd)
            for j in range(nd):
                res = true_res_cls[ids[j]]
                bbox = bboxs[j, :].astype(float)
                ovmax = -np.inf
                BBGT = res['bbox'].astype(float)
                if BBGT.size > 0:
                    ixmin = np.maximum(BBGT[:, 0], bbox[0])
                    iymin = np.maximum(BBGT[:, 1], bbox[1])
                    ixmax = np.minimum(BBGT[:, 2], bbox[2])
                    iymax = np.minimum(BBGT[:, 3], bbox[3])
                    iw = np.maximum(ixmax - ixmin + 1., 0.)
                    ih = np.maximum(iymax - iymin + 1., 0.)
                    inters = iw * ih

                    # union
                    uni = ((bbox[2] - bbox[0] + 1.) *
                           (bbox[3] - bbox[1] + 1.) +
                           (BBGT[:, 2] - BBGT[:, 0] + 1.) *
                           (BBGT[:, 3] - BBGT[:, 1] + 1.) - inters)

                    overlaps = inters / uni
                    ovmax = np.max(overlaps)
                    jmax = np.argmax(overlaps)
                if ovmax > self.iou:
                    if not res['difficult'][jmax]:
                        if not res['det'][jmax]:
                            tp[j] = 1.
                            res['det'][jmax] = 1
                        else:
                            fp[j] = 1.
                else:
                    fp[j] = 1.

            fp = np.cumsum(fp)
            tp = np.cumsum(tp)
            rec = tp / np.maximum(float(npos), np.finfo(np.float64).eps)
            prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps)
            ap = self._voc_ap(rec, prec)
            APs[cls] = ap
        return APs
Esempio n. 26
0
    def generate(self):
        if self.model_path:
            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 = True if self.model_name in [
            'tiny_yolo', 'tiny_yolo_infusion'
        ] else False
        if self.model_name == 'tiny_yolo_infusion':
            print('Loading model weights', self.model_path)
            #old style
            # self.yolo_model = tiny_yolo_infusion_body(Input(shape=(None,None,3)), num_anchors//2, num_classes)
            ## self.yolo_model.load_weights(self.model_path, by_name=True)
            #new style
            yolo_model, connection_layer = tiny_yolo_infusion_body(
                Input(shape=(None, None, 3)),
                num_anchors // self.num_yolo_heads, num_classes)
            seg_output = infusion_layer(connection_layer)
            self.yolo_model = Model(inputs=yolo_model.input,
                                    outputs=[*yolo_model.output, seg_output])
            # self.yolo_model.load_weights(self.model_path, by_name=True)
        elif self.model_name == 'tiny_yolo_infusion_hydra':
            #old style
            self.yolo_model = tiny_yolo_infusion_hydra_body(
                Input(shape=(None, None, 3)),
                num_anchors // self.num_yolo_heads, num_classes)
            # self.yolo_model.load_weights(self.model_path, by_name=True)
            #new style
            #not implemented yet
        elif self.model_name == 'yolo_infusion':
            print('Loading model weights', self.model_path)
            yolo_model, seg_output = yolo_infusion_body(
                Input(shape=(None, None, 3)),
                num_anchors // self.num_yolo_heads, num_classes)
            self.yolo_model = Model(inputs=yolo_model.input,
                                    outputs=[*yolo_model.output, seg_output])
            # self.yolo_model.load_weights(self.model_path, by_name=True)
        else:
            if self.model_name == 'yolo_small_objs':
                self.yolo_model = yolo_body_for_small_objs(
                    Input(shape=(None, None, 3)),
                    num_anchors // self.num_yolo_heads, num_classes)
            elif self.model_name == 'tiny_yolo_small_objs':
                self.yolo_model = tiny_yolo_small_objs_body(
                    Input(shape=(None, None, 3)),
                    num_anchors // self.num_yolo_heads, num_classes)
            else:
                try:
                    self.yolo_model = load_model(model_path, compile=False)
                except:
                    self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//self.num_yolo_heads, num_classes) \
                        if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//self.num_yolo_heads, 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'
        if self.model_path:
            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 gpu_num >= 2:
            self.yolo_model = multi_gpu_model(self.yolo_model, gpus=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,
                                           model_name=self.model_name)
        return boxes, scores, classes
Esempio n. 27
0
    def generate(self):
        '''构造测试模型
        内置输入参数
            model.input: image_data,修正尺寸后的图像数据
            input_image_shape: [image.size[1],image.size[0]],
                    原始图像尺寸;由yolo_val输入张量(2,),经session.run(feed_dict={self.input_image_shape:...})填充输入值 
            K.learning_phase():0,测试非训练标记
            anchors:锚点数组,[9 x 2],第一列为宽度,第二列为高度
            class_names:检测类别list[80 x 1]
            score_threshold:Bounding Box预测得分阈值
            iou_threshold:IOU阈值
        
        @return boxes, scores, classes
            boxes.shape  =>(?,4)  ,修正后的真实原始图像边框值,(ymin,xmin,ymax,xmax)
            scores.shape =>(?, )
            classes.shape=>(?, )
        '''
        print('============generate=============')
        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) #加载模型
            #self.yolo_model.summary(line_length=250)               #打印模型参数
            self.yolo_model.summary()                               #打印模型参数
            #输出日志
            summary_writer = tf.summary.FileWriter(self.log_path, tf.get_default_graph())
            summary_writer.close()
        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:
            #模型参数与anchor,class不匹配
            print('self.yolo_model.layers[-1].output_shape[-1]:',self.yolo_model.layers[-1].output_shape[-1])
            print('num_anchors/len(self.yolo_model.output) * (num_classes + 5):',num_anchors/len(self.yolo_model.output) * (num_classes + 5))
            print('num_anchors:',num_anchors)
            print('len(self.yolo_model.output):',len(self.yolo_model.output))
            print('num_classes:',num_classes)
            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)
        print('self.yolo.model.output.shape:')
        #此范例:anchors=9,num_classes=80,yolo_model.output为[y1,y2,y3],平均每个分配3个anchor => 9/3*(80+5)=255)
        print([x.shape for x in self.yolo_model.output])       #[(none,none,none,255)
                                                               #,(none,none,none,255)
                                                               #,(none,none,none,255)]
        print('self.anchors:',self.anchors)                    #(9,2)
        print('self.class_names:',self.class_names)            #(80,1)
        print('self.input_image_shape:',self.input_image_shape)#(2,1)
        print('self.score:',self.score)
        print('self.iou:',self.iou)

        #==================yolo评估====================
        boxes, scores, classes = yolo_eval(
                    self.yolo_model.output       #模型输出:[y1,y2,y3]
                    , self.anchors               #锚点数组:[9 x 2]
                    , len(self.class_names)      #分类数目:80
                    , self.input_image_shape     #原始图像大小:张量,placeholder,模型输入
                    , score_threshold=self.score #得分阈值
                    , iou_threshold=self.iou     #交并比阈值
                    )
        print('boxes.shape:',boxes.shape)                      #=>boxes.shape: (?, 4),回归框:[x1,y1,x2,y2]
        print('scores.shape:',scores.shape)                    #=>scores.shape: (?,) ,得分:[0-1]
        print('classes.shape:',classes.shape)                  #=>classes.shape: (?,),预测类别
        return boxes, scores, classes
Esempio n. 28
0
    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)
            if self.print_summary:
                self.yolo_model.summary(line_length=150)
        except FileNotFoundError:
            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
            if self.print_summary:
                self.yolo_model.summary(line_length=150)
        else:
            from_model_layers = self.yolo_model.layers[-1].output_shape[-1]
            from_computed_number = num_anchors / len(
                self.yolo_model.output) * (num_classes + 5)
            assert from_model_layers == from_computed_number, \
                'Mismatch between model and given anchor and class sizes'
        from_model_layers = len(self.yolo_model.output)
        print(
            '{} model with {} output layers, {} anchors, and {} classes loaded.'
            .format(model_path, from_model_layers, num_anchors, num_classes))

        # 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_outputs, features = yolo_eval(
            self.yolo_model.output,
            self.anchors,
            len(self.class_names),
            self.input_image_shape,
            score_threshold=self.score,
            iou_threshold=self.iou)
        if self.save_pb:
            # Freeze and save model to tf_model .pb file
            frozen_graph = freeze_session(
                K.get_session(),
                output_names=[out.op.name for out in self.yolo_model.outputs])
            tf.train.write_graph(frozen_graph,
                                 "pb_models",
                                 "{}.pb".format(self.model_name),
                                 as_text=False)

        return boxes, scores, classes, yolo_outputs, features
Esempio n. 29
0
    def detect_image(self, image) -> Image:
        if tf.executing_eagerly():
            if self.model_config[self.backbone]['input_size'] != (None, None):
                assert self.model_config[self.backbone]['input_size'][
                    0] % 32 == 0, 'Multiples of 32 required'
                assert self.model_config[self.backbone]['input_size'][
                    1] % 32 == 0, 'Multiples of 32 required'
                boxed_image, image_shape = letterbox_image(
                    image,
                    tuple(
                        reversed(
                            self.model_config[self.backbone]['input_size'])))
            else:
                height, width, _ = image.shape
                new_image_size = (width - (width % 32), height - (height % 32))
                boxed_image, image_shape = letterbox_image(
                    image, new_image_size)
            image_data = np.expand_dims(boxed_image, 0)
            start = timer()
            output = self.yolo_model.predict(image_data)
            out_boxes, out_scores, out_classes = yolo_eval(
                output,
                self.anchors,
                len(self.class_names),
                image_shape[0:2],
                score_threshold=self.score,
                iou_threshold=self.iou)
            end = timer()
            image = Image.fromarray((np.array(image) * 255).astype('uint8'),
                                    'RGB')
        else:
            size = self.model_config[self.backbone]['input_size']
            iw, ih = image.size
            w, h = size
            scale = min(w / iw, h / ih)
            nw = int(iw * scale)
            nh = int(ih * scale)

            resized_image = image.resize((nw, nh), Image.BILINEAR)
            new_image = Image.new('RGB', size, (128, 128, 128))
            new_image.paste(resized_image, ((w - nw) // 2, (h - nh) // 2))
            image_data = np.array(new_image, dtype='float32')
            image_data /= 255.
            image_data = np.expand_dims(image_data, 0)
            start = timer()
            out_boxes, out_scores, out_classes = self.sess.run(
                [self.boxes, self.scores, self.classes],
                feed_dict={
                    "predict_image:0": image_data,
                    "image_size:0": tuple(reversed(resized_image.size))
                })
            end = timer()

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[1] + image.size[0]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box * (tuple(reversed(image.size)) * 2)
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        print(end - start)
        return image
Esempio n. 30
0
    def generate(self):
        model_path = os.path.expanduser(
            self.model_config[self.backbone]['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)
        try:
            self.yolo_model = tf.keras.models.load_model(model_path,
                                                         compile=False)
        except:
            if self.backbone == BACKBONE.MOBILENETV2:
                self.yolo_model = mobilenetv2_yolo_body(
                    tf.keras.layers.Input(
                        shape=(*self.model_config[self.backbone]['input_size'],
                               3),
                        name='predict_image'), num_anchors // 3, num_classes,
                    self.alpha)
            elif self.backbone == BACKBONE.DARKNET53:
                self.yolo_model = darknet_yolo_body(
                    tf.keras.layers.Input(shape=(None, None, 3)),
                    num_anchors // 3, num_classes)
            elif self.backbone == BACKBONE.DENSENET:
                self.yolo_model = densenet_yolo_body(
                    tf.keras.layers.Input(shape=(None, None, 3)),
                    num_anchors // 3, num_classes)
            elif self.backbone == BACKBONE.INCEPTION_RESNET2:
                self.yolo_model = inception_yolo_body(
                    tf.keras.layers.Input(shape=(None, None, 3)),
                    num_anchors // 3, num_classes)
            self.yolo_model.load_weights(
                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: List[Tuple[float, float, float]] = [
            (x / len(self.class_names), 1., 1.)
            for x in range(len(self.class_names))
        ]
        self.colors: List[Tuple[float, float, float]] = list(
            map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors: List[Tuple[int, int, int]] = 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.
        if gpu_num >= 2:
            self.yolo_model = tf.keras.utils.multi_gpu_model(self.yolo_model,
                                                             gpus=gpu_num)
        if tf.executing_eagerly() is not True:
            self.input_image_shape = tf.placeholder(tf.float32,
                                                    shape=(2, ),
                                                    name="image_size")
            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
Esempio n. 31
0
    def __init__(self):
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        # self.video = cv2.VideoCapture(0)

        os.environ['CUDA_VISIBLE_DEVICES'] = '0'
        gpu_num = 1

        # model_path = '../logs/human_pose_dataset_1400_416_yolo/trained_weights_final.h5'
        model_path = '../logs/human_pose_dataset_2388_size_416_batch_size_4/human_pose_dataset_2388_size_416_batch_size_4.h5'
        anchors_path = '../model_data/yolo_anchors.txt'
        classes_path = '../model_data/human_pose.txt'
        score = 0.2
        iou = 0.2
        model_image_size = (416, 416)
        self.sess = K.get_session()

        # Get class
        classes_path = os.path.expanduser(classes_path)
        with open(classes_path) as f:
            class_names = f.readlines()

        self.class_names = [c.strip() for c in class_names]

        # Anchors
        anchors_path = os.path.expanduser(anchors_path)
        with open(anchors_path) as f:
            anchors = f.readline()
        anchors = [float(x) for x in anchors.split(',')]
        anchors = np.array(anchors).reshape(-1, 2)

        # Load model
        model_path = os.path.expanduser(model_path)
        assert model_path.endswith('.h5'), 'Keras model end with file .h5'

        num_anchors = len(anchors)
        num_classes = len(self.class_names)

        is_tiny_version = num_anchors == 6
        try:
            yolo_model = load_model(model_path, compile=False)
        except:
            if is_tiny_version:
                yolo_model = tiny_yolo_body(Input(shape=(None, None, 3)),
                                            num_anchors // 2, num_classes)
            else:
                self.yolo_model = yolo_body(Input(shape=(None, None, 3)),
                                            num_anchors // 3, num_classes)

            self.yolo_model.load_weights(model_path)
        else:
            self.yolo_model.layers[-1].output_shape[-1] == num_anchors / len(
                yolo_model.output) * (
                    num_classes + 5
                ), 'Mismatch between model and given anchor and class sizes'

        print("{} model, anchors, and classes loaded.".format(model_path))

        face_encodings_in_room = []
        face_names_in_room = []
        known_face_encodings_array = np.load(
            "../data/numpy/known_face_encoding.npy")
        known_face_names = np.load("../data/numpy/known_face_names.npy")

        # Convert nparray -> List to face_encoding
        len_of_array_known_face_names = len(known_face_names)
        known_face_encodings_array = known_face_encodings_array.reshape(
            len_of_array_known_face_names, 128)
        known_face_encodings = []
        for i in range(len_of_array_known_face_names):
            known_face_encodings.append(known_face_encodings_array[i])

        self.input_image_shape = K.placeholder(shape=(2, ))
        self.boxes, self.scores, self.classes = yolo_eval(
            self.yolo_model.output,
            anchors,
            len(self.class_names),
            self.input_image_shape,
            score_threshold=score,
            iou_threshold=iou)
        num_frame = 0
        self.font = cv2.FONT_HERSHEY_DUPLEX
        self.video = WebcamVideoStream(src=0).start()
Esempio n. 32
0
    def detect_image(self, image, draw=True) -> Image:
        if tf.executing_eagerly():
            image_data = tf.expand_dims(image, 0)
            if self.input_shape != (None, None):
                boxed_image, image_shape = letterbox_image(
                    image_data, tuple(reversed(self.input_shape)))
            else:
                height, width, _ = image_data.shape
                new_image_size = (width - (width % 32), height - (height % 32))
                boxed_image, image_shape = letterbox_image(
                    image_data, new_image_size)
            image_data = np.array(boxed_image)
            start = timer()
            output = self.yolo_model.predict(image_data)
            out_boxes, out_scores, out_classes = yolo_eval(
                output,
                self.anchors,
                len(self.class_names),
                image.shape[0:2],
                score_threshold=self.score,
                iou_threshold=self.nms)
            end = timer()
            image = Image.fromarray((np.array(image) * 255).astype('uint8'),
                                    'RGB')
        else:
            image_data = np.expand_dims(image, 0)
            start = timer()
            out_boxes, out_scores, out_classes = self.sess.run(
                self.yolo_model.output, feed_dict={self.input: image_data})
            end = timer()

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
        if draw:
            font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                      size=np.floor(3e-2 * image.size[1] +
                                                    0.5).astype('int32'))
            thickness = (image.size[1] + image.size[0]) // 300
            draw = ImageDraw.Draw(image)
            for i, c in reversed(list(enumerate(out_classes))):
                predicted_class = self.class_names[c]
                box = out_boxes[i]
                score = out_scores[i]

                label = '{} {:.2f}'.format(predicted_class, score)

                label_size = draw.textsize(label, font)

                top, left, bottom, right = box
                print(label, (left, top), (right, bottom))

                if top - label_size[1] >= 0:
                    text_origin = np.array([left, top - label_size[1]])
                else:
                    text_origin = np.array([left, top + 1])

                # My kingdom for a good redistributable image drawing library.
                for i in range(thickness):
                    draw.rectangle([left + i, top + i, right - i, bottom - i],
                                   outline=self.colors[c])
                draw.rectangle(
                    [tuple(text_origin),
                     tuple(text_origin + label_size)],
                    fill=self.colors[c])
                draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw
            print(end - start)
            return image
        else:
            return out_boxes, out_scores, out_classes