-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_VoxNet_v4.py
181 lines (154 loc) · 6.54 KB
/
main_VoxNet_v4.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
#%%
import json
import time
import torch as t
import torch.utils.data.dataloader as DataLoader
import multiprocessing
from model.dataloader_v2 import *
from model.DnCNN import DnCNN
from model import Resnet
from model import Conv3D_Net
from model.VoxNet_v4 import VoxNet
from model.baseline import FC_Net
from model.func import save_model, eval_model_new_thread, eval_model, load_model
import argparse
from tensorboardX import SummaryWriter
from sklearn.model_selection import KFold
#%%
time_start = time.time()
config = json.load(open("config.json"))
# os.environ["CUDA_VISIBLE_DEVICES"] = '1'
DEVICE = t.device(config["DEVICE"])
LR = config['lr']
LR = 1e-5
EPOCH = config['epoch']
WD = config['Weight_Decay']
parser = argparse.ArgumentParser()
parser.add_argument(
"--gpu", default=config["GPU"], type=str, help="choose which DEVICE U want to use")
parser.add_argument("--epoch", default=0, type=int,
help="The epoch to be tested")
parser.add_argument("--lr", default=LR, type=float,
help="The epoch to be tested")
parser.add_argument("--name", default='VoxNet_v4(200)_{}'.format(LR), type=str,
help="Whether to test after training")
args = parser.parse_args()
LR = args.lr
DataSet = MyDataSet()
# using K-fold
np.random.seed(1998)
kf = KFold(n_splits=5)
idx = np.arange(len(DataSet))
np.random.shuffle(idx)
print(args.name, kf.get_n_splits(idx))
# shuffle the data before the
for K_idx, [train_idx, test_idx] in enumerate(kf.split(idx)):
writer = SummaryWriter('runs/{}_{}_Fold'.format(args.name, K_idx+1))
train_data, test_data = data_set(train_idx), data_set(test_idx)
# train_data.data_argumentation()
train_loader = DataLoader.DataLoader(
train_data, batch_size=config["batch_size"], shuffle=True, num_workers=config["num_workers"])
test_loader = DataLoader.DataLoader(
test_data, batch_size=1, shuffle=False, num_workers=config["num_workers"])
model = VoxNet(2).to(DEVICE)
optimizer = t.optim.SGD(model.parameters(), lr=LR)
print(optimizer.param_groups[0]['lr'])
# optimizer = t.optim.Adam(model.parameters())
criterian = t.nn.CrossEntropyLoss().to(DEVICE)
# Test the train_loader
for epoch in range(args.epoch, EPOCH):
model = model.train()
train_loss = 0
correct = 0
# if epoch>50:
# optimizer.param_groups[0]['lr'] = 1e-5
for batch_idx, [data, label] in enumerate(train_loader):
data, label = data.to(DEVICE), label.to(DEVICE)
out = model(data).squeeze()
loss = criterian(out, label)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss += loss
pred = out.max(1, keepdim=True)[1] # 找到概率最大的下标
correct += pred.eq(label.view_as(pred)).sum().item()
train_loss /= len(train_loader.dataset)
train_acc = 100. * correct / len(train_loader.dataset)
# train_l.append(train_loss)
# train_a.append(train_acc)
print('\nEpoch: {}, Train set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
epoch, train_loss, correct, len(train_loader.dataset), train_acc))
model = model.eval()
with t.no_grad():
# Test the test_loader
test_loss = 0
correct = 0
for batch_idx, [data, label] in enumerate(test_loader):
data, label = data.to(DEVICE), label.to(DEVICE)
out = model(data)
# monitor the upper and lower boundary of output
# out_max = t.max(out)
# out_min = t.min(out)
# out = (out - out_min) / (out_max - out_min)
test_loss += criterian(out, label)
pred = out.max(1, keepdim=True)[1] # 找到概率最大的下标
correct += pred.eq(label.view_as(pred)).sum().item()
# store params
for name, param in model.named_parameters():
writer.add_histogram(
name, param.clone().cpu().data.numpy(), epoch)
test_loss /= len(test_loader.dataset)
test_acc = 100. * correct / len(test_loader.dataset)
# test_l.append(test_loss)
# test_a.append(test_acc)
print('Epoch: {}, Test set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
epoch, test_loss, correct, len(test_loader.dataset), test_acc))
save_model(model, epoch, '{}_{}_folds'.format(args.name, K_idx+1))
# eval_model_new_thread(epoch, 0)
# LZX pls using the following code instead
# multiprocessing.Process(target=eval_model(epoch, '0'), args=(multiprocess_idx,))
# multiprocess_idx += 1
writer.add_scalar('Training/Training_Loss', train_loss, epoch)
writer.add_scalar('Training/Training_Acc', train_acc, epoch)
writer.add_scalar('Testing/Testing_Loss', test_loss, epoch)
writer.add_scalar('Testing/Testing_Acc', test_acc, epoch)
writer.close()
#%%
training_loss = np.zeros(EPOCH, dtype=np.float)
testing_loss = np.zeros(EPOCH, dtype=np.float)
training_acc = np.zeros(EPOCH, dtype=np.float)
testing_acc = np.zeros(EPOCH, dtype=np.float)
# compute the mean acc and loss
import os
from tensorboard.backend.event_processing import event_accumulator
dirs = ['runs/{}_{}_Fold'.format(args.name, i+1) for i in range(5)]
writer = SummaryWriter('runs/{}'.format(args.name))
for dir in dirs:
try:
data = os.listdir(dir)
except:
break
print(data)
ea = event_accumulator.EventAccumulator(os.path.join(dir, data[0]))
ea.Reload()
# print(ea.scalars.Keys())
train_loss = ea.scalars.Items('Training/Training_Loss')
training_loss += np.array([i.value for i in train_loss])
train_acc = ea.scalars.Items('Training/Training_Acc')
training_acc += np.array([i.value for i in train_acc])
test_loss = ea.scalars.Items('Testing/Testing_Loss')
testing_loss += np.array([i.value for i in test_loss])
test_acc = ea.scalars.Items('Testing/Testing_Acc')
testing_acc += np.array([i.value for i in test_acc])
training_loss /= 5
training_acc /= 5
testing_loss /=5
testing_acc /= 5
print(training_acc)
for epoch in range(EPOCH):
writer.add_scalar('Training/Training_Loss', training_loss[epoch], epoch)
writer.add_scalar('Training/Training_Acc', training_acc[epoch], epoch)
writer.add_scalar('Testing/Testing_Loss', testing_loss[epoch], epoch)
writer.add_scalar('Testing/Testing_Acc', testing_acc[epoch], epoch)
writer.close()
# %%