import torch.utils.data as Data
import numpy as np
import torch
train_x_list = "x_train.npy"
train_y_list = "y_train.npy"
test_x_list = "x_test.npy"
test_y_list = "y_test.npy"
class HAR(Data.Dataset):
def __init__(self, filename_x, filename_y):
self.filename_x = filename_x
self.filename_y = filename_y
def HAR_data(self):
data_x_raw = np.load(self.filename_x)
print(data_x_raw.shape)
data_x = data_x_raw.reshape(-1, 1, data_x_raw.shape[1],
data_x_raw.shape[2])
print(data_x.shape)
data_y = np.load(self.filename_y)
torch_dataset = Data.TensorDataset(torch.from_numpy(data_x), torch.from_numpy(data_y))
return torch_dataset
if __name__ == "__main__":
data_train = HAR(train_x_list, train_y_list)
har_train_tensor = data_train.HAR_data()
data_test = HAR(test_x_list, test_y_list)
har_test_tensor = data_test.HAR_data()
train_loader = Data.DataLoader(dataset=har_train_tensor, batch_size=128, shuffle=True, num_workers=5,)
test_loader = Data.DataLoader(dataset=har_test_tensor, batch_size=128, shuffle=True, num_workers=5,)
'''
-*- coding: utf-8 -*-
@Time : 2021/8/7 20:55
@Author : Small_Volcano
@File : UCI_HAR_CNN.py
'''
import copy
import time
import torch
import torch.nn as nn
import torch.utils.data as Data
from torch.optim import Adam
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from torchsummary import summary
train_x_list = "x_train.npy"
train_y_list = "y_train.npy"
test_x_list = "x_test.npy"
test_y_list = "y_test.npy"
class HAR(Data.Dataset):
def __init__(self, filename_x, filename_y):
self.filename_x = filename_x
self.filename_y = filename_y
def HAR_data(self):
"""更改x的维度,加载x和y"""
data_x_raw = np.load(self.filename_x)
data_x = data_x_raw.reshape(-1, 1, data_x_raw.shape[1], data_x_raw.shape[2])
data_y = np.load(self.filename_y)
print("datay{}".format(data_y))
torch_dataset = Data.TensorDataset(torch.from_numpy(data_x), torch.from_numpy(data_y))
return torch_dataset
data_train = HAR(train_x_list, train_y_list)
har_train_tensor = data_train.HAR_data()
data_test = HAR(test_x_list, test_y_list)
har_test_tensor = data_test.HAR_data()
train_loader = Data.DataLoader(dataset=har_train_tensor,
batch_size=128,
shuffle=True,
num_workers=0, )
test_loader = Data.DataLoader(dataset=har_test_tensor,
batch_size=1,
shuffle=True,
num_workers=0, )
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels=1,
out_channels=12,
kernel_size=3,
stride=1,
padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2,stride=2)
)
self.conv2 = nn.Sequential(
nn.Conv2d(in_channels=12,
out_channels=32,
kernel_size=3,
stride=1,
padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2,stride=2)
)
self.conv3 = nn.Sequential(
nn.Conv2d(in_channels=32,
out_channels=64,
kernel_size=3,
stride=1,
padding=1),
nn.ReLU()
)
self.classifier = nn.Sequential(
nn.Linear(64*32*2,128),
nn.ReLU(),
nn.Dropout(p = 0.5),
nn.Linear(128,6)
)
def forward(self,x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = x.view(x.shape[0],-1)
output = self.classifier(x)
return output
net = Net()
print(net)
def train_model(model,traindataloader,train_rate,criterion,optimizer,num_epochs=25):
batch_num = len(traindataloader)
train_batch_num = round(batch_num * train_rate)
best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
train_loss_all = []
train_acc_all = []
val_loss_all = []
val_acc_all = []
since = time.time()
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch,num_epochs-1))
print('-' * 10)
train_loss = 0.0
train_corrects = 0
train_num = 0
val_loss = 0.0
val_corrects = 0
val_num = 0
for step,(b_x,b_y) in enumerate(traindataloader,1):
b_y = b_y.long()
if step < train_batch_num:
model.train()
output = model(b_x)
pre_lab = torch.argmax(output,1)
loss = criterion(output,b_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss += loss.item() * b_x.size(0)
train_corrects += torch.sum(pre_lab == b_y.data)
train_num += b_x.size(0)
else:
model.eval()
output = model(b_x)
pre_lab = torch.argmax(output,1)
loss = criterion(output,b_y)
val_loss += loss.item() * b_x.size(0)
val_corrects += torch.sum(pre_lab == b_y.data)
val_num += b_x.size(0)
train_loss_all.append(train_loss / train_num)
train_acc_all.append(train_corrects.double().item() / train_num)
val_loss_all.append(val_loss / val_num)
val_acc_all.append(val_corrects.double().item() / val_num)
print('{} Train Loss: {:.4f} Train Acc: {:.4f}'.format(epoch,train_loss_all[-1],train_acc_all[-1]))
print('{} Val Loss: {:.4f} Val Acc: {:.4f}'.format(epoch,val_loss_all[-1],val_acc_all[-1]))
if val_acc_all[-1] > best_acc:
best_acc = val_acc_all[-1]
best_model_wts = copy.deepcopy(model.state_dict())
torch.save(model.state_dict(),"UCI_HAR_model")
torch.save(optimizer.state_dict(),"UCI_HAR_optimizer")
time_use = time.time() - since
print("Train and val complete in {:.0f}m {:.0f}s".format(time_use // 60,time_use % 60))
model.load_state_dict(best_model_wts)
train_process = pd.DataFrame(data={"epoch":range(num_epochs),
"train_loss_all":train_loss_all,
"val_loss_all":val_loss_all,
"train_acc_all":train_acc_all,
"val_acc_all":val_acc_all})
return model,train_process
optimizer = Adam(net.parameters(),lr=0.0003)
criterion = nn.CrossEntropyLoss()
net,train_process = train_model(net,train_loader,0.8,
criterion,optimizer,num_epochs=25)
plt.figure(figsize=(12,4))
plt.subplot(1,2,1)
plt.plot(train_process.epoch,train_process.train_loss_all,"ro-",label="Train loss")
plt.plot(train_process.epoch,train_process.val_loss_all,"bs-",label="Val loss")
plt.legend()
plt.xlabel("epoch")
plt.ylabel("Loss")
plt.subplot(1,2,2)
plt.plot(train_process.epoch,train_process.train_acc_all,"ro-",label="Train acc")
plt.plot(train_process.epoch,train_process.val_acc_all,"bs-",label="Val acc")
plt.xlabel("epoch")
plt.ylabel("acc")
plt.legend()
plt.show()
def test(model,testdataloader,criterion):
test_loss_all = []
test_acc_all = []
test_loss = 0.0
test_corrects = 0
test_num = 0
for step,(input,target) in enumerate(testdataloader):
target = target.long()
model.eval()
output = model(input)
pre_lab = torch.argmax(output,1)
loss = criterion(output,target)
test_loss += loss.item() * input.size(0)
test_corrects += torch.sum(pre_lab == target.data)
test_num += input.size(0)
test_loss_all.append(test_loss / test_num)
test_acc_all.append(test_corrects.double().item() / test_num)
print('Test all Loss: {:.4f} Test Acc: {:.4f}'.format(test_loss_all[-1], test_acc_all[-1]))
test = test(net,test_loader,criterion)
"""
Epoch 0/24
----------
0 Train Loss: 1.3879 Train Acc: 0.4361
0 Val Loss: 0.8158 Val Acc: 0.6156
Train and val complete in 0m 8s
Epoch 1/24
----------
1 Train Loss: 0.8039 Train Acc: 0.5974
1 Val Loss: 0.6446 Val Acc: 0.7487
Train and val complete in 0m 15s
Epoch 2/24
----------
2 Train Loss: 0.6284 Train Acc: 0.7115
2 Val Loss: 0.5581 Val Acc: 0.7198
Train and val complete in 0m 22s
Epoch 3/24
----------
3 Train Loss: 0.5377 Train Acc: 0.7696
3 Val Loss: 0.4765 Val Acc: 0.8003
Train and val complete in 0m 29s
Epoch 4/24
----------
4 Train Loss: 0.4640 Train Acc: 0.7990
4 Val Loss: 0.3666 Val Acc: 0.8580
Train and val complete in 0m 36s
Epoch 5/24
----------
5 Train Loss: 0.3987 Train Acc: 0.8370
5 Val Loss: 0.3171 Val Acc: 0.8894
Train and val complete in 0m 43s
Epoch 6/24
----------
6 Train Loss: 0.3426 Train Acc: 0.8681
6 Val Loss: 0.2961 Val Acc: 0.8863
Train and val complete in 0m 49s
Epoch 7/24
----------
7 Train Loss: 0.3154 Train Acc: 0.8786
7 Val Loss: 0.2763 Val Acc: 0.8976
Train and val complete in 0m 56s
Epoch 8/24
----------
8 Train Loss: 0.2811 Train Acc: 0.9019
8 Val Loss: 0.2236 Val Acc: 0.9033
Train and val complete in 1m 3s
Epoch 9/24
----------
9 Train Loss: 0.2505 Train Acc: 0.9042
9 Val Loss: 0.2151 Val Acc: 0.9234
Train and val complete in 1m 10s
Epoch 10/24
----------
10 Train Loss: 0.2375 Train Acc: 0.9109
10 Val Loss: 0.1955 Val Acc: 0.9165
Train and val complete in 1m 17s
Epoch 11/24
----------
11 Train Loss: 0.2131 Train Acc: 0.9200
11 Val Loss: 0.1771 Val Acc: 0.9353
Train and val complete in 1m 23s
Epoch 12/24
----------
12 Train Loss: 0.1995 Train Acc: 0.9231
12 Val Loss: 0.1647 Val Acc: 0.9366
Train and val complete in 1m 30s
Epoch 13/24
----------
13 Train Loss: 0.1917 Train Acc: 0.9281
13 Val Loss: 0.1392 Val Acc: 0.9454
Train and val complete in 1m 37s
Epoch 14/24
----------
14 Train Loss: 0.1835 Train Acc: 0.9306
14 Val Loss: 0.1347 Val Acc: 0.9491
Train and val complete in 1m 44s
Epoch 15/24
----------
15 Train Loss: 0.1822 Train Acc: 0.9316
15 Val Loss: 0.1394 Val Acc: 0.9479
Train and val complete in 1m 52s
Epoch 16/24
----------
16 Train Loss: 0.1633 Train Acc: 0.9403
16 Val Loss: 0.1280 Val Acc: 0.9529
Train and val complete in 1m 59s
Epoch 17/24
----------
17 Train Loss: 0.1611 Train Acc: 0.9389
17 Val Loss: 0.1619 Val Acc: 0.9221
Train and val complete in 2m 6s
Epoch 18/24
----------
18 Train Loss: 0.1471 Train Acc: 0.9448
18 Val Loss: 0.1444 Val Acc: 0.9403
Train and val complete in 2m 14s
Epoch 19/24
----------
19 Train Loss: 0.1442 Train Acc: 0.9465
19 Val Loss: 0.1342 Val Acc: 0.9472
Train and val complete in 2m 22s
Epoch 20/24
----------
20 Train Loss: 0.1497 Train Acc: 0.9422
20 Val Loss: 0.1150 Val Acc: 0.9585
Train and val complete in 2m 31s
Epoch 21/24
----------
21 Train Loss: 0.1388 Train Acc: 0.9477
21 Val Loss: 0.1221 Val Acc: 0.9460
Train and val complete in 2m 39s
Epoch 22/24
----------
22 Train Loss: 0.1410 Train Acc: 0.9436
22 Val Loss: 0.1288 Val Acc: 0.9460
Train and val complete in 2m 47s
Epoch 23/24
----------
23 Train Loss: 0.1417 Train Acc: 0.9477
23 Val Loss: 0.1131 Val Acc: 0.9585
Train and val complete in 2m 55s
Epoch 24/24
----------
24 Train Loss: 0.1320 Train Acc: 0.9483
24 Val Loss: 0.1275 Val Acc: 0.9472
Train and val complete in 3m 3s
Test all Loss: 0.3446 Test Acc: 0.8683
"""
|