-
Notifications
You must be signed in to change notification settings - Fork 7
/
train.py
142 lines (113 loc) · 4.1 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
import os
import time
import random
import numpy as np
from glob import glob
import cv2
import torch
from torch.utils.data import DataLoader
import torch.nn as nn
from data import load_data, KvasirDataset
from utils import (
seeding,shuffling, make_channel_first, make_channel_last, create_dir, epoch_time, print_and_save
)
from model import CompNet
from loss import DiceLoss, DiceBCELoss
def train(model, loader, optimizer, loss_fn, device):
epoch_loss = 0
model.train()
for i, (x, y, m) in enumerate(loader):
x = x.to(device, dtype=torch.float32)
y = y.to(device, dtype=torch.float32)
m = m.to(device, dtype=torch.float32)
optimizer.zero_grad()
y_pred, m_pred = model(x)
loss1 = loss_fn(y_pred, y)
loss2 = nn.BCEWithLogitsLoss(m_pred, m)
loss = loss1 + loss2
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_loss = epoch_loss/len(loader)
return epoch_loss
def evaluate(model, loader, loss_fn, device):
epoch_loss = 0
model.eval()
with torch.no_grad():
for i, (x, y, m) in enumerate(loader):
x = x.to(device)
y = y.to(device)
m = m.to(device)
y_pred, m_pred = model(x)
loss1 = loss_fn(y_pred, y)
loss2 = nn.BCEWithLogitsLoss(m_pred, m)
loss = loss1 + loss2
epoch_loss += loss.item()
epoch_loss = epoch_loss/len(loader)
return epoch_loss
if __name__ == "__main__":
""" Seeding """
seeding(42)
""" Directories """
create_dir("files")
""" Training logfile """
train_log = open("files/train_log.txt", "w")
""" Load dataset """
train_x = sorted(glob("new_data/train/image/*"))[:100]
train_y = sorted(glob("new_data/train/mask/*"))[:100]
valid_x = sorted(glob("new_data/test/image/*"))
valid_y = sorted(glob("new_data/test/mask/*"))
train_x, train_y = shuffling(train_x, train_y)
data_str = f"Dataset Size:\nTrain: {len(train_x)} - Valid: {len(valid_x)}\n"
print_and_save(train_log, data_str)
""" Hyperparameters """
size = (512, 512)
batch_size = 1
num_epochs = 50
lr = 1e-4
checkpoint_path = "files/checkpoint.pth"
""" Dataset and loader """
train_dataset = KvasirDataset(train_x, train_y, size)
valid_dataset = KvasirDataset(valid_x, valid_y, size)
train_loader = DataLoader(
dataset=train_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=2
)
valid_loader = DataLoader(
dataset=valid_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=2
)
""" Model """
device = torch.device('cuda')
model = CompNet()
# model.load_state_dict(torch.load(checkpoint_path, map_location=device))
model = model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=5, verbose=True)
loss_fn = nn.BCEWithLogitsLoss()
# loss_fn = nn.BCEWithLogitsLoss()
loss_fn = DiceBCELoss()
loss_name = "BCE Dice Loss"
data_str = f"Hyperparameters:\nImage Size: {size}\nBatch Size: {batch_size}\nLR: {lr}\nEpochs: {num_epochs}\n"
data_str += f"Optimizer: Adam\nLoss: {loss_name}\n"
print_and_save(train_log, data_str)
""" Training the model. """
best_valid_loss = float('inf')
for epoch in range(num_epochs):
start_time = time.time()
train_loss = train(model, train_loader, optimizer, loss_fn, device)
valid_loss = evaluate(model, valid_loader, loss_fn, device)
scheduler.step(valid_loss)
if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
torch.save(model.state_dict(), checkpoint_path)
end_time = time.time()
epoch_mins, epoch_secs = epoch_time(start_time, end_time)
data_str = f'Epoch: {epoch+1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s\n'
data_str += f'\tTrain Loss: {train_loss:.3f}\n'
data_str += f'\t Val. Loss: {valid_loss:.3f}\n'
print_and_save(train_log, data_str)