-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_src.py
102 lines (84 loc) · 2.92 KB
/
train_src.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
""" Train source for ADDA """
import numpy as np
import scipy.io as sio
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable
from models import LeNetClassifier, LeNetEncoder
from GestureDataset import GestureDataset
from utils import save_model
# super parameters
batch_size = 75
num_workers = 2
num_epochs = 40
lr = 0.001
weight_decay = 1e-6
# load data
data = sio.loadmat('dataset/gesture_l.mat')
x_train, y_train, x_test, y_test = data['x_train'], data['y_train'], data['x_test'], data['y_test']
train_dataset = GestureDataset(x_train, y_train)
test_dataset = GestureDataset(x_test, y_test)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, num_workers=num_workers, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, num_workers=num_workers, shuffle=True)
encoder = LeNetEncoder()
classifier = LeNetClassifier()
optimizer = optim.Adam(list(encoder.parameters())+list(classifier.parameters()),lr=lr, weight_decay=weight_decay)
criterion = nn.CrossEntropyLoss()
# train network
if torch.cuda.is_available():
encoder.cuda()
classifier.cuda()
def train(epoch):
total_num = 0
correct_num = 0
encoder.train()
classifier.train()
for step, (inputs, targets) in enumerate(train_loader):
inputs = Variable(inputs).cuda()
targets = Variable(targets).cuda()
# zero gradients for optimizer
optimizer.zero_grad()
# compute loss
outputs = classifier(encoder(inputs))
loss = criterion(outputs, torch.max(targets, 1)[1])
# optimize src classifier
loss.backward()
optimizer.step()
_, predicted = torch.max(outputs.data, 1)
_, labels = torch.max(targets.data, 1)
total_num += targets.size(0)
correct_num += predicted.eq(labels).cpu().sum()
print("Epoch [{}/{}] Step [{}/{}]: loss={:.5f}, accuracy={:.2f}"
.format(epoch + 1,
num_epochs,
step + 1,
len(train_loader),
loss.data[0],
100.*correct_num/total_num))
# save model parameters
if ((epoch + 1) > 20):
save_model(encoder, "large-source-encoder-{}.pt".format(epoch + 1))
save_model(classifier, "large-source-classifier-{}.pt".format(epoch + 1))
print ('Save model of epoch {}'.format(epoch + 1))
def test(epoch):
total_num = 0
correct_num = 0
encoder.eval()
classifier.eval()
criterion = nn.CrossEntropyLoss()
for step, (inputs, targets) in enumerate(test_loader):
inputs = Variable(inputs).cuda()
targets = Variable(targets).cuda()
outputs = classifier(encoder(inputs))
loss = criterion(outputs, torch.max(targets, 1)[1])
total_num += targets.size(0)
_, predicted = torch.max(outputs.data, 1)
_, labels = torch.max(targets.data, 1)
correct_num += predicted.eq(labels).cpu().sum()
print("Test: Avg Loss = {:.5f}, Avg Accuracy = {:.2f}".format(loss.data[0], 100.*correct_num/total_num))
if __name__ == '__main__':
for epoch in range(num_epochs):
train(epoch)
test(epoch)