Esempio n. 1
0
    def test_pretrained(self):
        # Load efficientdet pretrained on VOC2007
        model = EfficientDet.from_pretrained('D0-VOC', score_threshold=.6)
        print('Done loading...')
        image = io.load_image('test/data/VOC2007/JPEGImages/000002.jpg',
                              (model.config.input_size, ) * 2)
        n_image = normalize_image(image)
        n_image = tf.expand_dims(n_image, axis=0)

        classes = voc.IDX_2_LABEL

        boxes, labels, scores = model(n_image, training=False)
        labels = [classes[l] for l in labels[0]]

        im = image.numpy()
        for l, box, s in zip(labels, boxes[0].numpy(), scores[0]):
            x1, y1, x2, y2 = box.astype('int32')

            cv2.rectangle(im, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.putText(im, l + ' {:.2f}'.format(s), (x1, y1 - 10),
                        cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)

        plt.imshow(im)
        plt.axis('off')
        plt.savefig('test.png')
        plt.show(block=True)
    def test_keras_pretrained(self):
        # Load efficientdet pretrained on VOC2007
        model = EfficientDet(D=0,
                             num_classes=20,
                             weights='D0-VOC',
                             score_threshold=.4)
        print('Done loading...')
        image = io.load_image('imgs/cat-dog.jpg', model.config.input_size)
        n_image = normalize_image(image)
        n_image = tf.expand_dims(n_image, axis=0)

        classes = voc.IDX_2_LABEL

        boxes, labels, scores = model(n_image, training=False)
        labels = [classes[l] for l in labels[0]]

        colors = visualizer.colors_per_labels(labels)
        im = visualizer.draw_boxes(image,
                                   boxes[0],
                                   labels,
                                   scores[0],
                                   colors=colors)

        plt.imshow(im)
        plt.axis('off')
        plt.savefig('test.png')

        plt.show(block=True)
Esempio n. 3
0
def _load_labelme_instance(
        images_base_path: Union[str, Path],
        annot_path: Union[str, Path],
        im_input_size: Tuple[int, int],
        class2idx: Mapping[str, int]) -> ObjectDetectionInstance:
    # Reads a labelme annotation and returns
    # a list of tuples containing the ground 
    # truth boxes and its respective label

    with Path(annot_path).open() as f:
        annot = json.load(f)

    bbs = []
    labels = []

    image_path = Path(images_base_path) / annot['imagePath']
    image = io_utils.load_image(str(image_path), 
                                im_input_size, 
                                normalize_image=True)
    
    w, h = annot['imageWidth'], annot['imageHeight']

    for shape in annot['shapes']:
        shape_type = shape['shape_type']
        if shape_type == 'rectangle':
            points = _load_bbox_from_rectangle(shape['points'])
        elif shape_type == 'polygon':
            points = _load_bbox_from_polygon(shape['points'])
        else:
            raise ValueError(
                f'Unexpected shape type: {shape_type} in file {annot_path}')
                
        label = shape['label']
        bbs.append(points)
        labels.append(class2idx[label])

    boxes = tf.stack(bbs)
    boxes = bb_utils.normalize_bndboxes(boxes, (h, w))

    return image, (tf.stack(labels), boxes)
Esempio n. 4
0
def build_dataset(dataset_path: Union[str, Path],
                  im_input_size: Tuple[int, int],
                  shuffle: bool = True) -> tf.data.Dataset:
    """
    Create model input pipeline using tensorflow datasets
    Parameters
    ----------
    dataset_path: Union[Path, str]
        Path to the voc2007 dataset. The dataset path should contain
        two subdirectories, one called images and another one called 
        annots
    im_input_size: Tuple[int, int]
        Model input size. Images will automatically be resized to this
        shape
    data_augmentation: bool, default False
        Wether or not to apply data augmentation
    Examples
    --------
    
    >>> ds = build_dataset('data/VOC2007', im_input_size=(128, 128))
    >>> for image, (labels, bbs) in ds.take(1):
    ...   print(image.shape)
    ...   print(labels, bbs.shape)
    ...
    (128, 128, 3)
    ([1, 13], (2, 4))
    Returns
    -------
    tf.data.Dataset
    """
    dataset_path = Path(dataset_path)
    im_path = dataset_path / 'JPEGImages'
    annot_path = dataset_path / 'Annotations'

    # List sorted annotation files
    annot_files = sorted(annot_path.glob('*.xml'))
    
    # Partially evaluate image loader to resize images
    # always with the same shape and normalize them
    load_im = lambda im, annots: (io_utils.load_image(im, im_input_size, 
                                                      normalize_image=True),
                                  annots)
    scale_boxes = partial(_scale_boxes, to_size=im_input_size)

    # We assume that tf datasets list files sorted when shuffle=False
    im_ds = tf.data.Dataset.list_files(str(im_path / '*.jpg'), shuffle=False)
    annot_ds = (tf.data.Dataset
                .from_generator(generator=lambda: _annot_gen(annot_files), 
                                output_types=(tf.int32, tf.float32))
                .map(scale_boxes))

    # Join both datasets
    ds = tf.data.Dataset.zip((im_ds, annot_ds))

    # Shuffle before loading the images
    if shuffle:
        ds = ds.shuffle(1024)

    ds = ds.map(load_im)

    return ds
Esempio n. 5
0
from efficientdet.data.voc import IDX_2_LABEL

import tensorflow as tf


# In[3]:


model = EfficientDet.from_pretrained('D0-VOC', score_threshold=.3)
image_size = model.config.input_size


# In[4]:


image = load_image('sample.jpg', image_size)
image.shape


# In[ ]:


n_image = preprocess.normalize_image(image)
n_image = tf.expand_dims(n_image, 0)


# In[ ]:


predictions = model(n_image, training=False)
boxes, labels, scores = predictions