-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solver.py
72 lines (52 loc) · 2.36 KB
/
Solver.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
import torch
import torch.nn as nn
import torch.optim as optim
from util.data_def import *
class Solver(object):
def __init__(self, model, epochs, trainloader, device, learning_rate=0.001):
self.model = model
self.criterion = nn.CrossEntropyLoss()
self.optimizer = optim.SGD(self.model.parameters(), lr=learning_rate, momentum=0.9)
self.epochs = epochs
self.device = device
self.training_loader = trainloader
def train(self):
train_acc = 0.
for epoch in range(self.epochs):
running_loss = 0.
last_loss = 0.
total_samples = 0
total_correct = 0
# Read the dataloader
for i, (image, label) in enumerate(self.training_loader):
image = image.to(self.device)
label = label.to(self.device)
# Zero gradients for every batch
self.optimizer.zero_grad()
# Make predictions for this batch
outputs = self.model(image.float())
_, pred = torch.max(outputs, 1)
total_correct += (pred == label).sum().item()
total_samples += label.size(0)
# Compute the loss and its gradients
loss = self.criterion(outputs, label)
# Backpropagation
loss.backward()
# Adjust learning weights
self.optimizer.step()
# Gather data and report
running_loss += loss.item()
# Print loss in every 10 batches
if i % 10 == 9:
last_loss = running_loss / 10
print("Epoch: ", epoch, " Batch : ", i+1, " Loss: ", last_loss)
running_loss = 0.
# Save the model in the end of epoch
torch.save(self.model.state_dict(), current_directory + f'/save_model/seg_network_{epoch}.pt')
print("################################################################")
print("Epoch:{} Training Loss:{}".format(epoch, last_loss))
print("################################################################")
# Print the Training accuracy while end of training
if epoch == 9:
train_acc = 100 * total_correct / total_samples
print("Training Accuracy", train_acc)