Exemple #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)
Exemple #3
0
def load_image(im_path: str,
               im_size: Tuple[int, int],
               normalize_image: bool = False) -> tf.Tensor:
                
    # Reads the image and resizes it
    im = tf.io.read_file(im_path)
    im = tf.image.decode_jpeg(im, channels=3)
    im = tf.image.convert_image_dtype(im, tf.float32)
    if normalize_image:
        im = preprocess.normalize_image(im)
        
    return tf.image.resize(im, im_size)
Exemple #4
0
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
labels = [IDX_2_LABEL[o] for o in labels[0]]


# In[7]:


colors = visualizer.colors_per_labels(labels)