def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 0
    weighted_bifpn = False
    model_path = 'checkpoints/flir_59_0.1409_0.6001.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    # coco classes
    classes = {
        value['id'] - 1: value['name']
        for value in json.load(open('coco_90.json', 'r')).values()
    }
    num_classes = 3
    score_threshold = 0.01
    colors = [
        np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
    ]
    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=num_classes,
                            score_threshold=score_threshold)
    model.load_weights(model_path, by_name=True)

    video_path = r'G:\datasets\iray\cap\20200515_100016.avi'
    video_path = r'G:\datasets\iray\cap\20200515_100016.avi'
    video_path = r'G:\projects\20200513_102922.avi'
    cap = cv2.VideoCapture(video_path)

    while True:
        ret, image = cap.read()
        if not ret:
            break
        src_image = image.copy()
        # BGR -> RGB
        image = image[:, :, ::-1]
        h, w = image.shape[:2]

        image, scale = preprocess_image(image, image_size=image_size)
        # run network
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(
            [np.expand_dims(image, axis=0)])
        boxes, scores, labels = np.squeeze(boxes), np.squeeze(
            scores), np.squeeze(labels)
        print(time.time() - start)
        boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

        # select indices which have a score above the threshold
        indices = np.where(scores[:] > score_threshold)[0]

        # select those detections
        boxes = boxes[indices]
        labels = labels[indices]

        draw_boxes(src_image, boxes, scores, labels, colors, classes)

        cv2.namedWindow('image', cv2.WINDOW_NORMAL)
        cv2.imshow('image', src_image)
        cv2.waitKey(0)
Exemple #2
0
    def __init__(self, dataset_dir, output_dir):
        BEST_MODEL_DIR = "best_model"

        best_model_dir = os.path.join(dataset_dir, BEST_MODEL_DIR)
        print("=== best_model_dir {}".format(best_model_dir))

        phi = 0

        weight_h5 = ""
        for i in [6, 5, 4, 3, 2, 1, 0]:
            weight_h5 = best_model_dir + "/" + "weight_d" + str(i) + ".h5"
            #print("=== weight_h5 {}".format(weight_h5))

            if os.path.exists(weight_h5):
                phi = i
                print("=== EfficientDet phi {}".format(phi))
                print("=== Found weight file {}".format(weight_h5))
                break

        if not os.path.exists(weight_h5):
            raise Exception("No found weight file {}".format(weight_h5))

        #self.dataset_dir = dataset_dir
        self.annotation_file = self.find_annotation_file(dataset_dir)

        self.output_dir = output_dir
        print("=== EfficienetDetObjectDetector weight {}".format(weight_h5))

        weighted_bifpn = True
        image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
        print("=== phi {}".format(phi))

        self.image_size = image_sizes[phi]

        print("=== Image size {}".format(self.image_size))
        self.score_threshold = 0.4

        print("=== Score threshold {}".format(self.score_threshold))

        self.classes = self.get_classes(self.annotation_file)
        print("=== classes {}".format(self.classes))

        self.num_classes = len(self.classes)
        print("=== num_classes {}", format(self.num_classes))

        self._, self.model = efficientdet(phi=phi,
                                          weighted_bifpn=True,
                                          num_classes=self.num_classes,
                                          freeze_bn=True,
                                          detect_quadrangle=False,
                                          score_threshold=self.score_threshold)

        self.model.load_weights(weight_h5, by_name=True)
        print("=== Loaded weight file {}".format(weight_h5))
        self.colors = [
            np.random.randint(0, 256, 3).tolist()
            for _ in range(self.num_classes)
        ]
def main(image_path):
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 1
    weighted_bifpn = True
    model_path = 'efficientdet-d1.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    # coco classes
    classes = {
        value['id'] - 1: value['name']
        for value in json.load(open('coco_90.json', 'r')).values()
    }
    num_classes = 90
    score_threshold = 0.3
    colors = [
        np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
    ]
    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=num_classes,
                            score_threshold=score_threshold)
    model.load_weights(model_path, by_name=True)

    #for image_path in glob.glob('datasets/VOC2007/JPEGImages/*.jpg'):
    try:
        image = cv2.imread(image_path)
        src_image = image.copy()
        # BGR -> RGB
        image = image[:, :, ::-1]
        h, w = image.shape[:2]

        image, scale = preprocess_image(image, image_size=image_size)
        # run network
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(
            [np.expand_dims(image, axis=0)])
        boxes, scores, labels = np.squeeze(boxes), np.squeeze(
            scores), np.squeeze(labels)
        print(time.time() - start)
        boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

        # select indices which have a score above the threshold
        indices = np.where(scores[:] > score_threshold)[0]

        # select those detections
        boxes = boxes[indices]
        labels = labels[indices]

        draw_boxes(src_image, boxes, scores, labels, colors, classes)

        cv2.namedWindow('image', cv2.WINDOW_NORMAL)
        cv2.imshow('image', src_image)
        cv2.waitKey(0)

    except Exception as ex:
        print(ex)
def get_model(phi, weighted_bifpn, num_classes, score_threshold):
    model_path = 'efficientdet-d' + str(phi) + '.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    
    _, model = efficientdet(phi=phi,
                        weighted_bifpn=weighted_bifpn,
                        num_classes=num_classes,
                        score_threshold=score_threshold)
    return model, model_path
Exemple #5
0
def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'
    
    phi = 1
    weighted_bifpn = False
    model_path = 'checkpoints/2019-12-03/pascal_05_0.6283_1.1975_0.8029.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    classes = [
        'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair',
        'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train',
        'tvmonitor',
    ]
    num_classes = len(classes)
    score_threshold = 0.5
    colors = [np.random.randint(0, 256, 3).tolist() for i in range(num_classes)]
    model, prediction_model = efficientdet(phi=phi,
                                           weighted_bifpn=weighted_bifpn,
                                           num_classes=num_classes,
                                           score_threshold=score_threshold)
    prediction_model.load_weights(model_path, by_name=True)
    
    video_path = 'datasets/video.mp4'
    cap = cv2.VideoCapture(video_path)

    while True:
        ret, frame = cap.read()
        if not ret:
            break
        h, w = frame.shape[:2]
        image, scale, offset_h, offset_w = preprocess_image(frame, image_size=image_size)
        
        anchors = anchors_for_shape((image_size, image_size))
        
        boxes_batch, scores_batch, labels_batch = prediction_model.predict_on_batch([np.expand_dims(image, axis=0),
                                                                                     np.expand_dims(anchors, axis=0)])
        
        for i, (boxes, scores, labels) in enumerate(zip(boxes_batch, scores_batch, labels_batch)):
            boxes = post_process_boxes(boxes=boxes,
                                       scale=scale,
                                       offset_h=offset_h,
                                       offset_w=offset_w,
                                       height=h,
                                       width=w)

            indices = np.where(scores[:] > score_threshold)[0]
            boxes = boxes[indices]
            labels = labels[indices]
            
            draw_boxes(frame, boxes, scores, labels, colors, classes)
            
            cv2.imshow('image', frame)
            cv2.waitKey(1)
Exemple #6
0
def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 0
    weighted_bifpn = True
    # model_path = 'efficientdet-d1.h5'
    # model_path = r"checkpoints\2020-06-23\csv_50_0.0431_0.9146.h5"
    model_path = r"checkpoints\2020-06-23\1\csv_44_0.0448_1.0142.h5"
    # class_path = 'coco_90.json'
    class_path = r"train_car_object\classes.json"
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    # coco classes
    classes = {value['id'] - 1: value['name'] for value in json.load(open(class_path, 'r')).values()}
    num_classes = 4
    score_threshold = 0.5
    colors = [np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)]
    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=num_classes,
                            score_threshold=score_threshold)
    model.load_weights(model_path, by_name=True)

    # for image_path in glob.glob('datasets/VOC2007/JPEGImages/*.jpg'):
    for image_path in glob.glob(r"E:\DATA\@car\car_photo\carplate_test\*.jpg"):
        image = cv2.imread(image_path)
        src_image = image.copy()
        # BGR -> RGB
        image = image[:, :, ::-1]
        h, w = image.shape[:2]

        image, scale = preprocess_image(image, image_size=image_size)
        # run network
        start = time.time()
        boxes, scores, labels = model.predict_on_batch([np.expand_dims(image, axis=0)])
        boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels)
        print(time.time() - start)
        boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

        # select indices which have a score above the threshold
        indices = np.where(scores[:] > score_threshold)[0]

        # select those detections
        boxes = boxes[indices]
        labels = labels[indices]

        draw_boxes(src_image, boxes, scores, labels, colors, classes)

        cv2.namedWindow('image', cv2.WINDOW_NORMAL)
        cv2.imshow('image', src_image)
        cv2.waitKey(0)
Exemple #7
0
def main(args=None):
    if len(sys.argv) is 3:
        model_path = str(sys.argv[1])
        dataset_path = str(sys.argv[2])
    else:
        print(
            "Pass model path and dataset path in respectively as command line argument"
        )
        exit()

    from generators.pascal import PascalVocGenerator
    from model import efficientdet
    import os

    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 4
    weighted_bifpn = False
    common_args = {
        'batch_size': 1,
        'phi': phi,
    }
    test_generator = PascalVocGenerator(dataset_path,
                                        'test',
                                        shuffle_groups=False,
                                        skip_truncated=False,
                                        skip_difficult=True,
                                        **common_args)
    model_path = model_path
    input_shape = (test_generator.image_size, test_generator.image_size)
    anchors = test_generator.anchors
    num_classes = test_generator.num_classes()
    model, prediction_model = efficientdet(phi=phi,
                                           num_classes=num_classes,
                                           weighted_bifpn=weighted_bifpn)
    prediction_model.load_weights(model_path, by_name=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))
Exemple #8
0
def main():

    from pprint import pprint

    config = B2Config()

    # pprint(vars(config))
    # exit()

    batch_size = 4
    num_classes = 15
    epochs = 5
    steps_per_epoch = 1000


    parser = Parser(
      config=config,
      batch_size=batch_size,
      num_classes=num_classes) 

    training_model = efficientdet(config, num_classes, weights=None)

    # compile model
    training_model.compile(
        optimizer=Adam(lr=1e-3), 
        loss={
            'regression': smooth_l1(),
            'classification': focal()})

    # print(training_model.summary())

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


    train_dataset_function = parser.get_dataset(filenames='./DATA/train*.tfrecord')

    training_model.fit(
        train_dataset_function, 
        epochs=epochs, 
        steps_per_epoch=steps_per_epoch,)
        # callbacks=callbacks)

    os.makedirs("./checkpoints", exist_ok=True)

    training_model.save("./checkpoints/efficientdetB2_final")
Exemple #9
0
def inf(phi, weighted_bi, num_classes):
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    model_path = 'efdet_model.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    # coco classes
    classes = {
        value['id'] - 1: value['name']
        for value in json.load(open('coco_90.json', 'r')).values()
    }

    score_threshold = 0.3
    colors = [
        np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
    ]

    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bi,
                            num_classes=num_classes,
                            score_threshold=score_threshold)
    model.load_weights(model_path, by_name=True)

    TEST_DIR = 'test_imgs/'

    test_img_list = os.listdir(TEST_DIR)
    for img_t in range(0, len(test_img_list) * 10):
        image = cv2.imread(TEST_DIR +
                           test_img_list[img_t % len(test_img_list)])
        src_image = image.copy()
        # BGR -> RGB
        image = image[:, :, ::-1]
        h, w = image.shape[:2]

        image, scale = preprocess_image(image, image_size=image_size)
        # run network
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(
            [np.expand_dims(image, axis=0)])
        boxes, scores, labels = np.squeeze(boxes), np.squeeze(
            scores), np.squeeze(labels)

        print(time.time() - start)
def main():
    phi = 1
    weighted_bifpn = False
    model_path = 'checkpoints/2019-12-03/pascal_05_0.6283_1.1975_0.8029.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    classes = [
        'aeroplane',
        'bicycle',
        'bird',
        'boat',
        'bottle',
        'bus',
        'car',
        'cat',
        'chair',
        'cow',
        'diningtable',
        'dog',
        'horse',
        'motorbike',
        'person',
        'pottedplant',
        'sheep',
        'sofa',
        'train',
        'tvmonitor',
    ]
    num_classes = len(classes)
    score_threshold = 0.5
    model, prediction_model = efficientdet(phi=phi,
                                           weighted_bifpn=weighted_bifpn,
                                           num_classes=num_classes,
                                           score_threshold=score_threshold)
    prediction_model.load_weights(model_path, by_name=True)

    frozen_graph = freeze_session(
        K.get_session(),
        output_names=[out.op.name for out in prediction_model.outputs])
    tf.train.write_graph(frozen_graph,
                         "./checkpoints/2019-12-03/",
                         "pascal_05.pb",
                         as_text=False)
def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 1
    weighted_bifpn = True
    model_path = 'efficientdet-d4.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    # coco classes
    classes = {
        value['id'] - 1: value['name']
        for value in json.load(open('coco_90.json', 'r')).values()
    }
    num_classes = 90
    score_threshold = 0.3
    colors = [
        np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
    ]
    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=num_classes,
                            score_threshold=score_threshold)
    model.load_weights(model_path, by_name=True)
Exemple #12
0
def main():
    phi = 1
    weighted_bifpn = False
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    score_threshold = 0.9
    nms_threshold = 0.5

    common_args = {
        'batch_size': 1,
        'phi': phi,
        'detect_ship': True,
        'detect_quadrangle': True,
    }

    #ship_path = '/home/minjun/Jupyter/Ship_Detection/Data/train_tfrecorder/train_data2.tfrecords'
    val_dir = '/home/minjun/Jupyter/Ship_Detection/Data/tfrecorder/val_data_1280.tfrecords'
    model_path = 'checkpoints/test4/ship_95_0.2889_0.3075.h5'
    print(model_path)

    #    train_generator = ShipGenerator(
    #        'train/ship_detection',
    #        ship_path,
    #        gen_type='train',
    #        ratio = ratio,
    #        group_method='none',
    #        **common_args
    #    )

    validation_generator = ShipGenerator('val/ship_detection',
                                         val_dir,
                                         gen_type='val',
                                         ratio=1,
                                         shuffle_groups=False,
                                         selection=False,
                                         **common_args)
    num_classes = validation_generator.num_classes()
    num_anchors = validation_generator.num_anchors
    anchor_parameters = AnchorParameters.ship

    model, prediction_model = efficientdet(phi,
                                           num_classes=num_classes,
                                           num_anchors=num_anchors,
                                           freeze_bn=True,
                                           detect_quadrangle=True,
                                           anchor_parameters=anchor_parameters,
                                           score_threshold=score_threshold,
                                           nms_threshold=0.3)
    prediction_model.load_weights(model_path, by_name=True)

    #    print(evaluate(generator=train_generator,
    #             model = prediction_model,
    #             score_threshold=0.01,
    #             max_detections=100,
    #             visualize=False,
    #            )
    #         )
    if False:
        for i in np.arange(0.2, 1, 0.05):
            print(
                evaluate(
                    generator=validation_generator,
                    model=prediction_model,
                    score_threshold=score_threshold,
                    max_detections=300,
                    visualize=False,
                    nms_threshold=i,
                ))
    print(
        evaluate(
            generator=validation_generator,
            model=prediction_model,
            score_threshold=score_threshold,
            max_detections=300,
            visualize=True,
            nms_threshold=nms_threshold,
        ))
def main(opt):
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = opt.phi
    weighted_bifpn = False
    model_path = opt.model
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]

    # Dhaka-ai classes
    dhaka_ai_classes = get_class_names(opt.class_names)
    dhaka_ai_num_classes = len(dhaka_ai_classes)

    score_threshold = opt.conf_thres

    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=dhaka_ai_num_classes,
                            score_threshold=score_threshold)
    model.load_weights(model_path, by_name=True)

    with open('submission_files/arafat_efficientdet-result_conf-{}_IOUthr-{}_{}_ac-0.0.csv'.format(opt.conf_thres, opt.iou_thres, time.strftime("%Y-%m-%d_%H-%M-%S")), mode='w') as result_file:
        fieldnames = ['image_id', 'class', 'score', 'xmin', 'ymin', 'xmax', 'ymax', 'width', 'height']
        result_file_writer = csv.writer(result_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        result_file_writer.writerow(fieldnames)
        for image_path in glob.glob(os.path.join(opt.source_dir, "*.jpg")):
            image = cv2.imread(image_path)
            assert image is not None, "Image cat not be read, path: "+image_path

            # BGR -> RGB
            image = image[:, :, ::-1]
            h, w = image.shape[:2]

            image, scale = preprocess_image(image, image_size=image_size)
            # run network
            start = time.time()
            boxes, scores, labels = model.predict_on_batch([np.expand_dims(image, axis=0)])
            boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels)
            print(time.time() - start)
            boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)
            
            selected_indices = tf.image.non_max_suppression(
                boxes, scores, 120, iou_threshold=opt.iou_thres, score_threshold=opt.conf_thres)
            selected_boxes = tf.gather(boxes, selected_indices)
            selected_labels = tf.gather(labels, selected_indices)
            selected_boxes = tf.Session().run(selected_boxes)
            selected_labels = tf.Session().run(selected_labels)

            for b, l, s in zip(selected_boxes, selected_labels, scores):
                class_id = int(l)
                class_name = dhaka_ai_classes[class_id]
            
                xmin, ymin, xmax, ymax = list(map(int, b))

                if xmax > w:
                    xmax = w
                if xmin < 0:
                    xmin = 0
                if ymax > h:
                    ymax = h
                if ymin < 0:
                    ymin = 0
                score = '{:.6f}'.format(s)
                check_badbox(image_path, h, w,
                             xmin, ymin, xmax, ymax)

                result_file_writer.writerow([os.path.basename(image_path), class_name, score, xmin, ymin, xmax, ymax, image_size, image_size])
#colors = np.random.randint(0, 255, size=(num_classes, 3), dtype="uint8")

################################################################
# model
phi = 1
#phi = 2
#phi = 3
#phi = 4

model_path = 'efficientdet-d' + str(phi) + '.h5'  #'d1.h5'
weighted_bifpn = True
image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
image_size = image_sizes[phi]

_, model = efficientdet(phi=phi,
                        weighted_bifpn=weighted_bifpn,
                        num_classes=num_classes,
                        score_threshold=score_threshold)
model.load_weights(model_path, by_name=True)

########################################################################

# initialize the video stream, pointer to output video file, and
# frame dimensions
vs = cv2.VideoCapture(input_video)
writer = None
(w, h) = (None, None)

# try to determine the total number of frames in the video file
try:
    prop = cv2.cv.CV_CAP_PROP_FRAME_COUNT if imutils.is_cv2() \
     else cv2.CAP_PROP_FRAME_COUNT
Exemple #15
0
    common_args = {
        'batch_size': 1,
        'phi': 0,
    }
    test_generator = PascalVocGenerator('datasets/voc_test/VOC2007',
                                        'test',
                                        shuffle_groups=False,
                                        skip_truncated=False,
                                        skip_difficult=True,
                                        **common_args)
    model_path = 'checkpoints/2019-11-27/pascal_01_1451.1366_880.4981.h5'
    input_shape = (test_generator.image_size, test_generator.image_size)
    anchors = test_generator.anchors
    num_classes = test_generator.num_classes()
    model, prediction_model = efficientdet(phi=0, 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)
Exemple #16
0
                    tag = '{}. {}'.format(index + 1, coco_tag[index])
                    tf.summary.scalar(tag, result, epoch)


if __name__ == '__main__':
    from model import efficientdet
    import os
    from generators.coco import CocoGenerator

    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 2
    weighted_bifpn = True
    model_path = 'efficientdet-d2.h5'
    common_args = {
        'batch_size': 1,
        'phi': phi,
    }

    test_generator = CocoGenerator('datasets/coco',
                                   'test-dev2017',
                                   shuffle_groups=False,
                                   **common_args)
    num_classes = test_generator.num_classes()
    model, prediction_model = efficientdet(phi=phi,
                                           num_classes=num_classes,
                                           weighted_bifpn=weighted_bifpn,
                                           score_threshold=0.01)
    prediction_model.load_weights(model_path, by_name=True)
    evaluate(test_generator, prediction_model, threshold=0.01)
def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 1
    weighted_bifpn = True
    model_path = 'efficientdet-d1.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    # coco classes
    classes = {
        value['id'] - 1: value['name']
        for value in json.load(open('coco_90.json', 'r')).values()
    }
    num_classes = 90
    score_threshold = 0.5
    colors = [
        np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
    ]
    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=num_classes,
                            score_threshold=score_threshold)
    model.load_weights(model_path, by_name=True)

    video_path = 'test/video.mp4'
    cap = cv2.VideoCapture(video_path)

    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    codec = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('./out.mp4', codec, fps, (width, height))
    times = []

    while True:
        ret, image = cap.read()
        if not ret:
            break
        src_image = image.copy()
        # BGR -> RGB
        image = image[:, :, ::-1]
        h, w = image.shape[:2]

        image, scale = preprocess_image(image, image_size=image_size)
        # run network
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(
            [np.expand_dims(image, axis=0)])
        boxes, scores, labels = np.squeeze(boxes), np.squeeze(
            scores), np.squeeze(labels)
        end = time.time()
        print(end - start)
        boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

        #calculating fps
        times.append(end - start)
        times = times[-20:]
        ms = sum(times) / len(times) * 1000
        fps = 1000 / ms

        # select indices which have a score above the threshold
        indices = np.where(scores[:] > score_threshold)[0]

        # select those detections
        boxes = boxes[indices]
        labels = labels[indices]

        draw_boxes(src_image, boxes, scores, labels, colors, classes)
        src_image = cv2.putText(src_image, "Time: {:.1f}FPS".format(fps),
                                (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1,
                                (0, 0, 255), 2)
        out.write(src_image)
Exemple #18
0
def capture(app, mail):
    save_path = "RealTimeTest/"
    GPU = '0'
    Model_path = 'efficientdet-d0.h5'
    phi = 0

    json_dict = {}
    json_dict["boundingbox"] = []

    os.environ['CUDA_VISIBLE_DEVICES'] = GPU

    weighted_bifpn = True
    model_path = Model_path
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    # coco classes
    classes = {value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values()}
    num_classes = 90
    score_threshold = 0.3
    colors = [np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)]
    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=num_classes,
                            score_threshold=score_threshold)
    
    model.load_weights(model_path, by_name=True)
    try:
        os.makedirs(save_path)
    except:
        print(save_path, "is aleardy exist")
        pass

    start = time.time()
    mon = {'top': 297, 'left': 224, 'width': 590, 'height': 450}
    sct = mss()
    image_num = 0
    before_object_num = 0
    while True:
        with mss() as sct:
            sct.get_pixels(mon)
            image = np.array(Image.frombuffer('RGB', (sct.width, sct.height), sct.image))
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

            # # TODO: np_img 에다가 prediction 추가할 것.
            src_image = image.copy()
            h, w = image.shape[:2]

            image, scale = preprocess_image(image, image_size=image_size)
            # run network
            boxes, scores, labels = model.predict_on_batch([np.expand_dims(image, axis=0)])
            boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels)
            
            boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

            # select indices which have a score above the threshold
            indices = np.where(scores[:] > score_threshold)[0]

            # select those detections
            boxes = boxes[indices]
            labels = labels[indices]
            # file_name = image_path.split('/')[-1]
            # cv2.imwrite('prediction.png', src_image)

            temp_dict = {}
            temp_dict["box"] = boxes[indices].tolist()
            temp_dict["label"] = labels[indices].tolist()
            temp_dict["label_name"] = [classes[int(i)] for i in labels]
            # temp_dict["image_name"] = file_name
            json_dict["boundingbox"].append(copy.deepcopy(temp_dict))

            draw_boxes(src_image, boxes, scores, labels, colors, classes)

            np_img = np.array(src_image)
            encode_return_code, image_buffer = cv2.imencode('.jpg', np_img)
            io_buf = io.BytesIO(image_buffer)

            yield (b'--frame\r\n'
                   b'Content-Type: image/png\r\n\r\n' + io_buf.read() + b'\r\n')
def load_efficient_det(config, LOCAL_ANNOTATIONS_PATH, LOCAL_ROOT_PATH,
                       LOCAL_CLASSES_PATH, LOCAL_VALIDATIONS_PATH,
                       LOCAL_LOGS_PATH, LOCAL_SNAPSHOTS_PATH):

    common_args = {
        'phi': config['phi'],
        'detect_text': config['detect_text'],
        'detect_quadrangle': config['detect_quadrangle']
    }

    # create random transform generator for augmenting training data
    if config['random_transform']:
        misc_effect = MiscEffect()
        visual_effect = VisualEffect()
    else:
        misc_effect = None
        visual_effect = None

    annotations_df = pd.read_csv(LOCAL_ANNOTATIONS_PATH, header=None)
    # stratified sampling
    N = int(len(annotations_df) * 0.15)
    evaluation_df = annotations_df.groupby(
        5, group_keys=False).apply(lambda x: x.sample(
            int(np.rint(N * len(x) / len(annotations_df))))).sample(frac=1)
    evaluation_path = f'{LOCAL_ROOT_PATH}/evaluation.csv'
    evaluation_df.to_csv(evaluation_path, index=False, header=None)

    config['steps_per_epoch'] = annotations_df.iloc[:, 0].nunique(
    ) / config['batch_size']

    train_generator = CSVGenerator(LOCAL_ANNOTATIONS_PATH,
                                   LOCAL_CLASSES_PATH,
                                   batch_size=config['batch_size'],
                                   misc_effect=misc_effect,
                                   visual_effect=visual_effect,
                                   **common_args)
    if config['train_evaluation']:
        evaluation_generator = CSVGenerator(evaluation_path,
                                            LOCAL_CLASSES_PATH,
                                            batch_size=config['batch_size'],
                                            misc_effect=misc_effect,
                                            visual_effect=visual_effect,
                                            **common_args)
    else:
        evaluation_generator = None
    if config['validation']:
        validation_generator = CSVGenerator(LOCAL_VALIDATIONS_PATH,
                                            LOCAL_CLASSES_PATH,
                                            batch_size=config['batch_size'],
                                            misc_effect=misc_effect,
                                            visual_effect=visual_effect,
                                            **common_args)
    else:
        validation_generator = None
    num_classes = train_generator.num_classes()
    num_anchors = train_generator.num_anchors

    model, prediction_model = efficientdet(
        config['phi'],
        num_classes=num_classes,
        num_anchors=num_anchors,
        weighted_bifpn=config['weighted_bifpn'],
        freeze_bn=config['freeze_bn'],
        detect_quadrangle=config['detect_quadrangle'])

    # freeze backbone layers
    if config['freeze_backbone']:
        # 227, 329, 329, 374, 464, 566, 656
        for i in range(1, [227, 329, 329, 374, 464, 566, 656][config['phi']]):
            model.layers[i].trainable = False
    # optionally choose specific GPU
    gpu = config['gpu']
    device = gpu.split(':')[0]
    if gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = device
    if gpu and len(gpu.split(':')) > 1:
        gpus = gpu.split(':')[1]
        model = tf.keras.utils.multi_gpu_model(model,
                                               gpus=list(
                                                   map(int, gpus.split(','))))

    if config['snapshot'] == 'imagenet':
        model_name = 'efficientnet-b{}'.format(config['phi'])
        file_name = '{}_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5'.format(
            model_name)
        file_hash = WEIGHTS_HASHES[model_name][1]
        weights_path = tf.keras.utils.get_file(file_name,
                                               BASE_WEIGHTS_PATH + file_name,
                                               cache_subdir='models',
                                               file_hash=file_hash)
        model.load_weights(weights_path, by_name=True)
    elif config['snapshot']:
        print('Loading model, this may take a second...')
        model.load_weights(config['snapshot'], by_name=True)

    return (model, prediction_model, train_generator, evaluation_generator,
            validation_generator, config)
def main():
    parser = argparse.ArgumentParser()
    
    model_path = parser.add_argument('--model_path', required = True)
    GPU = parser.add_argument('--GPU', default = '0')
#    image_path = parser.add_argument('--image_path', required = True)
    save_path = parser.add_argument('--save_path', required = True)
    json_path = parser.add_argument('--json_path', default = 'default_path_json.json')
    image_size = parser.add_argument('--image_size')
#    file_format = parser.add_argument('--file_format', default = 'jpeg')
    score_threshold = parser.add_argument('--score_threshold', default = 0.3)
    phi = parser.add_argument('--phi', default = 0, type = int)
    args = parser.parse_args()

    json_dict = {}
    json_dict["boundingbox"] = []

    os.environ['CUDA_VISIBLE_DEVICES'] = args.GPU
    # print(args.image_path + '/*.' + args.file_format)
    # file_lists = glob.glob(args.image_path + '/*.' + args.file_format)
    # length = len(file_lists)
    iteration = 0
    # print(length)

    
    print(args)
    
    weighted_bifpn = True
    model_path = args.model_path
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[args.phi]
    # coco classes
    classes = {value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values()}
    num_classes = 90
    score_threshold = 0.3
    colors = [np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)]
    _, model = efficientdet(phi=args.phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=num_classes,
                            score_threshold=score_threshold)
    
    model.load_weights(model_path, by_name=True)
    print(model_path)
    try:
        os.makedirs(args.save_path)
    except:
        print(args.save_path, "is aleardy exist")
        pass
    start = time.time()
    mon = {'top': 87, 'left': 224, 'width': 590, 'height': 950} # full screen

    # mon = {'top': 297, 'left': 224, 'width': 590, 'height': 450}
    sct = mss()
    while 1:
        sct.get_pixels(mon)
        image = np.array(Image.frombytes('RGB', (sct.width, sct.height), sct.image))
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

        src_image = image.copy()




        image = image[:, :, :]
        h, w = image.shape[:2]

        image, scale = preprocess_image(image, image_size=image_size)
        # run network
        boxes, scores, labels = model.predict_on_batch([np.expand_dims(image, axis=0)])
        boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels)
        
        boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

        # select indices which have a score above the threshold
        indices = np.where(scores[:] > score_threshold)[0]

        # select those detections
        boxes = boxes[indices]
        labels = labels[indices]
        # file_name = image_path.split('/')[-1]
        # cv2.imwrite(args.save_path +'/' + file_name, src_image)

        temp_dict = {}
        temp_dict["box"] = boxes[indices].tolist()
        temp_dict["label"] = labels[indices].tolist()
        temp_dict["label_name"] = [classes[int(i)] for i in labels]
        # temp_dict["image_name"] = file_name
        json_dict["boundingbox"].append(copy.deepcopy(temp_dict))

        draw_boxes(src_image, boxes, scores, labels, colors, classes)


        cv2.imshow('test', src_image)

    
    print("time : ", time.time()-start)
    with open(args.json_path ,'w') as json_data:
        json.dump(json_dict, json_data, indent = 4)
Exemple #21
0
parser.add_argument('--y2', type=int, default=700, help='y2')
opt = parser.parse_args()
x1, y1, x2, y2 = opt.x1, opt.y1, opt.x2, opt.y2
count = 0
phi = 1
weighted_bifpn = True
model_path = 'd1.h5'
image_size = 640
classes = {
    value['id'] - 1: value['name']
    for value in json.load(open('coco_90.json', 'r')).values()
}
n_classes = 90
colors = [np.random.randint(0, 256, 3).tolist() for _ in range(n_classes)]
_, model = efficientdet(phi=1,
                        weighted_bifpn=True,
                        num_classes=n_classes,
                        score_threshold=0.5)
model.load_weights(model_path, by_name=True)

video_path = opt.source
cap = cv2.VideoCapture(video_path)

fr = 0
while True:
    ret, image = cap.read()
    fr = fr + 1
    if not ret:
        break
    img = image.copy()
    # BGR -> RGB
    image = image[:, :, ::-1]
Exemple #22
0
def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 0
    weighted_bifpn = False
    model_path = 'checkpoints/flir_27_0.1779.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    # coco classes
    classes = {
        value['id'] - 1: value['name']
        for value in json.load(open('coco_90.json', 'r')).values()
    }
    num_classes = 3
    score_threshold = 0.8
    colors = [
        np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
    ]
    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=num_classes,
                            score_threshold=score_threshold)
    model.load_weights(model_path, by_name=True)

    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    path = r'G:\datasets\iray_infrad_384\JPEGImages'
    path = r'G:\datasets\iray\200m_404man'
    imgs = [os.path.join(path, x) for x in os.listdir(path)]
    for file in imgs:
        if file.find("Raw") > -1:
            img = np.fromfile(file, np.uint16).reshape((512, 640))
            image = cv2.normalize(img,
                                  dst=None,
                                  alpha=0,
                                  beta=65535,
                                  norm_type=cv2.NORM_MINMAX)
            image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
        elif file.split('.')[-1] in ['jpg', 'jpeg', 'bmp', 'png']:
            image = cv2.imread(file)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        else:
            continue
        src_image = image.copy()
        image = image.astype(np.float32)
        image[..., 0] *= std[0]
        image[..., 1] *= std[1]
        image[..., 2] *= std[2]
        image[..., 0] += mean[0]
        image[..., 1] += mean[1]
        image[..., 2] += mean[2]
        image *= 255.
        # BGR -> RGB
        image = image[:, :, ::-1]
        h, w = image.shape[:2]

        image, scale = preprocess_image(image, image_size=image_size)
        # run network
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(
            [np.expand_dims(image, axis=0)])
        boxes, scores, labels = np.squeeze(boxes), np.squeeze(
            scores), np.squeeze(labels)
        print(time.time() - start)
        boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

        # select indices which have a score above the threshold
        indices = np.where(scores[:] > score_threshold)[0]

        # select those detections
        boxes = boxes[indices]
        labels = labels[indices]

        draw_boxes(src_image, boxes, scores, labels, colors, classes)

        cv2.namedWindow('image', cv2.WINDOW_NORMAL)
        cv2.imshow('image', src_image)
        cv2.waitKey(0)
Exemple #23
0
def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 1
    weighted_bifpn = False
    model_path = 'checkpoints/2019-12-03/pascal_05_0.6283_1.1975_0.8029.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    classes = [
        'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat',
        'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person',
        'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'
    ]
    num_classes = len(classes)
    score_threshold = 0.5
    colors = [
        np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
    ]
    model, prediction_model = efficientdet(phi=phi,
                                           weighted_bifpn=weighted_bifpn,
                                           num_classes=num_classes,
                                           score_threshold=score_threshold)
    prediction_model.load_weights(model_path, by_name=True)

    image_path = 'datasets/VOC2007/JPEGImages/000002.jpg'
    image = cv2.imread(image_path)
    src_image = image.copy()
    image = image[:, :, ::-1]
    h, w = image.shape[:2]

    image, scale, offset_h, offset_w = preprocess_image(image,
                                                        image_size=image_size)
    anchors = anchors_for_shape((image_size, image_size))

    # run network
    start = time.time()
    boxes, scores, labels = prediction_model.predict_on_batch(
        [np.expand_dims(image, axis=0),
         np.expand_dims(anchors, axis=0)])
    boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(
        labels)
    print(time.time() - start)
    boxes = post_process_boxes(boxes=boxes,
                               scale=scale,
                               offset_h=offset_h,
                               offset_w=offset_w,
                               height=h,
                               width=w)

    # select indices which have a score above the threshold
    indices = np.where(scores[:] > score_threshold)[0]

    # select those detections
    boxes = boxes[indices]
    labels = labels[indices]

    draw_boxes(src_image, boxes, scores, labels, colors, classes)

    cv2.namedWindow('image', cv2.WINDOW_NORMAL)
    cv2.imshow('image', src_image)
    cv2.waitKey(0)
image_size = image_sizes[phi]
# classes = [
#     'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair',
#     'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor',
# ]
classes = ['text']
num_classes = len(classes)
anchor_parameters = AnchorParameters(
    ratios=(0.25, 0.5, 1., 2.),
    sizes=(16, 32, 64, 128, 256))
score_threshold = 0.4
colors = [np.random.randint(0, 256, 3).tolist() for i in range(num_classes)]
model, prediction_model = efficientdet(phi=phi,
                                       weighted_bifpn=weighted_bifpn,
                                       num_classes=num_classes,
                                       num_anchors=anchor_parameters.num_anchors(),
                                       score_threshold=score_threshold,
                                       detect_quadrangle=True,
                                       anchor_parameters=anchor_parameters,
                                       )
prediction_model.load_weights(model_path, by_name=True)

import glob

for image_path in glob.glob('datasets/ic15/test_images/*.jpg'):
    image = cv2.imread(image_path)
    src_image = image.copy()
    image = image[:, :, ::-1]
    h, w = image.shape[:2]

    image, scale, offset_h, offset_w = preprocess_image(image, image_size=image_size)
    inputs = np.expand_dims(image, axis=0)
Exemple #25
0
def main():
    parser = argparse.ArgumentParser()

    model_path = parser.add_argument('--model_path', required=True)
    GPU = parser.add_argument('--GPU', default='0')
    image_path = parser.add_argument('--image_path', required=True)
    save_path = parser.add_argument('--save_path', required=True)
    json_path = parser.add_argument('--json_path',
                                    default='default_path_json.json')
    image_size = parser.add_argument('--image_size')
    file_format = parser.add_argument('--file_format', default='jpeg')
    score_threshold = parser.add_argument('--score_threshold', default=0.3)
    phi = parser.add_argument('--phi', default=0, type=int)
    args = parser.parse_args()

    json_dict = {}
    json_dict["boundingbox"] = []

    os.environ['CUDA_VISIBLE_DEVICES'] = args.GPU
    print(args.image_path + '/*.' + args.file_format)
    file_lists = glob.glob(args.image_path + '/*.' + args.file_format)
    length = len(file_lists)
    iteration = 0
    print(length)

    print(args)

    weighted_bifpn = True
    model_path = args.model_path
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[args.phi]
    # coco classes
    classes = {
        value['id'] - 1: value['name']
        for value in json.load(open('coco_90.json', 'r')).values()
    }
    num_classes = 90
    score_threshold = 0.3
    colors = [
        np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
    ]
    _, model = efficientdet(phi=args.phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=num_classes,
                            score_threshold=score_threshold)

    model.load_weights(model_path, by_name=True)
    print(model_path)
    try:
        os.makedirs(args.save_path)
    except:
        print(args.save_path, "is aleardy exist")
        pass
    start = time.time()
    for image_path in tqdm(file_lists):
        #        iteration += 1
        #         if iteration % 20 == 19:
        #             break
        image = cv2.imread(image_path)
        src_image = image.copy()
        # BGR -> RGB
        image = image[:, :, ::-1]
        h, w = image.shape[:2]

        image, scale = preprocess_image(image, image_size=image_size)
        # run network
        boxes, scores, labels = model.predict_on_batch(
            [np.expand_dims(image, axis=0)])
        boxes, scores, labels = np.squeeze(boxes), np.squeeze(
            scores), np.squeeze(labels)

        boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

        # select indices which have a score above the threshold
        indices = np.where(scores[:] > score_threshold)[0]

        # select those detections
        boxes = boxes[indices]
        labels = labels[indices]
        file_name = image_path.split('/')[-1]

        temp_dict = {}
        temp_dict["box"] = boxes[indices].tolist()
        temp_dict["label"] = labels[indices].tolist()
        temp_dict["label_name"] = [classes[int(i)] for i in labels]
        temp_dict["image_name"] = file_name
        json_dict["boundingbox"].append(copy.deepcopy(temp_dict))
        draw_boxes(src_image, boxes, scores, labels, colors, classes)
        cv2.imwrite(args.save_path + '/' + file_name, src_image)

#         cv2.waitKey(0)
    print("time : ", time.time() - start)
    with open(args.json_path, 'w') as json_data:
        json.dump(json_dict, json_data, indent=4)
Exemple #26
0
def main(args=None):

    parser = argparse.ArgumentParser(description='Training script for training a EfficientDet network.')

    parser.add_argument('--dataset', help='Dataset type, must be one of csv or coco.')
    parser.add_argument('--coco_path', help='Path to COCO directory')
    parser.add_argument('--csv_train', help='Path to file containing training annotations (see readme)')
    parser.add_argument('--csv_classes', help='Path to file containing class list (see readme)')
    parser.add_argument('--csv_val', help='Path to file containing validation annotations (optional, see readme)')

    parser.add_argument('--phi', help='EfficientNet scaling coefficient.', type=int, default=0)
    parser.add_argument('--batch-size', help='Batch size', type=int, default=8)
    parser.add_argument('--epochs', help='Number of epochs', type=int, default=100)

    parser = parser.parse_args(args)

    # Create the data loaders
    if parser.dataset == 'coco':

        if parser.coco_path is None:
            raise ValueError('Must provide --coco_path when training on COCO,')

        dataset_train = CocoDataset(parser.coco_path, set_name='train2017', transform=transforms.Compose([Normalizer(), Augmenter(), Resizer(img_size=512)]))
        dataset_val = CocoDataset(parser.coco_path, set_name='val2017', transform=transforms.Compose([Normalizer(), Resizer(img_size=512)]))

    elif parser.dataset == 'csv':

        if parser.csv_train is None:
            raise ValueError('Must provide --csv_train when training on COCO')

        if parser.csv_classes is None:
            raise ValueError('Must provide --csv_classes when training on COCO')


        dataset_train = CSVDataset(train_file=parser.csv_train, class_list=parser.csv_classes, transform=transforms.Compose([Normalizer(), Augmenter(), Resizer()]))

        if parser.csv_val is None:
            dataset_val = None
            print('No validation annotations provided.')
        else:
            dataset_val = CSVDataset(train_file=parser.csv_val, class_list=parser.csv_classes, transform=transforms.Compose([Normalizer(), Resizer()]))

    else:
        raise ValueError('Dataset type not understood (must be csv or coco), exiting.')

    sampler = AspectRatioBasedSampler(dataset_train, batch_size=parser.batch_size, drop_last=False)
    dataloader_train = DataLoader(dataset_train, num_workers=3, collate_fn=collater, batch_sampler=sampler)

    if dataset_val is not None:
        sampler_val = AspectRatioBasedSampler(dataset_val, batch_size=1, drop_last=False)
        dataloader_val = DataLoader(dataset_val, num_workers=3, collate_fn=collater, batch_sampler=sampler_val)

    # Create the model
    efficientdet = model.efficientdet(num_classes=dataset_train.num_classes(), pretrained=True, phi=parser.phi)      

    use_gpu = True

    if use_gpu:
        efficientdet = efficientdet.cuda()
    
    efficientdet = torch.nn.DataParallel(efficientdet).cuda()

    efficientdet.training = True

    optimizer = optim.Adam(efficientdet.parameters(), lr=1e-5)

    scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=3, verbose=True)

    loss_hist = collections.deque(maxlen=500)

    efficientdet.train()
    efficientdet.module.freeze_bn()

    print('Num training images: {}'.format(len(dataset_train)))

    for epoch_num in range(parser.epochs):

        efficientdet.train()
        efficientdet.module.freeze_bn()
        
        epoch_loss = []
        
        print(('\n' + '%10s' * 5) % ('Epoch', 'gpu_mem', 'Loss', 'cls', 'rls'))
        
        pbar = tqdm(enumerate(dataloader_train), total=len(dataloader_train))
        for iter_num, data in pbar:
            try:
                optimizer.zero_grad()
                
                classification_loss, regression_loss = efficientdet([data['img'].cuda().float(), data['annot']])

                classification_loss = classification_loss.mean()
                regression_loss = regression_loss.mean()

                loss = classification_loss + regression_loss
                
                if bool(loss == 0):
                    continue

                loss.backward()

                torch.nn.utils.clip_grad_norm_(efficientdet.parameters(), 0.1)

                optimizer.step()

                loss_hist.append(float(loss))

                epoch_loss.append(float(loss))
                
                loss = (loss * iter_num) / (iter_num + 1)  # update mean losses
                mem = torch.cuda.memory_cached() / 1E9 if torch.cuda.is_available() else 0  # (GB)
                s = ('%10s' * 2 + '%10.3g' * 3) % (
                    '%g/%g' % (epoch_num, parser.epochs - 1), '%.3gG' % mem, np.mean(loss_hist), float(regression_loss), float(classification_loss))
                pbar.set_description(s)
                
                del classification_loss
                del regression_loss
            except Exception as e:
                raise(e)
                continue

        if parser.dataset == 'coco':

            print('Evaluating dataset')

            coco_eval.evaluate_coco(dataset_val, efficientdet)

        elif parser.dataset == 'csv' and parser.csv_val is not None:

            print('Evaluating dataset')

            mAP = csv_eval.evaluate(dataset_val, efficientdet)

        
        scheduler.step(np.mean(epoch_loss))    

        torch.save(efficientdet.module, '{}_retinanet_{}.pt'.format(parser.dataset, epoch_num))

    efficientdet.eval()

    torch.save(efficientdet, 'model_final.pt'.format(epoch_num))
Exemple #27
0
 common_args = {
     'batch_size': 1,
     'phi': phi,
 }
 test_generator = PascalVocGenerator('datasets/VOC2007',
                                     'test',
                                     shuffle_groups=False,
                                     skip_truncated=False,
                                     skip_difficult=True,
                                     **common_args)
 model_path = 'checkpoints/2019-12-03/pascal_05_0.6283_1.1975_0.8029.h5'
 input_shape = (test_generator.image_size, test_generator.image_size)
 anchors = test_generator.anchors
 num_classes = test_generator.num_classes()
 model, prediction_model = efficientdet(phi=phi,
                                        num_classes=num_classes,
                                        weighted_bifpn=weighted_bifpn)
 prediction_model.load_weights(model_path, by_name=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)
Exemple #28
0
def main(args=None):
    if len(sys.argv) is 3:
        model_path = str(sys.argv[1])
        image_data = os.path.join(str(sys.argv[2]), "*.jpg")
    else:
        print(
            "Pass model path and image data path in respectively as command line argument"
        )
        exit()

    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 4
    weighted_bifpn = False
    model_path = model_path
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    # coco classes
    coco_classes = {
        value['id'] - 1: value['name']
        for value in json.load(open('coco_90.json', 'r')).values()
    }
    coco_num_classes = 90

    # Dhaka-ai classes
    dhaka_ai_classes = {
        0: 'ambulance',
        1: 'auto rickshaw',
        2: 'bicycle',
        3: 'bus',
        4: 'car',
        5: 'garbagevan',
        6: 'human hauler',
        7: 'minibus',
        8: 'minivan',
        9: 'motorbike',
        10: 'pickup',
        11: 'army vehicle',
        12: 'policecar',
        13: 'rickshaw',
        14: 'scooter',
        15: 'suv',
        16: 'taxi',
        17: 'three wheelers (CNG)',
        18: 'truck',
        19: 'van',
        20: 'wheelbarrow'
    }
    dhaka_ai_num_classes = 21

    score_threshold = 0.05
    colors = [
        np.random.randint(0, 256, 3).tolist()
        for _ in range(dhaka_ai_num_classes)
    ]
    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=dhaka_ai_num_classes,
                            score_threshold=score_threshold)
    model.load_weights(model_path, by_name=True)

    for image_path in glob.glob(image_data):
        image = cv2.imread(image_path)
        src_image = image.copy()
        # BGR -> RGB
        image = image[:, :, ::-1]
        h, w = image.shape[:2]

        image, scale = preprocess_image(image, image_size=image_size)
        # run network
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(
            [np.expand_dims(image, axis=0)])
        boxes, scores, labels = np.squeeze(boxes), np.squeeze(
            scores), np.squeeze(labels)
        print(time.time() - start)
        boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

        # select indices which have a score above the threshold
        # indices = np.where(scores[:] > score_threshold)[0]

        # select those detections
        # boxes = boxes[indices]
        # labels = labels[indices]

        selected_indices = tf.image.non_max_suppression(boxes,
                                                        scores,
                                                        80,
                                                        iou_threshold=0.25,
                                                        score_threshold=0.30)
        selected_boxes = tf.gather(boxes, selected_indices)
        selected_labels = tf.gather(labels, selected_indices)
        selected_boxes = tf.Session().run(selected_boxes)
        selected_labels = tf.Session().run(selected_labels)
        # boxes = boxes[selected_indices]
        # labels = labels[selected_indices]

        draw_boxes(src_image, selected_boxes, scores, selected_labels, colors,
                   dhaka_ai_classes)

        cv2.namedWindow('image', cv2.WINDOW_NORMAL)
        cv2.imshow('image', src_image)
        cv2.waitKey(0)
Exemple #29
0
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

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

    num_classes = train_generator.num_classes()
    num_anchors = train_generator.num_anchors

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

    K.set_session(get_session())

    model, prediction_model = efficientdet(
        args.phi,
        num_classes=num_classes,
        num_anchors=num_anchors,
        weighted_bifpn=args.weighted_bifpn,
        freeze_bn=args.freeze_bn,
        detect_quadrangle=args.detect_quadrangle)
    # load pretrained weights
    if args.snapshot:
        if args.snapshot == 'imagenet':
            model_name = 'efficientnet-b{}'.format(args.phi)
            file_name = '{}_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5'.format(
                model_name)
            file_hash = WEIGHTS_HASHES[model_name][1]
            weights_path = keras.utils.get_file(file_name,
                                                BASE_WEIGHTS_PATH + file_name,
                                                cache_subdir='models',
                                                file_hash=file_hash)
            model.load_weights(weights_path, by_name=True)
        else:
            print('Loading model, this may take a second...')
            model.load_weights(args.snapshot, by_name=True)

    # freeze backbone layers
    if args.freeze_backbone:
        # 227, 329, 329, 374, 464, 566, 656
        for i in range(1, [227, 329, 329, 374, 464, 566, 656][args.phi]):
            model.layers[i].trainable = False

    if args.gpu and len(args.gpu.split(',')) > 1:
        model = keras.utils.multi_gpu_model(model,
                                            gpus=list(
                                                map(int, args.gpu.split(','))))

    # compile model
    model.compile(
        optimizer=Adam(lr=1e-3),
        loss={
            'regression':
            smooth_l1_quad() if args.detect_quadrangle else smooth_l1(),
            'classification':
            focal()
        },
    )

    # print(model.summary())

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

    if not args.compute_val_loss:
        validation_generator = None
    elif args.compute_val_loss and validation_generator is None:
        raise ValueError(
            'When you have no validation data, you should not specify --compute-val-loss.'
        )

    # 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)
Exemple #30
0
    def eveluate_testset(self, ckp_path: str = "checkpoints/"):
        os.environ["CUDA_VISIBLE_DEVICES"] = "0"

        phi = 1
        weighted_bifpn_flag = True
        model_path = ckp_path + self.ckp_model_dir + "/" + self.ckp_model_file
        image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
        image_size = image_sizes[phi]
        # my dataset classes
        classes = {
            value["id"] - 1: value["name"]
            for value in json.load(open("mine_classes_eveluation_ds.json",
                                        "r")).values()
        }
        num_classes = 13
        score_threshold = 0.50
        colors = [
            np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
        ]
        _, model = efficientdet(
            phi=phi,
            num_classes=num_classes,
            weighted_bifpn=weighted_bifpn_flag,
            score_threshold=score_threshold,
        )
        model.load_weights(model_path, by_name=True)

        counter = 0
        for image_path in glob.glob(self.base_path + "testset_images/*.jpg"):

            image = cv2.imread(image_path)
            src_image = image.copy()
            # BGR -> RGB
            image = image[:, :, ::-1]
            h, w = image.shape[:2]

            image, scale = preprocess_image(image, image_size=image_size)
            # run network
            start = time.time()
            boxes, scores, labels = model.predict_on_batch(
                [np.expand_dims(image, axis=0)])
            boxes, scores, labels = (
                np.squeeze(boxes),
                np.squeeze(scores),
                np.squeeze(labels),
            )
            print(time.time() - start)
            boxes = postprocess_boxes(boxes=boxes,
                                      scale=scale,
                                      height=h,
                                      width=w)

            # select indices which have a score above the threshold
            indices = np.where(scores[:] > score_threshold)[0]

            # select those detections
            boxes = boxes[indices]
            labels = labels[indices]

            draw_boxes(src_image, boxes, scores, labels, colors, classes)

            # cv2.namedWindow('image', cv2.WINDOW_NORMAL)
            cv2.imwrite(
                "evaluated_images/evaluated_image_" + str(counter) + ".jpg",
                src_image)
            counter += 1
            # cv2.imshow('image', src_image)
            # cv2.waitKey(0)

            if counter == 500:
                break