from fastai.vision.all import *
import numpy as np
from torch.nn.modules.loss import _Loss
import segmentation_models_pytorch as smp
from steel_segmentation.utils import get_train_df
from steel_segmentation.transforms import SteelDataBlock, SteelDataLoaders
path = Path("../data")
train_pivot = get_train_df(path=path, pivot=True)
block = SteelDataBlock(path)
dls = SteelDataLoaders(block, train_pivot, bs=8)
xb, yb = dls.one_batch()
print(xb.shape, xb.device)
print(yb.shape, yb.device)
device = "cuda" if torch.cuda.is_available() else "cpu"
device
model = smp.Unet("resnet18", classes=4).to(device)
logits = model(xb)
probs = torch.sigmoid(logits)
preds = ( probs > 0.5).float()
criterion = SoftDiceLoss()
criterion(logits.detach().cpu(), yb)
criterion = WeightedSoftDiceLoss()
criterion(logits.detach().cpu(), yb)
criterion = SoftBCEDiceLoss(bce_pos_weight=1.5)
criterion(logits.detach().cpu(), yb)
criterion = MultiClassesSoftBCEDiceLoss()
loss = criterion(logits.detach().cpu(), yb)
loss
criterion.decodes(logits.detach().cpu())
criterion.activation(logits.detach().cpu()).shape
For the Tensorboard callback we need this Learner Callback to handle the step after the prediction.
dls.valid.bs