Skip to content

Open In Colab

How to use EfficientDet

Installing IceVision

!pip install icevision[all] icedata

Imports

from icevision.all import *

Common part to all models

Loading Data

data_dir = icedata.pets.load()

Parser

class_map = icedata.pets.class_map()
parser = icedata.pets.parser(data_dir, class_map)
train_records, valid_records = parser.parse()
show_records(train_records[:3], ncols=3, class_map=class_map)

png

Datasets

presize = 512
# EffecientDet requires the image size to be divisible by 128
size = 384
train_tfms = tfms.A.Adapter([*tfms.A.aug_tfms(size=size, presize=presize), tfms.A.Normalize()])
valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(size), tfms.A.Normalize()])
train_ds = Dataset(train_records, train_tfms)
valid_ds = Dataset(valid_records, valid_tfms)
samples = [train_ds[0] for _ in range(3)]
show_samples(samples, ncols=3, class_map=class_map, denormalize_fn=denormalize_imagenet)

png

EffecientDet Specific Part

DataLoaders

train_dl = efficientdet.train_dl(train_ds, batch_size=16, num_workers=4, shuffle=True)
valid_dl = efficientdet.valid_dl(valid_ds, batch_size=16, num_workers=4, shuffle=False)

Model

model = efficientdet.model(model_name='tf_efficientdet_lite0', num_classes=len(class_map), img_size=size)

Fastai Learner

metrics = [COCOMetric()]
learn = efficientdet.fastai.learner(dls=[train_dl, valid_dl], model=model, metrics=metrics)

Fastai Training

learn.freeze()
learn.lr_find()
SuggestedLRs(lr_min=0.012022644281387329, lr_steep=0.10000000149011612)

png

learn.fine_tune(10, 1e-2, freeze_epochs=1)

Inference

DataLoader

infer_dl = efficientdet.infer_dl(valid_ds, batch_size=8)

Predict

samples, preds = efficientdet.predict_dl(model, infer_dl)
imgs = [sample['img'] for sample in samples]
show_preds(imgs=imgs[:6], preds=preds[:6], class_map=class_map, denormalize_fn=denormalize_imagenet, ncols=3)

png

Happy Learning!

If you need any assistance, feel free to join our forum.