-
Notifications
You must be signed in to change notification settings - Fork 11
/
train.py
69 lines (59 loc) · 2.79 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Basile Van Hoorick, Jan 2020
'''
Edit the paths here and run to train the GAN.
Uses GPU:0 with CUDA (feel free to switch to CPU or use DataParallel).
'''
if __name__ == '__main__':
import torch
from outpainting import *
print("PyTorch version: ", torch.__version__)
print("Torchvision version: ", torchvision.__version__)
# Define paths
model_save_path = 'outpaint_models'
html_save_path = 'outpaint_html'
train_dir = 'train'
val_dir = 'val'
test_dir = 'test'
# Define datasets & transforms
my_tf = transforms.Compose([
transforms.Resize(output_size),
transforms.CenterCrop(output_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()])
batch_size = 4
train_data = CEImageDataset(train_dir, my_tf, output_size, input_size, outpaint=True)
val_data = CEImageDataset(val_dir, my_tf, output_size, input_size, outpaint=True)
test_data = CEImageDataset(test_dir, my_tf, output_size, input_size, outpaint=True)
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=1)
val_loader = DataLoader(val_data, batch_size=batch_size, shuffle=True, num_workers=1)
test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=True, num_workers=1)
print('train:', len(train_data), 'val:', len(val_data), 'test:', len(test_data))
# Define model & device
device = torch.device('cuda:0')
# device = torch.device('cpu')
G_net = CEGenerator(extra_upsample=True)
D_net = CEDiscriminator()
G_net.apply(weights_init_normal)
D_net.apply(weights_init_normal)
# G_net = nn.DataParallel(G_net)
# D_net = nn.DataParallel(D_net)
G_net.to(device)
D_net.to(device)
print('device:', device)
# Define losses
criterion_pxl = nn.L1Loss()
criterion_D = nn.MSELoss()
optimizer_G = optim.Adam(G_net.parameters(), lr=3e-4, betas=(0.5, 0.999))
optimizer_D = optim.Adam(D_net.parameters(), lr=3e-4, betas=(0.5, 0.999))
criterion_pxl.to(device)
criterion_D.to(device)
# Start training
data_loaders = {'train': train_loader, 'val': val_loader, 'test': test_loader} # NOTE: test is evidently not used by the train method
n_epochs = 200
adv_weight = [0.001, 0.005, 0.015, 0.040] # corresponds to epochs 1-10, 10-30, 30-60, 60-onwards
hist_loss = train_CE(G_net, D_net, device, criterion_pxl, criterion_D, optimizer_G, optimizer_D,
data_loaders, model_save_path, html_save_path, n_epochs=n_epochs, outpaint=True, adv_weight=adv_weight)
# Save loss history and final generator
pickle.dump(hist_loss, open('hist_loss.p', 'wb'))
torch.save(G_net.state_dict(), 'generator_final.pt')
# Next steps: see forward.py