阿尔兹海默症分类2D方法

该博客介绍了如何利用2D卷积将79层切片数据转换为RGB格式,并调整输入尺寸为(79, 95, 79)。通过引入预训练的ImageNetResNet50模型加速训练过程,数据集经过预处理和划分后,分别展示了在epoch设为5、50、100时的模型准确率。最后,模型在测试集上进行了评估,得到了测试损失和准确率。
摘要由CSDN通过智能技术生成

使用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)


#神经网络,用ImageNet ResNet50预训练模型
num_classes = 3
inputdim = (79,95,79)
base_model =resnet.ResNet50(include_top=False, weights = None, input_shape = inputdim)
x = base_model.output
#GlobalAvgPool2D是将输入特征图的每一个通道求平均得到一个数值。
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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值