Esempio n. 1
0
def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size,
                 warmup_batches, ignore_thresh, multi_gpu, saved_weights_name):
    if multi_gpu > 1:
        with tf.device('/cpu:0'):
            template_model, infer_model = create_yolov3_model(
                nb_class=nb_class,
                anchors=anchors,
                max_box_per_image=max_box_per_image,
                max_grid=max_grid,
                batch_size=batch_size // multi_gpu,
                warmup_batches=warmup_batches,
                ignore_thresh=ignore_thresh)
    else:
        template_model, infer_model = create_yolov3_model(
            nb_class=nb_class,
            anchors=anchors,
            max_box_per_image=max_box_per_image,
            max_grid=max_grid,
            batch_size=batch_size // multi_gpu,
            warmup_batches=warmup_batches,
            ignore_thresh=ignore_thresh)

    # load the pretrained weight if exists, otherwise load the backend weight only
    if os.path.exists(saved_weights_name):
        print("\nLoading pretrained weights.\n")
        template_model.load_weights(saved_weights_name)
    else:
        template_model.load_weights("backend.h5", by_name=True)

    if multi_gpu > 1:
        train_model = multi_gpu_model(template_model, gpus=multi_gpu)
    else:
        train_model = template_model

    return train_model, infer_model
Esempio n. 2
0
def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size,
                 warmup_batches, ignore_thresh, multi_gpu, saved_weights_name,
                 lr, scales):
    if multi_gpu > 1:
        with tf.device('/cpu:0'):
            template_model, infer_model = create_yolov3_model(
                nb_class=nb_class,
                anchors=anchors,
                max_box_per_image=max_box_per_image,
                max_grid=max_grid,
                batch_size=batch_size // multi_gpu,
                warmup_batches=warmup_batches,
                ignore_thresh=ignore_thresh,
                scales=scales)
    else:
        template_model, infer_model = create_yolov3_model(
            nb_class=nb_class,
            anchors=anchors,
            max_box_per_image=max_box_per_image,
            max_grid=max_grid,
            batch_size=batch_size,
            warmup_batches=warmup_batches,
            ignore_thresh=ignore_thresh,
            scales=scales)

        # # Setting the session to allow growth, so it doesn't allocate all GPU memory.
        # gpu_ops = tf.GPUOptions(allow_growth=True)
        # config = tf.ConfigProto(gpu_options=gpu_ops)
        # sess = tf.Session(config=config)
        #
        # # Setting this as the default tensorflow session.
        # keras.backend.tensorflow_backend.set_session(sess)

    # aerial_model = load_model('aerial_model.h5')
    # print("aerial_model summary", aerial_model.summary())
    # backend_model= load_model('backend.h5')
    # print("backend_model summary", backend_model.summary())

    # load the pretrained weight if exists, otherwise load the backend weight only
    if os.path.exists(saved_weights_name):
        # print("\nLoading pretrained weights.\n")
        # print("saved_weights_name", saved_weights_name)
        # template_model.load_weights(saved_weights_name)
        template_model.load_weights("backend.h5", by_name=True)
        print("summary", template_model.summary())
    else:

        template_model.load_weights("backend.h5", by_name=True)

    if multi_gpu > 1:
        train_model = multi_gpu_model(template_model, gpus=multi_gpu)
    else:
        train_model = template_model

    optimizer = Adam(lr=lr, clipnorm=0.001)
    train_model.compile(loss=dummy_loss,
                        optimizer=optimizer,
                        metrics=['accuracy'])

    return train_model, infer_model
Esempio n. 3
0
def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size,
                 warmup_batches, ignore_thresh, multi_gpu, saved_weights_name,
                 lr, grid_scales, obj_scale, noobj_scale, xywh_scale,
                 class_scale):
    if multi_gpu > 1:
        with tf.device('/cpu:0'):
            template_model, infer_model = create_yolov3_model(
                nb_class=nb_class,
                anchors=anchors,
                max_box_per_image=max_box_per_image,
                max_grid=max_grid,
                batch_size=batch_size // multi_gpu,
                warmup_batches=warmup_batches,
                ignore_thresh=ignore_thresh,
                grid_scales=grid_scales,
                obj_scale=obj_scale,
                noobj_scale=noobj_scale,
                xywh_scale=xywh_scale,
                class_scale=class_scale)
    else:
        template_model, infer_model = create_yolov3_model(
            nb_class=nb_class,
            anchors=anchors,
            max_box_per_image=max_box_per_image,
            max_grid=max_grid,
            batch_size=batch_size,
            warmup_batches=warmup_batches,
            ignore_thresh=ignore_thresh,
            grid_scales=grid_scales,
            obj_scale=obj_scale,
            noobj_scale=noobj_scale,
            xywh_scale=xywh_scale,
            class_scale=class_scale)

    # load the pretrained weight if exists, otherwise load the backend weight only
    if os.path.exists(saved_weights_name):
        print("\nLoading pretrained weights.\n")
        template_model.load_weights(saved_weights_name)
    else:
        try:
            template_model.load_weights("backend.h5", by_name=True)
        except IOError:
            print("no weights")

    if multi_gpu > 1:
        train_model = multi_gpu_model(template_model, gpus=multi_gpu)
    else:
        train_model = template_model

    optimizer = Adam(lr=lr, clipnorm=0.001)
    train_model.compile(loss=dummy_loss, optimizer=optimizer)

    return train_model, infer_model
def create_model(num_of_classes, anchors, max_box_per_image, max_grid,
                 batch_size, warmup_batches, ignore_thresh, saved_weights_name,
                 lr, grid_scales, obj_scale, noobj_scale, xywh_scale,
                 class_scale):

    template_model, infer_model = create_yolov3_model(
        nb_class=num_of_classes,
        anchors=anchors,
        max_box_per_image=max_box_per_image,
        max_grid=max_grid,
        batch_size=batch_size,
        warmup_batches=warmup_batches,
        ignore_thresh=ignore_thresh,
        grid_scales=grid_scales,
        obj_scale=obj_scale,
        noobj_scale=noobj_scale,
        xywh_scale=xywh_scale,
        class_scale=class_scale)

    #pretrained or not
    if os.path.exists(saved_weights_name):
        print("\nLoading pretrained weights.\n")
        template_model.load_weights(saved_weights_name)
    else:
        template_model.load_weights("backend.h5", by_name=True)

    train_model = template_model
    optimizer = Adam(lr=lr, clipnorm=0.001)
    train_model.compile(loss=dummy_loss, optimizer=optimizer)

    return train_model, infer_model
Esempio n. 5
0
def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size,
                 warmup_batches, ignore_thresh, multi_gpu, saved_weights_name,
                 lr, grid_scales, obj_scale, noobj_scale, xywh_scale,
                 class_scale):
    template_model, infer_model = create_yolov3_model(
        nb_class=nb_class,
        anchors=anchors,
        max_box_per_image=max_box_per_image,
        max_grid=max_grid,
        batch_size=batch_size,
        warmup_batches=warmup_batches,
        ignore_thresh=ignore_thresh,
        grid_scales=grid_scales,
        obj_scale=obj_scale,
        noobj_scale=noobj_scale,
        xywh_scale=xywh_scale,
        class_scale=class_scale)
    plot_model(template_model, to_file='training_model.png', show_shapes=True)
    plot_model(infer_model, to_file='infer_model.png', show_shapes=True)

    # load the pretrained weight if exists, otherwise load the backend weight only
    if os.path.exists(saved_weights_name):
        print("\nLoading pretrained weights.\n")
        template_model.load_weights(saved_weights_name)
    else:
        template_model.load_weights("backend.h5", by_name=True)

    train_model = template_model

    optimizer = Adam(lr=0.0, clipnorm=0.001)
    train_model.compile(loss=dummy_loss, optimizer=optimizer)

    return train_model, infer_model
def create_model(
        nb_class,  #this is the total number of classes (labels)
        anchors,  #this is the anchors , from the config file
        max_box_per_image,  #max box per image
        max_grid,
        batch_size,  #max grid, batch size , from the config
        warmup_batches,  #warmup  you start with a small learning rate and then gradually increase it by a constant
        # for each epoch till it reaches “k times learning rate”.
    ignore_thresh,  #ignore threash the confidence level, if iou is less than this just ignore it and consider it false
        multi_gpu,  #multi gpu
        saved_weights_name,  #saved weights file name
        lr,  #
        grid_scales,  #grid scale
        obj_scale,  #object scale
        noobj_scale,  #no obj scale
        xywh_scale,  #xywh scale
        class_scale  #class scale
):
    if multi_gpu > 1:  # we have multi gpu use the best gpu, O:^) >:^)
        with tf.device('/cpu:0'):  #close the cpu, choose best gpu 3:^)

            #make a template model (training model )and Inference model ( the final model to predict )
            template_model, infer_model = create_yolov3_model(
                nb_class=nb_class,
                anchors=anchors,
                max_box_per_image=max_box_per_image,
                max_grid=max_grid,
                batch_size=batch_size // multi_gpu,
                warmup_batches=warmup_batches,
                ignore_thresh=ignore_thresh,
                grid_scales=grid_scales,
                obj_scale=obj_scale,
                noobj_scale=noobj_scale,
                xywh_scale=xywh_scale,
                class_scale=class_scale)
    else:  #no gpu >:^(   T_T use the cpu
        template_model, infer_model = create_yolov3_model(
            nb_class=nb_class,
            anchors=anchors,
            max_box_per_image=max_box_per_image,
            max_grid=max_grid,
            batch_size=batch_size,
            warmup_batches=warmup_batches,
            ignore_thresh=ignore_thresh,
            grid_scales=grid_scales,
            obj_scale=obj_scale,
            noobj_scale=noobj_scale,
            xywh_scale=xywh_scale,
            class_scale=class_scale)
    #=================================================== weights ======================================================#
    # load the pretrained weight if exists, otherwise load the backend weight only
    if os.path.exists(saved_weights_name):  #if the weight path exist
        print("\nLoading pretrained weights.\n")  #load this weights
        template_model.load_weights(
            saved_weights_name
        )  #add this weights to the template model (training model)
    else:  #the path doesn't exist, first time train for example
        template_model.load_weights("backend.h5",
                                    by_name=True)  #get the backend weights
    #==================================================================================================================#

    #=============================================== process ==========================================================#
    if multi_gpu > 1:  # we have multi gpu O:^)
        train_model = multi_gpu_model(
            template_model,
            gpus=multi_gpu)  #start train multi gpu template model

    else:  #we don't have multi gpu
        train_model = template_model  # the template is the train model
    #==================================================================================================================#

    #=========================================== optmizers ============================================================#
    optimizer = Adam(lr=lr, clipnorm=0.001)  #optmizer type and value
    train_model.compile(loss=dummy_loss,
                        optimizer=optimizer)  #loss type = dummy loss
    #==================================================================================================================#
    return train_model, infer_model  #return the train model and the infer model (note that we didn't do much on infer
Esempio n. 7
0
def _main_(args):
    config_path = args.conf

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    ###############################
    #   Create the validation generator
    ###############################
    valid_ints, labels = parse_voc_annotation(
        config['valid']['valid_annot_folder'],
        config['valid']['valid_image_folder'], config['valid']['cache_name'],
        config['model']['labels'])

    labels = labels.keys() if len(
        config['model']['labels']) == 0 else config['model']['labels']
    labels = sorted(labels)

    valid_generator = BatchGenerator(
        instances=valid_ints,
        anchors=config['model']['anchors'],
        labels=labels,
        downsample=
        32,  # ratio between network input's size and network output's size, 32 for YOLOv3
        max_box_per_image=0,
        batch_size=config['train']['batch_size'],
        min_net_size=config['model']['min_input_size'],
        max_net_size=config['model']['max_input_size'],
        shuffle=True,
        jitter=0.0,
        norm=normalize)

    ###############################
    #   Load the model and do evaluation
    ###############################

    ###############################
    #   Create the model
    ##############################

    train_model, infer_model = create_yolov3_model(
        nb_class=len(labels),
        anchors=config['model']['anchors'],
        max_box_per_image=0,
        max_grid=[
            config['model']['max_input_size'],
            config['model']['max_input_size']
        ],
        batch_size=config['train']['batch_size'],
        warmup_batches=0,
        ignore_thresh=config['train']['ignore_thresh'],
        grid_scales=config['train']['grid_scales'],
        obj_scale=config['train']['obj_scale'],
        noobj_scale=config['train']['noobj_scale'],
        xywh_scale=config['train']['xywh_scale'],
        class_scale=config['train']['class_scale'],
    )

    os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']

    saved_weights_name = config['train']['saved_weights_name']
    lr = config['train']['learning_rate'],

    infer_model.load_weights(saved_weights_name)
    optimizer = Adam(lr=lr, clipnorm=0.001)
    infer_model.compile(loss=dummy_loss, optimizer=optimizer)

    infer_model.summary()
    #infer_model = load_model(config['train']['saved_weights_name'])

    # compute mAP for all the classes
    recall, precision, average_precisions = evaluate(infer_model,
                                                     valid_generator)

    # print the score
    for label, average_precision in average_precisions.items():
        print(labels[label] + ': {:.4f}'.format(average_precision))
    print('mAP: {:.4f}'.format(
        sum(average_precisions.values()) / len(average_precisions)))

    return recall, precision, average_precisions
Esempio n. 8
0
def create_model(
    nb_class, 
    anchors, 
    max_box_per_image, 
    max_grid, batch_size, 
    warmup_batches, 
    ignore_thresh, 
    multi_gpu, 
    saved_weights_name, 
    lr,
    grid_scales,
    obj_scale,
    noobj_scale,
    xywh_scale,
    class_scale  
):
    if multi_gpu > 1:
        with tf.device('/cpu:0'):
            template_model, infer_model = create_yolov3_model(
                nb_class            = nb_class, 
                anchors             = anchors, 
                max_box_per_image   = max_box_per_image, 
                max_grid            = max_grid, 
                batch_size          = batch_size//multi_gpu, 
                warmup_batches      = warmup_batches,
                ignore_thresh       = ignore_thresh,
                grid_scales         = grid_scales,
                obj_scale           = obj_scale,
                noobj_scale         = noobj_scale,
                xywh_scale          = xywh_scale,
                class_scale         = class_scale
            )
    else:
        template_model, infer_model = create_yolov3_model(
            nb_class            = nb_class, 
            anchors             = anchors, 
            max_box_per_image   = max_box_per_image, 
            max_grid            = max_grid, 
            batch_size          = batch_size, 
            warmup_batches      = warmup_batches,
            ignore_thresh       = ignore_thresh,
            grid_scales         = grid_scales,
            obj_scale           = obj_scale,
            noobj_scale         = noobj_scale,
            xywh_scale          = xywh_scale,
            class_scale         = class_scale
        )  

    # load the pretrained weight if exists, otherwise load the backend weight only
    if os.path.exists(saved_weights_name): 
        print("\nLoading pretrained weights.\n")
        template_model.load_weights(saved_weights_name)
    else:
        template_model.load_weights("backend.h5", by_name=True)       

    if multi_gpu > 1:
        train_model = multi_gpu_model(template_model, gpus=multi_gpu)
    else:
        train_model = template_model      

    optimizer = Adam(lr=lr, clipnorm=0.001)
    train_model.compile(loss=dummy_loss, optimizer=optimizer)             

    return train_model, infer_model
Esempio n. 9
0
def create_model(nb_class,
                 anchors,
                 max_box_per_image,
                 max_grid,
                 batch_size,
                 warmup_batches,
                 ignore_thresh,
                 multi_gpu,
                 saved_weights_name,
                 lr,
                 grid_scales,
                 obj_scale,
                 noobj_scale,
                 xywh_scale,
                 class_scale,
                 optimizer=None):
    if multi_gpu > 1:
        logging.info(f"Create model for {multi_gpu} gpus")
        with tf.device('/cpu:0'):
            template_model, infer_model = create_yolov3_model(
                nb_class=nb_class,
                anchors=anchors,
                max_box_per_image=max_box_per_image,
                max_grid=max_grid,
                batch_size=batch_size // multi_gpu,
                warmup_batches=warmup_batches,
                ignore_thresh=ignore_thresh,
                grid_scales=grid_scales,
                obj_scale=obj_scale,
                noobj_scale=noobj_scale,
                xywh_scale=xywh_scale,
                class_scale=class_scale)
    else:
        template_model, infer_model = create_yolov3_model(
            nb_class=nb_class,
            anchors=anchors,
            max_box_per_image=max_box_per_image,
            max_grid=max_grid,
            batch_size=batch_size,
            warmup_batches=warmup_batches,
            ignore_thresh=ignore_thresh,
            grid_scales=grid_scales,
            obj_scale=obj_scale,
            noobj_scale=noobj_scale,
            xywh_scale=xywh_scale,
            class_scale=class_scale)

    # load the pretrained weight if exists, otherwise load the backend weight only
    if os.path.exists(saved_weights_name):
        logging.info("Loading pretrained weights.")
        template_model.load_weights(saved_weights_name)
    elif os.path.exists("backend.h5"):
        logging.info("Loading default backend weights.")
        template_model.load_weights("backend.h5", by_name=True)
    else:
        logging.info("Training from scratch")

    if multi_gpu > 1:
        train_model = multi_gpu_model(template_model, gpus=multi_gpu)
    else:
        train_model = template_model

    if not optimizer:
        optimizer = Adam(lr=lr, clipnorm=0.001)
    train_model.compile(loss=dummy_loss, optimizer=optimizer)

    return train_model, infer_model
Esempio n. 10
0
def create_model(nb_class,
                 anchors,
                 max_box_per_image,
                 max_grid,
                 batch_size,
                 warmup_batches,
                 ignore_thresh,
                 multi_gpu,
                 saved_weights_name,
                 lr,
                 grid_scales,
                 obj_scale,
                 noobj_scale,
                 xywh_scale,
                 class_scale,
                 tiny=False):

    if multi_gpu > 1:
        if tiny:
            with tf.device('/cpu:0'):
                template_model, infer_model = create_yolov3_tiny_model(
                    nb_class=nb_class,
                    anchors=anchors,
                    max_box_per_image=max_box_per_image,
                    max_grid=max_grid,
                    batch_size=batch_size // multi_gpu,
                    warmup_batches=warmup_batches,
                    ignore_thresh=ignore_thresh,
                    grid_scales=grid_scales,
                    obj_scale=obj_scale,
                    noobj_scale=noobj_scale,
                    xywh_scale=xywh_scale,
                    class_scale=class_scale)
        else:
            with tf.device('/cpu:0'):
                template_model, infer_model = create_yolov3_model(
                    nb_class=nb_class,
                    anchors=anchors,
                    max_box_per_image=max_box_per_image,
                    max_grid=max_grid,
                    batch_size=batch_size // multi_gpu,
                    warmup_batches=warmup_batches,
                    ignore_thresh=ignore_thresh,
                    grid_scales=grid_scales,
                    obj_scale=obj_scale,
                    noobj_scale=noobj_scale,
                    xywh_scale=xywh_scale,
                    class_scale=class_scale)
    else:
        if tiny:
            template_model, infer_model = create_yolov3_tiny_model(
                nb_class=nb_class,
                anchors=anchors,
                max_box_per_image=max_box_per_image,
                max_grid=max_grid,
                batch_size=batch_size,
                warmup_batches=warmup_batches,
                ignore_thresh=ignore_thresh,
                grid_scales=grid_scales,
                obj_scale=obj_scale,
                noobj_scale=noobj_scale,
                xywh_scale=xywh_scale,
                class_scale=class_scale)
        else:
            template_model, infer_model = create_yolov3_model(
                nb_class=nb_class,
                anchors=anchors,
                max_box_per_image=max_box_per_image,
                max_grid=max_grid,
                batch_size=batch_size,
                warmup_batches=warmup_batches,
                ignore_thresh=ignore_thresh,
                grid_scales=grid_scales,
                obj_scale=obj_scale,
                noobj_scale=noobj_scale,
                xywh_scale=xywh_scale,
                class_scale=class_scale)

    # load the pretrained weight if exists, otherwise start from scratch
    if os.path.exists(saved_weights_name):
        print("\nLoading pretrained weights.\n")
        try:
            template_model.load_weights(saved_weights_name)
        except:
            print("Weights file failed to load, starting from scratch")
    else:
        print("No pre-trained weights available, starting from scratch")

    if multi_gpu > 1:
        train_model = multi_gpu_model(template_model, gpus=multi_gpu)
    else:
        train_model = template_model

    optimizer = Adam(lr=lr, clipnorm=0.001)
    train_model.compile(loss=dummy_loss, optimizer=optimizer)

    return train_model, infer_model