-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
188 lines (146 loc) · 6.14 KB
/
main.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
182
183
184
185
186
187
188
import argparse
import helper
from config import Config
from json_helper.json_parser import ParseJson
from dataReader.dataset_reader import CreateDataset
from torch.utils.data import DataLoader
from models.resnet import ResNet50, ResNet101, ResNet152
from models.siamese2 import SiameseNetwork
from models.Siamese_EfficientNet import SiameseEff
from loss.loss_func import ContrastiveLoss
from models.siamese import Siamese
from torch.nn import BCEWithLogitsLoss
import torch.nn.functional as F
from sklearn.metrics import accuracy_score
import torch
from tqdm import tqdm
import os
import matplotlib.pyplot as plt
import numpy as np
#criterion = torch.nn.CosineEmbeddingLoss()
DEVICE = helper.get_device()
SAVE_MODEL = np.inf
def train(model, train_loader, val_loader, cfg, training_loss_graph, val_loss_graph, name):
global SAVE_MODEL
SAVE_MODEL = np.inf
optimizer = cfg.set_get_optim(model.parameters(),
cfg.learning_rate)
lr_scheduler = cfg.get_lr_scheduler(optimizer)
for i in range(0, cfg.epochs):
train_loss = 0.0
epoch_loss = 0.0
predictions = list()
true_labels = list()
model.train()
for img1, img2, lbl in tqdm(train_loader):
img1 = img1.to(DEVICE)
img2 = img2.to(DEVICE)
lbl = lbl.to(DEVICE)
optimizer.zero_grad()
output1, output2, output = model(img1, img2)
if cfg.criterion.__class__.__name__ == "ContrastiveLoss":
loss = cfg.criterion(output1, output2, lbl)
loss.backward()
train_loss += loss.item()
elif cfg.criterion.__class__.__name__ == "HingeEmbeddingLoss":
loss = cfg.criterion(output, lbl)
loss.backward()
train_loss += loss.item()
optimizer.step()
#pred = F.pairwise_distance(output1, output2)
lr_scheduler.step()
epoch_loss = train_loss / len(train_loader)
#print("Training Accuracy", accuracy_score(y_true=true_labels, y_pred=predictions))
training_loss_graph.append(epoch_loss)
print('Training Epoch: {}/{} || Loss: {:.4f} '.format(i+1, cfg.epochs, epoch_loss))
validation(model, val_loader, cfg, val_loss_graph, name)
def validation(model, val_loader, cfg, val_loss_graph, name):
model.eval()
val_loss, epoch_vloss = 0.0, 0.0
predictions = list()
true_labels = list()
for img1, img2, lbl in tqdm(val_loader):
img1, img2, lbl = img1.to(DEVICE), img2.to(DEVICE), lbl.to(DEVICE)
output1, output2, output = model(img1, img2)
if cfg.criterion.__class__.__name__ == "ContrastiveLoss":
loss = cfg.criterion(output1, output2, lbl)
loss.backward()
val_loss += loss.item()
elif cfg.criterion.__class__.__name__ == "HingeEmbeddingLoss":
loss = cfg.criterion(output, lbl)
loss.backward()
val_loss += loss.item()
epoch_vloss = val_loss / len(val_loader)
val_loss_graph.append(epoch_vloss)
#print("Validation Accuracy", accuracy_score(y_true=true_labels, y_pred=predictions))
print('Validation || Loss: {:.4f} '.format(epoch_vloss))
#print(predictions)
global SAVE_MODEL
model_path = os.path.join(cfg.save_model_path, name + '.pth')
if epoch_vloss < SAVE_MODEL:
SAVE_MODEL = epoch_vloss
torch.save(model.state_dict(), model_path)
print('Model Saved!')
def main():
args = helper.parse_args()
cfg = Config(args)
helper.check_create_paths(cfg.save_model_path,
cfg.save_plot_path,
cfg.save_roc_path)
print("Parsing JSON...")
json_dataset_parser = ParseJson()
print("Parsing Finished.. \nCreating Train/Val sets")
train_img, train_lbl, val_img, val_lbl = json_dataset_parser.get_train_val(args.train_json, args.val_json)
#print(np.unique(train_lbl, return_counts=True))
train_dataset = CreateDataset(train_img,
train_lbl,
transform=cfg.transform)
val_dataset = CreateDataset(val_img,
val_lbl,
transform=cfg.transform)
train_loader = DataLoader(train_dataset,
batch_size=cfg.train_batch,
num_workers=cfg.num_workers,
shuffle=True,
pin_memory=True)
val_loader = DataLoader(val_dataset,
batch_size=cfg.val_batch,
num_workers=cfg.num_workers,
shuffle=True,
pin_memory=True)
# for img1, img2, lbl in train_loader:
# print(lbl)
#
#
# #model = Cnn()
models = [Siamese(), SiameseNetwork(), SiameseEff(), ResNet50(), ResNet101(), ResNet152()]
names = ["Siamese", "SiameseNetwork", "SiameseEfficientNet", "ResNet50", "ResNet101", "ResNet152"]
for model, name in zip(models, names):
training_loss_graph = list()
val_loss_graph = list()
#model = SiameseNetwork()
model = model.to(DEVICE)
model_path = os.path.join(cfg.save_model_path, name+'.pth')
if os.path.isfile(model_path):
print('Saved Model found. Loading...')
model.load_state_dict(torch.load(model_path))
print("Training started")
train(model,
train_loader,
val_loader,
cfg, training_loss_graph, val_loss_graph, name)
print('Finished Training...')
epochs = range(1, cfg.epochs+1)
plt_path = os.path.join(cfg.save_plot_path , 'loss curve '+ name +'.png')
plt.figure(figsize=(7,7))
plt.plot(epochs, training_loss_graph, 'g', label='Training loss')
plt.plot(epochs, val_loss_graph, 'b', label='validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.rcParams["font.size"] = "20"
plt.savefig(plt_path)
#plt.show()
if __name__ == '__main__':
main()