def main(model_number):
    model = models[model_number]

    cfg = get_config(model['model_cfg'])

    detector = ObjectDetector(
        cfg,
        os.path.join(
            'resource',
            model['checkpoint']
        ),
        device_id=0
    )

    image = cv2.imread('image.jpg')

    for _ in trange(NUM_WARM, desc='Warm-up'):
        detector.predict(image, threshold=0.3)

    start = time.time()
    for _ in trange(NUM_RUNS, desc='Benches'):
        detector.predict(image, threshold=0.3)
    torch.cuda.synchronize()
    total = time.time() - start

    print(f'Performance: {NUM_RUNS / total:0.2f} rps')
Beispiel #2
0
class TrafficSignDetector(object):
    def __init__(self):
        self.detector = ObjectDetector()
        self.classifier = Classifier()

    def detect(self, image):
        objects = time.measure(lambda: self.detector.predict(image),
                               'detection')
        extend_bounding_boxes(objects, 0.15)
        images = time.measure(
            lambda: prepare_for_classification(objects, image),
            'image preprocessing')
        labels = time.measure(lambda: self.classifier.predict(images),
                              'classification')
        print(objects, labels)
        return objects, labels

    def detect_multiple(self, images):
        objects, preprocessed_images, box_scales = time.measure(
            lambda: self.detector.predict_multiple(images), 'detection')
        if len(objects[0]) == 0:
            return [[]], [[]]
        preprocessed = time.measure(
            lambda: resize_for_classification(objects, preprocessed_images,
                                              images), 'preprocessing')
        labels = time.measure(lambda: self.classifier.predict(preprocessed),
                              'classification')

        results = []
        j = 0
        for i in range(0, len(images)):
            results.append([labels[k] for k in range(j, j + len(objects[i]))])
            j += len(objects[i])

        r_objects = []
        for i in range(0, len(objects)):
            r_objects.append([])
            objs = objects[i]
            r_objects[i] = [[obj[0], *obj[1:] * box_scales[i]] for obj in objs]
        print(r_objects, results)
        return r_objects, results