-
Notifications
You must be signed in to change notification settings - Fork 5
/
train_cycle_gan.py
71 lines (52 loc) · 2.41 KB
/
train_cycle_gan.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
70
71
from math import ceil
import torch
from torch import Tensor
from torch.autograd import Variable
from cycle_gan import CycleGAN
from data_loader import DataLoader
from logger import logger
from utils import ensure_dir, get_opts
project_root = "./"
data_root = "./gta/images/"
models_prefix = project_root + "saved_models/"
images_prefix = project_root + "saved_images/"
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
def train_cycle_gan(data_root, semi_supervised=False):
opt = get_opts()
ensure_dir(models_prefix)
ensure_dir(images_prefix)
cycle_gan = CycleGAN(device, models_prefix, opt["lr"], opt["b1"],
train=True, semi_supervised=semi_supervised)
data = DataLoader(data_root=data_root,
image_size=(opt['img_height'], opt['img_width']),
batch_size=opt['batch_size'])
total_images = len(data.names)
print("Total Training Images", total_images)
total_batches = int(ceil(total_images / opt['batch_size']))
for epoch in range(cycle_gan.epoch_tracker.epoch, opt['n_epochs']):
for iteration in range(total_batches):
if (epoch == cycle_gan.epoch_tracker.epoch and
iteration < cycle_gan.epoch_tracker.iter):
continue
y, x = next(data.data_generator(iteration))
real_A = Variable(x.type(Tensor))
real_B = Variable(y.type(Tensor))
cycle_gan.set_input(real_A, real_B)
cycle_gan.train()
message = (
"\r[Epoch {}/{}] [Batch {}/{}] [DA:{}, DB:{}] [GA:{}, GB:{}, cycleA:{}, cycleB:{}, G:{}]"
.format(epoch, opt["n_epochs"], iteration, total_batches,
cycle_gan.loss_disA.item(),
cycle_gan.loss_disB.item(),
cycle_gan.loss_genA.item(),
cycle_gan.loss_genB.item(),
cycle_gan.loss_cycle_A.item(),
cycle_gan.loss_cycle_B.item(),
cycle_gan.loss_G))
print(message)
logger.info(message)
if iteration % opt['sample_interval'] == 0:
cycle_gan.save_progress(images_prefix, epoch, iteration)
cycle_gan.save_progress(images_prefix, epoch, total_batches, save_epoch=True)
if __name__ == "__main__":
train_cycle_gan(data_root)