IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> UCI-HAR数据集CNN分类 -> 正文阅读

[人工智能]UCI-HAR数据集CNN分类

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])  # (N, C, H, W) (7352, 1, 128, 9)
        # data_x = np.expand_dims(data_x_raw, 1)
        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,)

    # for step, (batch_x, batch_y) in enumerate(train_loader):

'''
-*- 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)
        #print(data_x_raw.shape)                                                        #为什么是1通道 128*9
        data_x = data_x_raw.reshape(-1, 1, data_x_raw.shape[1], data_x_raw.shape[2])  # (N, C, H, W) (7352, 1, 128, 9)
        # data_x = np.expand_dims(data_x_raw, 1)
        #print(data_x.shape)
        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()    #创造训练验证数据集
#print(har_train_tensor)
#测试集数据
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,          #输出高度12
                      kernel_size=3,            #卷积核尺寸3*3
                      stride=1,
                      padding=1),               #(1*128*9)-->(12*128*9)
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2,stride=2) #(12*128*9)-->(12*64*4)
        )
        #定义第二个卷积层
        self.conv2 = nn.Sequential(
            nn.Conv2d(in_channels=12,
                      out_channels=32,
                      kernel_size=3,
                      stride=1,
                      padding=1),               #(12*64*4)-->(32*64*4)
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2,stride=2) #池化后:(32*32*2)
        )
        self.conv3 = nn.Sequential(
            nn.Conv2d(in_channels=32,
                      out_channels=64,
                      kernel_size=3,
                      stride=1,
                      padding=1),                #(32*32*2)-->(64*32*2)
            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)
        #print(x.shape)
        x = self.conv2(x)
        #print(x.shape)
        x = self.conv3(x)
        #print(x.shape)
        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):
    #train_rate:训练集中训练数量的百分比
    #计算训练使用的batch数量
    batch_num = len(traindataloader)
    train_batch_num = round(batch_num * train_rate) #前train_rate(80%)的batch进行训练
    #复制最好模型的参数
    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)
        #每个epoch有两个训练阶段
        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:                      #前train_rate(80%)的batch进行训练
                model.train()                               #设置模型为训练模式,对Droopou有用
                output = model(b_x)
               # print(b_x)#取得模型预测结果
                pre_lab = torch.argmax(output,1)            #横向获得最大值位置
                #b_y = torch.Tensor(b_y).long()             #修改BUG
                #print(b_y)
                loss = criterion(output,b_y)                #每个样本的loss
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()                            #修改权值
                train_loss += loss.item() * b_x.size(0)
                #print(pre_lab)
                #print(b_y.data)
                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)           #一个epoch上的loss
        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])) #此处-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打印
    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,   #使用训练集的20%作为验证
                                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()
        #target = torch.Tensor(target).long()
        model.eval()                                       #设置模型为训练模式,对Droopou有用
        output = model(input)
        # print(b_x)#取得模型预测结果
        pre_lab = torch.argmax(output,1)                    #横向获得最大值位置
        loss = criterion(output,target)                     #每个样本的loss
        test_loss += loss.item() * input.size(0)            #此处的b_x.size(0)=batch_size。此处相当于一个batch的loss?计算的是整体训练的loss
        #print(pre_lab)
        #print(input.data)
        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
"""
  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-09-07 10:48:58  更:2021-09-07 10:50:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 15:54:55-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码