-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathrun.py
92 lines (79 loc) · 3.11 KB
/
run.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
import numpy as np
import torch
import torch.nn as nn
import argparse
from util import load_data_n_model
def train(model, tensor_loader, num_epochs, learning_rate, criterion, device):
model = model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr = learning_rate)
for epoch in range(num_epochs):
model.train()
epoch_loss = 0
epoch_accuracy = 0
for data in tensor_loader:
inputs,labels = data
inputs = inputs.to(device)
labels = labels.to(device)
labels = labels.type(torch.LongTensor)
optimizer.zero_grad()
outputs = model(inputs)
outputs = outputs.to(device)
outputs = outputs.type(torch.FloatTensor)
loss = criterion(outputs,labels)
loss.backward()
optimizer.step()
epoch_loss += loss.item() * inputs.size(0)
predict_y = torch.argmax(outputs,dim=1).to(device)
epoch_accuracy += (predict_y == labels.to(device)).sum().item() / labels.size(0)
epoch_loss = epoch_loss/len(tensor_loader.dataset)
epoch_accuracy = epoch_accuracy/len(tensor_loader)
print('Epoch:{}, Accuracy:{:.4f},Loss:{:.9f}'.format(epoch+1, float(epoch_accuracy),float(epoch_loss)))
return
def test(model, tensor_loader, criterion, device):
model.eval()
test_acc = 0
test_loss = 0
for data in tensor_loader:
inputs, labels = data
inputs = inputs.to(device)
labels.to(device)
labels = labels.type(torch.LongTensor)
outputs = model(inputs)
outputs = outputs.type(torch.FloatTensor)
outputs.to(device)
loss = criterion(outputs,labels)
predict_y = torch.argmax(outputs,dim=1).to(device)
accuracy = (predict_y == labels.to(device)).sum().item() / labels.size(0)
test_acc += accuracy
test_loss += loss.item() * inputs.size(0)
test_acc = test_acc/len(tensor_loader)
test_loss = test_loss/len(tensor_loader.dataset)
print("validation accuracy:{:.4f}, loss:{:.5f}".format(float(test_acc),float(test_loss)))
return
def main():
root = './Data/'
parser = argparse.ArgumentParser('WiFi Imaging Benchmark')
parser.add_argument('--dataset', choices = ['UT_HAR_data','NTU-Fi-HumanID','NTU-Fi_HAR','Widar'])
parser.add_argument('--model', choices = ['MLP','LeNet','ResNet18','ResNet50','ResNet101','RNN','GRU','LSTM','BiLSTM', 'CNN+GRU','ViT'])
args = parser.parse_args()
train_loader, test_loader, model, train_epoch = load_data_n_model(args.dataset, args.model, root)
criterion = nn.CrossEntropyLoss()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
train(
model=model,
tensor_loader= train_loader,
num_epochs= train_epoch,
learning_rate=1e-3,
criterion=criterion,
device=device
)
test(
model=model,
tensor_loader=test_loader,
criterion=criterion,
device= device
)
return
if __name__ == "__main__":
main()