-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrain_validate.py
171 lines (139 loc) · 7.19 KB
/
Train_validate.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
from os import write
import os
from sklearn.utils import shuffle
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
import torch.optim as optim
from sklearn.metrics import classification_report
import numpy as np
from MyDataLoader import MyNoiseDataset
from Bcolors import bcolors
from Network import network
BATCH_SIZE = 250
EPOCHS = 30
# using uniform distribution for weight initialization
def init_weights(m):
if isinstance(m, torch.nn.Conv1d):
torch.nn.init.xavier_uniform_(m.weight.data)
def create_data_loader(train_data, batch_size):
train_dataloader = DataLoader(train_data, batch_size)
return train_dataloader
def train_single_epoch(model, data_loader, loss_fn, optimizer, device):
train_loss = 0
train_acc = 0
model.train()
for input, target in data_loader:
input, target = input.to(device), target.to(device)
# calculate loss
prediction = model(input)
loss = loss_fn(prediction,target)
# backpropagate error and update weights,
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Recording the loss and accuracy
train_loss += loss.item()
_, pred = prediction.max(1)
num_correct = (pred == target).sum().item()
acc = num_correct / input.shape[0]
train_acc += acc
print(f"Training Loss: {train_loss/len(data_loader)}" + f" Training Accuracy: {train_acc / len(data_loader)}")
return train_acc/len(data_loader), train_loss/len(data_loader)
def validate_single_epoch(model, eva_data_loader, loss_fn, device):
eval_loss = 0
eval_acc = 0
model.eval()
for input, target in eva_data_loader:
input, target = input.to(device), target.to(device)
# Calculating the loss value
prediction = model(input)
loss = loss_fn(prediction,target)
# recording the validating loss and accuratcy
eval_loss += loss.item()
_, pred = prediction.max(1)
num_correct = (pred == target).sum().item()
acc = num_correct / input.shape[0]
eval_acc += acc
print(f"Validation Loss : {eval_loss/len(eva_data_loader)}" + f" Validation Accuracy : {eval_acc/len(eva_data_loader)}")
return eval_acc/len(eva_data_loader), eval_loss/len(eva_data_loader)
def train(model, data_loader, eva_data_loader, epochs, device, MODEL_PTH=None):
acc_max = 0
acc_train_max = 0
loss_fn = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.01, weight_decay=1e-4) # L2 regularization
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1) # reduce the learning after 10 epochs
train_loss_epochs = []
validate_loss_epochs = []
for i in range(epochs):
print(f"Epoch {i+1}")
print("Learning rate:", optimizer.param_groups[0]['lr'])
acc_train, train_loss_epoch = train_single_epoch(model, data_loader, loss_fn, optimizer, device)
acc_validate, validate_loss_epoch = validate_single_epoch(model, eva_data_loader, loss_fn, device)
scheduler.step() # after every epoch update learning rate
train_loss_epochs.append(train_loss_epoch)
validate_loss_epochs.append(validate_loss_epoch)
if acc_validate > acc_max:
acc_train_max, acc_max = acc_train, acc_validate
torch.save(model.state_dict(), MODEL_PTH)
print(bcolors.OKCYAN+ "Trained feed forward net saved at " + MODEL_PTH + bcolors.ENDC)
print("----------------------------------")
print("Finished trainning")
return acc_train_max, acc_max, train_loss_epochs, validate_loss_epochs
def Continue_train(model, data_loader, eva_data_loader, epochs, device, Pretrained_MODEL_PTH, MODEL_PTH):
acc_max = 0
acc_train_max = 0
loss_fn = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.01, weight_decay=1e-4) # L2 regularization
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1) # reduce the learning after 10 epochs
train_loss_epochs = []
validate_loss_epochs = []
model.load_state_dict(torch.load(Pretrained_MODEL_PTH)) # load pre-trained model parameters
for i in range(epochs):
print(f"Epoch {i+1}")
print("Learning rate:", optimizer.param_groups[0]['lr'])
acc_train, train_loss_epoch = train_single_epoch(model, data_loader, loss_fn, optimizer, device)
acc_validate, validate_loss_epoch = validate_single_epoch(model, eva_data_loader, loss_fn, device)
scheduler.step() # after every epoch update learning rate
train_loss_epochs.append(train_loss_epoch)
validate_loss_epochs.append(validate_loss_epoch)
if acc_validate > acc_max:
acc_train_max, acc_max = acc_train, acc_validate
torch.save(model.state_dict(), MODEL_PTH)
print(bcolors.OKCYAN+ "Trained feed forward net saved at " + MODEL_PTH + bcolors.ENDC)
print("----------------------------------")
print("Finished trainning")
return acc_train_max, acc_max, train_loss_epochs, validate_loss_epochs
#----------------------------------------------------------------------------------------
# Function : Training and validating 1D-CNN
#----------------------------------------------------------------------------------------
def Train_Validate_CNN(TRIAN_DATASET_FILE, VALIDATION_DATASET_FILE, MODEL_PTH, File_sheet):
train_data = MyNoiseDataset(TRIAN_DATASET_FILE, File_sheet)
valid_data = MyNoiseDataset(VALIDATION_DATASET_FILE, File_sheet)
train_dataloader = create_data_loader(train_data, BATCH_SIZE)
valid_dataloader = create_data_loader(valid_data, BATCH_SIZE)
# set the model
model = network
model.apply(init_weights)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # begin from #0 gpu
model = model.to(device)
# train model
acc_train, acc_validate, train_loss_epochs, validate_loss_epochs = train(model, train_dataloader, valid_dataloader, EPOCHS, device, MODEL_PTH)
return acc_train, acc_validate, train_loss_epochs, validate_loss_epochs
#----------------------------------------------------------------------------------------
# Function : Continue training and validating pre-trained CNN
#----------------------------------------------------------------------------------------
def Continue_Train_Validate_CNN(TRIAN_DATASET_FILE, VALIDATION_DATASET_FILE, Pretrained_MODEL_PTH, MODEL_PTH, File_sheet):
train_data = MyNoiseDataset(TRIAN_DATASET_FILE,File_sheet)
valid_data = MyNoiseDataset(VALIDATION_DATASET_FILE,File_sheet)
train_dataloader = create_data_loader(train_data, BATCH_SIZE)
valid_dataloader = create_data_loader(valid_data, BATCH_SIZE)
# set the model
model = network
model.apply(init_weights)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # begin from #0 gpu
model = model.to(device)
# train model
acc_train, acc_validate, train_loss_epochs, validate_loss_epochs = Continue_train(model, train_dataloader, valid_dataloader, EPOCHS, device, Pretrained_MODEL_PTH, MODEL_PTH)
return acc_train, acc_validate, train_loss_epochs, validate_loss_epochs