I used the lesson3-camvid code to create a model that identifies the left ventricle in a cardiac ultrasound. One problem is that the model sometimes segments multiple parts of the heart and labels them as the left ventricle (see top row in the image below). How can I discourage this behavior and have the model constrained to predict one contiguous shape?
Here’s the code
codes = [‘Void’, ‘Ventricle’]
src = (SegmentationItemList.from_folder(path_img)
.split_by_fname_file(path/'valids.txt')
.label_from_func(get_y_fn, classes=codes))
train_tfms = [
crop_pad(row_pct=(0,1), size=100, col_pct=(0,1), padding_mode='zeros'),
symmetric_warp(magnitude=(-0.15,0.15), p=0.5),
brightness(change=(0.3, 0.7), p=0.5),
contrast(scale=(0.8,1.25), p=0.75)
]
valid_tfms = [
crop_pad(row_pct=(0,1), size=100, col_pct=(0,1), padding_mode='zeros')
]
tfms = (train_tfms, valid_tfms)
data = (src.transform(tfms= tfms, tfm_y=True, size=112, padding_mode='zeros')
.databunch(bs=bs)
.normalize())
name2id = {v:k for k,v in enumerate(codes)}
void_code = name2id['Void']
def acc_camvid(input, target):
target = target.squeeze(1)
mask = target != void_code
return (input.argmax(dim=1)[mask]==target[mask]).float().mean()
metrics=acc_camvid
learn = unet_learner(data, models.resnet34, metrics=metrics, wd=wd)