-
Notifications
You must be signed in to change notification settings - Fork 52
/
train.py
125 lines (104 loc) · 7.9 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
import argparse
import os
import random
import time
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 dataset.dataset_nocs import Dataset
from libs.network import KeyNet
from libs.loss import Loss
cate_list = ['bottle', 'bowl', 'camera', 'can', 'laptop', 'mug']
parser = argparse.ArgumentParser()
parser.add_argument('--dataset_root', type=str, default = 'My_NOCS', help='dataset root dir')
parser.add_argument('--resume', type=str, default = '', help='resume model')
parser.add_argument('--category', type=int, default = 5, help='category to train')
parser.add_argument('--num_points', type=int, default = 500, help='points')
parser.add_argument('--num_cates', type=int, default = 6, help='number of categories')
parser.add_argument('--workers', type=int, default = 5, help='number of data loading workers')
parser.add_argument('--num_kp', type=int, default = 8, help='number of kp')
parser.add_argument('--outf', type=str, default = 'models/', help='save dir')
parser.add_argument('--lr', default=0.0001, help='learning rate')
opt = parser.parse_args()
model = KeyNet(num_points = opt.num_points, num_key = opt.num_kp, num_cates = opt.num_cates)
model.cuda()
if opt.resume != '':
model.load_state_dict(torch.load('{0}/{1}'.format(opt.outf, opt.resume)))
dataset = Dataset('train', opt.dataset_root, True, opt.num_points, opt.num_cates, 5000, opt.category)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True, num_workers=opt.workers)
test_dataset = Dataset('val', opt.dataset_root, False, opt.num_points, opt.num_cates, 1000, opt.category)
testdataloader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=True, num_workers=opt.workers)
criterion = Loss(opt.num_kp, opt.num_cates)
best_test = np.Inf
optimizer = optim.Adam(model.parameters(), lr=opt.lr)
for epoch in range(0, 500):
model.train()
train_dis_avg = 0.0
train_count = 0
optimizer.zero_grad()
for i, data in enumerate(dataloader, 0):
img_fr, choose_fr, cloud_fr, r_fr, t_fr, img_to, choose_to, cloud_to, r_to, t_to, mesh, anchor, scale, cate = data
img_fr, choose_fr, cloud_fr, r_fr, t_fr, img_to, choose_to, cloud_to, r_to, t_to, mesh, anchor, scale, cate = Variable(img_fr).cuda(), \
Variable(choose_fr).cuda(), \
Variable(cloud_fr).cuda(), \
Variable(r_fr).cuda(), \
Variable(t_fr).cuda(), \
Variable(img_to).cuda(), \
Variable(choose_to).cuda(), \
Variable(cloud_to).cuda(), \
Variable(r_to).cuda(), \
Variable(t_to).cuda(), \
Variable(mesh).cuda(), \
Variable(anchor).cuda(), \
Variable(scale).cuda(), \
Variable(cate).cuda()
Kp_fr, anc_fr, att_fr = model(img_fr, choose_fr, cloud_fr, anchor, scale, cate, t_fr)
Kp_to, anc_to, att_to = model(img_to, choose_to, cloud_to, anchor, scale, cate, t_to)
loss, _ = criterion(Kp_fr, Kp_to, anc_fr, anc_to, att_fr, att_to, r_fr, t_fr, r_to, t_to, mesh, scale, cate)
loss.backward()
train_dis_avg += loss.item()
train_count += 1
if train_count != 0 and train_count % 8 == 0:
optimizer.step()
optimizer.zero_grad()
print(train_count, float(train_dis_avg) / 8.0)
train_dis_avg = 0.0
if train_count != 0 and train_count % 100 == 0:
torch.save(model.state_dict(), '{0}/model_current_{1}.pth'.format(opt.outf, cate_list[opt.category-1]))
optimizer.zero_grad()
model.eval()
score = []
for j, data in enumerate(testdataloader, 0):
img_fr, choose_fr, cloud_fr, r_fr, t_fr, img_to, choose_to, cloud_to, r_to, t_to, mesh, anchor, scale, cate = data
img_fr, choose_fr, cloud_fr, r_fr, t_fr, img_to, choose_to, cloud_to, r_to, t_to, mesh, anchor, scale, cate = Variable(img_fr).cuda(), \
Variable(choose_fr).cuda(), \
Variable(cloud_fr).cuda(), \
Variable(r_fr).cuda(), \
Variable(t_fr).cuda(), \
Variable(img_to).cuda(), \
Variable(choose_to).cuda(), \
Variable(cloud_to).cuda(), \
Variable(r_to).cuda(), \
Variable(t_to).cuda(), \
Variable(mesh).cuda(), \
Variable(anchor).cuda(), \
Variable(scale).cuda(), \
Variable(cate).cuda()
Kp_fr, anc_fr, att_fr = model(img_fr, choose_fr, cloud_fr, anchor, scale, cate, t_fr)
Kp_to, anc_to, att_to = model(img_to, choose_to, cloud_to, anchor, scale, cate, t_to)
_, item_score = criterion(Kp_fr, Kp_to, anc_fr, anc_to, att_fr, att_to, r_fr, t_fr, r_to, t_to, mesh, scale, cate)
print(item_score)
score.append(item_score)
test_dis = np.mean(np.array(score))
if test_dis < best_test:
best_test = test_dis
torch.save(model.state_dict(), '{0}/model_{1}_{2}_{3}.pth'.format(opt.outf, epoch, test_dis, cate_list[opt.category-1]))
print(epoch, '>>>>>>>>----------BEST TEST MODEL SAVED---------<<<<<<<<')