本帖最后由 土司Apple 于 2023-4-19 15:56 编辑
https://github.com/Shahnawax/HAR-CNN-Keras
这是ST给的模型训练的py脚本,为啥我运行不起来呢
#!/usr/bin/envpython3 # -*-coding: utf-8 -*- """ Createdon Wed Sep 27 14:34:20 2017 Thisis a small project for CNN in KERAS. Thisfile creates, trains and save a convolutional neural network for HumanAcitivity Recognition. The data we used for this file is released and providedby WirelessSensor Data Mining (WISDM) lab and can be found on this link. http://www.cis.fordham.edu/wisdm/dataset.php Feelfree to use this code and site this repositry if you use it for your reports orproject. @author:Muhammad Shahnawaz """ #importing libraries and dependecies import os import pandas as pd import numpy as np importmatplotlib.pyplot as plt from scipy import stats from keras.models import Sequential from keras.layers import Dense, Conv2D,MaxPooling2D, Flatten, Dropout #fromkeras import backend as K from keras import optimizers #K.set_image_dim_ordering('th') #setting up a random seed for reproducibility random_seed = 611 np.random.seed(random_seed) #matplotlib inline plt.style.use('ggplot') #defining function for loading the dataset def readData(filePath): #attributes of the dataset columnNames = ['user_id','activity','timestamp','x-axis','y-axis','z-axis'] data = pd.read_csv(filePath,header = None, names=columnNames,na_values=';') return data #defining a function for feature normalization #(feature - mean)/stdiv def featureNormalize(dataset): mu = np.mean(dataset,axis=0) sigma = np.std(dataset,axis=0) return (dataset-mu)/sigma #defining the function to plot a single axis data def plotAxis(axis,x,y,title): axis.plot(x,y) axis.set_title(title) axis.xaxis.set_visible(False) axis.set_ylim([min(y)-np.std(y),max(y)+np.std(y)]) axis.set_xlim([min(x),max(x)]) axis.grid(True) #defining a function to plot the data for a given activity def plotActivity(activity,data): fig,(ax0,ax1,ax2) = plt.subplots(nrows=3, figsize=(15,10),sharex=True) plotAxis(ax0,data['timestamp'],data['x-axis'],'x-axis') plotAxis(ax1,data['timestamp'],data['y-axis'],'y-axis') plotAxis(ax2,data['timestamp'],data['z-axis'],'z-axis') plt.subplots_adjust(hspace=0.2) fig.suptitle(activity) plt.subplots_adjust(top=0.9) plt.show() #defining a window function for segmentation purposes def windows(data,size): start = 0 while start< data.count(): yield int(start), int(start + size) start+= (size/2) #segmenting the time series def segment_signal(data, window_size = 90): segments = np.empty((0,window_size,3)) labels= np.empty((0)) for (start, end) in windows(data['timestamp'],window_size): x = data['x-axis'][start:end] y = data['y-axis'][start:end] z = data['z-axis'][start:end] if(len(data['timestamp'][start:end])==window_size): segments =np.vstack([segments,np.dstack([x,y,z])]) labels =np.append(labels,stats.mode(data['activity'][start:end])[0][0]) return segments, labels '''Main Code ''' # # ## # # # # # reading the data # # # # # # # # # # #Path of file # dataset = readData('actitracker_raw.txt') #plotting a subset of the data to visualize for activity in np.unique(dataset['activity']): subset = dataset[dataset['activity']==activity][:180] plotActivity(activity,subset) #segmenting the signal in overlapping windows of 90 samples with 50% overlap segments,labels = segment_signal(dataset) #categoricallydefining the classes of the activities labels = np.asarray(pd.get_dummies(labels),dtype = np.int8) #defining parameters for the input and network layers # weare treating each segmeent or chunk as a 2D image (90 X 3) numOfRows = segments.shape[1] numOfColumns = segments.shape[2] numChannels = 1 numFilters = 128 # number of filters in Conv2D layer #kernal size of the Conv2D layer kernalSize1 = 2 # maxpooling window size poolingWindowSz = 2 #number of filters in fully connected layers numNueronsFCL1 = 128 numNueronsFCL2 = 128 #split ratio for test and validation trainSplitRatio = 0.8 #number of epochs Epochs = 10 #batchsize batchSize = 10 #number of total clases numClasses = labels.shape[1] #dropout ratio for dropout layer dropOutRatio = 0.2 #reshaping the data for network input reshapedSegments = segments.reshape(segments.shape[0], numOfRows,numOfColumns,1) #splitting in training and testing data trainSplit = np.random.rand(len(reshapedSegments))< trainSplitRatio trainX= reshapedSegments[trainSplit] testX= reshapedSegments[~trainSplit] trainX= np.nan_to_num(trainX) testX= np.nan_to_num(testX) trainY= labels[trainSplit] testY= labels[~trainSplit] def cnnModel(): model = Sequential() # addingthe first convolutionial layer with 32 filters and 5 by 5 kernal size, usingthe rectifier as the activation function model.add(Conv2D(numFilters,(kernalSize1,kernalSize1),input_shape=(numOfRows, numOfColumns,1),activation='relu')) # addinga maxpooling layer model.add(MaxPooling2D(pool_size=(poolingWindowSz,poolingWindowSz),padding='valid')) # addinga dropout layer for the regularization and avoiding over fitting model.add(Dropout(dropOutRatio)) #flattening the output in order to apply the fully connected layer model.add(Flatten()) # addingfirst fully connected layer with 256 outputs model.add(Dense(numNueronsFCL1, activation='relu')) #addingsecond fully connected layer 128 outputs model.add(Dense(numNueronsFCL2, activation='relu')) # addingsoftmax layer for the classification model.add(Dense(numClasses, activation='softmax')) #Compiling the model to generate a model adam = optimizers.Adam(lr = 0.001, decay=1e-6) model.compile(loss='categorical_crossentropy',optimizer=adam,metrics=['accuracy']) return model model= cnnModel() for layer in model.layers: print(layer.name) model.fit(trainX,trainY, validation_split=1-trainSplitRatio,epochs=10,batch_size=batchSize,verbose=2) score = model.evaluate(testX,testY,verbose=2) print('Baseline Error: %.2f%%' %(100-score[1]*100)) model.save('model.h5') np.save('groundTruth.npy',testY) np.save('testData.npy',testX)
运行结果
PS E:\STM32_AI\HAR-CNN-Keras-master\HAR-CNN-Keras-master> & "D:/Program Files/Python311_64/python.exe" e:/STM32_AI/HAR-CNN-Keras-master/HAR-CNN-Keras-master/HAR.py
Traceback (most recent call last):
File "e:\STM32_AI\HAR-CNN-Keras-master\HAR-CNN-Keras-master\HAR.py", line 81, in <module>
dataset = readData('actitracker_raw.txt')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "e:\STM32_AI\HAR-CNN-Keras-master\HAR-CNN-Keras-master\HAR.py", line 34, in readData
data = pd.read_csv(filePath,header = None, names=columnNames,na_values=';')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Program Files\Python311_64\Lib\site-packages\pandas\io\parsers\readers.py", line 912, in read_csv
return _read(filepath_or_buffer, kwds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Program Files\Python311_64\Lib\site-packages\pandas\io\parsers\readers.py", line 583, in _read
return parser.read(nrows)
^^^^^^^^^^^^^^^^^^
File "D:\Program Files\Python311_64\Lib\site-packages\pandas\io\parsers\readers.py", line 1704, in read
) = self._engine.read( # type: ignore[attr-defined]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Program Files\Python311_64\Lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 234, in read
chunks = self._reader.read_low_memory(nrows)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "pandas\_libs\parsers.pyx", line 812, in pandas._libs.parsers.TextReader.read_low_memory
File "pandas\_libs\parsers.pyx", line 873, in pandas._libs.parsers.TextReader._read_rows
File "pandas\_libs\parsers.pyx", line 848, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas\_libs\parsers.pyx", line 859, in pandas._libs.parsers.TextReader._check_tokenize_status
File "pandas\_libs\parsers.pyx", line 2025, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 6 fields in line 134634, saw 11
PS E:\STM32_AI\HAR-CNN-Keras-master\HAR-CNN-Keras-master> |