Exemple #1
0
    def predict(self, imagePaths):

        image_size = self.image_sizes[self.model]
        LABELS = open(self.classesFile).read().strip().split("\n")
        classes = [label.split(',')[0] for label in LABELS]
        num_classes = len(classes)
        weighted_bifpn = False
        colors = [
            np.random.randint(0, 256, 3).tolist() for i in range(num_classes)
        ]
        model, prediction_model = efficientdet(phi=self.model,
                                               weighted_bifpn=weighted_bifpn,
                                               num_classes=num_classes,
                                               score_threshold=self.CONFIDENCE)
        prediction_model.load_weights(self.modelWeights, by_name=True)

        imagePaths = list(paths.list_images(imagePaths))

        for (i, image_path) in enumerate(imagePaths):
            print("[INFO] predicting on image {} of {}".format(
                i + 1, len(imagePaths)))
            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)
            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[0, :, [0, 2]] = boxes[0, :, [0, 2]] - offset_w
            boxes[0, :, [1, 3]] = boxes[0, :, [1, 3]] - offset_h
            boxes /= scale
            boxes[0, :, 0] = np.clip(boxes[0, :, 0], 0, w - 1)
            boxes[0, :, 1] = np.clip(boxes[0, :, 1], 0, h - 1)
            boxes[0, :, 2] = np.clip(boxes[0, :, 2], 0, w - 1)
            boxes[0, :, 3] = np.clip(boxes[0, :, 3], 0, h - 1)

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

            # select those detections
            boxes = boxes[0, indices]
            scores = scores[0, indices]
            labels = labels[0, indices]

            file = open(image_path[0:image_path.rfind(".")] + ".xml", "w")
            file.write(
                self.generateXML(image_path[0:image_path.rfind(".")],
                                 image_path, h, w, 3, boxes, scores, labels,
                                 classes))
            file.close()
Exemple #2
0
def mainDataset(dataset, output, confidence, weights, fichClass, phi=0):
    f = open(fichClass)
    LABELS = f.read().strip().split("\n")
    LABELS = {int(L.split(",")[1]): L.split(",")[0] for L in LABELS}
    f.close()

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

    num_classes = len(LABELS)
    score_threshold = confidence
    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)

    imagePaths = list(paths.list_images(dataset))
    # loop over the input image paths
    for (i, imagePath) in enumerate(imagePaths):
        image = cv2.imread(imagePath)
        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)
        anchors = anchors_for_shape((image_size, image_size))
        boxes, scores, labels = prediction_model.predict_on_batch(
            [np.expand_dims(image, axis=0),
             np.expand_dims(anchors, axis=0)])
        boxes[0, :, [0, 2]] = boxes[0, :, [0, 2]] - offset_w
        boxes[0, :, [1, 3]] = boxes[0, :, [1, 3]] - offset_h
        boxes /= scale
        boxes[0, :, 0] = np.clip(boxes[0, :, 0], 0, w - 1)
        boxes[0, :, 1] = np.clip(boxes[0, :, 1], 0, h - 1)
        boxes[0, :, 2] = np.clip(boxes[0, :, 2], 0, w - 1)
        boxes[0, :, 3] = np.clip(boxes[0, :, 3], 0, h - 1)
        indices = np.where(scores[0, :] > score_threshold)[0]
        boxes = boxes[0, indices]
        scores = scores[0, indices]
        labels = labels[0, indices]

        # parse the filename from the input image path, construct the
        # path to the output image, and write the image to disk
        filename = imagePath.split(os.path.sep)[-1]
        file = open(imagePath[0:imagePath.rfind(".")] + ".xml", "w")
        file.write(
            generateXML(imagePath[0:imagePath.rfind(".")], imagePath, h, w, 3,
                        boxes, scores, labels, LABELS))
        file.close()
    def __init__(self, engine, size=640, num_classes=20):
        super().__init__()

        self.size = size
        self.score_threshold = 0.5
        self.num_classes = num_classes

        assert size in [512, 640], f'Net size {size} not in [512, 640]'

        def setPreferableEngine(engine):
            if engine == 'gpu':
                os.environ["CUDA_VISIBLE_DEVICES"] = "0"
            else:
                os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

        setPreferableEngine(engine)
        import tensorflow as tf
        tf.logging.set_verbosity(tf.logging.ERROR)

        from EfficientDet.model import efficientdet

        if size == 512:
            phi = 0
            weighted_bifpn = True
            model_path = 'models/EfficientDet/EfficientDet-d0/EfficientDet-d0.weights'
        else:
            phi = 1
            weighted_bifpn = False
            model_path = 'models/EfficientDet/EfficientDet-d1/EfficientDet-d1.weights'

        assert Path(model_path).is_file(), 'Not find model file'

        _, self.net = efficientdet(phi=phi,
                                   weighted_bifpn=weighted_bifpn,
                                   num_classes=self.num_classes,
                                   score_threshold=self.score_threshold)
        self.net.load_weights(model_path, by_name=True)
os.environ['CUDA_VISIBLE_DEVICES'] = '0'

phi = 1
weighted_bifpn = False
model_path = 'EfficientDet-d1.weights'
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)

image_path = 'sample.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)
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),