Beispiel #1
0
    def build_model(self):
        """
        Instantiate the model, loss criterion, and optimizer
        """

        # instantiate anchor boxes
        anchor_boxes = AnchorBox(new_size=self.new_size,
                                 config=self.anchor_config)
        self.anchor_boxes = anchor_boxes.get_boxes()

        if torch.cuda.is_available() and self.use_gpu:
            self.anchor_boxes = self.anchor_boxes.cuda()

        # instatiate model
        self.model = get_model(config=self.config, anchors=self.anchor_boxes)

        # instatiate loss criterion
        self.criterion = get_loss(config=self.config)

        # instatiate optimizer
        self.optimizer = optim.SGD(params=self.model.parameters(),
                                   lr=self.lr,
                                   momentum=self.momentum,
                                   weight_decay=self.weight_decay)

        self.scaler = GradScaler()

        # print network
        self.print_network(self.model)

        # use gpu if enabled
        if torch.cuda.is_available() and self.use_gpu:
            self.model.cuda()
            self.criterion.cuda()
Beispiel #2
0
def test(model, device, testloader, criterion=None):
    correct = 0
    total = 0
    test_loss = 0
    mean_acc_depth_mask = 0
    with torch.no_grad():
        for data in testloader:
            inputs, labels = data
            overlay = inputs[1]
            bg = inputs[0]
            mask = labels[0]
            depth = labels[1]
            # Adding bg with overlay
            input_depth_mask = bg + overlay

            # Coverting to run on GPU/CPU
            input_s, depth_gt, mask_gt = input_depth_mask.to(device), depth.to(
                device), mask.to(device)

            output_depth, output_mask = model(input_s)
            output_depth, output_mask = output_depth.to(
                device), output_mask.to(device)

            # Appending depth train output to display
            output_images_depth.append(output_depth)

            # Appending mask train output to display
            output_images_mask.append(output_mask)

            # Loss Computations
            loss_mask = criterion(output_mask, mask_gt)

            loss_depth, ssim_val = get_loss(output_depth, depth_gt)

            loss = loss_depth.sum() + loss_mask
            #print("Total loss: {} sum of Depth loss :{} Mask loss: ".format(loss,loss_depth,loss_mask))

            test_loss += loss
            f1_score = dice_coef(mask_gt, output_mask)
            mean_acc_depth_mask += (ssim_val + f1_score) / 2

    test_losses.append(test_loss)

    print('\nTest Set: Average loss: {:.2f} , Accuracy: ({:.2f}%)\n'.format(
        test_loss / len(testloader), mean_acc_depth_mask / len(testloader)))
Beispiel #3
0
    def build_model(self):
        """
        Instantiate the model, loss criterion, and optimizer
        """

        # instatiate anchor boxes
        anchor_boxes = AnchorBox(map_sizes=[1, 3, 5, 9, 18, 36],
                                 aspect_ratios=self.aspect_ratios)
        self.anchor_boxes = anchor_boxes.get_boxes()
        if torch.cuda.is_available and self.use_gpu:
            self.anchor_boxes = self.anchor_boxes.cuda()

        # instatiate model
        self.model = STDN(mode=self.mode,
                          stdn_config=self.stdn_config,
                          channels=self.input_channels,
                          class_count=self.class_count,
                          anchors=self.anchor_boxes,
                          num_anchors=self.num_anchors,
                          new_size=self.new_size)

        # instatiate loss criterion
        self.criterion = get_loss(config=self.config)

        # instatiate optimizer
        self.optimizer = optim.SGD(params=self.model.parameters(),
                                   lr=self.lr,
                                   momentum=self.momentum,
                                   weight_decay=self.weight_decay)

        # print network
        self.print_network(self.model, 'STDN')

        # use gpu if enabled
        if torch.cuda.is_available() and self.use_gpu:
            self.model.cuda()
            self.criterion.cuda()
Beispiel #4
0
def train(model, device, trainloader, epoch, optimizer, criterion=None):
    running_loss = 0.0
    correct = 0
    processed = 0
    if criterion is None:
        criterion = nn.BCELoss()
    criterion = criterion
    pbar = tqdm(trainloader)
    for i, data in enumerate(pbar):
        inputs, labels = data
        overlay = inputs[1]
        bg = inputs[0]
        mask = labels[0]
        depth = labels[1]
        # Adding bg with overlay
        input_depth_mask = bg + overlay

        # Coverting to run on GPU/CPU
        input_s, depth_gt, mask_gt = input_depth_mask.to(device), depth.to(
            device), mask.to(device)

        # zero the parameter gradients
        optimizer.zero_grad()

        output_depth, output_mask = model(input_s)
        output_depth, output_mask = output_depth.to(device), output_mask.to(
            device)

        # Appending depth train output to display
        output_images_depth.append(output_depth)

        # Appending mask train output to display
        output_images_mask.append(output_mask)

        # Loss Computations
        loss_mask = criterion(output_mask, mask_gt)
        loss_depth, ssim_val = get_loss(output_depth, depth_gt)
        loss = loss_depth.sum() + loss_mask
        #print("Total loss: {} sum of Depth loss : {}, SSIM : {} & Mask loss: {}".format(loss,loss_depth,ssim_val,loss_mask))

        train_losses.append(loss)

        loss.backward(
        )  # One can set retain graph if there are other losses to accumulate separately (retain_graph=True)

        optimizer.step()

        # Save the model, optimiser and epoch at every 100 parameters
        if i % 100 == 0:
            torch.save(
                {
                    'epoch': epoch,
                    'model_state_dict': model.state_dict(),
                    'optimizer_state_dict': optimizer.state_dict()
                }, 'Depth_Mask_ModelV6_56_240k_250k_v2-local-transform.pt')

        f1_score = dice_coef(mask_gt, output_mask)
        mean_acc_depth_mask = (ssim_val + f1_score) / 2
        train_acc.append(mean_acc_depth_mask)
        pbar.set_description(
            desc=
            f'Loss={loss.item()} Batch_id={i} Accuracy={100*mean_acc_depth_mask:0.2f}'
        )
Beispiel #5
0
    # Get model.
    base_model, diff_attention_model = model.get_model(config)
    base_model = base_model.to(device)
    base_model.eval()
    if diff_attention_model is not None:
        diff_attention_model = diff_attention_model.to(device)
        diff_attention_model.eval()
    logger.info('Get model.')

    # Get data loaders.
    train_loader, query_loader, gallery_loader = loader.get_data_loaders(config, base_model=base_model, device=device)
    logger.info('Get data loaders.')

    # Get loss.
    loss, center_loss = loss.get_loss(config, device)
    logger.info('Get loss.')

    # Get optimizer.
    base_optimizer, base_scheduler, diff_optimizer, diff_scheduler, center_optimizer, center_scheduler = optimizer.get_optimizer(
        config, base_model, diff_attention_model, center_loss=center_loss)
    logger.info('Get optimizer.')

    # Get trainer.
    trainer = trainer.get_trainer(config, base_model, diff_attention_model, loss, device, logger, query_loader,
                                  gallery_loader, base_optimizer=base_optimizer, base_scheduler=base_scheduler,
                                  diff_optimizer=diff_optimizer, diff_scheduler=diff_scheduler,
                                  center_optimizer=center_optimizer, center_scheduler=center_scheduler)
    logger.info('Get trainer.')

    # Do train.