def make_val_step(self, data): report = {} top, left, height, width = data['top'][0], data['left'][0], data['height'][0], data['width'][0] image = variable(data['image'], volatile=True) mask = variable(data['mask'], volatile=True) prediction = self.model(image) loss = self.criterion(prediction, mask) report['loss'] = loss.data for name, func in self.metrics: report[name] = func( prediction[:, :, top:height + top, left:width + left].contiguous(), mask[:, :, top:height + top, left:width + left].contiguous() ).data return report
def make_train_step(self, data): report = {} images = variable(data['image']) masks = variable(data['mask']) self.optimizer.zero_grad() predictions = self.model(images) loss = self.criterion(predictions, masks) report['loss'] = loss.data for name, func in self.metrics: report[name] = func(predictions, masks).data loss.backward() torch.nn.utils.clip_grad_norm(self.model.parameters(), 1.0) self.optimizer.step() return report
def predict(model, ids, path_images, transforms, period, flips, num_workers): dataset = TestDataset(ids, path_images, transforms, period) loader = DataLoader(dataset=dataset, shuffle=False, drop_last=False, num_workers=num_workers, pin_memory=torch.cuda.is_available()) test_predictions = [] for data in tqdm(loader, desc='Predict', total=len(ids)): images, tops, lefts, heights, widths = data['image'], data[ 'top'], data['left'], data['height'], data['width'] inputs = variable(images, volatile=True) predictions = batch_predict(model, inputs, flips=flips) for i, prediction in enumerate(predictions): prediction = np.moveaxis(prediction, 0, -1) test_predictions.append(prediction[tops[i]:heights[i] + tops[i], lefts[i]:widths[i] + lefts[i]]) return test_predictions
def flip_tensor_ud(batch): invert_indices = torch.arange(batch.data.size()[-2] - 1, -1, -1).long() return torch.index_select(batch, 2, variable(invert_indices))
def flip_tensor_lr(batch): invert_indices = torch.arange(batch.data.size()[-1] - 1, -1, -1).long() return batch.index_select(3, variable(invert_indices))