Exemplo n.º 1
0
def main():
    batch_size = 100
    mnist_train = datasets.MNIST("/hdd/Data/MNIST/",
                                 train=True,
                                 transform=transforms.ToTensor(),
                                 download=True)
    mnist_test = datasets.MNIST("/hdd/Data/MNIST/",
                                train=False,
                                transform=transforms.ToTensor(),
                                download=True)
    data_feeder = DataFeeder(mnist_train,
                             preprocess_workers=1,
                             cuda_workers=1,
                             cpu_size=10,
                             cuda_size=10,
                             batch_size=batch_size,
                             use_cuda=True,
                             volatile=False)
    data_feeder.start_queue_threads()
    cnn = ModelStruct(CNN().cuda(), 0.001)
    fcc = ModelStruct(FCC().cuda(), 0.001)

    test_data = make_batch(len(mnist_test), 0, mnist_test, use_cuda=True)

    for i in range(100001):
        images, labels = data_feeder.get_batch()
        train(cnn, images, labels, i)
        train(fcc, images, labels, i)
        if i % 100 == 0:
            evaluate_acc(cnn, test_data, i)
            evaluate_acc(fcc, test_data, i)
        if i in [33333, 66666]:
            decrease_lr(cnn)
            decrease_lr(fcc)
        if i % 20000 == 0:
            torch.save(cnn.model, "savedir/cnn_it" + str(i // 1000) + "k.pt")
            torch.save(fcc.model, "savedir/fcc_it" + str(i // 1000) + "k.pt")

    print(max(cnn.acc))
    print(max(fcc.acc))
    graph(fcc, cnn)
    cnn.losses = losses_to_ewma(cnn.losses)
    cnn.val_losses = losses_to_ewma(cnn.val_losses, alpha=0.3)
    cnn.acc = losses_to_ewma(cnn.acc)
    fcc.losses = losses_to_ewma(fcc.losses)
    fcc.val_losses = losses_to_ewma(fcc.val_losses, alpha=0.3)
    fcc.acc = losses_to_ewma(fcc.acc)
    graph(fcc, cnn)

    data_feeder.kill_queue_threads()
Exemplo n.º 2
0
def main():
    batch_size = 100
    train_data, val_data, test_data = create_train_val_test_split(batch_size)
    data_feeder = DataFeeder(train_data,
                             preprocess_workers=1,
                             cuda_workers=1,
                             cpu_size=10,
                             cuda_size=10,
                             batch_size=batch_size,
                             use_cuda=True,
                             volatile=False)
    data_feeder.start_queue_threads()
    val_data = make_batch(len(val_data),
                          0,
                          val_data,
                          use_cuda=True,
                          volatile=True)
    test_data = make_batch(len(test_data),
                           0,
                           test_data,
                           use_cuda=True,
                           volatile=True)

    cnn = CNN().cuda()
    fcc = FCC().cuda()

    optimizer_cnn = optim.SGD(cnn.parameters(),
                              lr=0.001,
                              momentum=0.9,
                              weight_decay=0.00001)
    optimizer_fcc = optim.SGD(fcc.parameters(),
                              lr=0.001,
                              momentum=0.9,
                              weight_decay=0.00001)

    cnn_train_loss = Logger("cnn_train_losses.txt")
    cnn_val_loss = Logger("cnn_val_losses.txt")
    cnn_val_acc = Logger("cnn_val_acc.txt")
    fcc_train_loss = Logger("fcc_train_losses.txt")
    fcc_val_loss = Logger("fcc_val_losses.txt")
    fcc_val_acc = Logger("fcc_val_acc.txt")

    #permute = Variable(torch.from_numpy(np.random.permutation(28*28)).long().cuda(), requires_grad=False)
    permute = None

    for i in range(100001):
        images, labels = data_feeder.get_batch()
        train(cnn, optimizer_cnn, images, labels, i, cnn_train_loss, permute)
        train(fcc, optimizer_fcc, images, labels, i, fcc_train_loss, permute)
        if i % 100 == 0:
            print(i)
            evaluate_acc(batch_size, cnn, val_data, i, cnn_val_loss,
                         cnn_val_acc, permute)
            evaluate_acc(batch_size, fcc, val_data, i, fcc_val_loss,
                         fcc_val_acc, permute)
        if i in [70000, 90000]:
            decrease_lr(optimizer_cnn)
            decrease_lr(optimizer_fcc)
        if i % 1000 == 0:
            torch.save(cnn.state_dict(),
                       "savedir/cnn_it" + str(i // 1000) + "k.pth")
            torch.save(fcc.state_dict(),
                       "savedir/fcc_it" + str(i // 1000) + "k.pth")

    data_feeder.kill_queue_threads()

    import evaluate
    evaluate.main(permute)
Exemplo n.º 3
0
image_transformer_network.train()
for i in range(num_iterations):
    # training loss
    optimizer.zero_grad()
    total_loss = train_batch(i, train_data_feeder)
    total_loss.backward()
    optimizer.step()

    losses += [total_loss.data.cpu().numpy()]
    x_indices += [i]

    if i % 10000 == 0 and i != 0:
        torch.save(image_transformer_network,
                   "savedir/model_mosaic_it" + str(i // 1000) + "k.pt")

    # print progress
    if i % 100 == 0:
        print(i)

graph_losses(losses, x_indices, val_losses, val_x_indices)

image_transformer_network.eval()
batch = train_data_feeder.get_batch()
content = batch
content = content / 255
image = image_transformer_network(content)
show_image(content)
show_image(style)
show_image(image)
train_data_feeder.kill_queue_threads()