赛事介绍
如今的智能机已经很智能了,如果手机可以觉察到我们在生活中的一举一动,知道我们行动的意图,你觉得会如何?智能手机不仅搭载了多种惯性传感器,这使得基于智能手机的人体行为识别研究越来越受关注。
在本次赛题由志愿者使用智能手机时,通过基本活动的行为构建而成。希望选手能够构建模型对活动行为进行预测。
赛事任务
实验是在 19-48 岁年龄段的 30 名志愿者中进行的。每个人在腰部佩戴智能手机(三星 Galaxy S II)进行六项活动(步行、楼上步行、楼下步行、坐、站、躺)。实验以 50Hz 的恒定速率捕获 3 轴线性加速度和 3 轴角速度。
赛题训练集案例如下:
- 训练集8千条数据;
- 测试集共2000条数据;
数据总共100MB,赛题数据均为csv格式,列使用逗号分割。若使用Pandas读取数据,可参考如下代码:
import pandas as pd import numpy as np train = pd.read_csv('train.csv.zip')
对于数据集中的每一条记录,都提供了以下内容,来自加速度计的三轴加速度(总加速度)和估计的身体加速度、和来自陀螺仪的三轴角速度。总共是具有时域和频域变量的561个特征向量。
测试集中label字段Activity为空,需要选手预测。
评审规则
- 数据说明:选手需要提交测试集队伍排名预测,具体的提交格式如下:
Activity STANDING LAYING WALKING SITTING WALKING WALKING_DOWNSTAIRS STANDING
- 评估指标:本次竞赛的使用准确率进行评分,数值越高精度越高,评估代码参考:
from sklearn.metrics import accuracy_score y_pred = [0, 2, 1, 3] y_true = [0, 1, 2, 3] accuracy_score(y_true, y_pred)
Baseline使用指导
1、点击‘fork按钮’,出现‘fork项目’弹窗
2、点击‘创建按钮’ ,出现‘运行项目’弹窗
3、点击‘运行项目’,自动跳转至新页面
4、点击‘启动环境’ ,出现‘选择运行环境’弹窗
5、选择运行环境(启动项目需要时间,请耐心等待),出现‘环境启动成功’弹窗,点击确定
6、点击进入环境,即可进入notebook环境
7、鼠标移至下方每个代码块内(代码块左侧边框会变成浅蓝色),再依次点击每个代码块左上角的‘三角形运行按钮’,待一个模块运行完以后再运行下一个模块,直至全部运行完成
8、下载页面左侧submission.zip压缩包
9、在比赛页提交submission.zip压缩包,等待系统评测结束后,即可登榜!
10、点击页面左侧‘版本-生成新版本’
11、填写‘版本名称’,点击‘生成版本按钮’,即可在个人主页查看到该项目(可选择公开此项目哦)
数据分析
import pandas as pd import paddle import numpy as np %pylab inline import seaborn as sns train_df = pd.read_csv('data/data137267/train.csv.zip') test_df = pd.read_csv('data/data137267/test.csv.zip')
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working from collections import MutableMapping /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working from collections import Iterable, Mapping /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working from collections import Sized Populating the interactive namespace from numpy and matplotlib
train_df.shape
(8000, 562)
train_df.columns
Index(['tBodyAcc-mean()-X', 'tBodyAcc-mean()-Y', 'tBodyAcc-mean()-Z', 'tBodyAcc-std()-X', 'tBodyAcc-std()-Y', 'tBodyAcc-std()-Z', 'tBodyAcc-mad()-X', 'tBodyAcc-mad()-Y', 'tBodyAcc-mad()-Z', 'tBodyAcc-max()-X', ... 'fBodyBodyGyroJerkMag-skewness()', 'fBodyBodyGyroJerkMag-kurtosis()', 'angle(tBodyAccMean,gravity)', 'angle(tBodyAccJerkMean),gravityMean)', 'angle(tBodyGyroMean,gravityMean)', 'angle(tBodyGyroJerkMean,gravityMean)', 'angle(X,gravityMean)', 'angle(Y,gravityMean)', 'angle(Z,gravityMean)', 'Activity'], dtype='object', length=562)
print(len(train_df.columns))
562
train_df['Activity'].value_counts().plot(kind='bar')
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2349: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working if isinstance(obj, collections.Iterator): /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2366: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working return list(data) if isinstance(data, collections.MappingView) else data <matplotlib.axes._subplots.AxesSubplot at 0x7f0755c66210>
plt.figure(figsize=(10, 5)) sns.boxplot(y='tBodyAcc-mean()-X', x='Activity', data=train_df) plt.tight_layout()
train_df['Activity'] = train_df['Activity'].map({ 'LAYING': 0, 'STANDING': 1, 'SITTING': 2, 'WALKING': 3, 'WALKING_UPSTAIRS': 4, 'WALKING_DOWNSTAIRS': 5 })
from sklearn.preprocessing import StandardScaler scaler = StandardScaler() scaler.fit(train_df.values[:, :-1]) train_df.iloc[:, :-1] = scaler.transform(train_df.values[:, :-1]) test_df.iloc[:, :] = scaler.transform(test_df.values)
搭建模型
class Classifier(paddle.nn.Layer): # self代表类的实例自身 def __init__(self): # 初始化父类中的一些参数 super(Classifier, self).__init__() self.conv1 = paddle.nn.Conv1D(in_channels=1, out_channels=16, kernel_size=3) self.conv2 = paddle.nn.Conv1D(in_channels=16, out_channels=32, kernel_size=3) self.conv3 = paddle.nn.Conv1D(in_channels=32, out_channels=64, kernel_size=3) self.flatten = paddle.nn.Flatten() self.dropout = paddle.nn.Dropout() self.fc = paddle.nn.Linear(in_features=128, out_features=6) self.relu = paddle.nn.ReLU() self.pool = paddle.nn.MaxPool1D(6) self.softmax = paddle.nn.Softmax() # 网络的前向计算 def forward(self, inputs): x = self.pool(self.relu(self.conv1(inputs))) x = self.pool(self.relu(self.conv2(x))) x = self.dropout(x) x = self.pool(self.relu(self.conv3(x))) x = self.dropout(x) x = self.flatten(x) x = self.relu(self.fc(x)) x = self.softmax(x) return x
class Classifier(paddle.nn.Layer): # self代表类的实例自身 def __init__(self): # 初始化父类中的一些参数 super(Classifier, self).__init__() self.fc = paddle.nn.Linear(in_features=561, out_features=6) self.relu = paddle.nn.ReLU() self.softmax = paddle.nn.Softmax() # 网络的前向计算 def forward(self, inputs): x = self.relu(self.fc(inputs)) x = self.softmax(x) return x
class Classifier(paddle.nn.Layer): # self代表类的实例自身 def __init__(self): # 初始化父类中的一些参数 super(Classifier, self).__init__() self.conv1 = paddle.nn.Conv1D(in_channels=1, out_channels=16, kernel_size=3) self.conv2 = paddle.nn.Conv1D(in_channels=16, out_channels=32, kernel_size=3) self.conv3 = paddle.nn.Conv1D(in_channels=32, out_channels=64, kernel_size=3) self.flatten = paddle.nn.Flatten() self.dropout = paddle.nn.Dropout() self.fc = paddle.nn.Linear(in_features=128, out_features=6) self.relu = paddle.nn.ReLU() self.pool = paddle.nn.MaxPool1D(6) self.softmax = paddle.nn.Softmax() # 网络的前向计算 def forward(self, inputs): x = self.pool(self.relu(self.conv1(inputs))) x = self.pool(self.relu(self.conv2(x))) x = self.dropout(x) x = self.pool(self.relu(self.conv3(x))) x = self.dropout(x) x = self.flatten(x) x = self.relu(self.fc(x)) x = self.softmax(x) return x
model = Classifier() model.train() opt = paddle.optimizer.SGD(learning_rate=0.005, parameters=model.parameters()) loss_fn = paddle.nn.CrossEntropyLoss()
EPOCH_NUM = 10000 # 设置外层循环次数 BATCH_SIZE = 512 # 设置batch大小 training_data = train_df.iloc[:-1000].values.astype(np.float32) val_data = train_df.iloc[-1000:].values.astype(np.float32) training_data = training_data.reshape(-1, 1, 562) val_data = val_data.reshape(-1, 1, 562)
# 定义外层循环 for epoch_id in range(EPOCH_NUM): # 在每轮迭代开始之前,将训练数据的顺序随机的打乱 np.random.shuffle(training_data) # 将训练数据进行拆分,每个batch包含10条数据 mini_batches = [training_data[k:k+BATCH_SIZE] for k in range(0, len(training_data), BATCH_SIZE)] # 定义内层循环 for iter_id, mini_batch in enumerate(mini_batches): model.train() x = np.array(mini_batch[:,:, :-1]) # 获得当前批次训练数据 y = np.array(mini_batch[:,:, -1:]) # 获得当前批次训练标签 # 将numpy数据转为飞桨动态图tensor的格式 features = paddle.to_tensor(x) y = paddle.to_tensor(y) # 前向计算 predicts = model(features) print( predicts) print( y.flatten()) # 计算损失 loss = loss_fn(predicts, y.flatten().astype(int)) avg_loss = paddle.mean(loss) # 反向传播,计算每层参数的梯度值 avg_loss.backward() # 更新参数,根据设置好的学习率迭代一步 opt.step() # 清空梯度变量,以备下一轮计算 opt.clear_grad() # 训练与验证 if iter_id%2000==0 and epoch_id % 10 == 0: acc = predicts.argmax(1) == y.flatten().astype(int) acc = acc.astype(float).mean() model.eval() val_predict = model(paddle.to_tensor(val_data[:, :, :-1])).argmax(1) val_label = val_data[:, :, -1] val_acc = np.mean(val_predict.numpy() == val_label.flatten()) print("epoch: {}, iter: {}, loss is: {}, acc is {} / {}".format( epoch_id, iter_id, avg_loss.numpy(), acc.numpy(), val_acc))
Tensor(shape=[512, 1, 6], dtype=float32, place=CPUPlace, stop_gradient=False, [[[0.13394213, 0.13086532, 0.13086532, 0.14816959, 0.32529235, 0.13086532]], [[0.03296970, 0.04811743, 0.13990396, 0.03118659, 0.07828671, 0.66953558]], [[0.04501747, 0.62196940, 0.13563752, 0.06802886, 0.04847150, 0.08087535]], ..., [[0.03349594, 0.06035097, 0.03349594, 0.08224845, 0.03349594, 0.75691271]], [[0.04077548, 0.72053790, 0.08643766, 0.07069805, 0.04077548, 0.04077548]], [[0.11198503, 0.23658347, 0.13117823, 0.11198503, 0.14417638, 0.26409188]]]) Tensor(shape=[512], dtype=float32, place=CPUPlace, stop_gradient=True, [0., 4., 3., 0., 1., 4., 5., 0., 2., 2., 0., 5., 1., 0., 5., 2., 1., 5., 5., 3., 2., 4., 0., 2., 2., 5., 4., 4., 4., 5., 1., 2., 0., 1., 0., 2., 3., 3., 1., 4., 0., 3., 0., 4., 5., 1., 1., 0., 0., 3., 3., 5., 5., 3., 3., 3., 4., 4., 0., 0., 4., 3., 1., 3., 0., 4., 2., 4., 3., 4., 4., 2., 1., 1., 1., 0., 3., 1., 3., 4., 0., 1., 2., 0., 4., 3., 2., 1., 5., 1., 4., 0., 2., 1., 1., 3., 0., 3., 1., 0., 4., 5., 5., 3., 1., 4., 0., 3., 2., 5., 1., 4., 3., 4., 3., 3., 3., 3., 1., 1., 4., 0., 5., 0., 1., 2., 0., 5., 0., 0., 0., 3., 3., 2., 2., 4., 5., 0., 0., 0., 2., 3., 4., 0., 2., 5., 3., 4., 2., 1., 4., 4., 3., 2., 5., 3., 4., 1., 5., 2., 1., 2., 0., 4., 5., 1., 0., 3., 1., 3., 3., 2., 0., 1., 0., 2., 3., 1., 1., 1., 2., 0., 3., 1., 5., 3., 3., 0., 1., 5., 3., 3., 3., 0., 4., 1., 2., 0., 1., 2., 3., 0., 1., 2., 3., 2., 1., 2., 0., 3., 5., 4., 0., 1., 0., 0., 4., 1., 3., 3., 2., 0., 4., 4., 3., 0., 5., 2., 1., 1., 2., 5., 5., 3., 5., 2., 4., 2., 0., 0., 2., 4., 2., 5., 3., 5., 0., 1., 2., 0., 5., 4., 5., 2., 0., 0., 0., 0., 0., 2., 5., 0., 1., 3., 0., 2., 0., 1., 1., 5., 5., 0., 2., 1., 0., 3., 0., 5., 0., 2., 3., 2., 1., 3., 3., 2., 1., 3., 1., 3., 3., 1., 0., 3., 0., 3., 1., 5., 2., 3., 0., 4., 1., 3., 5., 2., 2., 4., 3., 3., 2., 5., 1., 2., 3., 5., 2., 2., 2., 4., 5., 5., 1., 4., 2., 2., 2., 1., 5., 1., 1., 2., 2., 1., 3., 4., 0., 0., 4., 3., 1., 5., 0., 2., 0., 1., 0., 0., 4., 5., 1., 1., 4., 0., 2., 0., 2., 0., 3., 4., 0., 2., 4., 4., 2., 4., 0., 2., 0., 4., 2., 2., 3., 0., 1., 5., 3., 0., 1., 1., 5., 0., 4., 4., 1., 1., 5., 4., 5., 1., 3., 1., 3., 2., 1., 0., 1., 1., 1., 3., 5., 1., 4., 1., 2., 1., 3., 1., 1., 5., 0., 0., 3., 5., 1., 0., 0., 4., 2., 1., 5., 4., 4., 3., 3., 0., 2., 5., 4., 1., 2., 2., 1., 5., 4., 3., 3., 1., 2., 0., 3., 1., 0., 4., 0., 3., 0., 0., 1., 2., 0., 2., 0., 1., 4., 5., 2., 0., 0., 4., 3., 5., 0., 4., 5., 5., 4., 1., 4., 0., 2., 3., 4., 5., 0., 2., 2., 2., 2., 2., 3., 0., 3., 1., 1., 0., 2., 3., 0., 4., 1., 3., 1., 5., 2., 4., 3., 0., 5., 1., 2., 4., 5., 3., 2., 3., 2., 2., 3., 4., 3., 4.]) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_461/2483119558.py in <module> 23 print( y.flatten()) 24 # 计算损失 ---> 25 loss = loss_fn(predicts, y.flatten().astype(int)) 26 avg_loss = paddle.mean(loss) 27 /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py in __call__(self, *inputs, **kwargs) 915 916 def __call__(self, *inputs, **kwargs): --> 917 return self._dygraph_call_func(*inputs, **kwargs) 918 919 def forward(self, *inputs, **kwargs): /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py in _dygraph_call_func(self, *inputs, **kwargs) 905 self._built = True 906 --> 907 outputs = self.forward(*inputs, **kwargs) 908 909 for forward_post_hook in self._forward_post_hooks.values(): /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/nn/layer/loss.py in forward(self, input, label) 404 axis=self.axis, 405 use_softmax=self.use_softmax, --> 406 name=self.name) 407 408 return ret /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/nn/functional/loss.py in cross_entropy(input, label, weight, ignore_index, reduction, soft_label, axis, use_softmax, name) 1646 raise ValueError( 1647 'Expected nput_dims - 1 = label_dims or input_dims == label_dims\ -> 1648 (got nput_dims{}, label_dims{})'.format(input_dims, label_dims)) 1649 if input_dims - 1 == label_dims: 1650 label = paddle.unsqueeze(label, axis=axis) ValueError: Expected nput_dims - 1 = label_dims or input_dims == label_dims (got nput_dims3, label_dims1)
model.eval() test_data = paddle.to_tensor(test_df.values.reshape(-1, 1, 561).astype(np.float32)) test_predict = model(test_data) test_predict = test_predict.argmax(1).numpy()
test_predict = pd.DataFrame({'Activity': test_predict}) test_predict['Activity'] = test_predict['Activity'].map({ 0:'LAYING', 1:'STANDING', 2:'SITTING', 3:'WALKING', 4:'WALKING_UPSTAIRS', 5:'WALKING_DOWNSTAIRS' })
test_predict.to_csv('submission.csv', index=None) !zip submission.zip submission.csv
未来上分点
- 模型可以加入残差结构,参考resnet。
- 数据可以加入数据扩增,比如加噪音。