precision = cm[1][1] / (cm[1][1] + cm[0][1]) recall = cm[1][1] / (cm[1][1] + cm[1][0]) return precision * recall * 100 cmforlogging = [] cm = {} with torch.no_grad(): for town in cia.towns: print(town) cm[town] = torch.zeros((2, 2)).cuda() for i in range(cia.data[town].nbImages): imageraw, label = cia.data[town].getImageAndLabel(i) label = torch.Tensor(label).cuda() distance = dataloader.distancetransform(label) image = torch.Tensor(numpy.transpose(imageraw, axes=(2, 0, 1))).unsqueeze(0) h, w = image.shape[2], image.shape[3] globalresize = torch.nn.AdaptiveAvgPool2d((h, w)) power2resize = torch.nn.AdaptiveAvgPool2d( ((h // 64) * 64, (w // 64) * 64)) image = power2resize(image) pred = dataloader.largeforward(net, image) pred = globalresize(pred) _, pred = torch.max(pred[0], 0) cm[town][0][0] += torch.sum(
print("train") optimizer = torch.optim.Adam(net.parameters(), lr=0.0001) if whereIam in ["ldtis706z", "wdtim719z"]: batchsize = 16 else: batchsize = 32 nbbatchs = 100000 printloss = torch.zeros(1).cuda() stats = torch.zeros((len(miniworld.cities), 2, 2)).cuda() for i in range(nbbatchs): x, y, batchchoise = miniworld.getbatch(batchsize) x, y = x.cuda(), y.cuda() z = net(x) D = dataloader.distancetransform(y) nb0, nb1 = torch.sum((y == 0).float()), torch.sum((y == 1).float()) weights = torch.Tensor([1, nb0 / (nb1 + 1)]).cuda() criterion = torch.nn.CrossEntropyLoss(weight=weights, reduction="none") CE = criterion(z, y.long()) CE = torch.mean(CE * D) criteriondice = smp.losses.dice.DiceLoss(mode="multiclass") dice = criteriondice(z, y.long()) loss = CE + 0.1 * dice with torch.no_grad(): printloss += loss.clone().detach() if i > nbbatchs // 10: loss = loss * 0.5 if i > nbbatchs // 5:
eps = 1e-7 y = torch.nn.functional.one_hot(y, num_classes=2) y = y.transpose(2, 3).transpose(1, 2).float() z = z.log_softmax(dim=1).exp() intersection = torch.sum(y * z * D) cardinality = torch.sum((y + z) * D).clamp_min(eps) return (2.0 * intersection + eps) / (cardinality + eps) for i in range(nbbatchs): x, y, batchchoise, goodlabel = miniworld.getBatch(batchsize) x, y, batchchoise = x.cuda(), y.cuda(), batchchoise.cuda() z = net(x) D = dataloader.distancetransform(y.float()) CE = criterion(z, y) CE = torch.mean(CE * D) tmp = torch.stack([D, D], dim=1) dice = diceloss(y, z, tmp) loss = CE + dice with torch.no_grad(): printloss += loss.clone().detach() z = (z[:, 1, :, :] > z[:, 0, :, :]).clone().detach().float() for j in range(batchsize): cm = torch.zeros(2, 2).cuda() for a, b in [(0, 0), (0, 1), (1, 0), (1, 1)]: cm[a][b] = torch.sum( (z[j] == a).float() * (y[j] == b).float() * D[j])