def validate(loader, num_classes, device, net, criterion): num_samples = 0 running_loss = 0 metrics = Metrics(range(num_classes)) net.eval() for images, masks, tiles in tqdm(loader, desc="Validate", unit="batch", ascii=True): images = images.to(device) masks = masks.to(device) assert images.size()[2:] == masks.size()[1:], "resolutions for images and masks are in sync" num_samples += int(images.size(0)) outputs = net(images) assert outputs.size()[2:] == masks.size()[1:], "resolutions for predictions and masks are in sync" assert outputs.size()[1] == num_classes, "classes for predictions and dataset are in sync" loss = criterion(outputs, masks) running_loss += loss.item() for mask, output in zip(masks, outputs): metrics.add(mask, output) return { "loss": running_loss / num_samples, "miou": metrics.get_miou(), "fg_iou": metrics.get_fg_iou(), "mcc": metrics.get_mcc(), }
def compare(masks, labels, tile, classes): x, y, z = list(map(str, tile)) label = np.array(Image.open(os.path.join(labels, z, x, "{}.png".format(y)))) mask = np.array(Image.open(os.path.join(masks, z, x, "{}.png".format(y)))) assert label.shape == mask.shape assert len(label.shape) == 2 and len(classes) == 2 # Still binary centric metrics = Metrics(classes) metrics.add(torch.from_numpy(label), torch.from_numpy(mask), is_prob=False) fg_iou = metrics.get_fg_iou() fg_ratio = 100 * max(np.sum(mask != 0), np.sum(label != 0)) / mask.size dist = 0.0 if math.isnan(fg_iou) else 1.0 - fg_iou qod = 100 - (dist * (math.log(fg_ratio + 1.0) + np.finfo(float).eps) * (100 / math.log(100))) qod = 0.0 if qod < 0.0 else qod # Corner case prophilaxy return dist, fg_ratio, qod
def train(loader, num_classes, device, net, optimizer, criterion): num_samples = 0 running_loss = 0 metrics = Metrics(range(num_classes)) net.train() for images, masks, tiles in tqdm(loader, desc="Train", unit="batch", ascii=True): images = images.to(device) masks = masks.to(device) assert images.size()[2:] == masks.size( )[1:], "resolutions for images and masks are in sync" num_samples += int(images.size(0)) optimizer.zero_grad() outputs = net(images) assert outputs.size()[2:] == masks.size( )[1:], "resolutions for predictions and masks are in sync" assert outputs.size( )[1] == num_classes, "classes for predictions and dataset are in sync" loss = criterion(outputs, masks) loss.backward() optimizer.step() running_loss += loss.item() for mask, output in zip(masks, outputs): prediction = output.detach() metrics.add(mask, prediction) assert num_samples > 0, "dataset contains training images and labels" return { "loss": running_loss / num_samples, "miou": metrics.get_miou(), "fg_iou": metrics.get_fg_iou(), "mcc": metrics.get_mcc(), }