-
Notifications
You must be signed in to change notification settings - Fork 10
/
train_ours.py
199 lines (171 loc) · 8.77 KB
/
train_ours.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import argparse
import os
import pickle
import time
import datetime
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
from tensorboardX import SummaryWriter
from torchvision.utils import make_grid
from utils.util import *
from data.cocostuff_loader_ours import *
from data.vg import *
from model.resnet_generator_v1 import *
from model.rcnn_discriminator import *
from model.sync_batchnorm import DataParallelWithCallback
from utils.logger import setup_logger
from tqdm import tqdm
def get_dataset(dataset, img_size):
if dataset == "coco":
data = CocoSceneGraphDataset(image_dir='./datasets/coco/images/train2017/',
instances_json='./datasets/coco/annotations/instances_train2017.json',
stuff_json='./datasets/coco/annotations/stuff_train2017.json',
stuff_only=True, image_size=(img_size, img_size), left_right_flip=True)
elif dataset == 'vg':
data = VgSceneGraphDataset(vocab=vocab, h5_path='./datasets/vg/train.h5',
image_dir='./datasets/vg/images/',
image_size=(img_size, img_size), max_objects=10, left_right_flip=True)
return data
def main(args):
# parameters
img_size = args.img_size
z_dim = 128
lamb_obj = 1.0
lamb_img = 0.1
num_classes = 184 if args.dataset == 'coco' else 179
if args.dataset == 'coco':
background_cls = 92
foreground_cls = 91
num_obj = 8 if args.dataset == 'coco' else 31
args.out_path = os.path.join(args.out_path, args.dataset, str(args.img_size))
if not os.path.exists(args.out_path):
os.makedirs(args.out_path)
num_gpus = torch.cuda.device_count()
num_workers = 2
if num_gpus > 1:
parallel = True
args.batch_size = args.batch_size * num_gpus
num_workers = num_workers * num_gpus
else:
parallel = False
# data loader
train_data = get_dataset(args.dataset, img_size)
dataloader = torch.utils.data.DataLoader(
train_data, batch_size=args.batch_size,
drop_last=True, shuffle=True, num_workers=num_workers)
# Load model
device = torch.device('cuda')
#netG = ResnetGenerator128(num_classes=num_classes, output_dim=3).to(device)
netG = background_foreground_generator(background_cla=background_cls, foreground_cla=foreground_cls, output_dim=3).to(device)
# netD = CombineDiscriminator128(num_classes=num_classes).to(device)
netD = CombineDiscriminator64(num_classes=num_classes).to(device)
if parallel:
netG = DataParallelWithCallback(netG)
netD = nn.DataParallel(netD)
g_lr, d_lr = args.g_lr, args.d_lr
gen_parameters = []
for key, value in dict(netG.named_parameters()).items():
if value.requires_grad:
if 'mapping' in key:
gen_parameters += [{'params': [value], 'lr': g_lr * 0.1}]
else:
gen_parameters += [{'params': [value], 'lr': g_lr}]
g_optimizer = torch.optim.Adam(gen_parameters, betas=(0, 0.999))
dis_parameters = []
for key, value in dict(netD.named_parameters()).items():
if value.requires_grad:
dis_parameters += [{'params': [value], 'lr': d_lr}]
d_optimizer = torch.optim.Adam(dis_parameters, betas=(0, 0.999))
# make dirs
if not os.path.exists(args.out_path):
os.mkdir(args.out_path)
if not os.path.exists(os.path.join(args.out_path, 'model/')):
os.mkdir(os.path.join(args.out_path, 'model/'))
writer = SummaryWriter(os.path.join(args.out_path, 'log'))
logger = setup_logger("lostGAN", args.out_path, 0)
logger.info(netG)
logger.info(netD)
start_time = time.time()
vgg_loss = VGGLoss()
vgg_loss = nn.DataParallel(vgg_loss)
l1_loss = nn.DataParallel(nn.L1Loss())
for epoch in range(args.total_epoch):
netG.train()
netD.train()
print("============================")
print("Training {}th epoch".format(epoch))
for idx, data in enumerate(tqdm(dataloader)):
real_images, label, bbox, label_f, bbox_f, label_b, bbox_b = data
# print(real_images.shape)
# print(label.shape)
# print(bbox.shape)
# real_images, label, bbox = real_images.to(device), label.long().to(device).unsqueeze(-1), bbox.float()
real_images, label, bbox, label_f, bbox_f, label_b, bbox_b = real_images.to(device), label.long().to(device).unsqueeze(-1), bbox.float(), label_f.long().to(device), bbox_f.float(), label_b.long().to(device), bbox_b.float()
# update D network
netD.zero_grad()
real_images, label = real_images.to(device), label.long().to(device)
d_out_real, d_out_robj = netD(real_images, bbox, label)
d_loss_real = torch.nn.ReLU()(1.0 - d_out_real).mean()
d_loss_robj = torch.nn.ReLU()(1.0 - d_out_robj).mean()
z_f = torch.randn(real_images.size(0), num_obj, z_dim).to(device)
z_b = torch.randn(real_images.size(0), num_obj, z_dim).to(device)
# fake_images = netG(z, bbox, y=label.squeeze(dim=-1))
fake_images = netG(z_f, bbox_f, z_b, bbox_b, y_f=label_f, y_b=label_b)
# print(fake_images.shape)
d_out_fake, d_out_fobj = netD(fake_images.detach(), bbox, label)
d_loss_fake = torch.nn.ReLU()(1.0 + d_out_fake).mean()
d_loss_fobj = torch.nn.ReLU()(1.0 + d_out_fobj).mean()
d_loss = lamb_obj * (d_loss_robj + d_loss_fobj) + lamb_img * (d_loss_real + d_loss_fake)
d_loss.backward()
d_optimizer.step()
# update G network
if (idx % 1) == 0:
netG.zero_grad()
g_out_fake, g_out_obj = netD(fake_images, bbox, label)
g_loss_fake = - g_out_fake.mean()
g_loss_obj = - g_out_obj.mean()
pixel_loss = l1_loss(fake_images, real_images).mean()
feat_loss = vgg_loss(fake_images, real_images).mean()
g_loss = g_loss_obj * lamb_obj + g_loss_fake * lamb_img + pixel_loss + feat_loss
g_loss.backward()
g_optimizer.step()
if (idx + 1) % 500 == 0:
elapsed = time.time() - start_time
elapsed = str(datetime.timedelta(seconds=elapsed))
logger.info("Time Elapsed: [{}]".format(elapsed))
logger.info("Step[{}/{}], d_out_real: {:.4f}, d_out_fake: {:.4f}, g_out_fake: {:.4f} ".format(epoch + 1,
idx + 1,
d_loss_real.item(),
d_loss_fake.item(),
g_loss_fake.item()))
logger.info("d_obj_real: {:.4f}, d_obj_fake: {:.4f}, g_obj_fake: {:.4f} ".format(
d_loss_robj.item(),
d_loss_fobj.item(),
g_loss_obj.item()))
logger.info("pixel_loss: {:.4f}, feat_loss: {:.4f}".format(pixel_loss.item(), feat_loss.item()))
writer.add_image("real images", make_grid(real_images.cpu().data * 0.5 + 0.5, nrow=4), epoch * len(dataloader) + idx + 1)
writer.add_image("fake images", make_grid(fake_images.cpu().data * 0.5 + 0.5, nrow=4), epoch * len(dataloader) + idx + 1)
# save model
if (epoch + 1) % 5 == 0:
torch.save(netG.state_dict(), os.path.join(args.out_path, 'model/', 'G_%d.pth' % (epoch + 1)))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default='coco',
help='training dataset')
parser.add_argument('--batch_size', type=int, default=32,
help='mini-batch size of training data. Default: 32')
parser.add_argument('--total_epoch', type=int, default=200,
help='number of total training epoch')
parser.add_argument('--d_lr', type=float, default=0.0001,
help='learning rate for discriminator')
parser.add_argument('--g_lr', type=float, default=0.0001,
help='learning rate for generator')
parser.add_argument('--out_path', type=str, default='./outputs/tmp/app',
help='path to output files')
parser.add_argument('--img_size', type=str, default=64,
help='generated image size')
args = parser.parse_args()
main(args)