-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
82 lines (65 loc) · 2.65 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
73
74
75
76
77
78
79
80
81
82
import os
import time
import datetime
import torch
import torch.optim as optim
import torch.nn as nn
from model import Classifier
class Solver(object):
def __init__(self,config, loader):
# Loader
self.loader = loader
# Model configuration
self.model = config.model
# Training configuration
self.batch_size = config.batch_size
self.lr = config.lr
self.momentum = config.momentum
self.EPOCH = config.EPOCH
# Directories
self.model_save_path = config.model_save_path
# Steps
self.model_save_step = config.model_save_step
# Miscellaneous
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f'GPU Availability: {torch.cuda.is_available()}')
if torch.cuda.is_available():
print(torch.cuda.get_device_properties(self.device))
# Build model
self.build_model()
def build_model(self):
self.net = Classifier(self.model).build_model().to(self.device)
print('Net built')
self.print_network(self.net)
self.optimizer = optim.SGD(self.net.parameters(), lr=self.lr, momentum=self.momentum)
self.criterion = nn.CrossEntropyLoss()
def print_network(self, net):
num = 0
for param in net.parameters():
num += param.numel()
print('The number of parameters is :{}'.format(num))
def train(self):
self.net.train()
print('Start training...')
start_time = time.time()
for epoch in range(self.EPOCH):
for batch_idx, (img, target) in enumerate(self.loader):
img = img.to(self.device)
target = target.to(self.device)
preds = self.net(img)
loss = self.criterion(preds, target)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
et = time.time() - start_time
et = str(datetime.timedelta(seconds=et))[:-7]
print(f'Elapsed time: {et}, Batch:{batch_idx}, epoch:{epoch+1}/{self.EPOCH}, Loss:{loss}')
if (epoch+1) % self.model_save_step == 0:
save_path = os.path.join(self.model_save_path, f'{epoch+1}.ckpt')
torch.save(self.net.state_dict(), save_path)
print(f'Save model check point into {save_path}')
def test(self):
self.model.eval()
for batch_idx, (img, target) in enumerate(self.loader):
img = img.to(self.device)
preds = self.model(img)