def train(self, net, samples, optimizer, e): alpha = 2 * max(0, ((50 - e) / 50)) criterion = losses.ELULovaszFocalWithLogitsLoss(alpha, 2 - alpha) transforms = generator.TransformationsGenerator([ random.RandomFlipLr(), random.RandomAffine( image_size=101, translation=lambda rs: (rs.randint(-10, 10), rs.randint(-10, 10)), scale=lambda rs: (rs.uniform(0.85, 1.15), 1), rotation=lambda rs: rs.randint(-5, 5), **utils.transformations_options ), transformations.Padding(((13, 14), (13, 14), (0, 0))) ]) transforms_image = generator.TransformationsGenerator([ random.RandomColorPerturbation(std=1) ]) dataset = datasets.ImageDataset(samples, settings.train, transforms, transforms_image=transforms_image) dataloader = DataLoader( dataset, num_workers=10, batch_size=16, shuffle=True ) average_meter_train = meters.AverageMeter() with tqdm(total=len(dataloader), leave=False) as pbar, torch.enable_grad(): net.train() for images, masks_targets in dataloader: masks_targets = masks_targets.to(gpu) masks_predictions, aux_pam, aux_cam = net(images) loss_pam = criterion(F.interpolate(aux_pam, size=128, mode='bilinear'), masks_targets) loss_cam = criterion(F.interpolate(aux_cam, size=128, mode='bilinear'), masks_targets) loss_segmentation = criterion(masks_predictions, masks_targets) loss = loss_segmentation + loss_pam + loss_cam loss.backward() optimizer.step() optimizer.zero_grad() average_meter_train.add('loss', loss.item()) self.update_pbar( torch.sigmoid(masks_predictions), masks_targets, pbar, average_meter_train, 'Training epoch {}'.format(e) ) train_stats = {'train_' + k: v for k, v in average_meter_train.get_all().items()} return train_stats
def test(self, samples_test, dir_test=settings.test, predict=None): if predict is None: predict = self.predict net = DataParallel(self.net).cuda() transforms = generator.TransformationsGenerator([]) test_dataset = datasets.ImageDataset(samples_test, dir_test, transforms, test=True) test_dataloader = DataLoader(test_dataset, num_workers=10, batch_size=32) with tqdm(total=len(test_dataloader), leave=True, ascii=True) as pbar, torch.no_grad(): net.eval() for images, ids in test_dataloader: masks_predictions = predict(net, images) pbar.set_description('Creating test predictions...') pbar.update() masks_predictions = masks_predictions.cpu().squeeze().numpy() for p, id in zip(masks_predictions, ids): yield p, id
def validate(self, net, samples, e): transforms = generator.TransformationsGenerator([]) dataset = datasets.ImageDataset(samples, settings.train, transforms) dataloader = DataLoader( dataset, num_workers=10, batch_size=16 ) average_meter_val = meters.AverageMeter() with tqdm(total=len(dataloader), leave=True, ascii=True) as pbar, torch.no_grad(): net.eval() for images, masks_targets in dataloader: masks_targets = masks_targets.to(gpu) masks_predictions = self.predict(net, images) self.update_pbar( masks_predictions, masks_targets, pbar, average_meter_val, 'Validation epoch {}'.format(e) ) val_stats = {'val_' + k: v for k, v in average_meter_val.get_all().items()} return val_stats
def train(self, net, samples, optimizer, e): alpha = 2 * max(0, ((100 - e) / 100)) criterion = losses.ELULovaszFocalWithLogitsLoss(alpha, 2 - alpha) transforms = generator.TransformationsGenerator([ random.RandomFlipLr(), random.RandomAffine(image_size=101, translation=lambda rs: (rs.randint(-20, 20), rs.randint(-20, 20)), scale=lambda rs: (rs.uniform(0.85, 1.15), 1), **utils.transformations_options), transformations.Padding(((13, 14), (13, 14), (0, 0))) ]) pseudo_dataset = datasets.SemiSupervisedImageDataset( samples_test, settings.test, transforms, size=len(samples_test), test_predictions=self.test_predictions, momentum=0.0) dataset = datasets.ImageDataset(samples, settings.train, transforms) weights = [len(pseudo_dataset) / len(dataset) * 2 ] * len(dataset) + [1] * len(pseudo_dataset) dataloader = DataLoader(ConcatDataset([dataset, pseudo_dataset]), num_workers=10, batch_size=16, sampler=WeightedRandomSampler( weights=weights, num_samples=3200)) average_meter_train = meters.AverageMeter() with tqdm(total=len(dataloader), leave=False) as pbar, torch.enable_grad(): net.train() for images, masks_targets in dataloader: masks_targets = masks_targets.to(gpu) masks_predictions = net(images) loss = criterion(masks_predictions, masks_targets) loss.backward() optimizer.step() optimizer.zero_grad() average_meter_train.add('loss', loss.item()) self.update_pbar(torch.sigmoid(masks_predictions), masks_targets, pbar, average_meter_train, 'Training epoch {}'.format(e)) train_stats = { 'train_' + k: v for k, v in average_meter_train.get_all().items() } return train_stats
def train(self, net, samples, optimizer, e): alpha = 2 * max(0, ((50 - e) / 50)) criterion = losses.ELULovaszFocalWithLogitsLoss(alpha, 2 - alpha) transforms = generator.TransformationsGenerator([ random.RandomFlipLr(), random.RandomAffine( image_size=101, translation=lambda rs: (rs.randint(-20, 20), rs.randint(-20, 20)), scale=lambda rs: (rs.uniform(0.85, 1.15), 1), **utils.transformations_options ) ]) dataset = datasets.ImageDataset(samples, settings.train, transforms) dataloader = DataLoader( dataset, num_workers=10, batch_size=16, shuffle=True ) average_meter_train = meters.AverageMeter() with tqdm(total=len(dataloader), leave=False, ascii=True) as pbar, torch.enable_grad(): net.train() padding = tta.Pad((13, 14, 13, 14)) for images, masks_targets in dataloader: masks_targets = masks_targets.to(gpu) masks_predictions = padding.transform_backward(net(padding.transform_forward(images))).contiguous() loss = criterion(masks_predictions, masks_targets) loss.backward() optimizer.step() optimizer.zero_grad() average_meter_train.add('loss', loss.item()) self.update_pbar( torch.sigmoid(masks_predictions), masks_targets, pbar, average_meter_train, 'Training epoch {}'.format(e) ) train_stats = {'train_' + k: v for k, v in average_meter_train.get_all().items()} return train_stats
def train(self, net, samples, optimizer, e): alpha = 2 * max(0, ((50 - e) / 50)) criterion = losses.ELULovaszFocalWithLogitsLoss(alpha, 2 - alpha) transforms = generator.TransformationsGenerator([ random.RandomFlipLr(), random.RandomAffine(image_size=101, translation=lambda rs: (rs.randint(-20, 20), rs.randint(-20, 20)), scale=lambda rs: (rs.uniform(0.85, 1.15), 1), **utils.transformations_options) ]) samples_aux = list( set(samples).intersection(set(utils.get_aux_samples()))) dataset_aux = datasets.BoundaryImageDataset(samples_aux, settings.train, transforms) dataset_pseudo = datasets.BoundarySemiSupervisedImageDataset( samples_test, settings.test, transforms, size=len(samples_test), test_predictions=self.test_predictions, momentum=0.0) dataset = datasets.BoundaryImageDataset(samples, settings.train, transforms) weight_train = len(dataset_pseudo) / len(dataset) * 2 weight_aux = weight_train / 2 weights = [weight_train] * len(dataset) + [weight_aux] * len( dataset_aux) + [1] * len(dataset_pseudo) dataloader = DataLoader( ConcatDataset([dataset, dataset_aux, dataset_pseudo]), num_workers=10, batch_size=16, sampler=WeightedRandomSampler(weights=weights, num_samples=3200)) average_meter_train = meters.AverageMeter() with tqdm(total=len(dataloader), leave=False, ascii=True) as pbar, torch.enable_grad(): net.train() padding = tta.Pad((13, 14, 13, 14)) criterion_boundary = losses.BCEWithLogitsLoss() for images, masks_targets, boundary_targets in dataloader: masks_targets = masks_targets.to(gpu) boundary_targets = boundary_targets.to(gpu) masks_predictions, boundary_predictions = net( padding.transform_forward(images)) masks_predictions = padding.transform_backward( masks_predictions).contiguous() boundary_predictions = padding.transform_backward( boundary_predictions).contiguous() loss = 0.1 * criterion( masks_predictions, masks_targets) + criterion_boundary( boundary_predictions, boundary_targets) loss.backward() optimizer.step() optimizer.zero_grad() average_meter_train.add('loss', loss.item()) self.update_pbar(torch.sigmoid(masks_predictions), masks_targets, pbar, average_meter_train, 'Training epoch {}'.format(e)) train_stats = { 'train_' + k: v for k, v in average_meter_train.get_all().items() } return train_stats
'nopoolrefinenet_seresnet152_dual_hypercolumn_aux_data_poly_lr_pseudo_labels', 'nopoolrefinenet_seresnet152_dual_hypercolumn_aux_data_poly_lr_pseudo_labels_ensemble', 'nopoolrefinenet_dpn92_dual_hypercolumn_poly_lr_aux_data_pseudo_labels', ] output = 'ensemble-top-12-val' test_predictions_experiment = [] for name in experiments: test_predictions = utils.TestPredictions('{}'.format(name), mode='val') test_predictions_experiment.append(test_predictions.load_raw()) train_samples = utils.get_train_samples() transforms = generator.TransformationsGenerator([]) dataset = datasets.AnalysisDataset(train_samples, settings.train, transforms, utils.TestPredictions('{}'.format(name), mode='val').load()) split_map = [] val = utils.get_train_samples() predictions = [] masks = [] with tqdm(total=len(val), leave=False) as pbar: for id in val: _, mask, _ = dataset.get_by_id(id) test_prediction = np.concatenate([predictions[id] for predictions in test_predictions_experiment], axis=0) prediction = torch.FloatTensor(test_prediction) mask = torch.FloatTensor(mask) predictions.append(prediction)