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
drive.mount('/content/gdrive')
root_dir = "/content/gdrive/My Drive/"
base_dir = root_dir + 'Colab Notebooks/camvid/'

#Install FastAI v2
!pip install fastai2

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

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

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

#DataBlock
camvid = DataBlock(blocks=(ImageBlock, MaskBlock(codes)),
                   get_items = get_image_files,
                   get_y = label_func,
                   splitter=RandomSplitter(),
                   batch_tfms=aug_transforms(size=(120,160)))

#DataLoaders
dls = camvid.dataloaders(path/"images", path=path, bs=8)
dls.show_batch(max_n=6)

#Learner
learn = unet_learner(dls, resnet34)
learn.fine_tune(8)

#Grab an image for inference + inference
images = path/"images"
items = get_image_files(images)
img = items[0]
img = PILImage.create(img)
learn.predict(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