使用Polyman和Python相结合的方法。
1、Polyman:先从pysiobank中下载到与原始数据相对应的标签文件,用Pysiobank可以打开文件,然后导出csv文件(即excel表格),将里面的所有标签改成0、1、2、3、4、5的分类名称(为了好区分),将movement状态改成一个接近的状态,将最后一个标签删除。
2、在python中将csv文件转换成txt文件(在txt文件中标签以0、1、2、3、4、5的形式出现,一行一个数)
# coding: utf-8
r"""
生成标签
"""
import pandas as pd
def main():
save_path = '4041_label.txt'
file_path = r"C:\Users\10133\Desktop\标签集\SC4041_label.csv"
df = pd.read_csv(file_path)
values = df.values
duration = values[:, 3]
label = values[:, 4]
result = []
for i, d in enumerate(duration):
num = d // 30
current_label = label[i]
item = [current_label] * num
result = result + item
result = list(map(str, result))
content = '\n'.join(result)
open(save_path, 'w+').write(content)
print()
if __name__ == '__main__':
main()
3、将上个博客中导出的30s一段的信号根据标签进行分类,首先第一步是建立训练集和验证集,利用代码建成train和valid两个文件夹,下面分设六个种类的文件夹(如果嫌麻烦,可以手动建文件夹)本文中使用的方法是将前七个人的数据分成训练集和验证集,将第八个人的数据直接用来测试分成六个种类。
# -*- coding:utf-8 -*-
# In[1]:
import os
import shutil
import numpy as np
#先建立一个数据的文件夹
base_dir = r'C:\Users\10133\Desktop\bishe\matlab\traintest'
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.mkdir(base_dir) # 在该路径下创建目录
#在总的文件夹下自动创建训练集和验证集两个文件夹
train_dir = os.path.join(base_dir, 'train') # 训练文件夹
os.mkdir(train_dir)
test_dir = os.path.join(base_dir, 'valid') # 验证文件夹
os.mkdir(test_dir)
print('主目录已经建立好了!')
#在训练集文件夹下自动建立六个类别文件夹
train_0_dir = os.path.join(train_dir, '0')
os.mkdir(train_0_dir)
train_1_dir = os.path.join(train_dir, '1')
os.mkdir(train_1_dir)
train_2_dir = os.path.join(train_dir, '2')
os.mkdir(train_2_dir)
train_3_dir = os.path.join(train_dir, '3')
os.mkdir(train_3_dir)
train_4_dir = os.path.join(train_dir, '4')
os.mkdir(train_4_dir)
train_5_dir = os.path.join(train_dir, '5')
os.mkdir(train_f5_dir)
print('类别目录已经建立好了!')
第二步就是根据标签将图片分类(只是分成了五类,但是还没有分训练集和验证集)
# -*- coding:utf-8 -*-
# In[1]:
import os
import shutil
import numpy as np
train_0_dir = r'C:\Users\10133\Desktop\bishe\matlab\classification\0'
train_1_dir = r'C:\Users\10133\Desktop\bishe\matlab\classification\1'
train_2_dir = r'C:\Users\10133\Desktop\bishe\matlab\classification\2'
train_3_dir = r'C:\Users\10133\Desktop\bishe\matlab\classification\3'
train_4_dir = r'C:\Users\10133\Desktop\bishe\matlab\classification\4'
train_5_dir = r'C:\Users\10133\Desktop\bishe\matlab\classification\5'
original_dataset_dir = r'D:\SC4032jpg'#这个例子用的是4032的例子
labelpath = r'C:\Users\10133\PycharmProjects\practice\4032_label.txt'#导入4032的标签
y = np.loadtxt(fname=labelpath, delimiter='\n')
print(y[0])
print(y.shape)
print('finished!')
for i in range(len(y)):
if y[i] == 0:
src = os.path.join(original_dataset_dir, f"{i + 1}.jpg")
dst = os.path.join(train_0_dir, f"{i + 1}.jpg")
shutil.copyfile(src, dst)
elif y[i] == 1:
src = os.path.join(original_dataset_dir, f"{i + 1}.jpg")
dst = os.path.join(train_1_dir, f"{i + 1}.jpg")
shutil.copyfile(src, dst)
elif y[i] == 2:
src = os.path.join(original_dataset_dir, f"{i + 1}.jpg")
dst = os.path.join(train_2_dir, f"{i + 1}.jpg")
shutil.copyfile(src, dst)
elif y[i] == 3:
src = os.path.join(original_dataset_dir, f"{i + 1}.jpg")
dst = os.path.join(train_3_dir, f"{i + 1}.jpg")
shutil.copyfile(src, dst)
elif y[i] == 4:
src = os.path.join(original_dataset_dir, f"{i + 1}.jpg")
dst = os.path.join(train_4_dir, f"{i + 1}.jpg")
shutil.copyfile(src, dst)
else:
src = os.path.join(original_dataset_dir, f"{i + 1}.jpg")
dst = os.path.join(train_5_dir, f"{i + 1}.jpg")
shutil.copyfile(src, dst)