import segmentation_models_pytorch as smp import cropextractor import dataloader print("define model") net = smp.Unet( encoder_name="efficientnet-b7", encoder_weights="imagenet", in_channels=3, classes=2, ) net = net.cuda() net.train() print("load data") miniworld = dataloader.MiniWorld(flag="train") miniworld.start() 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)
sys.path.append("/d/achanhon/github/pytorch-image-models") sys.path.append("/d/achanhon/github/pretrained-models.pytorch") sys.path.append("/d/achanhon/github/segmentation_models.pytorch") import segmentation_models_pytorch with torch.no_grad(): net = torch.load("build/model.pth") net = net.to(device) net.eval() print("massif benchmark") import dataloader if False: miniworld = dataloader.MiniWorld(flag="custom", custom=["toulouse/test"]) else: miniworld = dataloader.MiniWorld("test") def accu(cm): return 100.0 * (cm[0][0] + cm[1][1]) / np.sum(cm) def f1(cm): return 50.0 * cm[0][0] / (cm[0][0] + cm[1][0] + cm[0][1]) + 50.0 * cm[1][1] / ( cm[1][1] + cm[1][0] + cm[0][1] ) from skimage import measure
import torch import torch.backends.cudnn as cudnn device = "cuda" if torch.cuda.is_available() else "cpu" if device == "cuda": torch.cuda.empty_cache() cudnn.benchmark = True whereIam = os.uname()[1] print("massif benchmark") import dataloader if whereIam == "super": miniworld = dataloader.MiniWorld(flag="custom", custom=["potsdam/test", "bruges/test"]) else: miniworld = dataloader.MiniWorld("test") def accu(cm): return 100.0 * (cm[0][0] + cm[1][1]) / np.sum(cm) def f1(cm): return 50.0 * cm[0][0] / (cm[0][0] + cm[1][0] + cm[0][1]) + 50.0 * cm[1][1] / ( cm[1][1] + cm[1][0] + cm[0][1]) cm = {}
torch.cuda.empty_cache() cudnn.benchmark = True else: print("no cuda") quit() sys.path.append("/d/achanhon/github/EfficientNet-PyTorch") sys.path.append("/d/achanhon/github/pytorch-image-models") sys.path.append("/d/achanhon/github/pretrained-models.pytorch") sys.path.append("/d/achanhon/github/segmentation_models.pytorch") import segmentation_models_pytorch as smp import dataloader print("load data") miniworld = dataloader.MiniWorld("/test/") print("load model") with torch.no_grad(): net = torch.load("build/model.pth") net = net.cuda() net.eval() print("test") def largeforward(net, image, tilesize=128, stride=64): pred = torch.zeros(1, 2, image.shape[2], image.shape[3]).cuda() image = image.cuda() for row in range(0, image.shape[2] - tilesize + 1, stride): for col in range(0, image.shape[3] - tilesize + 1, stride):
"Arlington", "Austin", "DC", "NewYork", "SanFrancisco", "Atlanta", "NewHaven", "Norfolk", "Seekonk", ] assert len(sys.argv) >= 2 assert sys.argv[1] in cities cities = [city + "/train/" for city in cities if city != sys.argv[1]] miniworld = dataloader.MiniWorld(flag="custom", custom=cities) miniworld.start() 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)
if whereIam in ["calculon", "astroboy", "flexo", "bender"]: sys.path.append("/d/achanhon/github/EfficientNet-PyTorch") sys.path.append("/d/achanhon/github/pytorch-image-models") sys.path.append("/d/achanhon/github/pretrained-models.pytorch") sys.path.append("/d/achanhon/github/segmentation_models.pytorch") import segmentation_models_pytorch with torch.no_grad(): net = torch.load("build/model.pth") net = net.to(device) net.eval() print("massif benchmark") import dataloader miniworld = dataloader.MiniWorld("test") def accu(cm): return 100.0 * (cm[0][0] + cm[1][1]) / np.sum(cm) def f1(cm): return 50.0 * cm[0][0] / (cm[0][0] + cm[1][0] + cm[0][1]) + 50.0 * cm[1][1] / ( cm[1][1] + cm[1][0] + cm[0][1]) cm = {} with torch.no_grad(): for town in miniworld.towns:
sys.path.append("/d/achanhon/github/pytorch-image-models") sys.path.append("/d/achanhon/github/pretrained-models.pytorch") sys.path.append("/d/achanhon/github/segmentation_models.pytorch") import segmentation_models_pytorch as smp import cropextractor import dataloader print("load model") with torch.no_grad(): net = torch.load("build/model.pth") net = net.cuda() net.eval() print("load data") miniworld = dataloader.MiniWorld(flag="test") print("test") def perf(cm): if len(cm.shape) == 2: accu = 100.0 * (cm[0][0] + cm[1][1]) / (torch.sum(cm) + 1) iou0 = 50.0 * cm[0][0] / (cm[0][0] + cm[1][0] + cm[0][1] + 1) iou1 = 50.0 * cm[1][1] / (cm[1][1] + cm[1][0] + cm[0][1] + 1) return torch.Tensor((iou0 + iou1, accu)) else: out = torch.zeros(cm.shape[0], 2) for k in range(cm.shape[0]): out[k] = perf(cm[k]) return out
else: print("no cuda") quit() net = smp.Unet( encoder_name="efficientnet-b7", encoder_weights="imagenet", in_channels=3, classes=2, ) net = net.cuda() net.eval() print("load data") import dataloader miniworld = dataloader.MiniWorld() print("train") import collections import random earlystopping = miniworld.getrandomtiles(5000, 128, 32) def trainaccuracy(): with torch.no_grad(): net.eval() good, tot = torch.zeros(1).cuda(), torch.zeros(1).cuda() for x, y in earlystopping: x, y = x.cuda(), y.cuda()
import collections import random net = smp.Unet( encoder_name="efficientnet-b0", encoder_weights="imagenet", in_channels=3, classes=2, ) net = net.cuda() net.train() print("load data") import dataloader miniworld = dataloader.MiniWorld( flag="custom", custom=["potsdam/train", "bruges/train", "toulouse/test"]) earlystopping = miniworld.getrandomtiles(1000, 128, 32) weights = torch.Tensor([1, miniworld.balance]).to(device) criterion = torch.nn.CrossEntropyLoss(weight=weights) print("train") def accu(cm): return 100.0 * (cm[0][0] + cm[1][1]) / np.sum(cm) def trainaccuracy(): cm = np.zeros((2, 2), dtype=int) net.eval()
whereIam = os.uname()[1] print("define model") import collections import random import lipschitz_unet net = lipschitz_unet.UNET() net = net.cuda() net.normalize() net.train() print("load data") import dataloader miniworld = dataloader.MiniWorld(flag="custom", custom=["potsdam/train"]) earlystopping = miniworld.getrandomtiles(5000, 128, 32) weights = torch.Tensor([1, miniworld.balance, 0.00001]).to(device) criterion = torch.nn.CrossEntropyLoss(weight=weights) print("train") def accu(cm): return 100.0 * (cm[0][0] + cm[1][1]) / np.sum(cm) def f1(cm): return 50.0 * cm[0][0] / (cm[0][0] + cm[1][0] + cm[0][1]) + 50.0 * cm[1][1] / (
torch.cuda.empty_cache() cudnn.benchmark = True else: print("no cuda") quit() sys.path.append("/d/achanhon/github/EfficientNet-PyTorch") sys.path.append("/d/achanhon/github/pytorch-image-models") sys.path.append("/d/achanhon/github/pretrained-models.pytorch") sys.path.append("/d/achanhon/github/segmentation_models.pytorch") import segmentation_models_pytorch as smp import dataloader print("load data") miniworld = dataloader.MiniWorld("/train/") print("define model") net = smp.Unet( encoder_name="efficientnet-b7", encoder_weights="imagenet", in_channels=3, classes=2, ) net = net.cuda() net.eval() # avoid issue with batchnorm print("train") weights = torch.Tensor([1, 10]).cuda() criterion = torch.nn.CrossEntropyLoss(weight=weights, reduction="none") optimizer = torch.optim.Adam(net.parameters(), lr=0.0001)
sys.path.append("/d/achanhon/github/pretrained-models.pytorch") sys.path.append("/d/achanhon/github/segmentation_models.pytorch") import segmentation_models_pytorch as smp import cropextractor import dataloader print("load model") with torch.no_grad(): net = torch.load("build/model.pth") net = net.cuda() net.eval() print("load data") if len(sys.argv) == 2 and sys.argv[1] == "debug": miniworld = dataloader.MiniWorld(flag="train") else: if len(sys.argv) == 2: miniworld = dataloader.MiniWorld(flag="custom", custom=[sys.argv[1] + "/test/"]) else: miniworld = dataloader.MiniWorld(flag="test") print("test") def perf(cm): if len(cm.shape) == 2: accu = 100.0 * (cm[0][0] + cm[1][1]) / (torch.sum(cm) + 1) iou0 = 50.0 * cm[0][0] / (cm[0][0] + cm[1][0] + cm[0][1] + 1) iou1 = 50.0 * cm[1][1] / (cm[1][1] + cm[1][0] + cm[0][1] + 1)