-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_utils.py
61 lines (54 loc) · 2.15 KB
/
model_utils.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
from typing import Tuple, List
import torch
from torch.nn import Module
from torch.utils.data import DataLoader
from torchvision.transforms import Compose
from torchvision.datasets import ImageFolder
def testAccuracy(dataPath: str, model: Module, transform: Compose, device) -> float:
testset = ImageFolder(root=dataPath, transform=transform)
testloader = DataLoader(testset, batch_size=10, shuffle=False)
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Accuracy of the network on the images: {100 * correct / total} on {dataPath}')
return 100 * correct / total
def trainModel(
model: torch.nn.Module,
dataloader: DataLoader,
optimizer: torch.optim.Optimizer,
criterion: torch.nn.Module,
transform: Compose,
numEpochs: int = 15
) -> Tuple[List[float], List[float]]:
train_acc_history = []
val_acc_history = []
i = 0
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(f'TRAINING ON {device}')
for epoch in range(numEpochs):
model.train()
for images, labels in dataloader:
images = images.to(device)
labels = labels.to(device)
optimizer.zero_grad()
print(f"Training: {epoch} {i}")
i += 1
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
model.eval()
print(f"Evaluating on Train: {epoch}")
train_acc_history.append(testAccuracy("./split_dataset/train", model, transform, device))
print(f"Evaluating on Val: {epoch}")
val_acc_history.append(testAccuracy("./split_dataset/val", model, transform, device))
print(f"Epoch [{epoch+1}/{numEpochs}], Loss: {loss.item():.4f}")
return train_acc_history, val_acc_history