-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
143 lines (109 loc) · 5.2 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import numpy as np
from model.net import Net
from dataset.dataset import Kidney_Dataset
import argparse
import pandas as pd
from tqdm import tqdm
def parsing():
parser = argparse.ArgumentParser()
parser.add_argument('--classnum', '--c', type=int, default=2)
parser.add_argument('--data', '--d', type=str, default='preprocessed_data/tsaoapp_with_filter_data.csv')
parser.add_argument('--label', '--l', type=str, default='preprocessed_data/tsaoapp_with_filter_label.csv')
parser.add_argument('--epoch', '--e', type=int, default=100)
parser.add_argument('--lr_rate', '--lr', type=float, default=1e-3)
parser.add_argument('--warmup', type=int, default=30)
parser.add_argument('--batch-size', '--b', type=int, default=512)
parser.add_argument('--device', type=str, default='cuda')
parser.add_argument('--optimizer', '--opt', type=str, default='Adam')
parser.add_argument('--save_loss', type=str, default='runs/loss/out.csv')
parser.add_argument('--save_acc', type=str, default='runs/acc/out.csv')
opt = parser.parse_args()
return opt
def train(opt):
print(opt)
#####################
# Hyperparameter
warm_up_epoch = opt.warmup
EPOCH = opt.epoch + warm_up_epoch
Batch_size = opt.batch_size
Learning_rate = opt.lr_rate
init_lr = 1e-5
data_path = opt.data
lable_path = opt.label
#####################
device = torch.device(opt.device)
dataset = Kidney_Dataset(data_path, lable_path)
train_set_size = int(np.round(0.9 * dataset.__len__()))
valid_set_size = dataset.__len__() - train_set_size
print(f'train_set_size = {train_set_size}')
print(f'valid_set_size = {valid_set_size}')
train_set, val_set = torch.utils.data.random_split(dataset, [train_set_size, valid_set_size])
trainloader = DataLoader(train_set, batch_size=Batch_size, shuffle=True)
validloader = DataLoader(val_set, batch_size=Batch_size)
train_batch_num = train_set_size / Batch_size
valid_batch_num = valid_set_size / Batch_size
net = Net(opt.classnum)
net.to(device)
criterion = nn.CrossEntropyLoss()
if opt.optimizer == 'Adam':
optimizer = optim.Adam([ {'params':net.parameters(), 'lr':init_lr}], lr=Learning_rate)
elif opt.optimizer == 'AdamW':
optimizer = optim.AdamW([ {'params':net.parameters(), 'lr':init_lr}], lr=Learning_rate)
elif opt.optimizer == 'SGD':
optimizer = optim.SGD([ {'params':net.parameters(), 'lr':init_lr}], lr=Learning_rate, momentum=0.9)
loss_pack = []
val_acc = []
for epoch in (range(1, EPOCH+1)):
running_loss = 0.0
val_loss = 0.0
val_image_num = 0
val_hit = 0
train_hit = 0
train_image_num = 0
#learning rate scheduling
if epoch <= warm_up_epoch:
optimizer.param_groups[0]['lr']= (Learning_rate - init_lr)/(warm_up_epoch-1) * (epoch-1) + init_lr
elif epoch == 75:
optimizer.param_groups[0]['lr']*= 0.05
elif epoch == 100:
optimizer.param_groups[0]['lr']*= 0.01
print(f'lr: %8.5f'%(optimizer.param_groups[0]['lr']))
for i, data in enumerate(tqdm((trainloader))):
inputs, labels = data['Data'].to(device), data['Label'].to(device)
optimizer.zero_grad()
outputs = net(inputs.float())
labels = labels.view(labels.shape[0])
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
train_image_num += inputs.shape[0]
for i in range(len(outputs)):
if(np.argmax(outputs[i].cpu().detach().numpy())==labels[i]):
train_hit += 1
with torch.no_grad():
for data in validloader:
inputs, labels = data['Data'].to(device), data['Label'].to(device)
outputs = net(inputs.float())
labels = labels.view(labels.shape[0])
loss = criterion(outputs, labels)
val_loss += loss.item()
val_image_num += inputs.shape[0]
for i in range(len(outputs)):
if(np.argmax(outputs[i].cpu().detach().numpy())==labels[i]):
val_hit += 1
print('Epoch:%3d'%epoch, '|Train Loss:%8.4f'%(running_loss/train_batch_num), '|Train Acc:%3.4f'%(train_hit/(train_image_num)*100.0))
print('Epoch:%3d'%epoch, '|Valid Loss:%8.4f'%(val_loss/valid_batch_num), '|Valid Acc:%3.4f'%(val_hit/(val_image_num)*100.0))
val_acc.append((val_hit/(val_image_num)*100.0))
loss_pack.append(val_loss/valid_batch_num)
loss_df = pd.DataFrame(data=loss_pack)
val_acc_df = pd.DataFrame(data=val_acc)
loss_df.to_csv(opt.save_loss, index_label=False, index = False,header = False)
val_acc_df.to_csv(opt.save_acc, index_label=False, index = False,header = False)
if __name__ == "__main__":
opt = parsing()
train(opt)