脑PET图像分类示例笔记

1. 数据准备与预处理

在进行医学图像分类时,数据预处理是关键的一步。这个过程涉及数据加载、路径管理和一些基本信息的了解。

  • 使用glob库获取图像路径,确保数据读取的便捷性。
  • 对训练集和测试集的路径进行随机打乱,避免数据顺序性影响模型性能。
# 导入所需库
import glob
import numpy as np
import pandas as pd
import nibabel as nib
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# 读取训练集和测试集路径
train_path = glob.glob('./脑PET图像分析和疾病预测挑战赛数据集/Train/*/*')
test_path = glob.glob('./脑PET图像分析和疾病预测挑战赛数据集/Test/*')

# 随机种子
random_seed = 42

2. 特征提取与数据准备

特征提取是将图像转化为模型可用的数值特征的过程。在这个示例中,将提取基本的统计特征。

  • 利用nibabel库读取.nii图像,了解其数据结构。
  • 设计合适的特征提取函数,例如计算图像的平均值、标准差以及分位数等,以提取图像的有用信息。
  • 将特征与标签配对,创建训练集特征矩阵和标签列表。
  • 使用StandardScaler进行特征标准化,确保模型训练的稳定性和准确性。
# 定义特征提取函数
def extract_features(path):
    img = nib.load(path)
    img_data = img.dataobj[:, :, :, 0]
    
    features = [
        img_data.mean(),
        img_data.std(),
        np.percentile(img_data, 25),
        np.percentile(img_data, 75),
        np.percentile(img_data, 50)
    ]
    
    return features

# 特征提取和数据准备
train_features = []
train_labels = []

for path in train_path:
    features = extract_features(path)
    train_features.append(features)
    if 'NC' in path:
        train_labels.append('NC')  # 健康标签
    else:
        train_labels.append('MCI')  # 轻度认知障碍标签

# 数据标准化
scaler = StandardScaler()
train_features_scaled = scaler.fit_transform(train_features)

3. 模型训练与预测

使用已准备好的特征和标签,可以进行模型训练和预测

  • 使用train_test_split划分训练集和验证集,进行模型评估。
  • 选择适当的分类算法,本示例中使用RandomForestClassifier作为模型,可根据情况尝试其他算法。
  • 训练模型并进行验证集预测,通过accuracy_score计算分类准确率。
  • 针对测试集进行特征提取和标准化,然后使用训练好的模型进行预测。
  • 生成提交结果的DataFrame,按照样本ID排序并保存为CSV文件。
# 划分训练集和验证集
X_train, X_valid, y_train, y_valid = train_test_split(train_features_scaled, train_labels, test_size=0.2, random_state=random_seed)

# 使用随机森林模型
model = RandomForestClassifier(n_estimators=100, random_state=random_seed)
model.fit(X_train, y_train)

# 验证集预测
valid_predictions = model.predict(X_valid)
accuracy = accuracy_score(y_valid, valid_predictions)
print(f"Validation Accuracy: {accuracy:.2f}")

# 测试集预测
test_features = []

for path in test_path:
    features = extract_features(path)
    test_features.append(features)

test_features_scaled = scaler.transform(test_features)
test_predictions = model.predict(test_features_scaled)

# 生成提交结果的DataFrame
submit = pd.DataFrame(
    {
        'uuid': [int(x.split('/')[-1][:-4]) for x in test_path],  # 提取测试集文件名中的ID
        'label': test_predictions                                 # 预测的类别
    }
)

# 按照ID对结果排序并保存为CSV文件
submit = submit.sort_values(by='uuid')
submit.to_csv('submit.csv', index=None)

感想与展望

通过完成这个示例,我对医学影像处理和机器学习分类有了更深刻的理解。特别是在特征提取和模型选择方面,我学到了如何从图像中提取有意义的信息,并将其应用于模型中。此外,探索不同的特征、模型和参数设置,以及应用交叉验证和集成技术等,都可以进一步提高分类准确率。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值