-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
129 lines (108 loc) · 4.63 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
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
import os.path
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import torch.backends.cudnn as cudnn
from model import Generator,Discriminator,LocalDiscriminator
from dataset import Dataset
from torch.autograd import Variable
from utils import PerceptualLoss
from Gronet import Generator as Grogenerator
batch_size = 8
ROOT = r'/home/jianjian/yangxunyu/yzz2/data/depth_map'
output_dir = r'/yy/code/pix2pix/output/checkpoint/10_24'
gro_checkpoint = r'/home/jianjian/yangxunyu/yzz2/pix2pix/output/gronet/best_generator.pth'
epochs = 500
L1_lambda = 100
Lpg_lambda = 50
Lgro_lambda = 50
save_interv = 50
layer_index = [3,8,15,22]
train_dataset = Dataset('train',ROOT)
train_loader = DataLoader(train_dataset,batch_size=batch_size,shuffle=True)
D = Discriminator()
G = Generator()
LD = LocalDiscriminator()
Gro = Grogenerator()
LD.weight_init(mean=0.0,std = 0.02)
D.weight_init(mean=0.0,std=0.02)
G.weight_init(mean=0.0,std=0.02)
criterionL1 = nn.L1Loss().cuda()
criterionMSE = nn.MSELoss().cuda()
criterionBCE = nn.BCELoss().cuda()
loss_func = nn.MSELoss().cuda()
criterionPG = PerceptualLoss(loss_func,layer_index)
G.cuda()
D.cuda()
Gro.cuda()
LD.cuda()
Gro.load_state_dict(torch.load(gro_checkpoint))
G.train()
D.train()
G_optimizer = optim.Adam(G.parameters(), lr=0.0002, betas=(0.5, 0.999))
D_optimizer = optim.Adam(D.parameters(), lr=0.00001, betas=(0.5, 0.999))
LD_optimizer = optim.Adam(LD.parameters(),lr=0.00001,betas=(0.5,0.999))
train_hist = {}
train_hist['D_losses'] = []
train_hist['G_losses'] = []
G_best_loss = 999999999999
for epoch in range(epochs):
print('Epoch [%d/%d]' % (epoch+1,epochs))
D_losses = []
G_losses = []
LD_losses = []
for data in train_loader:
D.zero_grad()
inp = data['inp']
label = data['gt']
inp,label = Variable(inp.cuda()),Variable(label.cuda())
D_real = D(inp,label).squeeze()
#损失
D_real_loss = criterionBCE(D_real,Variable(torch.ones(D_real.size()).cuda()))
G_result = G(inp)
D_fake = D(inp,G_result).squeeze()
D_fake_loss = criterionBCE(D_fake,Variable(torch.zeros(D_fake.size()).cuda()))
D_train_loss = (D_fake_loss+D_real_loss) * 0.5
D_train_loss.backward()
D_optimizer.step()
train_hist['D_losses'].append(D_train_loss.item())
D_losses.append(D_train_loss.item())
#local_discriminator
LD.zero_grad()
real_local = label[:,:,64:192,64:192]
fake = G(inp)
fake_local = fake[:,:,64:192,64:192]
#print(fake_local.shape)
LD_real = LD(real_local).squeeze()
LD_real_loss = criterionBCE(LD_real,Variable(torch.ones(LD_real.size()).cuda()))
LD_fake = LD(fake_local).squeeze()
LD_fake_loss = criterionBCE(LD_fake,Variable(torch.zeros(LD_fake.size()).cuda()))
LD_train_loss = (LD_real_loss+LD_fake_loss) * 0.5
LD_train_loss.backward()
LD_optimizer.step()
LD_losses.append(LD_train_loss.item())
#train generator
G.zero_grad()
G_result = G(inp)
D_fake = D(inp,G_result).squeeze()
fake_local_G = G_result[:,:,64:192,64:192]
LD_fake = LD(fake_local_G).squeeze()
Gro_fake = Gro(G_result)
Gro_real = Gro(label)
#生成器损失
loss_PG = criterionPG(torch.cat([G_result,G_result,G_result],dim = 1),torch.cat([label,label,label],dim = 1))
G_train_loss = criterionBCE(D_fake,Variable(torch.ones(D_fake.size()).cuda())) +criterionBCE(LD_fake,Variable(torch.ones(LD_fake.size()).cuda()))+ L1_lambda*criterionL1(G_result,label)+Lgro_lambda*criterionL1(Gro_fake,Gro_real)+Lpg_lambda*loss_PG
G_train_loss.backward()
G_optimizer.step()
train_hist['G_losses'].append(G_train_loss.item())
G_losses.append(G_train_loss.item())
if G_train_loss<G_best_loss:
G_best_loss = G_train_loss
torch.save(G.state_dict(),os.path.join(output_dir,'best_generator.pth'))
torch.save(D.state_dict(),os.path.join(output_dir,'best_discriminator.pth'))
torch.save(LD.state_dict(), os.path.join(output_dir, 'best_localdiscriminator.pth'))
if (epoch+1)%save_interv == 0:
torch.save(G.state_dict(), os.path.join(output_dir, '{}_generator.pth'.format(epoch+1)))
torch.save(D.state_dict(), os.path.join(output_dir, '{}_discriminator.pth'.format(epoch+1)))
print("epoch-{};,D_loss : {:.4}, G_loss : {:.4}".format(epoch+1,sum(D_losses)/len(D_losses),sum(G_losses)/len(G_losses)))