((h // 64) * 64, (w // 64) * 64)) x = power2resize(x) z = largeforward(net, x.unsqueeze(0)) z = globalresize(z) z = (z[0, 1, :, :] > z[0, 0, :, :]).float() cm[k] += miniworld.confusion(y, z, size=size) if False: nextI = len(os.listdir("build")) debug = miniworld.torchTOpil(globalresize(x)) debug = PIL.Image.fromarray(numpy.uint8(debug)) debug.save("build/" + str(nextI) + "_x.png") debug = y.float() debug = debug * 2 * (1 - miniworld.isborder(y, size=size)) debug = debug + miniworld.isborder(y, size=size) debug *= 127 debug = debug.cpu().numpy() debug = PIL.Image.fromarray(numpy.uint8(debug)) debug.save("build/" + str(nextI) + "_y.png") debug = z.cpu().numpy() * 255 debug = PIL.Image.fromarray(numpy.uint8(debug)) debug.save("build/" + str(nextI) + "_z.png") print("perf=", miniworld.perf(cm[k])) perfs = miniworld.perf(cm) print("miniworld", perfs[-1]) print(perfs) numpy.savetxt(name,
inter0, inter1 = (y0 * z0 * D).sum(), (y1 * z1 * D).sum() union0, union1 = ((y0 + z1 * y0) * D).sum(), ((y1 + z0 * y1) * D).sum() iou0, iou1 = (inter0 + eps) / (union0 + eps), (inter1 + eps) / (union1 + eps) iou = 0.5 * (iou0 + iou1) return 1 - iou for i in range(nbbatchs): x, y, batchchoise, _ = dataset.getBatch(batchsize) x, y, batchchoise = x.cuda(), y.cuda(), batchchoise.cuda() z = net(x) if flagborder: D = 1 - miniworld.isborder(y, size=size) else: if size == 0: D = 1 + 9 * miniworld.isborder(y, size=1) else: border = miniworld.isborder(y, size=1) borderbis = miniworld.isborder(y, size=2) borderbis = (borderbis == 1).float() * (border == 0).float() D = 1 - border + 9 * borderbis CE = criterion(z, y) CE = torch.mean(CE * D) dice = diceloss(y, z, D) loss = CE + dice with torch.no_grad():
inter0, inter1 = (y0 * z0 * D).sum(), (y1 * z1 * D).sum() union0, union1 = ((y0 + z1 * y0) * D).sum(), ((y1 + z0 * y1) * D).sum() iou0, iou1 = (inter0 + eps) / (union0 + eps), (inter1 + eps) / (union1 + eps) iou = 0.5 * (iou0 + iou1) return 1 - iou for i in range(nbbatchs): x, y, batchchoise, _ = dataset.getBatch(batchsize) x, y, batchchoise = x.cuda(), y.cuda(), batchchoise.cuda() z = net(x) D = 1 - miniworld.isborder(y) CE = criterion(z, y) CE = torch.mean(CE * D) dice = diceloss(y, z, D) loss = CE + dice with torch.no_grad(): printloss += loss.clone().detach() z = (z[:, 1, :, :] > z[:, 0, :, :]).clone().detach().float() for j in range(batchsize): stats[batchchoise[j]] += miniworld.confusion(y[j], z[j]) if i < 10: print(i, "/", nbbatchs, printloss) if i < 1000 and i % 100 == 99: print(i, "/", nbbatchs, printloss / 100)
((h // 64) * 64, (w // 64) * 64)) x = power2resize(x) z = largeforward(net, x.unsqueeze(0)) z = globalresize(z) z = (z[0, 1, :, :] > z[0, 0, :, :]).float() cm[k] += miniworld.confusion(y, z) if False: nextI = len(os.listdir("build")) debug = miniworld.torchTOpil(globalresize(x)) debug = PIL.Image.fromarray(numpy.uint8(debug)) debug.save("build/" + str(nextI) + "_x.png") debug = y.float() debug = debug * 2 * (1 - miniworld.isborder(y)) debug = debug + miniworld.isborder(y) debug *= 127 debug = debug.cpu().numpy() debug = PIL.Image.fromarray(numpy.uint8(debug)) debug.save("build/" + str(nextI) + "_y.png") debug = z.cpu().numpy() * 255 debug = PIL.Image.fromarray(numpy.uint8(debug)) debug.save("build/" + str(nextI) + "_z.png") print("perf=", miniworld.perf(cm[k])) numpy.savetxt("build/tmp.txt", miniworld.perf(cm).cpu().numpy()) perfs = miniworld.perf(cm) print(perfs)