Using fastai library, model only returns 0s

I’m trying to train a U-Net like model using the fastai library. Unfortunately, even though the training runs fine the model doesn’t seem to train at all - it only returns 0s for its predictions. I checked the learn.summary() and all the layers appear to be trainable.

Even when I change the ground truth masks to something simple like all 0.5, the model continues to only return 0.

Any ideas on what I’m doing wrong? Here’s the relevant code:

tfms = tfms_from_model(resnet34, sz, crop_type=CropType.NO, tfm_y=TfmType.CLASS, aug_tfms=aug_tfms)
datasets = ImageData.get_ds(MatchedFilesDataset, (trn_x,trn_y), (val_x,val_y), tfms)
md = ImageData('./', datasets, bs, num_workers=16, classes=None)
denorm = md.trn_ds.denorm

def mask_loss(pred,targ):
    return F.binary_cross_entropy_with_logits(pred[:,0],targ)

def mask_acc(pred,targ): return accuracy_multi(pred[:,0], targ, 0.)

def dice(pred, targs):
    m1 = (pred[:,0]>0).float()
    m2 = targs
    return 2. * (m1*m2).sum() / (m1+m2).sum()

model = linknet.LinkNet34(num_classes=1, num_channels=3)
model = nn.Sequential(
    model,
    nn.Sigmoid()
)

models = BasicModel(model.cuda())

learn = ConvLearner(md, models, clip=1)
learn.unfreeze()
learn.opt_fn=optim.Adam
learn.crit=mask_loss
learn.metrics=[mask_acc,dice]

learn.unfreeze()
learn.fit(4e-2,1,cycle_len=4,use_clr=(20,8)) 
1 Like