Camvid – Counting objects

Hi everybody,

I was wondering if there is a way to count the objects/segments detected with a U-net learner on the CAMVID_TINY dataset, on a new/unseen image at inference time?

When I print the output of learn.predict on an image, I get a TensorMask and two other tensors.

But I’m curious as to whether any of this output can be resolved to countable objects – ie, “Bicycle” x 2, “Sky” x 1, “Fence” x 1, etc.?

Here is my code (adapted from the fastaiv2 notebooks) :

# Use Google Drive + Colab
from google.colab import drive
root_dir = "/content/gdrive/My Drive/"
base_dir = root_dir + 'Colab Notebooks/camvid/'

#Install FastAI v2
!pip install fastai2

# Required Fast AI imports
from import *
from import *
from fastai2.callback.all import *

#CamVID data
path = untar_data(URLs.CAMVID_TINY)
codes = np.loadtxt(path/'codes.txt', dtype=str)

#Label function
def label_func(fn): return path/"labels"/f"{fn.stem}_P{fn.suffix}"

camvid = DataBlock(blocks=(ImageBlock, MaskBlock(codes)),
                   get_items = get_image_files,
                   get_y = label_func,

dls = camvid.dataloaders(path/"images", path=path, bs=8)

learn = unet_learner(dls, resnet34)

#Grab an image for inference + inference
images = path/"images"
items = get_image_files(images)
img = items[0]
img = PILImage.create(img)

The closest thing I’ve found is interp.show_xyz(i, sz=15) from here, but I can’t make sense of how to apply this to a new image at inference time.

Thanks in advance!

1 Like