Jovian
⭐️
Sign In

Image Segmentation with CamVid

In [1]:
%reload_ext autoreload
%autoreload 2
%matplotlib inline
In [2]:
from fastai.vision import *
from fastai.callbacks.hooks import *
from fastai.utils.mem import *

Download CamVid dataset

In [3]:
path = untar_data(URLs.CAMVID); path.ls()
Out[3]:
[PosixPath('/home/ubuntu/.fastai/data/camvid/images'),
 PosixPath('/home/ubuntu/.fastai/data/camvid/valid.txt'),
 PosixPath('/home/ubuntu/.fastai/data/camvid/labels'),
 PosixPath('/home/ubuntu/.fastai/data/camvid/codes.txt')]
In [4]:
path_lbl = path/'labels'
path_img = path/'images'

Data

In [5]:
file_names = get_image_files(path_img)
lbl_names = get_image_files(path_lbl); lbl_names[:3]
Out[5]:
[PosixPath('/home/ubuntu/.fastai/data/camvid/labels/0001TP_008640_P.png'),
 PosixPath('/home/ubuntu/.fastai/data/camvid/labels/0016E5_05640_P.png'),
 PosixPath('/home/ubuntu/.fastai/data/camvid/labels/0016E5_07650_P.png')]

Retrieve the label from path_label. If you look closely these images and labels file names have similarity except _P suffix for segmented masks.

In [6]:
get_y_fn = lambda x: path_lbl/f'{x.stem}_P{x.suffix}'
In [7]:
#Do not understand the below lines. Need to watch the lecture to make sense
#Copying from the original notebook
mask = open_mask(get_y_fn(file_names[0]))
mask.show(figsize=(5, 5), alpha=1)
Notebook Image
In [8]:
#Do not understand the below lines. Need to watch the lecture to make sense
src_size = np.array(mask.shape[1:])
src_size,mask.data
Out[8]:
(array([720, 960]), tensor([[[ 4,  4,  4,  ..., 21, 21, 21],
          [ 4,  4,  4,  ..., 21, 21, 21],
          [ 4,  4,  4,  ..., 21, 21, 21],
          ...,
          [19, 19, 19,  ..., 17, 17, 17],
          [19, 19, 19,  ..., 17, 17, 17],
          [19, 19, 19,  ..., 17, 17, 17]]]))
In [9]:
codes = np.loadtxt(path/'codes.txt', dtype=str); codes
Out[9]:
array(['Animal', 'Archway', 'Bicyclist', 'Bridge', 'Building', 'Car', 'CartLuggagePram', 'Child', 'Column_Pole',
       'Fence', 'LaneMkgsDriv', 'LaneMkgsNonDriv', 'Misc_Text', 'MotorcycleScooter', 'OtherMoving', 'ParkingBlock',
       'Pedestrian', 'Road', 'RoadShoulder', 'Sidewalk', 'SignSymbol', 'Sky', 'SUVPickupTruck', 'TrafficCone',
       'TrafficLight', 'Train', 'Tree', 'Truck_Bus', 'Tunnel', 'VegetationMisc', 'Void', 'Wall'], dtype='<U17')

Datasets

In [10]:
size = src_size//2

free = gpu_mem_get_free_no_cache()
# max size of batch size depends on available GPU RAM
if free > 8200: bs=8
else: bs=4
print(f"using bs={bs}, have {free}MB of GPU RAM free")
using bs=8, have 11430MB of GPU RAM free
In [11]:
doc(SegmentationItemList)
In [12]:
# Directory
src = (SegmentationItemList.from_folder(path=path_img)
        .split_by_fname_file('../valid.txt')
        .label_from_func(get_y_fn, classes=codes))
In [13]:
# Create a databunch
data = (src.transform(tfms=get_transforms(), size=size, tfm_y=True)
             .databunch(bs=bs)
             .normalize(imagenet_stats))
In [14]:
data.show_batch(2, figsize=(10, 7))
Notebook Image
In [15]:
data.show_batch(2, figsize=(10, 7), ds_type=DatasetType.Valid)