Ejemplo 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.'

        # 计算总的种类
        self.num_classes = len(self.class_names) + 1

        # 载入模型,如果原来的模型里已经包括了模型结构则直接载入。
        # 否则先构建模型再载入
        self.model_rpn, self.model_classifier = frcnn.get_predict_model(
            self.config, self.num_classes)
        self.model_rpn.load_weights(self.model_path, by_name=True)
        self.model_classifier.load_weights(self.model_path,
                                           by_name=True,
                                           skip_mismatch=True)

        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))
Ejemplo n.º 2
0
    def load_rcnn_model(self):
        '''
        加载分开预测的载模型
        Returns
        -------

        '''
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith(
            '.h5'), 'Keras model or weights must be a .h5 file.'

        try:
            # 若是完整的模型,直接载入,不需网络结构,也不需要编译.
            model_rpn = load_model(model_path, compile=False)
            model_classifier = load_model(model_path, compile=False)
        except Exception as v:
            print(v)
            # 包含背景,所以要+1
            num_classes = len(self.class_names) + 1
            try:
                model_rpn, model_classifier = get_predict_model(
                    self.config, num_classes)
                model_rpn.load_weights(self.model_path, by_name=True)
                model_classifier.load_weights(self.model_path, by_name=True)
            except Exception as e:
                print('模型加载失败:', e)
                raise

        return model_rpn, model_classifier
    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.'

        #-------------------------------#
        #   Calculate the total number of classes
        #-------------------------------#
        self.num_classes = len(self.class_names) + 1

        #-------------------------------#
        #   Loading model and weight
        #-------------------------------#
        self.model_rpn, self.model_classifier = frcnn.get_predict_model(
            self.config, self.num_classes)
        self.model_rpn.load_weights(self.model_path, by_name=True)
        self.model_classifier.load_weights(self.model_path, by_name=True)

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

        # Set different colors for the frame
        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))
Ejemplo 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.'
     #-------------------------------#
     #   载入模型与权值
     #-------------------------------#
     self.model_rpn, self.model_classifier = frcnn.get_predict_model(self.num_classes, self.backbone)
     self.model_rpn.load_weights(self.model_path, by_name=True)
     self.model_classifier.load_weights(self.model_path, by_name=True)
     print('{} model, anchors, and classes loaded.'.format(model_path))