def train(iterations, batch_size=64, log_dir=None):
    train_inputs, train_labels = load(
        os.path.join('..', 'data', 'tux', 'tux_train.dat'))
    val_inputs, val_labels = load(
        os.path.join('..', 'data', 'tux', 'tux_valid.dat'))

    model = ResNetModel()

    log = None
    if log_dir is not None:
        from tensorboardX import SummaryWriter
        # from dl.core.utils import SummaryWriter
        log = SummaryWriter(log_dir)

    optimizer = optim.Adam(model.parameters(), lr=1e-4)
    loss = nn.CrossEntropyLoss()

    for iteration in range(iterations):
        model.train()
        # Construct a mini-batch
        batch = np.random.choice(train_inputs.shape[0], batch_size)
        batch_inputs = augment_train(train_inputs[batch])
        batch_labels = torch.as_tensor(train_labels[batch], dtype=torch.long)

        # zero the gradients (part of pytorch backprop)
        optimizer.zero_grad()

        # Compute the model output and loss (view flattens the input)
        model_outputs = model(batch_inputs)
        t_loss_val = loss(model_outputs, batch_labels)
        t_acc_val = accuracy(model_outputs, batch_labels)

        # Compute the gradient
        t_loss_val.backward()

        # Update the weights
        optimizer.step()

        if iteration % 10 == 0:
            model.eval()
            batch = np.random.choice(val_inputs.shape[0], 256)
            batch_inputs = augment_val(val_inputs[batch])
            batch_labels = torch.as_tensor(val_labels[batch], dtype=torch.long)
            model_outputs = model(batch_inputs)
            v_acc_val = accuracy(model_outputs, batch_labels)

            print(
                f'[{iteration:5d}] loss = {t_loss_val:1.6f} t_acc = {t_acc_val:1.6f} v_acc = {v_acc_val:1.6f}'
            )
            if log is not None:
                log.add_scalar('train/loss', t_loss_val, iteration)
                log.add_scalar('train/acc', t_acc_val, iteration)
                log.add_scalar('val/acc', v_acc_val, iteration)

    # Save the trained model
    torch.save(model.state_dict(),
               os.path.join(dirname, 'model_state',
                            'resnet.th'))  # Do NOT modify this line
Example #2
0
def test(model_name, iterations, batch_size=256):
    train_inputs, train_labels = load(os.path.join('..', 'data', 'tux', 'tux_valid.dat'))

    if model_name == 'regress':
        model = ScalarModel()
        pred = lambda x: x.detach().numpy().round().astype(int)
    else:
        model = VectorModel()
        pred = lambda x: np.argmax(x.detach().numpy(), axis=1)

    model.load_state_dict(torch.load(os.path.join(dirname, 'model_state', model_name + '.th')))

    accuracies = []
    for iteration in range(iterations):
        # Construct a mini-batch
        batch = np.random.choice(train_inputs.shape[0], batch_size)
        batch_inputs = torch.as_tensor(train_inputs[batch], dtype=torch.float32)

        pred_val = pred(model(batch_inputs.view(batch_size, -1)))
        accuracies.append(np.mean(pred_val == train_labels[batch]))
    print(f'Accuracy {np.mean(accuracies)} +- {np.std(accuracies)} ')
def test(iterations, batch_size=256):
    # train_inputs, train_labels = load(os.path.join('tux_valid.dat'))
    train_inputs, train_labels = load(
        os.path.join('..', 'data', 'tux', 'tux_valid.dat'))

    model = ConvNetModel2()
    pred = lambda x: np.argmax(x.detach().numpy(), axis=1)

    model.load_state_dict(
        torch.load(os.path.join(dirname, 'model_state', 'convnet2.th')))
    model.eval()

    accuracies = []
    for iteration in range(iterations):
        # Construct a mini-batch
        batch = np.random.choice(train_inputs.shape[0], batch_size)
        batch_inputs = transform_val(train_inputs[batch])
        pred_val = pred(model(batch_inputs.view(batch_size, 3, 64, 64)))
        accuracies.append(np.mean(pred_val == train_labels[batch]))
    print(
        f'Accuracy {np.mean(accuracies):.6f} +- {np.std(accuracies) / np.sqrt(len(accuracies)):.6f}'
    )
Example #4
0
def train(model_name, iterations, batch_size=64):
    train_inputs, train_labels = load(os.path.join('..', 'data', 'tux', 'tux_train.dat'))
    loss = eval(model_name.capitalize()+'Loss')()
    if model_name == 'regress':
        model = ScalarModel()
    else:
        model = VectorModel()

    # We use the ADAM optimizer with default learning rate
    optimizer = optim.Adam(model.parameters(), lr=1e-4)

    for iteration in range(iterations):
        # Construct a mini-batch
        batch = np.random.choice(train_inputs.shape[0], batch_size)
        batch_inputs = torch.as_tensor(train_inputs[batch], dtype=torch.float32)
        batch_labels = torch.as_tensor(train_labels[batch], dtype=torch.long)

        if model_name == 'regress': # Regression expects float labels
            batch_labels = batch_labels.float()

        # zero the gradients (part of pytorch backprop)
        optimizer.zero_grad()

        # Compute the model output and loss (view flattens the input)
        loss_val = loss( model(batch_inputs.view(batch_size, -1)), batch_labels)

        # Compute the gradient
        loss_val.backward()

        # Update the weights
        optimizer.step()

        if iteration % 10 == 0:
            print(f'[{iteration:5d}] loss = {loss_val:1.6f}')

    # Save the trained model
    torch.save(model.state_dict(), os.path.join(dirname, 'model_state', model_name + '.th')) # Do NOT modify this line