<jupyter_start><jupyter_text>Basic image segmentation using CamVid dataset representation<jupyter_code>%reload_ext autoreload
%autoreload 2
%matplotlib inline
from fastai.vision import *
from fastai.callbacks.hooks import *
from fastai.utils.mem import *
path = Path("/storage_2/dataset_segmentation/camvid/")
path.ls()
path_img = path / "images"
path_img
path_lbl = path / "labels"
path_lbl
doc(SegmentationItemList)<jupyter_output><empty_output><jupyter_text>Data<jupyter_code>fnames = get_image_files(path_img)
fnames[:3]
lbl_names = get_image_files(path_lbl)
lbl_names[:3]
img_f = fnames[2]
img = open_image(img_f)
img.show(figsize=(5, 5))
import re
get_y_fn = lambda x: Path(re.sub("/images/", "/labels/", str(x)))
mask = open_mask(get_y_fn(str(img_f)))
mask.show(figsize=(5, 5), alpha=1)
src_size = np.array(mask.shape[1:])
src_size, mask.data
codes = np.loadtxt(path / 'codes.txt', dtype='str'); codes<jupyter_output><empty_output><jupyter_text>Datasets<jupyter_code># size = src_size // 2
size = src_size

free = gpu_mem_get_free_no_cache()
# the max size of bs depends on the available GPU RAM
if free > 8200: bs=8
else:           bs=4
print(f"using bs={bs}, have {free}MB of GPU RAM free")
src = (SegmentationItemList.from_folder(path_img)
       .split_by_fname_file('../valid.txt')
       .label_from_func(get_y_fn, classes=codes))
data = (src.transform(get_transforms(), size=size, tfm_y=True)
        .databunch(bs=bs)
        .normalize(imagenet_stats))
data.show_batch(2, figsize=(10,7))
data.show_batch(2, figsize=(10,7), ds_type=DatasetType.Valid)<jupyter_output><empty_output><jupyter_text>Model<jupyter_code># metrics = accuracy
wd = 1e-2
# learn = unet_learner(data, models.resnet34, metrics=metrics, wd=wd)
learn = unet_learner(data, models.resnet34, wd=wd)
lr_find(learn)
learn.recorder.plot()
lr=3e-3
learn.fit_one_cycle(10, slice(lr), pct_start=0.9)
learn.save('stage-1')
learn.load('stage-1');
# learn.show_results(rows=3, figsize=(8, 9))
learn.show_results(rows=2)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup