-
Notifications
You must be signed in to change notification settings - Fork 27
/
train_gan_comp.py
144 lines (102 loc) · 4.36 KB
/
train_gan_comp.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
from __future__ import print_function
import argparse
import os
import random
import numpy as np
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from torch.autograd import Variable
from datasets import PartDataset
from pointnet import PointNetCls, PointGen, PointGenC, PointGenComp
import torch.nn.functional as F
parser = argparse.ArgumentParser()
parser.add_argument('--batchSize', type=int, default=32, help='input batch size')
parser.add_argument('--workers', type=int, help='number of data loading workers', default=4)
parser.add_argument('--nepoch', type=int, default=25, help='number of epochs to train for')
parser.add_argument('--outf', type=str, default='gan', help='output folder')
parser.add_argument('--model', type=str, default = '', help='model path')
opt = parser.parse_args()
print (opt)
blue = lambda x:'\033[94m' + x + '\033[0m'
opt.manualSeed = random.randint(1, 10000) # fix seed
print("Random Seed: ", opt.manualSeed)
random.seed(opt.manualSeed)
torch.manual_seed(opt.manualSeed)
dataset = PartDataset(root = 'shapenetcore_partanno_segmentation_benchmark_v0', class_choice = ['Chair'], shape_comp = True)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,
shuffle=True, num_workers=int(opt.workers))
test_dataset = PartDataset(root = 'shapenetcore_partanno_segmentation_benchmark_v0', class_choice = ['Chair'],shape_comp = True, train = False)
testdataloader = torch.utils.data.DataLoader(test_dataset, batch_size=opt.batchSize,
shuffle=True, num_workers=int(opt.workers))
cudnn.benchmark = True
print(len(dataset), len(test_dataset))
num_classes = len(dataset.classes)
print('classes', num_classes)
try:
os.makedirs(opt.outf)
except OSError:
pass
classifier = PointNetCls(k = 2)
gen = PointGenComp()
if opt.model != '':
classifier.load_state_dict(torch.load(opt.model))
print(classifier)
print(gen)
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
classifier.apply(weights_init)
gen.apply(weights_init)
classifier.cuda()
gen.cuda()
optimizerD = optim.Adagrad(classifier.parameters(), lr = 0.001)
optimizerG = optim.Adagrad(gen.parameters(), lr = 0.001)
num_batch = len(dataset)/opt.batchSize
for epoch in range(opt.nepoch):
for i, data in enumerate(dataloader, 0):
optimizerD.zero_grad()
points, incomp = data
points = Variable(points)
bs = points.size()[0]
target = Variable(torch.from_numpy(np.ones(bs,).astype(np.int64))).cuda()
points = points.transpose(2,1)
incomp = incomp.transpose(2,1)
points = points.cuda()
incomp = Variable(incomp).cuda()
#print(points.size())
pred, trans = classifier(points)
loss1 = F.nll_loss(pred, target)
sim_noise = Variable(torch.randn(bs, 1024)).cuda()
fake = gen(incomp, sim_noise)
fake = torch.cat([fake, incomp], 2)
fake_target = Variable(torch.from_numpy(np.zeros(bs,).astype(np.int64))).cuda()
pred2, trans2 = classifier(fake)
loss2 = F.nll_loss(pred2, fake_target)
lossD = (loss1 + loss2)/2
lossD.backward()
#print(pred, target)
optimizerD.step()
optimizerG.zero_grad()
sim_noise = Variable(torch.randn(bs, 1024)).cuda()
points = gen(incomp, sim_noise)
points = torch.cat([points, incomp], 2)
pred, trans = classifier(points)
target = Variable(torch.from_numpy(np.ones(bs,).astype(np.int64))).cuda()
#print(pred, target)
lossG = F.nll_loss(pred, target)
lossG.backward()
optimizerG.step()
print('[%d: %d/%d] train lossD: %f lossG: %f' %(epoch, i, num_batch, lossD.data[0], lossG.data[0]))
torch.save(classifier.state_dict(), '%s/modelD_%d.pth' % (opt.outf, epoch))
torch.save(gen.state_dict(), '%s/modelG_%d.pth' % (opt.outf, epoch))