예제 #1
0
 def training_step(self, batch, batch_idx):
     sample, bbox_list, category_list, road_image = batch
     sample, road_image = sample, road_image
     outputs = self(sample, [bbox_list, category_list])
     loss = self.loss_function(outputs, road_image)
     output0 = outputs[0].view(-1, 2, 400, 400)
     road_image = road_image.view(-1, 400, 400)
     _, predicted = torch.max(output0.data, 1)
     AC = compute_ts_road_map(predicted, road_image)
     P = torch.tensor(
         (self.yolo0.metrics['precision'] + self.yolo1.metrics['precision'])
         / 2)
     R = torch.tensor(
         (self.yolo0.metrics['recall50'] + self.yolo1.metrics['recall50']) /
         2)
     lr = torch.tensor(self.trainer.optimizers[0].param_groups[0]['lr'])
     log_bar = {'roadmap_score': AC, 'precision': P, 'recall': R, 'lr': lr}
     log = {
         'loss': loss,
         'roadmap_score': AC,
         'precision': P,
         'recall': R,
         'lr': lr
     }
     return {'loss': loss, 'log': log, 'progress_bar': log_bar}
 def validation_step(self, batch, batch_idx):
     sample, bbox_list, category_list, road_image, loss_mask = batch
     sample, road_image = sample, road_image
     outputs = self(sample, [bbox_list, category_list], loss_mask)
     loss, self_loss = self.loss_function(outputs, road_image, sample,
                                          loss_mask)
     # Pass data through model
     outputs = self(sample, [bbox_list, category_list])
     output0 = outputs[0].view(-1, 2, 400, 400)
     road_image = road_image.view(-1, 400, 400)
     _, predicted = torch.max(output0.data, 1)
     AC = compute_ts_road_map(predicted, road_image)
     P = torch.tensor(
         (self.yolo0.metrics['precision'] + self.yolo1.metrics['precision'])
         / 2)
     R = torch.tensor(
         (self.yolo0.metrics['recall50'] + self.yolo1.metrics['recall50']) /
         2)
     return {
         'val_loss': loss,
         'roadmap_score': AC,
         'precision': P,
         'recall': R,
         'self_loss': self_loss
     }
예제 #3
0
def train(model, train_loader, optimizer, epoch, log_interval=50):
    # Set model to training mode
    model.train()
    # Loop through data points
    for batch_idx, data in enumerate(train_loader):
        # Send data and target to device
        sample, bbox_list, category_list, road_image = data
        sample, road_image = sample.cuda(), road_image.cuda()
        # Zero out the optimizer
        optimizer.zero_grad()
        # Pass data through model
        outputs = model(sample, [bbox_list, category_list])
        # Compute the negative log likelihood loss
        road_loss = nn.NLLLoss()(outputs[0], road_image)
        detection_loss = outputs[3]
        loss = road_loss + detection_loss
        # Compute the negative log likelihood loss
        output0 = outputs[0].view(-1, 2, 400, 400)
        road_image = road_image.view(-1, 400, 400)
        _, predicted = torch.max(output0.data, 1)
        AUC = compute_ts_road_map(predicted, road_image)
        # Backpropagate loss
        loss.backward()
        # Make a step with the optimizer
        optimizer.step()
        # Print loss (uncomment lines below once implemented)
        if batch_idx % log_interval == 0:
            print(
                'Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tRoad Loss: {:.6f}\tDetection Loss: {:.6f}\tAccuracy: {:.6f}\tPrecision: {:.6f}\tRecall50: {:.6f}'
                .format(epoch, batch_idx * len(sample),
                        len(train_loader.dataset),
                        100. * batch_idx / len(train_loader), loss.item(),
                        road_loss.item(), detection_loss.item(), AUC,
                        (model.yolo0.metrics['precision'] +
                         model.yolo1.metrics['precision']) / 2,
                        (model.yolo0.metrics['recall50'] +
                         model.yolo1.metrics['recall50']) / 2))
    print(
        'Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tRoad Loss: {:.6f}\tDetection Loss: {:.6f}\tAccuracy: {:.6f}\tPrecision: {:.6f}\tRecall50: {:.6f}'
        .format(epoch, len(train_loader.dataset), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item(),
                road_loss.item(), detection_loss.item(), AUC,
                (model.yolo0.metrics['precision'] +
                 model.yolo1.metrics['precision']) / 2,
                (model.yolo0.metrics['recall50'] +
                 model.yolo1.metrics['recall50']) / 2))
예제 #4
0
def test(model, test_loader):
    # Set model to evaluation mode
    model.eval()
    # Variable for the total loss
    test_loss = 0
    AUC = 0
    P = 0
    R = 0
    with torch.no_grad():
        # Loop through data points
        batch_num = 0

        for batch_idx, data in enumerate(test_loader):
            # Send data to device
            sample, bbox_list, category_list, road_image = data
            sample, road_image = sample.cuda(), road_image.cuda()
            # Pass data through model
            outputs = model(sample, [bbox_list, category_list])
            road_loss = nn.NLLLoss()(outputs[0], road_image)
            detection_loss = outputs[3]
            test_loss += road_loss + detection_loss
            # Compute the negative log likelihood loss
            output0 = outputs[0].view(-1, 2, 400, 400)
            road_image = road_image.view(-1, 400, 400)
            _, predicted = torch.max(output0.data, 1)
            AUC += compute_ts_road_map(predicted, road_image)
            P += (model.yolo0.metrics['precision'] +
                  model.yolo1.metrics['precision']) / 2
            R += (model.yolo0.metrics['recall50'] +
                  model.yolo1.metrics['recall50']) / 2
            batch_num += 1
            # Add number of correct predictions to total num_correct
        # Compute the average test_loss
        avg_test_loss = test_loss / batch_num
        avg_AUC = AUC / batch_num
        avg_P = P / batch_num
        avg_R = R / batch_num
        # Print loss (uncomment lines below once implemented)
        print(
            '\nTest set: Average loss: {:.4f}\t Accuracy: {:.4f}\tPrecision: {:.4f}\tRecall50: {:.4f}\n'
            .format(avg_test_loss, avg_AUC, avg_P, avg_R))
    return avg_test_loss