-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_new_task_3D_dgcnn.py
184 lines (162 loc) · 6.41 KB
/
train_new_task_3D_dgcnn.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
import os, sys, copy
import torch
import torch.nn.parallel
import numpy as np
import argparse
import scipy.io as sio
import yaml
from tqdm import tqdm
from src.dgcnn import DGCNN, DGCNNSem, dgcnn_loss
from src.losses import *
from src.models_3D import *
from src.provider import *
from src.data_utils.datautil_3D import *
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
"""
Arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default='ModelNet', choices=['ModelNet', 'ScanObjectNN'], help='name of dataset i.e. ModelNet, ScanObjectNN, McGill')
parser.add_argument('--epoch', default=60, type=int, help='number of epochs')
parser.add_argument('--sem', default='w2v', type=str, choices=['w2v', 'glove', 'none'], help='using sem representation ie.e w2v, glove, none')
args = parser.parse_args()
config_file = open('config/'+args.dataset+'_config.yaml', 'r') # args.config_path
config = yaml.load(config_file, Loader=yaml.FullLoader)
config = {**config, **vars(args)} # merger args and config
"""
Hyperparameters
"""
epochs = config['epoch']
batch_size = 32
lr = float(config['lr'])
amsgrad = True
eps = 1e-8
wd = float(config['wd'])
print(config)
""""
Dataset
"""
trainDataLoaderNew, testDataLoaderOld, testDataLoaderNew, old_w2v, new_w2v, old_att, new_att = get_dataset(config)
"""
Model
"""
OLD_MODEL_PATH = config['dgcnn_old_model_path_'+config['sem']]
SAVED_MODEL_PATH = config['dgcnn_old_model_path_'+config['sem']].replace('old', 'new')
print('Old model path :', OLD_MODEL_PATH)
print('Saved model path :', SAVED_MODEL_PATH)
if config['sem']=='none':
old_model = DGCNN(num_classes=config['seen_class'])
else:
old_model = DGCNNSem(att_size=old_att.shape[1])
old_model.load_state_dict(torch.load(OLD_MODEL_PATH), strict=False)
old_model.to(device)
old_model = old_model.eval()
for param in old_model.parameters():
param.requires_grad = False
# print(old_model)
# sys.exit()
shared_model = copy.deepcopy(old_model)
if config['sem'] != 'none':
# with w2v
new_model = DGCNNLwf3Dv2_2(shared_model, att_size=old_att.shape[1]).to(device)
else:
# without w2v
new_model = DGCNNLwf3Dv1_2(shared_model, num_classes=config['unseen_class']).to(device)
optimizer = torch.optim.Adam(new_model.parameters(), lr=lr, betas=(0.9, 0.999), eps=eps, amsgrad=amsgrad)
# loss
hinton_loss = SoftTargetKDLoss(T=float(config['T'])).to(device)
"""
Training and evaluation
"""
for epoch in range(epochs):
print('Epoch : ',epoch+1)
# Training
t = tqdm(enumerate(trainDataLoaderNew, 0), total=len(trainDataLoaderNew), ncols=100, smoothing=0.9, position=0, leave=True)
for batch_id, data in t:
points, target = data
points = points.data.numpy()
points = translate_pointcloud(points)
shuffle_points(points)
points = torch.Tensor(points)
target = target[:, 0]
points = points.permute(0, 2, 1)
points, target = points.to(device).float(), target.to(device)
optimizer.zero_grad()
# old task's target
old_model.eval()
if config['sem']=='none':
old_target = old_model(points)
else:
old_target = old_model(points, old_w2v)
# new model's training
new_model.train()
if config['sem']!='none':
old_pred, new_pred = new_model(points, old_w2v, new_w2v) # with w2v
else:
old_pred, new_pred = new_model(points) # without w2v
# loss calculation
loss = dgcnn_loss(new_pred, target.long())
kd_loss= hinton_loss(old_pred, old_target)
t.set_postfix(KDLoss=kd_loss.item(), PointLoss=loss.item())
loss = loss + 3.0*kd_loss
loss.backward()
optimizer.step()
# scheduler.step()
# Evaluation
with torch.no_grad():
# accuracy for old task from old model
num_class=config['seen_class']
old_model.eval()
mean_correct = []
for j, data in enumerate(testDataLoaderOld):
points, target = data
target = target[:, 0]
points = points.permute(0, 2, 1)
points, target = points.to(device).float(), target.to(device)
if config['sem']=='none':
pred = old_model(points)
else:
pred = old_model(points, old_att)
pred_choice = pred.data.max(1)[1]
correct = pred_choice.eq(target.long().data).cpu().sum()
mean_correct.append(correct.item()/float(points.size()[0]))
instance_acc = np.mean(mean_correct)*100
print('Accuracy of old task from old model :', round(instance_acc,2))
# accuracy for old task from new model
num_class = config['seen_class']
new_model.eval()
mean_correct = []
for j, data in enumerate(testDataLoaderOld):
points, target = data
target = target[:, 0]
points = points.permute(0, 2, 1)
points, target = points.to(device), target.to(device)
if config['sem']!='none':
pred, _ = new_model(points.float(), old_att, new_att) # with w2v
else:
pred, _ = new_model(points.float()) # without w2v
pred_choice = pred.data.max(1)[1]
correct = pred_choice.eq(target.long().data).cpu().sum()
mean_correct.append(correct.item()/float(points.size()[0]))
old_acc = np.mean(mean_correct)*100
print('Accuracy of old task from new model :', round(old_acc,2))
# accuracy for new task from new model
num_class = config['unseen_class']
new_model.eval()
mean_correct = []
for j, data in enumerate(testDataLoaderNew):
points, target = data
target = target[:, 0]
points = points.permute(0, 2, 1)
points, target = points.to(device), target.to(device)
if config['sem']!='none':
_, pred = new_model(points.float(), old_att, new_att) # with w2v
else:
_, pred = new_model(points.float()) # without w2v
pred_choice = pred.data.max(1)[1]
correct = pred_choice.eq(target.long().data).cpu().sum()
mean_correct.append(correct.item()/float(points.size()[0]))
new_acc = np.mean(mean_correct)*100
print('Accuracy of new task from new model :', round(new_acc,2))
diff = round((instance_acc-old_acc)*100/instance_acc, 2)
print('Difference :', diff)