-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
158 lines (117 loc) · 6.07 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import argparse
import cv2
import numpy as np
from tqdm import tqdm
import torch
import torchvision.utils as vutils
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import matplotlib.pyplot as plt
from siamese import SiameseNetwork
from libs.plant_dataset import PlantDataset
from libs.masked_plant_dataset import MaskedPlantDataset
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--name',type=str,help="Name of this experiment.",required=True)
parser.add_argument('--train_csv_file',type=str,help="Name of csv file containing training dataset.",required=True)
parser.add_argument('--val_csv_file',type=str,help="Name of csv file containing validation dataset.",required=True)
parser.add_argument('--root_dir',type=str,help="Path to train image and csv files.",required=True)
parser.add_argument('-o','--out_path',type=str,help="Path for outputting model weights and tensorboard summary.",required=True)
parser.add_argument('-b','--backbone',type=str,help="Network backbone from torchvision.models to be used in the siamese network.",default="resnet18")
parser.add_argument('-lr','--learning_rate',type=float,help="Learning Rate",default=1e-4)
parser.add_argument('-bs','--batch_size',type=int,help="Batch size",default=12)
parser.add_argument('-e','--epochs',type=int,help="Number of epochs to train",default=120)
parser.add_argument('-op', '--optimizer',type=str,help="Optimizer to use.",default="SGD")
parser.add_argument('-s','--save_after',type=int,help="Model checkpoint is saved after each specified number of epochs.",default=5)
args = parser.parse_args()
print(f"Starting experiment: {args.name}\n")
os.makedirs(args.out_path, exist_ok=True)
os.makedirs(os.path.join(args.out_path, args.name), exist_ok=True)
# Set device to CUDA if a CUDA device is available, else CPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Creating Datasets\n")
print(args.train_csv_file)
train_dataset = PlantDataset(args.train_csv_file, args.root_dir)
val_dataset = PlantDataset(args.val_csv_file, args.root_dir)
train_loader = DataLoader(train_dataset, batch_size=args.batch_size, drop_last=True, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=args.batch_size, drop_last=True)
print(f"Train Dataset Size: {len(train_dataset)}\n")
print(f"Validation Dataset Size: {len(val_dataset)}\n")
print("Datasets Created\n\n")
print("Creating Model\n")
model = SiameseNetwork(backbone=args.backbone)
model.to(device)
print("Model Created\n")
optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate, weight_decay=1e-5)
criterion = torch.nn.MSELoss()
writer = SummaryWriter(os.path.join(args.out_path, args.name, "summary"))
print("Starting Training\n")
best_val_loss = 10000.0
best_checkpoint = None
loss_history = []
val_history = []
for epoch in range(args.epochs):
losses = []
val_losses = []
#Training Phase
model.train()
pbar = tqdm(train_loader, desc=f'Training Epoch {epoch}')
for i, data in enumerate(pbar, 0):
img1, img2, labels = data
img1, img2, labels = map(lambda x: x.to(device), [img1, img2, labels])
optimizer.zero_grad()
output = model(img1, img2)
loss = criterion(output, labels.float().unsqueeze(1))
loss.backward()
optimizer.step()
losses.append(loss.item())
writer.add_scalar('train_loss', sum(losses)/len(losses), epoch)
pbar.set_postfix({'output': output.tolist()[0][0], 'label': labels.tolist()[0], 'total loss': sum(losses)/len(losses)})
if i % 100 == 0:
vutils.save_image(img1[0], os.path.join(args.out_path, args.name, f"image_{i}.png"))
loss_history.append(sum(losses)/len(losses))
#Validation Phase
with torch.no_grad():
val_pbar = tqdm(val_loader, desc=f'Validation Epoch {epoch}')
for i, data in enumerate(val_pbar, 0):
img1, img2, labels = data
img1, img2, labels = map(lambda x: x.to(device), [img1, img2, labels])
output = model(img1, img2)
loss = criterion(output, labels.float().unsqueeze(1))
val_losses.append(loss.item())
val_loss = sum(val_losses)/len(val_losses)
writer.add_scalar('val_loss', val_loss, epoch)
val_pbar.set_postfix({'validation loss': val_loss})
if val_loss < best_val_loss:
best_val_loss = val_loss
best_checkpoint = {
'epoch': epoch + 1,
'model_state_dict': model.state_dict(),
"backbone": args.backbone,
'optimizer_state_dict': optimizer.state_dict(),
'loss': val_loss
}
torch.save(best_checkpoint, os.path.join(args.out_path, args.name, "best_model.pth"))
print(f"Saving best at epoch {epoch}\n")
val_history.append(sum(val_losses)/len(val_losses))
if (epoch + 1) % args.save_after == 0:
torch.save(
{
"epoch": epoch + 1,
"model_state_dict": model.state_dict(),
"backbone": args.backbone,
"optimizer_state_dict": optimizer.state_dict()
},
os.path.join(args.out_path, args.name, "epoch_{}.pth".format(epoch + 1))
)
print(f"Saving Epoch {epoch}\n")
# Plot the training loss
plt.plot(loss_history, label='train loss', color="blue")
plt.plot(val_history, label='val loss', color="orange")
plt.title('Training Loss')
plt.xlabel('Epoch')
plt.ylabel('MSE Loss')
plt.savefig(os.path.join(args.out_path, args.name, 'loss_plot_epoch'+str(epoch)+'.png'))
plt.close()
print("Concluded Training\n")