예제 #1
0
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    # optionally choose specific GPU
    if args.gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu

    K.set_session(get_session())

    # create the generators
    train_generator, validation_generator = create_generators(
        args)  # 训练与验证数据生成器

    input_shape = (train_generator.image_size, train_generator.image_size)
    anchors = train_generator.anchors
    num_classes = train_generator.num_classes()
    model, prediction_model = yolo_body(anchors, num_classes=num_classes)
    ###
    classes_path = 'model_data/gw0219_classes.txt'  # model_data/voc_classes.txt
    anchors_path = 'model_data/yolo_anchors_gw0219.txt'  # model_data/yolo_anchors.txt
    class_names = get_classes(classes_path)
    num_classes = len(class_names)
    anchors = get_anchors(anchors_path)
    ###
    model, prediction_model = yolo_body(anchors, num_classes=num_classes)

    # create the model
    print('Loading model, this may take a second...')
    model.load_weights(args.snapshot, by_name=True, skip_mismatch=True)

    # freeze layers
    if args.freeze_body == 'darknet':
        for i in range(185):
            model.layers[i].trainable = False
    elif args.freeze_body == 'yolo':
        for i in range(len(model.layers) - 7):
            model.layers[i].trainable = False

    # compile model
    model.compile(optimizer=Adam(lr=1e-3),
                  loss={
                      'yolo_loss': lambda y_true, y_pred: y_pred
                  })

    # print(model.summary())

    # create the callbacks  ###
    # callbacks = create_callbacks(
    #     model,
    #     prediction_model,
    #     validation_generator,
    #     args,
    # )

    if not args.compute_val_loss:
        validation_generator = None
예제 #2
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 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
예제 #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)

        my_input = Input(shape=[], dtype=tf.string)
        self.base_input = my_input
        my_input = Lambda(get_inputs, output_shape=(416, 416, 3))(my_input)
        my_input = Input(tensor=my_input)
        self.yolo_model = yolo_body(my_input, num_anchors // 3, num_classes)
        self.yolo_model.load_weights(
            self.model_path)  # make sure model, anchors and classes match

        print(
            'Detection model, {} model, {} anchors, and {} classes load success!.'
            .format(model_path, num_anchors, num_classes))

        self.input_image_shape = K.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(self.yolo_model.output,
                                           self.anchors,
                                           len(self.class_names),
                                           self.input_image_shape,
                                           score_threshold=self.score,
                                           iou_threshold=self.iou)
        return boxes, scores, classes
예제 #4
0
def load():
    """
    加载模型,回传模型和模型参数
    """

    # 模型参数
    class_names = get_classes(classes_path)
    num_classes = len(class_names)

    anchors = get_anchors(anchors_path)
    num_anchors = len(anchors)

    input_shape = (416, 416)

    train_weights_path = log_dir + '/ep169-loss17.356-val_loss6.844.h5'

    # 获取模型结构,加载权重
    image_input = Input(shape=(None, None, 3))

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

    model.load_weights(train_weights_path, by_name=True, skip_mismatch=True)
    print('Load weights {}.'.format(train_weights_path))

    # 回传参数
    param = [class_names, num_classes, anchors, input_shape]

    return model, param
예제 #5
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
예제 #6
0
def create_model(input_shape,
                 no_classes,
                 anchors,
                 load_pretrained_weight=True,
                 freeze_body=2,
                 weight_path='CWD + .txt'):  #please add weight path next
    """this function is to create a yolo Model"""
    K.clear_session()
    image_shape = Input(shape=(None, None, 3))
    h, w = input_shape
    no_anchors = len(anchors) // 3
    print(weight_path)
    _yolo_body = yolo_body(image_shape, no_anchors, no_classes)

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

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

    loss = Lambda(
        yolo_loss,
        output_shape=(1, ),
        name="yolo_loss",
        arguments={
            "anchors": anchors,
            "num_classes": no_classes,
            "ignore_thresh": 0.5
        },
    )([*_yolo_body.output, *y_true])

    model = Model([_yolo_body.input, *y_true], loss)
    return model
예제 #7
0
    def get_model(cls, model_path='../model'):
        modelpath = os.path.join(model_path, 'YOLOv3_608_cl2_ep013_val_loss51.h5')

        class_names = cls._get_class()
        anchors = cls._get_anchors()

        # 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
        cls.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)
        cls.yolo_model.load_weights(modelpath)  # make sure model, anchors and classes match
        return True
예제 #8
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
예제 #9
0
    def get_model(cls, model_path='../model'):
        cls.IDvalue = 0 # Reset Object ID
        cls.Mem_IDvalue = 0 # Reset Memory Object ID
        cls.trackers = cv2.MultiTracker_create()#Multi Object tracker init
        
        modelpath = os.path.join(model_path, 'YOLOv3_608_cl10_val_loss71.h5')

        class_names = cls._get_class()
        anchors = cls._get_anchors()

        # 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
        cls.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)
        cls.yolo_model.load_weights(modelpath)  # make sure model, anchors and classes match
        return True
예제 #10
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('cfg', 'yolo_weights.h5')
        if not os.path.exists(weights_path):
            print("CREATING WEIGHTS FILE" + weights_path)
            yolo_path = os.path.join('cfg', 'yolo.h5')
            model_body = load_model(yolo_path, compile=False)
            model_body.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)

    return model_body, model
예제 #11
0
def create_model(input_shape,
                 anchors,
                 num_classes,
                 load_pretrained=False,
                 freeze_body=False,
                 weights_path=save_path + '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:  #如果载入权重,这里的权重就是原模型训练好的h5文件
        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.
            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': 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
                summary_value.simple_value = result
                summary_value.tag = '{}. {}'.format(index + 1, coco_tag[index])
                self.tensorboard.writer.add_summary(summary, epoch)
                logs[coco_tag[index]] = result


if __name__ == '__main__':
    dataset_dir = '/home/adam/.keras/datasets/coco/2017_118_5'
    test_generator = CocoGenerator(
        anchors_path='yolo_anchors.txt',
        data_dir=dataset_dir,
        set_name='test-dev2017',
        shuffle_groups=False,
    )
    input_shape = (416, 416)
    model, prediction_model = yolo_body(test_generator.anchors, num_classes=80)
    model.load_weights('checkpoints/yolov3_weights.h5', by_name=True)
    coco_eval_stats = evaluate(test_generator, model)
    coco_tag = [
        'AP @[ IoU=0.50:0.95 | area=   all | maxDets=100 ]',
        'AP @[ IoU=0.50      | area=   all | maxDets=100 ]',
        'AP @[ IoU=0.75      | area=   all | maxDets=100 ]',
        'AP @[ IoU=0.50:0.95 | area= small | maxDets=100 ]',
        'AP @[ IoU=0.50:0.95 | area=medium | maxDets=100 ]',
        'AP @[ IoU=0.50:0.95 | area= large | maxDets=100 ]',
        'AR @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ]',
        'AR @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ]',
        'AR @[ IoU=0.50:0.95 | area=   all | maxDets=100 ]',
        'AR @[ IoU=0.50:0.95 | area= small | maxDets=100 ]',
        'AR @[ IoU=0.50:0.95 | area=medium | maxDets=100 ]',
        'AR @[ IoU=0.50:0.95 | area= large | maxDets=100 ]'
예제 #13
0
import cv2
import glob
import numpy as np
import os
import os.path as osp
from utils import get_anchors, get_classes, preprocess_image
from model import yolo_body

input_shape = (416, 416)
anchors = get_anchors('voc_anchors_416.txt')
classes = get_classes('voc_classes.txt')
num_classes = len(classes)
model, prediction_model = yolo_body(anchors=anchors, score_threshold=0.1)
model.load_weights('checkpoints/pascal_21_9.4463_12.8289_0.8334_0.8535.h5', by_name=True)
batch_size = 1
image_paths = glob.glob('datasets/VOC2007/JPEGImages/*.jpg')
num_images = len(image_paths)
colors = [np.random.randint(0, 256, 3).tolist() for i in range(num_classes)]


def show_image(image, name, contours=None):
    image = image.astype(np.uint8)
    cv2.namedWindow(name, cv2.WINDOW_NORMAL)
    if contours is not None:
        if isinstance(contours, list):
            cv2.drawContours(image, contours, -1, (0, 0, 255), 2)
        else:
            cv2.drawContours(image, [contours], -1, (0, 0, 255), 2)
    cv2.imshow(name, image)

예제 #14
0
    common_args = {
        'batch_size': 1,
        'image_size': 416
    }
    test_generator = PascalVocGenerator(
        'datasets/VOC2007',
        'test',
        shuffle_groups=False,
        skip_truncated=False,
        skip_difficult=True,
        anchors_path='voc_anchors_416.txt',
        **common_args
    )
    model_path = 'checkpoints/pascal_21_9.4463_12.8289_0.8334_0.8535.h5'
    input_shape = (test_generator.image_size, test_generator.image_size)
    anchors = test_generator.anchors
    num_classes = test_generator.num_classes()
    model, prediction_model = yolo_body(anchors, num_classes=num_classes)
    prediction_model.load_weights(model_path, by_name=True, skip_mismatch=True)
    average_precisions = evaluate(test_generator, prediction_model, visualize=False)
    # compute per class average precision
    total_instances = []
    precisions = []
    for label, (average_precision, num_annotations) in average_precisions.items():
        print('{:.0f} instances of class'.format(num_annotations), test_generator.label_to_name(label),
              'with average precision: {:.4f}'.format(average_precision))
        total_instances.append(num_annotations)
        precisions.append(average_precision)
    mean_ap = sum(precisions) / sum(x > 0 for x in total_instances)
    print('mAP: {:.4f}'.format(mean_ap))
import coremltools
from keras.layers import Input
from model import yolo_body
'''
import json
from keras.models import model_from_json
# use json to load the architecture first, then weights
modelName = 'trained_weights_stage_1'

with open('%s.json' % modelName, 'r') as f:
    model_json  = f.read()
model = model_from_json(model_json)
print('model -> ', model)
model.load_weights('%s.h5' % modelName)
'''

num_classes = 2
model = yolo_body(Input(shape=(None, None, 3)), 3, num_classes)
model.load_weights('trained_3.h5')

coreml_model = coremltools.converters.keras.convert(
    model,
    input_names='image',
    image_input_names='image',
    input_name_shape_dict={'image': [None, 416, 416, 3]},
    image_scale=1 / 255.)

coreml_model.license = 'Public Domain'
coreml_model.input_description['image'] = 'Input image'

coreml_model.save('yolo.mlmodel')
예제 #16
0
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    # optionally choose specific GPU
    if args.gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu

    K.set_session(get_session())

    # create the generators
    train_generator, validation_generator = create_generators(args)

    input_shape = (train_generator.image_size, train_generator.image_size)
    anchors = train_generator.anchors
    num_classes = train_generator.num_classes()
    model, prediction_model = yolo_body(anchors, num_classes=num_classes)

    # create the model
    print('Loading model, this may take a second...')
    model.load_weights(args.snapshot, by_name=True, skip_mismatch=True)

    # freeze layers
    if args.freeze_body == 'darknet':
        for i in range(185):
            model.layers[i].trainable = False
    elif args.freeze_body == 'yolo':
        for i in range(len(model.layers) - 7):
            model.layers[i].trainable = False

    # compile model
    model.compile(optimizer=Adam(lr=1e-3), loss={'yolo_loss': lambda y_true, y_pred: y_pred})

    # print(model.summary())

    # create the callbacks
    callbacks = create_callbacks(
        model,
        prediction_model,
        validation_generator,
        args,
    )

    if not args.compute_val_loss:
        validation_generator = None

    # start training
    return model.fit_generator(
        generator=train_generator,
        steps_per_epoch=args.steps,
        initial_epoch=0,
        epochs=args.epochs,
        verbose=1,
        callbacks=callbacks,
        workers=args.workers,
        use_multiprocessing=args.multiprocessing,
        max_queue_size=args.max_queue_size,
        validation_data=validation_generator
    )