使用2D卷积,把79层切片 理解成 RGB,把输入改成(79,95,79)。试着用ImageNet预训练权重加速训练。 这里用ImageNet ResNet50预训练模型。 把epoch设置5时的准确率
把epoch设置50时的准确率 把epoch设置100时的准确率
import os
import h5py
import numpy as np
from keras.utils import np_utils
import pandas as pd
from keras.applications import resnet
from sklearn.model_selection import train_test_split
import tensorflow.keras as keras
from keras.layers import Dense,GlobalAvgPool2D
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
train_dir = 'train'
train_data = 'train_pre_data.h5'
train_label = 'train_pre_label.csv'
train = h5py.File(os.path.join(train_dir,train_data),'r')
labels = pd.read_csv(os.path.join(train_dir,train_label))
features = np.array(train['data'])
features = features.reshape(300,79,95,79)
X_train, X_test, y_train, y_test = train_test_split(features,labels['label'].values,test_size = 0.3,random_state = 42)
y_train = np_utils.to_categorical(y_train,num_classes=3)
y_test = np_utils.to_categorical(y_test,num_classes=3)
num_classes = 3
inputdim = (79,95,79)
base_model =resnet.ResNet50(include_top=False, weights = None, input_shape = inputdim)
x = base_model.output
x = GlobalAvgPool2D()(x)
x = Dense(64,activation='relu')(x)
x = Dense(32,activation='relu')(x)
x = Dense(num_classes,activation='softmax')(x)
model = keras.Model(inputs = base_model.input, outputs =x)
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.summary()
print('Training---------')
model.fit(X_train,y_train,epochs=100,batch_size=32)
print('\nTesting---------')
loss,accuracy=model.evaluate(X_test,y_test)
print('\ntest loss',loss)
print('\ntest accuracy',accuracy)
|