Example #1
0
 def test_model_cpu(self):
     model = M3EP(config)
     for step, batch in enumerate(data_loader):
         batch = batch_to_device(batch, torch.device('cpu'))
         pred = model(batch)
         output_size = config['late_fusion']['output_size']
         self.assertEqual(pred.shape[1], output_size)
         self.assertEqual(pred.device, torch.device('cpu'))
Example #2
0
def val_acc(model, val_loader, device):
    val_batch = next(iter(val_loader))
    val_batch = batch_to_device(val_batch, device)
    val_pred = model(val_batch)
    y_pred = val_pred.cpu().detach().numpy()
    y_pred = [np.argmax(p) for p in y_pred]
    score = accuracy_score(val_batch[1], y_pred)
    return score
Example #3
0
    def test_loss(self):
        loss_fn = torch.nn.CrossEntropyLoss()

        for step, batch in enumerate(data_loader):
            model = M3EP(config)
            pred = model(batch_to_device(batch, torch.device('cpu')))
            targets = batch[1]
            loss = loss_fn(pred, targets)
            self.assertEqual(type(loss).__name__, 'Tensor')
Example #4
0
    def test_correct_device(self):
        device = torch.device(
            'cuda') if torch.cuda.is_available() else torch.device('cpu')
        model = M3EP(config)
        model = model.to(device)

        for step, batch in enumerate(data_loader):
            batch = batch_to_device(batch, device)
            pred = model(batch)
            self.assertEqual(pred.device.type, device.type)
Example #5
0
    def test_correct_device_cuda_call(self):
        device = torch.device(
            'cuda') if torch.cuda.is_available() else torch.device('cpu')
        model = M3EP(config)
        if torch.cuda.is_available():
            model.cuda()

        for step, batch in enumerate(data_loader):
            batch = batch_to_device(batch, device)
            pred = model(batch)
            # output_size = config['hp']['output_size']
            self.assertEqual(pred.device.type, device.type)
Example #6
0
    def test_parallel_data_loader_batch(self):
        data_loader = DataLoader(dataset,
                                 batch_size=50,
                                 collate_fn=dict_pad_collate)
        device = torch.device(
            'cuda') if torch.cuda.is_available() else torch.device('cpu')

        for step, batch in enumerate(data_loader):
            model = M3EP(config)
            if torch.cuda.is_available():
                model.cuda()
            batch = batch_to_device(batch, device)
            pred = model(batch)
            self.assertEqual(pred.device.type, device.type)
Example #7
0
        num_workers=config['data_loader']['num_workers'],
        collate_fn=dict_pad_collate,
        shuffle=True)

    loss_fn = nn.CrossEntropyLoss()
    optimizer = optim.Adam(params=model.parameters(),
                           lr=float(config['learning_rate']))

    for epoch in range(config['epochs']):
        print('Epoch {} of {}:'.format(str(epoch + 1), config['epochs']))
        loss_msg = {}
        loss_values = []

        with tqdm(total=len(train_loader)) as progress_bar:
            for step, batch in enumerate(train_loader):
                batch = batch_to_device(batch, device)
                labels = batch[1]

                prediction = model(batch)
                loss = loss_fn(prediction, labels)
                # noinspection PyUnresolvedReferences
                loss_msg['loss'] = loss.item()
                progress_bar.postfix = loss_msg
                progress_bar.update()

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                if step % config['log_frequency'] == 0 and len(
                        loss_values) > 0:
                    writer.add_scalar('train_loss',