Exemplo n.º 1
0
def evaluateSet(setpath,weightnumber=13):
    gpu = 0
    setup_gpu(gpu)
    model_path = os.path.join('..', 'keras_retinanet', 'resnet50_csv_' + str(weightnumber) + '_final.h5')
    # load retinanet model
    model = models.load_model(model_path, backbone_name='resnet50')
    iset = readSet(setpath) #read set
    imagelist = iset[:,0] # load imagelist
    resultlist = [["image","bounds","score", "label"]]
    for i in imagelist:
        image = read_image_bgr(str("../" + i)) #originally bgr(i)
        if draw:
            image_copy = image.copy()
        image = preprocess_image(image)
        image, scale = resize_image(image)
        boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
        boxes /= scale
        coords = []
        cats = []
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
          if score < 0.4:
            break
          else:
            resultlist.append([i,box,label,score])
    return resultlist
Exemplo n.º 2
0
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    # make sure keras is the minimum required version
    check_keras_version()

    # optionally choose specific GPU
    if args.gpu:
        setup_gpu(args.gpu)

    # optionally load config parameters
    if args.config:
        args.config = read_config_file(args.config)

    # make save path if it doesn't exist
    if args.save_path is not None and not os.path.exists(args.save_path):
        os.makedirs(args.save_path)

    # create the generator
    generator = create_generator(args)

    # load the model
    print('Loading model, this may take a second...')
    model = models.load_model(args.model, backbone_name=args.backbone)

    # print model summary
    # print(model.summary())

    # start evaluation
    if args.dataset_type == 'coco':
        from ..utils.coco_eval import evaluate_coco
        evaluate_coco(generator, model, args.score_threshold)
    else:
        average_precisions = evaluate(
            generator,
            model,
            iou_threshold=args.iou_threshold,
            score_threshold=args.score_threshold,
            max_detections=args.max_detections,
            binarize_threshold=args.binarize_threshold,
            save_path=args.save_path
        )

        # print evaluation
        total_instances = []
        precisions = []
        for label, (average_precision, num_annotations) in average_precisions.items():
            print('{:.0f} instances of class'.format(num_annotations),
                  generator.label_to_name(label), 'with average precision: {:.4f}'.format(average_precision))
            total_instances.append(num_annotations)
            precisions.append(average_precision)
        if args.weighted_average:
            print('mAP: {:.4f}'.format(sum([a * b for a, b in zip(total_instances, precisions)]) / sum(total_instances)))
        else:
            print('mAP: {:.4f}'.format(sum(precisions) / sum(x > 0 for x in total_instances)))
Exemplo n.º 3
0
    def __init__(self):
        # Reading the classes and respective index from classes.json file
        self.classes = {
            value["id"] - 1: value["name"]
            for value in json.load(open("classes.json", "r")).values()
        }
        self.num_classes = 2
        self.colors_classes = [
            np.random.randint(0, 256, 3).tolist()
            for _ in range(self.num_classes)
        ]
        #Threshold on score to filter detections with (defaults to 0.05).
        self.score_threshold = 0.5
        # IoU Threshold to count for a positive detection (defaults to 0.5).
        self.iou_threshold = 0.05
        # Max Detections per image (defaults to 100).
        self.max_detections = 100
        # Setup GPU device
        self.gpu = 0
        setup_gpu(self.gpu)
        # Rescale the image so the smallest side is min_side.
        self.image_min_side = 800
        # Rescale the image if the largest side is larger than max_side.
        self.image_max_side = 1333
        # make save path if it doesn't exist
        self.save_path = "/eveluation"
        if self.save_path is not None and not os.path.exists(self.save_path):
            os.makedirs(self.save_path)

        # optionally load anchor parameters when the inference model has been generated along with training
        # Provide the path of config of file such as (self.config = "path_to_config_file")
        self.config = None
        self.anchor_params = None
        if self.config and "anchor_parameters" in self.config:
            self.anchor_params = parse_anchor_parameters(self.config)

        # Backbone Network
        self.backbone_network = "resnet50"
        self.weight_dir = "snapshots"
        # Model to be evaluated
        self.model_to_load = os.path.join(self.weight_dir,
                                          "resnet50_csv_17.h5")
        # Convert the trained model to ind=ference model
        self.convert_model = True

        # load the model
        print("Loading model, this may take a second...")
        self.model = models.load_model(self.model_to_load,
                                       backbone_name=self.backbone_network)
        self.model = models.convert_model(self.model,
                                          anchor_params=self.anchor_params)
Exemplo n.º 4
0
    def auto_label_coco(self):
        # use this to change which GPU to use
        gpu = 0
        # set the modified tf session as backend in keras
        setup_gpu(gpu)
        model_path = self.retina_weight
        model = models.load_model(model_path, backbone_name='resnet50')

        pic_list = os.listdir(self.pic_dir)
        no_label_img_list = []
        for pic_name in pic_list:
            pic_start = time.time()
            mpos = []
            mclass = []
            mscore = []

            pic_fullpath = Path(self.pic_dir).joinpath(pic_name)
            print("[INFO]picfullpath:", pic_fullpath, type(pic_fullpath))

            image = read_image_bgr(pic_fullpath)
            image = preprocess_image(image)
            image, scale = resize_image(image)
            boxes, scores, labels = model.predict_on_batch(
                np.expand_dims(image, axis=0))
            boxes /= scale
            class_dict = {}
            if self.coco_classes != "all":
                class_dict = self.coco_classes
            for box, score, label in zip(boxes[0], scores[0], labels[0]):
                # scores are sorted so we can break
                # if score < 0.5:
                if score < float(self.retina_threshold):
                    break
                involed_class = labels_to_names[label]
                if self.coco_classes != "all" and involed_class not in class_dict:
                    continue
                mpos.append(box)
                mclass.append(involed_class)
                mscore.append(score)
            no_label_img = annotation_single_img(self.pic_dir, pic_name,
                                                 self.xml_dir, mclass, mpos)
            pic_end = time.time()
            print("[INFO]single pic time:", str(pic_end - pic_start))
            if no_label_img != None:
                no_label_img_list.append(no_label_img)
        if no_label_img_list != []:
            print("[WARNING] There are some picture which have no label, Please remove them:", \
                  str(no_label_img_list))
Exemplo n.º 5
0
def retina_detect_img(model_path="load_weight/resnet50_coco_best_v2.1.0.h5",
                      img_path="voc/JPEGImages/1.jpg"):
    # use this to change which GPU to use
    gpu = 0
    # set the modified tf session as backend in keras
    setup_gpu(gpu)
    # ## Load RetinaNet model
    # adjust this to point to your downloaded/trained model
    # models can be downloaded here: https://github.com/fizyr/keras-retinanet/releases
    # load retinanet model
    model = models.load_model(model_path, backbone_name='resnet50')
    # if the model is not converted to an inference model, use the line below
    # see: https://github.com/fizyr/keras-retinanet#converting-a-training-model-to-inference-model
    # model = models.convert_model(model)
    # print(model.summary())
    # ## Run detection on example
    # load image
    image = read_image_bgr(img_path)
    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)
    # process image
    boxes, scores, labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))
    # correct for image scale
    boxes /= scale
    # visualize detections
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        # scores are sorted so we can break
        # if score < 0.5:
        if score < 0.38:
            break
        color = label_color(label)
        b = box.astype(int)
        draw_box(draw, b, color=color)
        caption = "{} {:.3f}".format(labels_to_names[label], score)
        draw_caption(draw, b, caption)
    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    plt.show()
Exemplo n.º 6
0
def detect_init():
    # use this to change which GPU to use
    gpu = 0

    # set the modified tf session as backend in keras
    setup_gpu(gpu)
    # adjust this to point to your downloaded/trained model
    # models can be downloaded here: https://github.com/fizyr/keras-retinanet/releases
    # model_path = 'retinanet/BOP/resnet50_mydata_800_1333.h5'
    # model_path = 'BOP/resnet50_mydata_800_1333.h5'
    # model_path = 'retinanet/MYRGB/MYRGB_50.h5'
    model_path = 'MYRGB/MYRGB_50.h5'

    global label_2_name
    label_2_name = {
        0: 'M1',
        1: 'M3',
        2: 'M4',
        3: 'M7',
        4: 'M8',
        5: 'M5',
        6: 'M6',
        7: 'M2'
    }
    # label_2_name = {0: 'duck', 1: 'pot', 2: 'cup', 3: 'drill'}

    # load retinanet model

    model = models.load_model(model_path)

    # if the model is not converted to an inference model, use the line below
    # see: https://github.com/fizyr/keras-retinanet#converting-a-training-model-to-inference-model
    # model = models.convert_model(model)

    # print(model.summary())
    return model
Exemplo n.º 7
0
#use the below command when you are giving your own config file
#from keras_retinanet.utils.config import read_config_file, parse_anchor_parameters
from keras_retinanet.utils.gpu import setup_gpu
from keras_retinanet.utils.model import freeze as freeze_model
from keras_retinanet.utils.transform import random_transform_generator
from keras_maskrcnn import losses
from keras_maskrcnn import models
from keras_maskrcnn.utils.visualization import draw_mask
from keras_retinanet.utils.visualization import draw_box, draw_caption, draw_annotations
from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image
from keras_retinanet.utils.colors import label_color
import matplotlib.pyplot as plt
import cv2
import numpy as np
import time
setup_gpu(0)


class Retinamask:
    def __init__(self, annotations, classes, Input_weights_path,
                 trained_weights_path, test_image_path, output_image_path,
                 epoch):
        self.annotations = annotations
        self.classes = classes
        self.Input_weights_path = Input_weights_path
        self.trained_weights_path = trained_weights_path
        self.test_image_path = test_image_path
        self.output_image_path = output_image_path
        self.epoch = epoch

    def create_models(backbone_retinanet,
Exemplo n.º 8
0
# import miscellaneous modules
import matplotlib.pyplot as plt
import cv2
import os
import numpy as np
import time

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw

# use this to change which GPU to use
gpu = 0

# set the modified tf session as backend in keras
setup_gpu(gpu)

model = models.load_model(model_path, backbone_name='resnet50')
labels_to_names = {
    0: 'person',
    1: 'bicycle',
    2: 'car',
    3: 'motorcycle',
    4: 'airplane',
    5: 'bus',
    6: 'train',
    7: 'truck',
    8: 'boat',
    9: 'traffic light',
    10: 'fire hydrant',
    11: 'stop sign',
Exemplo n.º 9
0
def main():
    setup_gpu(1)
Exemplo n.º 10
0
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    # make sure keras is the minimum required version
    check_keras_version()

    # create object that stores backbone information
    backbone = models.backbone(args.backbone)

    # optionally choose specific GPU
    if args.gpu:
        setup_gpu(args.gpu)

    # optionally load config parameters
    if args.config:
        args.config = read_config_file(args.config)

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

    # create the model
    if args.snapshot is not None:
        print('Loading model, this may take a second...')
        model            = models.load_model(args.snapshot, backbone_name=args.backbone)
        training_model   = model
        prediction_model = model
    else:
        weights = args.weights
        # default to imagenet if nothing else is specified
        if weights is None and args.imagenet_weights:
            weights = backbone.download_imagenet()

        anchor_params = None
        if args.config and 'anchor_parameters' in args.config:
            anchor_params = parse_anchor_parameters(args.config)

        print('Creating model, this may take a second...')
        model, training_model, prediction_model = create_models(
            backbone_retinanet=backbone.maskrcnn,
            num_classes=train_generator.num_classes(),
            weights=weights,
            freeze_backbone=args.freeze_backbone,
            class_specific_filter=args.class_specific_filter,
            anchor_params=anchor_params
        )

    # print model summary
    print(model.summary())

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

    # Use multiprocessing if workers > 0
    if args.workers > 0:
        use_multiprocessing = True
    else:
        use_multiprocessing = False

    # start training
    training_model.fit_generator(
        generator=train_generator,
        steps_per_epoch=args.steps,
        epochs=args.epochs,
        verbose=1,
        callbacks=callbacks,
        workers=args.workers,
        use_multiprocessing=use_multiprocessing,
        max_queue_size=args.max_queue_size
    )
Exemplo n.º 11
0
def main(forest_object,
         args=None,
         input_type="fit_generator",
         list_of_tfrecords=None,
         comet_experiment=None):
    """
    Main Training Loop
    Args:
        forest_object: a deepforest class object
        args: Keras retinanet argparse
        list_of_tfrecords: list of tfrecords to parse
        input_type: "fit_generator" or "tfrecord" input type
    """
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    # create object that stores backbone information
    backbone = models.backbone(args.backbone)

    # make sure keras is the minimum required version
    check_keras_version()

    # optionally choose specific GPU
    if args.gpu:
        setup_gpu(args.gpu)

    # optionally load config parameters
    if args.config:
        args.config = read_config_file(args.config)

    # data input
    if input_type == "fit_generator":
        # create the generators
        train_generator, validation_generator = create_generators(
            args, backbone.preprocess_image)

        # placeholder target tensor for creating models
        targets = None

    elif input_type == "tfrecord":
        # Create tensorflow iterators
        iterator = tfrecords.create_dataset(list_of_tfrecords, args.batch_size)
        next_element = iterator.get_next()

        # Split into inputs and targets
        inputs = next_element[0]
        targets = [next_element[1], next_element[2]]

        validation_generator = None

    else:
        raise ValueError(
            "{} input type is invalid. Only 'tfrecord' or 'for_generator' "
            "input types are accepted for model training".format(input_type))

    # create the model
    if args.snapshot is not None:
        print('Loading model, this may take a second...')
        model = models.load_model(args.snapshot, backbone_name=args.backbone)
        training_model = model
        anchor_params = None
        if args.config and 'anchor_parameters' in args.config:
            anchor_params = parse_anchor_parameters(args.config)
        prediction_model = retinanet_bbox(model=model,
                                          anchor_params=anchor_params)
    else:
        weights = args.weights
        # default to imagenet if nothing else is specified
        if weights is None and args.imagenet_weights:
            weights = backbone.download_imagenet()

        print('Creating model, this may take a second...')
        if input_type == "fit_generator":
            num_of_classes = train_generator.num_classes()
        else:
            # Add background class
            num_of_classes = len(forest_object.labels.keys())

        model, training_model, prediction_model = create_models(
            backbone_retinanet=backbone.retinanet,
            num_classes=num_of_classes,
            weights=weights,
            multi_gpu=args.multi_gpu,
            freeze_backbone=args.freeze_backbone,
            lr=args.lr,
            config=args.config,
            targets=targets,
            freeze_layers=args.freeze_layers)

    # print model summary
    print(model.summary())

    # this lets the generator compute backbone layer shapes using the actual backbone model
    if 'vgg' in args.backbone or 'densenet' in args.backbone:
        train_generator.compute_shapes = make_shapes_callback(model)
        if validation_generator:
            validation_generator.compute_shapes = train_generator.compute_shapes

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

    if not args.compute_val_loss:
        validation_generator = None

    # start training
    if input_type == "fit_generator":
        history = training_model.fit_generator(
            generator=train_generator,
            steps_per_epoch=args.steps,
            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)
    elif input_type == "tfrecord":

        # Fit model
        history = training_model.fit(x=inputs,
                                     steps_per_epoch=args.steps,
                                     epochs=args.epochs,
                                     callbacks=callbacks)
    else:
        raise ValueError(
            "{} input type is invalid. Only 'tfrecord' or 'for_generator' "
            "input types are accepted for model training".format(input_type))

    # Assign history to deepforest model class
    forest_object.history = history

    # return trained model
    return model, prediction_model, training_model
Exemplo n.º 12
0
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
        args = parse_args(args)

    # create object that stores backbone information
    backbone = models.backbone(args.backbone)

    # make sure keras and tensorflow are the minimum required version
    check_keras_version()
    check_tf_version()

    # optionally choose specific GPU
    if args.gpu is not None:
        setup_gpu(args.gpu)

    # optionally load config parameters
    if args.config:
        args.config = read_config_file(args.config)

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

    # create the model
    if args.snapshot is not None:
        print('Loading model, this may take a second...')
        model            = models.load_model(args.snapshot, backbone_name=args.backbone)
        training_model   = model
        anchor_params    = None
        if args.config and 'anchor_parameters' in args.config:
            anchor_params = parse_anchor_parameters(args.config)
        prediction_model = retinanet_bbox(model=model, anchor_params=anchor_params)
    else:
        weights = args.weights
        # default to imagenet if nothing else is specified
        if weights is None and args.imagenet_weights:
            weights = backbone.download_imagenet()

        print('Creating model, this may take a second...')
        model, training_model, prediction_model = create_models(
            backbone_retinanet=backbone.retinanet,
            num_classes=train_generator.num_classes(),
            weights=weights,
            multi_gpu=args.multi_gpu,
            freeze_backbone=args.freeze_backbone,
            lr=args.lr,
            config=args.config,
            regularisation=args.regularisation
        )

    # print model summary
    #print(model.summary())

    # this lets the generator compute backbone layer shapes using the actual backbone model
    if 'vgg' in args.backbone or 'densenet' in args.backbone:
        train_generator.compute_shapes = make_shapes_callback(model)
        if validation_generator:
            validation_generator.compute_shapes = train_generator.compute_shapes

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

    if not args.compute_val_loss:
        validation_generator = None

    # start training
    return training_model.fit_generator(
        generator=train_generator,
        steps_per_epoch=args.steps,
        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,
        initial_epoch=args.initial_epoch
    )
Exemplo n.º 13
0

if __name__ == '__main__':
    """Entry point."""
    parser = argparse.ArgumentParser(description='Tensorflow inference server')
    parser.add_argument('tensorflow_model_path', help='path to frozen model')
    parser.add_argument('--app_port', default=80, help='server port')
    parser.add_argument('--gpu', help='which GPU to use')
    args = parser.parse_args()

    check_keras_version()
    check_tf_version()

    # optionally choose specific GPU
    if args.gpu:
        setup_gpu(args.gpu)

    LOGGER.info(f'loading {args.tensorflow_model_path}')
    model = models.load_model(args.tensorflow_model_path,
                              backbone_name='resnet50')

    quad_offset_queue = queue.Queue()
    quad_file_path_queue = queue.Queue()

    # make clipper workers
    for _ in range(multiprocessing.cpu_count()):
        quad_processor_worker_thread = threading.Thread(
            target=quad_processor,
            args=(quad_offset_queue, quad_file_path_queue))
        quad_processor_worker_thread.daemon = True
        quad_processor_worker_thread.start()
Exemplo n.º 14
0
def main():
    ax = plt.gca()
    #ax.set_aspect(20)

    score_threshold = [0.3]  #, 0.4, 0.5, 0.6, 0.7, 0.8]
    setup_gpu(0)
    classes = Path(
        '/data/students_home/fschipani/thesis/MSc-Thesis-PJ/Dataset/KAIST_MPD/class_name_to_ID_CARS.csv'
    )
    annotations = Path(
        '/data/students_home/fschipani/thesis/MSc-Thesis-PJ/Dataset/KAIST_MPD/fine_tuning_kaist_cars/ds/test_w_people.csv'
    )
    save_path = Path(
        '/data/students_home/fschipani/thesis/MSc-Thesis-PJ/Dataset/tests_manual_annotations_08_cars/'
    )
    generator = create_generator(annotations, classes)
    #weight = '/data/students_home/fschipani/thesis/MSc-Thesis-PJ/Dataset/weights/manual_annotation_08.h5'
    weight_path_ra = '/data/students_home/fschipani/rand_augment/snapshots_ra_scratch'
    weight_path = '/data/students_home/fschipani/thesis/MSc-Thesis-PJ/Dataset/snapshots/'

    # optionally load anchor parameters
    anchor_params = None
    dataframe = pd.DataFrame(columns=[
        'epoch', 'score_threshold', 'person_instances', 'cyclist_instances',
        'cars_instances', 'map_person', 'map_cyclist', 'map_cars',
        'weighted_map', 'map', 'false_positives', 'true_positives', 'recall',
        'precision'
    ])
    for weight in glob.iglob(weight_path + '/*.h5'):
        print(weight)
        for threshold in [0.3]:
            os.makedirs(save_path.joinpath(str(threshold * 100)),
                        exist_ok=True)
            name_folder = weight.replace('/weights', '').replace('.h5', '')
            print(weight)
            model = models.load_model(weight, backbone_name='resnet50')
            model = models.convert_model(model, anchor_params=anchor_params)
            os.makedirs(save_path.joinpath(str(threshold *
                                               100)).joinpath(name_folder),
                        exist_ok=True)
            average_precisions, other_metrics = evaluate(
                generator,
                model,
                iou_threshold=0.5,
                score_threshold=threshold,
                max_detections=100,
                save_path=save_path.joinpath(str(threshold *
                                                 100)).joinpath(name_folder))
            total_instances = []
            precisions = []
            for label, (average_precision,
                        num_annotations) in average_precisions.items():
                print(
                    '{:.0f} instances of class'.format(num_annotations),
                    generator.label_to_name(label),
                    'with average precision: {:.4f}'.format(average_precision))
                total_instances.append(num_annotations)
                precisions.append(average_precision)
            if sum(total_instances) == 0:
                print('No test instances found.')
                return
            values = {
                'epoch':
                int(
                    weight.replace('resnet50_csv_', '').replace('.h5', '').
                    replace(
                        '/data/students_home/fschipani/thesis/MSc-Thesis-PJ/Dataset/snapshots/',
                        '')),
                'score_threshold':
                threshold,
                'person_instances':
                int(average_precisions[0][1]),
                'cyclist_instances':
                int(average_precisions[1][1]),
                'cars_instances':
                int(average_precisions[2][1]),
                'map_person':
                average_precisions[0][0],
                'map_cyclist':
                average_precisions[1][0],
                'map_cars':
                average_precisions[2][0],
                'weighted_map':
                (sum([a * b for a, b in zip(total_instances, precisions)]) /
                 sum(total_instances)),
                'map': (sum(precisions) / sum(x > 0 for x in total_instances)),
                'false_positives':
                pd.Series(other_metrics[0]),
                'true_positives':
                pd.Series(other_metrics[1]),
                'recall':
                pd.Series(other_metrics[2]),
                'precision':
                pd.Series(other_metrics[3])
            }
            dataframe = dataframe.append(values, ignore_index=True)
            K.clear_session()
            #plt.plot(other_metrics[2][2], other_metrics[3][2], label = str(threshold)) #[recall, precision][class] usually class: 0->person 1->cyclist 2->cars
    dataframe.to_csv('./spero_sia_l_ultimo.csv')
Exemplo n.º 15
0
# import keras_retinanet
from keras_retinanet import models
from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image
from keras_retinanet.utils.visualization import draw_box, draw_caption
from keras_retinanet.utils.colors import label_color
from keras_retinanet.utils.gpu import setup_gpu
from keras_retinanet.losses import smooth_l1, focal
from keras_retinanet.preprocessing import csv_generator
from keras_retinanet.utils.eval import evaluate

# other imports
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt

setup_gpu(1)

# In[2]:

default_params = {'N': 3, 'M': 10}
dataset_path = Path(
    '/data/students_home/fschipani/thesis/MSc-Thesis-PJ/Dataset/KAIST_MPD/fine_tuning_kaist_cars/ds'
)
train_annotations = dataset_path.joinpath('train_no_people.csv')
test_annotations = dataset_path.joinpath('test_w_people.csv')
classes = Path(
    '/data/students_home/fschipani/thesis/MSc-Thesis-PJ/Dataset/KAIST_MPD/class_name_to_ID_CARS.csv'
)


def create_train_generator(N, M):
def main():
    """Entry point."""
    args = parse_args(sys.argv[1:])
    check_keras_version()
    check_tf_version()

    # optionally choose specific GPU
    if args.gpu:
        setup_gpu(args.gpu)

    model = models.load_model(args.model, backbone_name='resnet50')

    for dir_path in [
            WORKSPACE_DIR, ECOSHARD_DIR, CHURN_DIR, DETECTED_DAM_IMAGERY_DIR]:
        try:
            os.makedirs(dir_path)
        except OSError:
            pass

    task_graph = taskgraph.TaskGraph(
        WORKSPACE_DIR, -1, 5.0)

    country_borders_vector_path = os.path.join(
        ECOSHARD_DIR, os.path.basename(COUNTRY_BORDER_VECTOR_URI))
    country_borders_dl_task = task_graph.add_task(
        func=copy_from_gs,
        args=(
            COUNTRY_BORDER_VECTOR_URI,
            country_borders_vector_path),
        task_name='download country borders vector',
        target_path_list=[country_borders_vector_path])

    planet_grid_id_to_quad_path = os.path.join(
        ECOSHARD_DIR, os.path.basename(PLANET_GRID_ID_TO_QUAD_URI))
    country_borders_dl_task = task_graph.add_task(
        func=copy_from_gs,
        args=(
            PLANET_GRID_ID_TO_QUAD_URI,
            planet_grid_id_to_quad_path),
        task_name='download planet grid to quad id db',
        target_path_list=[planet_grid_id_to_quad_path])

    if not os.path.exists(WORK_DATABASE_PATH):
        task_graph.add_task(
            func=create_work_database,
            args=(WORK_DATABASE_PATH, country_borders_vector_path),
            hash_target_files=False,
            target_path_list=[country_borders_vector_path, WORK_DATABASE_PATH],
            dependent_task_list=[country_borders_dl_task],
            task_name='create status database')

    # find the most recent mosaic we can use
    with open(PLANET_API_KEY_FILE, 'r') as planet_api_key_file:
        planet_api_key = planet_api_key_file.read().rstrip()

    quad_queue = multiprocessing.Queue(10)
    grid_done_queue = multiprocessing.Queue()
    work_queue = multiprocessing.Queue(10)
    inference_queue = multiprocessing.Queue(10)
    postprocessing_queue = multiprocessing.Queue(10)

    grid_done_worker_thread = threading.Thread(
        target=grid_done_worker,
        args=(WORK_DATABASE_PATH, grid_done_queue))
    grid_done_worker_thread.start()

    process_quad_worker_list = []
    for _ in range(1):
        process_quad_worker_process = threading.Thread(
            target=process_quad_worker,
            args=(planet_api_key, quad_queue, work_queue, grid_done_queue))
        process_quad_worker_process.start()
        process_quad_worker_list.append(process_quad_worker_process)

    detect_dams_worker_list = []
    for _ in range(1):
        detect_dams_worker_process = threading.Thread(
            target=detect_dams_worker,
            args=(work_queue, inference_queue))
        detect_dams_worker_process.start()
        detect_dams_worker_list.append(detect_dams_worker_process)

    inference_worker_thread = threading.Thread(
        target=inference_worker,
        args=(model, inference_queue, postprocessing_queue))
    inference_worker_thread.start()

    postprocessing_worker_list = []
    for _ in range(1):
        postprocessing_worker_process = threading.Thread(
            target=postprocessing_worker,
            args=(
                postprocessing_queue, country_borders_vector_path,
                WORK_DATABASE_PATH, grid_done_queue))
        postprocessing_worker_process.start()
        postprocessing_worker_list.append(postprocessing_worker_process)

    # iterate through country priorities and do '' -- all, last.
    for country_iso3 in COUNTRY_PRIORITIES + ['']:
        LOGGER.debug('***** Process country %s', country_iso3)
        work_grid_list = _execute_sqlite(
            '''
            SELECT grid_id
            FROM work_status
            WHERE country_list LIKE ? AND processed=0
            ''', WORK_DATABASE_PATH, argument_list=['%%%s%%' % country_iso3],
            fetch='all')
        for (grid_id,) in work_grid_list:
            quad_id_payload = _execute_sqlite(
                '''
                SELECT quad_id_list
                FROM grid_id_to_quad_id
                WHERE grid_id=?
                ''', planet_grid_id_to_quad_path, argument_list=[grid_id],
                fetch='one')
            if not quad_id_payload:
                continue
            quad_id_list = quad_id_payload[0].split(',')
            # make the score really high
            grid_done_queue.put((grid_id, 100000))
            for quad_id in quad_id_list:
                quad_queue.put((grid_id, quad_id))
            grid_done_queue.put((grid_id, -100000))

    LOGGER.debug('waiting for quad workers to stop')
    quad_queue.put('STOP')
    for quad_worker_process in process_quad_worker_list:
        quad_worker_process.join()

    LOGGER.debug('waiting for detect dams to stop')
    work_queue.put('STOP')
    for detect_dams_process in detect_dams_worker_list:
        detect_dams_process.join()

    LOGGER.debug('waiting for inference worker to stop')
    inference_queue.put('STOP')
    inference_worker_thread.join()

    LOGGER.debug('waiting for postprocessing worker to stop')
    postprocessing_queue.put('STOP')
    for postprocessing_worker_process in postprocessing_worker_list:
        postprocessing_worker_process.join()

    grid_done_queue.put('STOP')
    grid_done_worker_thread.join()

    task_graph.join()
    task_graph.close()
    return
Exemplo n.º 17
0
def main(args=None, model_filename=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
        args = parse_args(args)

    # make sure keras and tensorflow are the minimum required version
    check_keras_version()
    check_tf_version()

    # optionally choose specific GPU
    if args.gpu:
        setup_gpu(args.gpu)

    # make save path if it doesn't exist
    if args.save_path is not None and not os.path.exists(args.save_path):
        os.makedirs(args.save_path)

    # optionally load config parameters
    if args.config:
        args.config = read_config_file(args.config)

    # create the generator
    backbone = models.backbone(args.backbone)
    generator = create_generator(args, backbone.preprocess_image)

    # optionally load anchor parameters
    anchor_params = None
    if args.config and 'anchor_parameters' in args.config:
        anchor_params = parse_anchor_parameters(args.config)

    # load the model
    print('Loading model, this may take a second...')
    if args.continual_learning_model == 'dual_memory':  # Continual learning dual-memory modelling treatment
        base_models = LoadModels(args.historical_snapshots_folder,
                                 args.backbone, args.day_number)
        all_models = []
        for model in base_models:
            generator.compute_shapes = make_shapes_callback(model)
            if args.convert_model:
                model = models.convert_model(model,
                                             anchor_params=anchor_params)
            all_models.append(model)

        (average_precisions, inference_time, detections_per_model,
         final_detections) = evaluate_dual_memory_model(
             generator,
             all_models,
             iou_threshold=args.iou_threshold,
             score_threshold=args.score_threshold,
             max_detections=args.max_detections,
             save_path=args.save_path)

        # bbox_savepath given, save bounding box coordinates from dual-memory model predictions:

        if args.bbox_savepath:
            detections_per_model = [[
                [class_predictions.tolist() for class_predictions in image]
                for image in model_predictions
            ] for model_predictions in detections_per_model]
            detections_with_filenames = {
                'final_detections': final_detections,
                'annotations': args.annotations,
                'detections_per_model': detections_per_model
            }
            with open(args.bbox_savepath, 'wt') as outf:
                json.dump(detections_with_filenames, outf)

            print("Finished dual memory model")
            print(average_precisions, inference_time)

    else:
        if model_filename is None:
            model_filename = args.model
        model = models.load_model(model_filename, backbone_name=args.backbone)

        generator.compute_shapes = make_shapes_callback(model)

        # optionally convert the model
        if args.convert_model:
            model = models.convert_model(model, anchor_params=anchor_params)

        # print model summary
        # print(model.summary())

        # start evaluation
        if args.dataset_type == 'coco':
            from ..utils.coco_eval import evaluate_coco
            evaluate_coco(generator, model, args.score_threshold)
        else:
            average_precisions, inference_time = evaluate(
                generator,
                model,
                iou_threshold=args.iou_threshold,
                score_threshold=args.score_threshold,
                max_detections=args.max_detections,
                save_path=args.save_path)

    # print evaluation
    total_instances = []
    precisions = []
    #labels = []
    for label, (average_precision,
                num_annotations) in average_precisions.items():
        print('{:.0f} instances of class'.format(num_annotations),
              generator.label_to_name(label),
              'with average precision: {:.4f}'.format(average_precision))
        #labels.append(label)
        total_instances.append(num_annotations)
        precisions.append(average_precision)

    if sum(total_instances) == 0:
        print('No test instances found.')
        return

    print('Inference time for {:.0f} images: {:.4f}'.format(
        generator.size(), inference_time))

    print('mAP using the weighted average of precisions among classes: {:.4f}'.
          format(
              sum([a * b for a, b in zip(total_instances, precisions)]) /
              sum(total_instances)))
    print('mAP: {:.4f}'.format(
        sum(precisions) / sum(x > 0 for x in total_instances)))

    #print(labels)
    print(precisions)
    print(total_instances)

    # Save mAP and other accuracy statistics to mAP_savepath:

    mAP = sum(precisions) / sum(x > 0 for x in total_instances)
    date = datetime.now().strftime("%Y%m%d%H%M")
    with open(args.mAP_savepath, 'a') as outf:
        outf.write(
            f"{date}, {mAP}, {precisions}, {total_instances}, {model_filename}, {args.continual_learning_model}"
            + "\n")
    return mAP