测试集准确率特别低,为什么呢,帮忙看看。
1、数据转图片(利用格拉姆角场,数据是肌电信号,有12个传感器,所以数据变成(300,300,12)格式。
imageData=[]
imageLabel=[]#空数据集
imageLength=300#窗口长
classes = 49
method = 'summation'
n_sample, n_channels = scaled_X.shape
img_sz =300#图片大小
for i in range(classes):
index = [];
for j in range(label_ex.shape[0]):
if(label_ex[j,:]==i):
index.append(j)
iemg = scaled_X[index,:]
length = math.floor((iemg.shape[0]-imageLength)/imageLength)
#print("class ",i," number of sample: ",iemg.shape[0],length)
#length为每个标签的窗口数
for j in range(length):
subImage = iemg[imageLength*j:imageLength*(j+1),:]#每一个小窗口(300*12)
gaf = GramianAngularField(image_size=img_sz,method=method)#定义格拉姆角场
gaf_images = gaf.fit_transform(subImage.T)#窗口转格拉姆角场图片
#print(gaf_images.shape)
#gaf_images = gaf_images / 255.0
#print("gaf_images的形状是:",gaf_images.shape)
connectdata=[]
#gaf_images_convert=gaf_images.convert("LA")
for c in range(n_channels):
gaf_img = gaf_images[c, :, :] #得到每个通道的图片/12
#print("gaf_img的形状是:",gaf_img.shape)
#下面处理hog的特征#处理每个通道的图片/12
fd, hog_img = hog(gaf_img,
orientations=8,
pixels_per_cell=(16, 16),
cells_per_block=(1, 1),
visualize=True,
multichannel=False)#多通道模式
connectdata.append(hog_img)#每个通道存放处
connectdata=np.stack(connectdata)#图片进行堆叠
connectdatas=np.array(connectdata)
print("connectdatas的形状是:",connectdatas.shape)
imageData.append(connectdatas)
imageLabel.append(i)
imageData=np.array(imageData)
connectdatas=np.transpose(imageData, (0,2, 3, 1))
imageLabel=np.array(imageLabel)
print(connectdatas.shape)
print(imageLabel.shape)
2、之后划分数据集和对标签进行升维。
mport h5py
import numpy as np
import tensorflow as tf
import keras
from keras.layers import Input, Dense, ZeroPadding2D, Dropout, Activation, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D
from keras.models import Model
import matplotlib.pyplot as plt
%matplotlib inline
def convert_to_one_hot(Y, C):
Y = np.eye(C)[Y.reshape(-1)].T
return Y
imageData = connectdatas
imageLabel = imageLabel
# 随机打乱数据和标签
N = imageData.shape[0]
index = np.random.permutation(N)
data = imageData[index,:,:]
label = imageLabel[index]
# 对数据升维,标签one-hot
#data = np.expand_dims(data, axis=3)
label = convert_to_one_hot(label,49).T
# 划分数据集
N = data.shape[0]
num_train = round(N*0.8)
X_train = data[0:num_train,:,:,:]
Y_train = label[0:num_train,:]
X_test = data[num_train:N,:,:,:]
Y_test = label[num_train:N,:]
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))#
3、建立模型进行分类
import tensorflow as tf
import tensorflow as tf
from tensorflow import keras
from keras import layers,models
from keras.models import load_model
# 定义Inceptionv3网络模型
def Inceptionv3():
input_tensor = layers.Input(shape=(300, 300, 12))
x = layers.Conv2D(32, (3, 3), strides=(2, 2), padding='valid', activation='relu')(input_tensor)
x = layers.BatchNormalization()(x)
x = layers.Conv2D(32, (3, 3), strides=(1, 1), padding='valid', activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.Conv2D(64, (3, 3), strides=(1, 1), padding='same', activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x)
x = layers.Conv2D(80, (1, 1), strides=(1, 1), padding='valid', activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.Conv2D(192, (3, 3), strides=(1, 1), padding='valid', activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x)
# Inception模块
x = inception_module(x, [64, 96, 128, 16, 32, 32])
x = inception_module(x, [128, 128, 192, 32, 96, 64])
x = layers.MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x)
# Inception模块
x = inception_module(x, [192, 96, 208, 16, 48, 64])
x = inception_module(x, [160, 112, 224, 24, 64, 64])
x = inception_module(x, [128, 128, 256, 24, 64, 64])
x = inception_module(x, [112, 144, 288, 32, 64, 64])
x = inception_module(x, [256, 160, 320, 32, 128, 128])
x = layers.MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x)
# Inception模块
x = inception_module(x, [256, 160, 320, 32, 128, 128])
x = inception_module(x, [384, 192, 384, 48, 128, 128])
x = layers.GlobalAveragePooling2D()(x)
output_tensor = layers.Dense(49, activation='softmax')(x)
model = models.Model(inputs=input_tensor, outputs=output_tensor)
return model
# 定义Inception模块
def inception_module(x, filters):
branch1x1 = layers.Conv2D(filters[0], (1, 1), strides=(1, 1), padding='same', activation='relu')(x)
branch3x3 = layers.Conv2D(filters[1], (1, 1), strides=(1, 1), padding='same', activation='relu')(x)
branch3x3 = layers.Conv2D(filters[2], (3, 3), strides=(1, 1), padding='same', activation='relu')(branch3x3)
branch5x5 = layers.Conv2D(filters[3], (1, 1), strides=(1, 1), padding='same', activation='relu')(x)
branch5x5 = layers.Conv2D(filters[4], (5, 5), strides=(1, 1), padding='same', activation='relu')(branch5x5)
branch_pool = layers.MaxPooling2D(pool_size=(3, 3), strides=(1, 1), padding='same')(x)
branch_pool =layers.Conv2D(filters[5], (1, 1), strides=(1, 1), padding='same', activation='relu')(branch_pool)
output = layers.concatenate([branch1x1, branch3x3, branch5x5, branch_pool], axis=-1)
return output
# 构建Inceptionv3模型
model = Inceptionv3()
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# 训练模型
model.fit(X_train, Y_train, batch_size=128, epochs=50, verbose=1)
# 评估模型
test_loss, test_acc = model.evaluate(X_test, Y_test, verbose=0)
print('Test loss:', test_loss)
print('Test accuracy:', test_acc)
4、结果所示
Epoch 1/50
3/3 [==============================] - 30s 8s/step - loss: 4.7497 - accuracy: 0.0116
Epoch 2/50
3/3 [==============================] - 25s 8s/step - loss: 3.8962 - accuracy: 0.0260
Epoch 3/50
3/3 [==============================] - 25s 8s/step - loss: 3.8852 - accuracy: 0.0318
Epoch 4/50
3/3 [==============================] - 25s 8s/step - loss: 3.8771 - accuracy: 0.0260
Epoch 5/50
3/3 [==============================] - 25s 8s/step - loss: 3.8285 - accuracy: 0.0289
Epoch 6/50
3/3 [==============================] - 25s 8s/step - loss: 3.7667 - accuracy: 0.0289
Epoch 7/50
3/3 [==============================] - 24s 8s/step - loss: 3.7337 - accuracy: 0.0405
Epoch 8/50
3/3 [==============================] - 24s 8s/step - loss: 3.6868 - accuracy: 0.0405
Epoch 9/50
3/3 [==============================] - 24s 8s/step - loss: 3.6023 - accuracy: 0.0520
Epoch 10/50
3/3 [==============================] - 25s 8s/step - loss: 3.5208 - accuracy: 0.0347
Epoch 11/50
3/3 [==============================] - 25s 8s/step - loss: 3.4885 - accuracy: 0.0434
Epoch 12/50
3/3 [==============================] - 25s 8s/step - loss: 3.4126 - accuracy: 0.0578
Epoch 13/50
3/3 [==============================] - 26s 8s/step - loss: 3.4616 - accuracy: 0.0405
Epoch 14/50
3/3 [==============================] - 26s 8s/step - loss: 3.3438 - accuracy: 0.0520
Epoch 15/50
3/3 [==============================] - 25s 8s/step - loss: 3.3497 - accuracy: 0.0578
Epoch 16/50
3/3 [==============================] - 25s 8s/step - loss: 3.1960 - accuracy: 0.0867
Epoch 17/50
3/3 [==============================] - 24s 8s/step - loss: 3.1662 - accuracy: 0.0838
Epoch 18/50
3/3 [==============================] - 24s 8s/step - loss: 3.0967 - accuracy: 0.0694
Epoch 19/50
3/3 [==============================] - 26s 8s/step - loss: 3.0880 - accuracy: 0.0751
Epoch 20/50
3/3 [==============================] - 25s 8s/step - loss: 3.0908 - accuracy: 0.0751
Epoch 21/50
3/3 [==============================] - 24s 8s/step - loss: 3.1432 - accuracy: 0.0925
Epoch 22/50
3/3 [==============================] - 24s 8s/step - loss: 3.0984 - accuracy: 0.0954
Epoch 23/50
3/3 [==============================] - 25s 8s/step - loss: 2.9826 - accuracy: 0.1185
Epoch 24/50
3/3 [==============================] - 25s 8s/step - loss: 2.9005 - accuracy: 0.1127
Epoch 25/50
3/3 [==============================] - 26s 8s/step - loss: 2.7154 - accuracy: 0.1561
Epoch 26/50
3/3 [==============================] - 25s 8s/step - loss: 2.7371 - accuracy: 0.1503
Epoch 27/50
3/3 [==============================] - 25s 8s/step - loss: 2.7225 - accuracy: 0.1214
Epoch 28/50
3/3 [==============================] - 27s 9s/step - loss: 2.5903 - accuracy: 0.1792
Epoch 29/50
3/3 [==============================] - 26s 8s/step - loss: 2.4250 - accuracy: 0.1994
Epoch 30/50
3/3 [==============================] - 25s 8s/step - loss: 2.2562 - accuracy: 0.2486
Epoch 31/50
3/3 [==============================] - 24s 8s/step - loss: 2.2740 - accuracy: 0.2254
Epoch 32/50
3/3 [==============================] - 24s 8s/step - loss: 2.5253 - accuracy: 0.1936
Epoch 33/50
3/3 [==============================] - 24s 8s/step - loss: 2.4184 - accuracy: 0.1676
Epoch 34/50
3/3 [==============================] - 24s 8s/step - loss: 2.3348 - accuracy: 0.2312
Epoch 35/50
3/3 [==============================] - 24s 8s/step - loss: 2.3162 - accuracy: 0.2543
Epoch 36/50
3/3 [==============================] - 24s 8s/step - loss: 2.1927 - accuracy: 0.2688
Epoch 37/50
3/3 [==============================] - 24s 8s/step - loss: 2.0228 - accuracy: 0.2775
Epoch 38/50
3/3 [==============================] - 24s 8s/step - loss: 2.0253 - accuracy: 0.3439
Epoch 39/50
3/3 [==============================] - 24s 8s/step - loss: 2.0712 - accuracy: 0.2746
Epoch 40/50
3/3 [==============================] - 24s 8s/step - loss: 2.0722 - accuracy: 0.2803
Epoch 41/50
3/3 [==============================] - 24s 8s/step - loss: 1.9843 - accuracy: 0.3208
Epoch 42/50
3/3 [==============================] - 24s 8s/step - loss: 1.9570 - accuracy: 0.3121
Epoch 43/50
3/3 [==============================] - 24s 8s/step - loss: 1.9189 - accuracy: 0.3526
Epoch 44/50
3/3 [==============================] - 24s 8s/step - loss: 1.7406 - accuracy: 0.3988
Epoch 45/50
3/3 [==============================] - 24s 8s/step - loss: 1.6777 - accuracy: 0.4075
Epoch 46/50
3/3 [==============================] - 24s 8s/step - loss: 1.4576 - accuracy: 0.4798
Epoch 47/50
3/3 [==============================] - 24s 7s/step - loss: 1.5254 - accuracy: 0.4653
Epoch 48/50
3/3 [==============================] - 24s 8s/step - loss: 1.9673 - accuracy: 0.3526
Epoch 49/50
3/3 [==============================] - 25s 8s/step - loss: 1.4825 - accuracy: 0.4913
Epoch 50/50
3/3 [==============================] - 24s 8s/step - loss: 1.4271 - accuracy: 0.5058
Test loss: 11.708770751953125
Test accuracy: 0.023255813866853714