Exemple #1
0
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,
            weights_path='model_data/yolo_weights.h5'):
    '''create the training model'''
    K.clear_session() # get a new session
    image_input = Input(shape=(None, None, 3))
    h, w = input_shape
    num_anchors = len(anchors)

    y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
        num_anchors//3, num_classes+5)) for l in range(3)]

    model_body = yolo_body(image_input, num_anchors//3, num_classes)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))

    if load_pretrained:
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))
        if freeze_body in [1, 2]:
            # Freeze darknet53 body or freeze all but 3 output layers.
            num = (185, len(model_body.layers)-3)[freeze_body-1]
            for i in range(num): model_body.layers[i].trainable = False
            print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))

    model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
        arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
        [*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss)

    return model
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,
            weights_path='model_data/yolo_weights.h5'):
    '''create the training model'''
    K.clear_session() # get a new session
    image_input = Input(shape=(None, None, 3))
    h, w = input_shape
    num_anchors = len(anchors)

    y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
        num_anchors//3, num_classes+5)) for l in range(3)]

    model_body = yolo_body(image_input, num_anchors//3, num_classes)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))

    if load_pretrained:
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))
        if freeze_body in [1, 2]:
            # Freeze darknet53 body or freeze all but 3 output layers.
            num = (185, len(model_body.layers)-3)[freeze_body-1]
            for i in range(num): model_body.layers[i].trainable = False
            print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))

    # get output of second last layers and create bottleneck model of it
    out1=model_body.layers[246].output
    out2=model_body.layers[247].output
    out3=model_body.layers[248].output
    bottleneck_model = Model([model_body.input, *y_true], [out1, out2, out3])

    # create last layer model of last layers from yolo model
    in0 = Input(shape=bottleneck_model.output[0].shape[1:].as_list()) 
    in1 = Input(shape=bottleneck_model.output[1].shape[1:].as_list())
    in2 = Input(shape=bottleneck_model.output[2].shape[1:].as_list())
    last_out0=model_body.layers[249](in0)
    last_out1=model_body.layers[250](in1)
    last_out2=model_body.layers[251](in2)
    model_last=Model(inputs=[in0, in1, in2], outputs=[last_out0, last_out1, last_out2])
    model_loss_last =Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
        arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
        [*model_last.output, *y_true])
    last_layer_model = Model([in0,in1,in2, *y_true], model_loss_last)

    
    model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
        arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
        [*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss)

    return model, bottleneck_model, last_layer_model
Exemple #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.'

        # 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:
            print("about to load model from ", model_path)
            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. (Num of classes: {})'.format(model_path, 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_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
Exemple #4
0
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,
            weights_path='model_data/yolo_weights.h5'):
    '''create the training model,
    yolo3 模型的输入:
    input_shape:图片尺寸
    anchors:9个anchor box
    num_classes:类别数
    freeze_body:冻结模式,1是冻结DarkNet53的层,2是冻结全部,只保留最后3层
    weights_path:预训练模型的权重
    '''
    K.clear_session() # get a new session
    image_input = Input(shape=(None, None, 3)) # 图片输入格式
    h, w = input_shape # 尺寸
    num_anchors = len(anchors) # anchor数量

    # YOLO的三种尺度,每个尺度的anchor数,类别数+边框4个+置信度1
    y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
        num_anchors//3, num_classes+5)) for l in range(3)]

    model_body = yolo_body(image_input, num_anchors//3, num_classes)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))

    if load_pretrained: # 加载预训练模型
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True) # 加载参数,跳过错误
        print('Load weights {}.'.format(weights_path))
        if freeze_body in [1, 2]:
            '''选择冻结模式:模式1是冻结185层(DarkNet53网络的层数),模式2是保留最底部3层,其余全部冻结。
            整个模型共有252层;将所冻结的层,设置为不可训练,trainable=False;'''
            num = (185, len(model_body.layers)-3)[freeze_body-1]
            for i in range(num): model_body.layers[i].trainable = False
            print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))

    model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
        arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
        [*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss) # 模型
    # model = Model(inputs=[model_body.input] + y_true, outputs=model_loss)
    # plot_model(model, to_file=os.path.join('model_data', 'model.png'), show_shapes=True, show_layer_names=True)
    model.summary() # 打印网络

    return model
Exemple #5
0
def create_model(
        input_shape,
        anchor_ndarray,
        num_classes,
        load_pretrained=True,
        freeze_body=False,
        weights_h5FilePath='../resources/saved_models/trained_weights.h5'):
    K.clear_session()  # get a new session
    image_input = Input(shape=(None, None, 3))
    height, width = input_shape
    num_anchors = len(anchor_ndarray)
    y_true = [
        Input(shape=(height // k, width // k, num_anchors // 3,
                     num_classes + 5)) for k in [32, 16, 8]
    ]
    model_body = yolo_body(image_input, num_anchors // 3, num_classes)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(
        num_anchors, num_classes))

    if load_pretrained and os.path.exists(weights_h5FilePath):
        model_body.load_weights(weights_h5FilePath,
                                by_name=True,
                                skip_mismatch=True)
        print('Load weights from this path: {}.'.format(weights_h5FilePath))
        if freeze_body:
            num = len(model_body.layers) - 7
            for i in range(num):
                model_body.layers[i].trainable = False
            print('Freeze the first {} layers of total {} layers.'.format(
                num, len(model_body.layers)))

    model_loss = Lambda(yolo_loss,
                        output_shape=(1, ),
                        name='yolo_loss',
                        arguments={
                            'anchors': anchor_ndarray,
                            'num_classes': num_classes,
                            'ignore_thresh': 0.5
                        })([*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss)
    return model
Exemple #6
0
def create_model(input_shape,
                 anchors,
                 num_classes,
                 load_pretrained=True,
                 freeze_body=True,
                 weights_path='model_data/yolo_weights.h5'):
    K.clear_session()  # get a new session
    image_input = Input(shape=(None, None, 3))
    h, w = input_shape
    num_anchors = len(anchors)
    y_true = [Input(shape=(h // {0: 32, 1: 16, 2: 8}[l], w // {0: 32, 1: 16, 2: 8}[l], \
                           num_anchors // 3, num_classes + 5)) for l in range(3)]

    model_body = yolo_body(image_input, num_anchors // 3, num_classes)

    print('Create YOLOv3 model with {} anchors and {} classes.'.format(
        num_anchors, num_classes))

    if load_pretrained:
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))
        if freeze_body:
            # Do not freeze 3 output layers.
            #20181002 0100 修改 laster three layer are conv2D 因此从-7 改为-3
            num = len(model_body.layers) - 3
            print(num)
            for i in range(num):
                model_body.layers[i].trainable = False
            print('Freeze the first {} layers of total {} layers.'.format(
                num, len(model_body.layers)))

    model_loss = Lambda(yolo_loss,
                        output_shape=(1, ),
                        name='yolo_loss',
                        arguments={
                            'anchors': anchors,
                            'num_classes': num_classes,
                            'ignore_thresh': 0.5
                        })([*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss)
    return model
Exemple #7
0
def create_model(input_shape,
                 anchors,
                 num_classes,
                 load_pretrained=True,
                 freeze_body=2,
                 weights_path='model_data/yolo.h5'):
    '''create the training model'''
    K.clear_session()  # get a new session
    image_input = Input(shape=(None, None, 3))
    h, w = input_shape
    num_anchors = len(anchors)

    y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
        num_anchors//3, num_classes+5)) for l in range(3)]

    model_body = yolo_body(image_input, num_anchors // 3, num_classes)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(
        num_anchors, num_classes))

    if load_pretrained:
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))
        if freeze_body in [1, 2]:
            # Freeze darknet53 body or freeze all but 3 output layers.
            num = (185, len(model_body.layers) - 3)[freeze_body - 1]
            for i in range(num):
                model_body.layers[i].trainable = False
            print('Freeze the first {} layers of total {} layers.'.format(
                num, len(model_body.layers)))

    model_loss = Lambda(yolo_loss,
                        output_shape=(1, ),
                        name='yolo_loss',
                        arguments={
                            'anchors': anchors,
                            'num_classes': num_classes,
                            'ignore_thresh': 0.5
                        })([*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss)

    return model
Exemple #8
0
def YOLOv3Model(input_shape, h5_path, class_names, anchors):
    # Load model, or construct model and load weights.
    # anyway, it's important for the class
    num_anchors = len(anchors)
    num_classes = len(class_names)
    try:
        yolo_model = load_model(h5_path, compile=False)
    except:
        yolo_model = yolo_body(
            Input(shape=(input_shape[0], input_shape[1], 3)), num_anchors // 3,
            num_classes)
        yolo_model.load_weights(
            h5_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'

    # 至此,Keras-YOLOv3 模型加载完毕
    print('{} model, anchors, and classes loaded.'.format(h5_path))
    return yolo_model
Exemple #9
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
Exemple #10
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
Exemple #11
0
def create_model(input_shape,
                 anchors,
                 num_classes,
                 load_pretrained=True,
                 freeze_body=True):
    '''create the training model'''
    image_input = Input(shape=(None, None, 3))
    h, w = input_shape
    num_anchors = len(anchors)

    y_true = [Input(shape=(h // {0: 32, 1: 16, 2: 8}[l], w // {0: 32, 1: 16, 2: 8}[l], \
                           num_anchors // 3, num_classes + 5)) for l in range(3)]

    model_body = yolo_body(image_input, num_anchors // 3, num_classes)

    if load_pretrained:
        weights_path = os.path.join('model_data', 'yolo_weights.h5')
        if not os.path.exists(weights_path):
            print("CREATING WEIGHTS FILE" + weights_path)
            yolo_path = os.path.join('model_data', 'yolo.h5')
            orig_model = load_model(yolo_path, compile=False)
            orig_model.save_weights(weights_path)
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
        if freeze_body:
            # Do not freeze 3 output layers.
            print("Total layer: %d" % len(model_body.layers))
            for i in range(len(model_body.layers) - TRAINABLE_LAYER):
                model_body.layers[i].trainable = False

    model_loss = Lambda(yolo_loss,
                        output_shape=(1, ),
                        name='yolo_loss',
                        arguments={
                            'anchors': anchors,
                            'num_classes': num_classes,
                            'ignore_thresh': 0.5
                        })([*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss)

    return model
Exemple #12
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
Exemple #13
0
def create_model(input_shape,
                 anchors,
                 num_classes,
                 load_pretrained=True,
                 freeze_body=True):
    '''create the training model'''
    image_input = Input(shape=(None, None, 3))
    h, w = input_shape
    num_anchors = len(anchors) // 3
    y_true = [
        Input(shape=(h // 32, w // 32, num_anchors, num_classes + 5)),
        Input(shape=(h // 16, w // 16, num_anchors, num_classes + 5)),
        Input(shape=(h // 8, w // 8, num_anchors, num_classes + 5))
    ]

    model_body = yolo_body(image_input, num_anchors, num_classes)

    if load_pretrained:
        weights_path = os.path.join('model_data', 'yolo_weights.h5')
        if not os.path.exists(weights_path):
            print("CREATING WEIGHTS FILE" + weights_path)
            yolo_path = os.path.join('model_data', 'yolo.h5')
            orig_model = load_model(yolo_path, compile=False)
            orig_model.save_weights(weights_path)
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
        if freeze_body:
            # Do not freeze 3 output layers.
            for i in range(len(model_body.layers) - 3):
                model_body.layers[i].trainable = False

    model_loss = Lambda(yolo_loss,
                        output_shape=(1, ),
                        name='yolo_loss',
                        arguments={
                            'anchors': anchors,
                            'num_classes': num_classes
                        })([*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss)
    #    pdb.set_trace()
    return model_body, model
Exemple #14
0
def create_model(input_shape, anchors, num_classes, load_pretrained=True, weights_path='model_data/yolo_weights.h5'):
    '''create the training model'''
    K.clear_session()  # get a new session
    h, w = input_shape
    image_input = Input(shape=(h, w, 3))
    num_anchors = len(anchors)

    y_true = [Input(shape=(h // {0: 32, 1: 16, 2: 8}[l], w // {0: 32, 1: 16, 2: 8}[l],
                           num_anchors // 3, num_classes + 5)) for l in range(3)]

    model_body = yolo_body(image_input, num_anchors // 3, num_classes)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))

    if load_pretrained:
        yolo_weight = load_model(weights_path).get_weights()
        for i, w in enumerate(yolo_weight):
            if w.shape == (1, 1, 1024, 255):
                yolo_weight[i] = w[..., :(num_anchors // 3) * (num_classes + 5)]
            if w.shape == (1, 1, 512, 255):
                yolo_weight[i] = w[..., :(num_anchors // 3) * (num_classes + 5)]
            if w.shape == (1, 1, 256, 255):
                yolo_weight[i] = w[..., :(num_anchors // 3) * (num_classes + 5)]
            if w.shape == (255,):
                yolo_weight[i] = w[:(num_anchors // 3) * (num_classes + 5)]
        model_body.set_weights(yolo_weight)
        print('Load weights {}.'.format(weights_path))
        # freeze_body = 2
        # if freeze_body in [1, 2]:
        #     # Freeze the darknet body or freeze all but 2 output layers.
        #     num = (20, len(model_body.layers) - 2)[freeze_body - 1]
        #     for i in range(num):
        #         model_body.layers[i].trainable = False
        #     print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
    model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
                        arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
        [*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss)

    return model
    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))
Exemple #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
Exemple #17
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
Exemple #18
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)
        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:{} are 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
    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
Exemple #20
0
    def load_model(self):
        model_path = self._get_data_path(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'

        input_image_shape = keras.Input(shape=(2, ), name='image_shape')
        image_input = keras.Input((None, None, 3), dtype='float32')
        y1, y2, y3 = self.yolo_model(image_input)

        boxes, box_scores = \
            YOLOEvaluationLayer(anchors=self.anchors, num_classes=len(self.class_names))(
                inputs=[y1, y2, y3, input_image_shape])

        out_boxes, out_scores, out_indices = \
            YOLONMSLayer(anchors=self.anchors, num_classes=len(self.class_names))(
                inputs=[boxes, box_scores])
        self.final_model = keras.Model(
            inputs=[image_input, input_image_shape],
            outputs=[out_boxes, out_scores, out_indices])

        self.final_model.save('model_data/final_model.h5')
        print('{} model, anchors, and classes loaded.'.format(model_path))
def create_model(input_shape,
                 embedding_shape,
                 anchors,
                 num_seen,
                 load_pretrained=True,
                 weights_path='model_data/yolo_weights.h5'):
    """create the training model"""
    h, w = input_shape
    num_anchors = len(anchors)
    num_classes, _ = embedding_shape

    image_input = KL.Input(shape=input_shape + (3, ))
    y_true = [
        KL.Input(shape=(h // [32, 16, 8][l], w // [32, 16, 8][l],
                        num_anchors // 3, 5 + num_classes)) for l in range(3)
    ]
    y_embedding = KL.Input(shape=embedding_shape)

    model_body = yolo_body(image_input, num_anchors // 3)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(
        num_anchors, num_classes))

    if load_pretrained:
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))
        num = len(model_body.layers) - 3
        for i in range(num):
            model_body.layers[i].trainable = False
        print('Freeze the first {} layers of total {} layers.'.format(
            num, len(model_body.layers)))

    model_loss = KL.Lambda(
        lambda x: yolo_loss(
            x, anchors=anchors, num_seen=num_seen, ignore_thresh=0.5),
        name='yolo_loss')([*model_body.output[3:], *y_true, y_embedding])

    model = Model([model_body.input, *y_true, y_embedding], model_loss)
    return model_body, model
    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}
    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
Exemple #24
0
def create_model(config, num_classes, load_pretrained=True, freeze_body=2):
    '''create the training model'''
    K.clear_session()  # get a new session
    input_shape = config.input_shape
    image_input = Input(shape=(input_shape[0], input_shape[1], 3))
    # image_input = Input(shape=(None, None, 3))
    # h, w = config.input_shape
    num_anchors = len(config.anchors)

    # y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
    #     num_anchors//3, num_classes+5)) for l in range(3)]

    model_body = yolo_body(image_input, num_anchors // 3, num_classes)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(
        num_anchors, num_classes))

    if load_pretrained:
        # weights_path = keras.utils.get_file(
        #     fname='darknet53_notop_weights.h5',
        #     origin='https://drive.google.com/u/0/uc?export=download&confirm=uCPi&id=1RwvRnB-t2x-LMhU9oKcuJmKzCXInSsUw')
        weights_path = './pretrained/darknet53.h5'
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))
        if freeze_body in [1, 2]:
            # Freeze darknet53 body or freeze all but 3 output layers.
            num = (185, len(model_body.layers) - 3)[freeze_body - 1]
            for i in range(num):
                model_body.layers[i].trainable = False
            print('Freeze the first {} layers of total {} layers.'.format(
                num, len(model_body.layers)))

    # model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
    #     arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
    #     [*model_body.output, *y_true])
    # model = Model([model_body.input, *y_true], model_loss)

    return model_body
Exemple #25
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
Exemple #26
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
Exemple #27
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
def create_model(input_shape,
                 anchors,
                 num_classes,
                 load_pretrained=True,
                 freeze_body=2,
                 weights_path='model_data/yolo_weights.h5'):
    '''create the training model'''
    K.clear_session()  # get a new session
    image_input = Input(shape=(None, None, 3))
    h, w = input_shape
    num_anchors = len(anchors)

    y_true = [Input(shape=(h // {0: 32, 1: 16, 2: 8}[l], w // {0: 32, 1: 16, 2: 8}[l], \
                           num_anchors // 3, num_classes + 5)) for l in range(3)]

    model_body = yolo_body(image_input, num_anchors // 3, num_classes)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(
        num_anchors, num_classes))

    if load_pretrained:
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))
        if freeze_body in [1, 2]:
            # Freeze darknet53 body or freeze all but 3 output layers.
            num = (185, len(model_body.layers) - 3)[freeze_body - 1]
            for i in range(num):
                model_body.layers[i].trainable = False
            print('Freeze the first {} layers of total {} layers.'.format(
                num, len(model_body.layers)))

    # get output of second last layers and create bottleneck model of it
    out1 = model_body.layers[246].output
    out2 = model_body.layers[247].output
    out3 = model_body.layers[248].output
    bottleneck_model = Model([model_body.input, *y_true], [out1, out2, out3])

    # create last layer model of last layers from yolo model
    in0 = Input(shape=bottleneck_model.output[0].shape[1:].as_list())
    in1 = Input(shape=bottleneck_model.output[1].shape[1:].as_list())
    in2 = Input(shape=bottleneck_model.output[2].shape[1:].as_list())
    last_out0 = model_body.layers[249](in0)
    last_out1 = model_body.layers[250](in1)
    last_out2 = model_body.layers[251](in2)
    model_last = Model(inputs=[in0, in1, in2],
                       outputs=[last_out0, last_out1, last_out2])
    model_loss_last = Lambda(yolo_loss,
                             output_shape=(1, ),
                             name='yolo_loss',
                             arguments={
                                 'anchors': anchors,
                                 'num_classes': num_classes,
                                 'ignore_thresh': 0.5
                             })([*model_last.output, *y_true])
    last_layer_model = Model([in0, in1, in2, *y_true], model_loss_last)

    model_loss = Lambda(yolo_loss,
                        output_shape=(1, ),
                        name='yolo_loss',
                        arguments={
                            'anchors': anchors,
                            'num_classes': num_classes,
                            'ignore_thresh': 0.5
                        })([*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss)

    return model, bottleneck_model, last_layer_model
Exemple #29
0
def main():
    yolo_model = yolo_body(Input(shape=(416, 416, 3)), 3, 10)
    print("dyolo_model have", len(yolo_model.layers), "layers")
    yolo_model.summary()
        if c == 27:
            capture.release()
            break


# yolo = YOLO()
class_names = yolo_utils.read_classes(current_path +
                                      "/model_data/voc_classes.txt")
anchors = yolo_utils.read_anchors(current_path +
                                  "/model_data/yolo_anchors.txt")
num_classes = len(class_names)
num_anchors = len(anchors)
input_shape = (416, 416)
image_input = Input(shape=(None, None, 3))
h, w = input_shape
model = yolo_body(image_input, num_anchors // 3, num_classes)
model.load_weights("logs/ep055-loss18.931-val_loss20.760.h5",
                   by_name=True,
                   skip_mismatch=True)
model.summary()
model.compile(optimizer='Adam',
              loss={
                  'yolo_loss': lambda y_true, y_pred: y_pred
              },
              metrics=['accuracy'])

print('please input video path ')
camera_test(model, input())

# while(True):
#     path=input()
Exemple #31
0
# )
from yolo3.model import yolo_body
import keras
from keras.layers import Input
import numpy as np


def get_anchors(anchors_path):
    '''loads the anchors from a file'''
    with open(anchors_path) as f:
        anchors = f.readline()
    anchors = [float(x) for x in anchors.split(',')]
    return np.array(anchors).reshape(-1, 2)


anchors_path = 'model_data/yolo_anchors.txt'
anchors = get_anchors(anchors_path)
num_anchors = len(anchors)
image_input = Input(shape=(None, None, 3))
model = yolo_body(image_input, num_anchors // 3, 1)

model.load_weights('logs/yolo-attention-log/trained_weights_final.h5')

from keract import get_activations
from PIL import Image
img = '/Ted/datasets/Garbage/VOC_Test_Easy/JPEGImages/im183.jpg'
image = Image.open(img)
activations = get_activations(model, image)

from keract import display_heatmaps
display_activations(activations, save=True)
    def load_model(self, yolo_weights=None):
        model_path = self._get_data_path(self.model_path, self.yolo3_dir)
        assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.'
        if yolo_weights is None:
            # 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'
        else:
            self.yolo_model = yolo_weights

        input_image_shape = keras.Input(shape=(2,), name='image_shape')
        image_input = keras.Input((None, None, 3), dtype='float32', name='input_1')
        y = list(self.yolo_model(image_input))
        y.append(input_image_shape)

        if len(y) == 3:
            evaluation_input = [keras.Input((None, None, 255), dtype='float32', name='conv2d_10'),
                                keras.Input((None, None, 255), dtype='float32', name='conv2d_13'),
                                keras.Input(shape=(2,), name='image_shape')
                                ]
        elif len(y) == 4:
            evaluation_input = [keras.Input((None, None, 255), dtype='float32', name='conv2d_59'),
                                keras.Input((None, None, 255), dtype='float32', name='conv2d_67'),
                                keras.Input((None, None, 255), dtype='float32', name='conv2d_75'),
                                keras.Input(shape=(2,), name='image_shape')
                                ]

        boxes, box_scores = \
            YOLOEvaluationLayer(anchors=self.anchors, num_classes=len(self.class_names))(inputs=evaluation_input)
        self.evaluation_model = keras.Model(inputs=evaluation_input,
                                            outputs=[boxes, box_scores])

        nms_input = [keras.Input((4,), dtype='float32', name='concat_9'),
                     keras.Input((80,), dtype='float32', name='concat_10'),]
        out_boxes, out_scores, out_indices = \
            YOLONMSLayer(anchors=self.anchors, num_classes=len(self.class_names))(
                inputs=nms_input)
        self.nms_model = keras.Model(inputs=nms_input,
                                     outputs=[out_boxes, out_scores, out_indices])

        boxes, box_scores = \
            YOLOEvaluationLayer(anchors=self.anchors, num_classes=len(self.class_names))(inputs=y)
        out_boxes, out_scores, out_indices = \
            YOLONMSLayer(anchors=self.anchors, num_classes=len(self.class_names))(
                         inputs = [boxes, box_scores])
        self.final_model = keras.Model(inputs=[image_input, input_image_shape],
                                       outputs = [out_boxes, out_scores, out_indices])
        self.final_model.save('final_model.h5')
        print('{} model, anchors, and classes loaded.'.format(model_path))
Exemple #33
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