-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
137 lines (110 loc) · 4.06 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
import torch
import torch.nn as nn
import torch.utils
from torch.utils.data import DataLoader
import torch.utils.data
import torchvision.transforms as transforms
from dataset import DementiaDataset
from model import DementiaModel
DATA_PATH = 'all_images'
CSV_FILE = 'labels.csv'
EPOCHS = 100
LEARNING_RATE = 1e-4
BATCH_SIZE = 16
SAVING_PATH = './'
device = torch.device('cuda' if torch.cuda.is_available() else 'mps')
def get_data_loader(dataset, batch_size):
dataloader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True)
return dataloader
def train_one_epoch(model, train_dataloader, test_dataloader, criterion, optimizer, device):
"""
We set the model to train mode. We also create these varaiables to compute performance
- total_train_loss
- correct_train
- total_train
"""
model.train()
total_train_loss = 0
correct_train = 0
total_train = 0
for inputs, targets in train_dataloader:
inputs, targets = inputs.to(device), targets.to(device)
y_hat = model(inputs)
loss = criterion(y_hat, targets)
total_train_loss += loss.item()
_, predicted = torch.max(y_hat.data, 1)
total_train += targets.size(0)
correct_train += (predicted == targets).sum().item()
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_acc = 100 * correct_train / total_train
train_loss = total_train_loss / len(train_dataloader)
"""
Now we set up the evaluation loop
"""
model.eval()
total_test_loss = 0
correct_test = 0
total_test = 0
with torch.no_grad():
for inputs, targets in test_dataloader:
inputs, targets = inputs.to(device), targets.to(device)
y_hat_test = model(inputs)
loss = criterion(y_hat_test, targets)
total_test_loss += loss.item()
_, test_predicted = torch.max(y_hat_test.data, 1)
total_test += targets.size(0)
correct_test += (test_predicted == targets).sum().item()
test_acc = 100 * correct_test / total_test
test_loss = total_test_loss / len(test_dataloader)
return train_acc, train_loss, test_acc, test_loss
def train(epochs, model, train_dataloader, test_dataloader, criterion, optimizer, device):
for epoch in range(epochs):
print(f"Epoch: {epoch+1}")
train_acc, train_loss, test_acc, test_loss = train_one_epoch(
model=model,
train_dataloader=train_dataloader,
test_dataloader=test_dataloader,
criterion=criterion,
optimizer=optimizer,
device=device
)
print(f"Train loss: {train_loss:.2f} - Train acc: {train_acc:.2f} || Test loss: {test_loss:.2f} - Test acc: {test_acc:.2f}")
print("-"*50)
print('Training finished!')
if __name__ == '__main__':
img_transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.ToTensor(),
transforms.Normalize([0.2786], [0.3268])
])
dataset = DementiaDataset(
csv_file='labels.csv',
dir_to_images='all_images',
transform=img_transform
)
# total dataset size = 6400
train_size = int(len(dataset) * 0.8)
test_size = int(len(dataset) - train_size)
train_split, test_split = torch.utils.data.random_split(dataset, [train_size, test_size])
train_dataloader = get_data_loader(train_split, batch_size=BATCH_SIZE)
test_dataloader = get_data_loader(test_split, batch_size=BATCH_SIZE)
cnn_model = DementiaModel(
in_channels=1,
n_filters=8,
classes=4,
dropout_rate=0.5
).to(device)
loss_criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.AdamW(cnn_model.parameters(), lr=LEARNING_RATE)
train(
epochs=EPOCHS,
model=cnn_model,
train_dataloader=train_dataloader,
test_dataloader=test_dataloader,
criterion=loss_criterion,
optimizer=optimizer,
device=device
)
torch.save(cnn_model.state_dict(), 'dementia_detection_model.pth')