使用深度学习进行脑肿瘤检测和定位:第 2 部分

2023-10-09 15:20

本文主要是介绍使用深度学习进行脑肿瘤检测和定位:第 2 部分,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题陈述

通过使用 Kaggle 的 MRI 数据集的图像分割来预测和定位脑肿瘤。

这是该系列的第二部分。如果你还没有阅读第一部分,我建议你访问使用深度学习进行脑肿瘤检测和定位:第1部分以更好地理解代码,因为这两个部分是相互关联的。

文章地址:https://mp.weixin.qq.com/s/vBsTsVvHjA0gtQy3X1wdmw

我们在 ResNet50 上训练了一个分类模型,该模型使用回调对脑部 MRI 是否有肿瘤进行分类以提高我们的性能。在这一部分,我们将训练一个模型来使用图像分割来定位肿瘤。

先决条件

深度学习

数据集链接:https://www.kaggle.com/mateuszbuda/lgg-mri-segmentation

现在,让我们开始实施第二部分,即构建分割模型来定位肿瘤。

图像分割的目标是在像素级别理解图像。它将每个像素与某个类相关联。图像分割模型产生的输出称为图像的蒙版。

  • 首先,从我们在上一部分创建的数据帧中选择蒙版值为 1 的记录,因为只有肿瘤存在,我们才能对其进行定位。

# Get the dataframe containing MRIs which have masks associated with them.
brain_df_mask = brain_df[brain_df['mask'] == 1]
brain_df_mask.shape

输出:(1373, 4)

  • 将数据拆分为训练和测试数据集。首先,我们将整个数据拆分为训练和验证数据,然后将一半的验证数据拆分为测试数据。

from sklearn.model_selection import train_test_split
X_train, X_val = train_test_split(brain_df_mask, test_size=0.15)
X_test, X_val = train_test_split(X_val, test_size=0.5)
  • 我们将再次使用DataGenerator 生成虚拟数据,即training_generator 和validation_generator。为此,我们将首先创建要传递到生成器的图像和蒙版路径的列表。

train_ids = list(X_train.image_path)
train_mask = list(X_train.mask_path)val_ids = list(X_val.image_path)
val_mask= list(X_val.mask_path)# Utilities file contains the code for custom data generator
from utilities import DataGenerator# create image generators
training_generator = DataGenerator(train_ids,train_mask)
validation_generator = DataGenerator(val_ids,val_mask)
  • 定义一个如下所示的方法 Resblock ,以在我们的深度学习模型中使用。

模型中使用 Resblocks 以获得更好的结果。这些块只是一堆层。resblocks 的主要功能是在顶部学习残差函数,而信息沿底部传递不变。

def resblock(X, f):# make a copy of inputX_copy = XX = Conv2D(f, kernel_size = (1,1) ,strides = (1,1),kernel_initializer ='he_normal')(X)X = BatchNormalization()(X)X = Activation('relu')(X) X = Conv2D(f, kernel_size = (3,3), strides =(1,1), padding = 'same', kernel_initializer ='he_normal')(X)X = BatchNormalization()(X)X_copy = Conv2D(f, kernel_size = (1,1), strides =(1,1), kernel_initializer ='he_normal')(X_copy)X_copy = BatchNormalization()(X_copy)# Adding the output from main path and short path togetherX = Add()([X,X_copy])X = Activation('relu')(X)return X
  • 同样,定义 upsample_concat 方法来放大和连接传递的值。Upsampling 层是一个简单的层,没有权重,可以将输入的维度加倍。

def upsample_concat(x, skip):x = UpSampling2D((2,2))(x)merge = Concatenate()([x, skip])return merge
  • 建立一个分割模型,添加下面显示的层,包括上面定义的 resblock 和 upsample_concat。

input_shape = (256,256,3)# Input tensor shape
X_input = Input(input_shape)# Stage 1
conv1_in = Conv2D(16,3,activation= 'relu', padding = 'same', kernel_initializer ='he_normal')(X_input)
conv1_in = BatchNormalization()(conv1_in)
conv1_in = Conv2D(16,3,activation= 'relu', padding = 'same', kernel_initializer ='he_normal')(conv1_in)
conv1_in = BatchNormalization()(conv1_in)
pool_1 = MaxPool2D(pool_size = (2,2))(conv1_in)# Stage 2
conv2_in = resblock(pool_1, 32)
pool_2 = MaxPool2D(pool_size = (2,2))(conv2_in)# Stage 3
conv3_in = resblock(pool_2, 64)
pool_3 = MaxPool2D(pool_size = (2,2))(conv3_in)# Stage 4
conv4_in = resblock(pool_3, 128)
pool_4 = MaxPool2D(pool_size = (2,2))(conv4_in)# Stage 5 (Bottle Neck)
conv5_in = resblock(pool_4, 256)# Upscale stage 1
up_1 = upsample_concat(conv5_in, conv4_in)
up_1 = resblock(up_1, 128)# Upscale stage 2
up_2 = upsample_concat(up_1, conv3_in)
up_2 = resblock(up_2, 64)# Upscale stage 3
up_3 = upsample_concat(up_2, conv2_in)
up_3 = resblock(up_3, 32)# Upscale stage 4
up_4 = upsample_concat(up_3, conv1_in)
up_4 = resblock(up_4, 16)# Final Output
output = Conv2D(1, (1,1), padding = "same", activation = "sigmoid")(up_4)model_seg = Model(inputs = X_input, outputs = output )
  • 编译上面训练的模型。这次我们将自定义优化器的参数。Focal tversky 是损失函数,tversky 是度量。

# Utilities file also contains the code for custom loss function
from utilities import focal_tversky, tversky# Compile the model
adam = tf.keras.optimizers.Adam(lr = 0.05, epsilon = 0.1)
model_seg.compile(optimizer = adam, loss = focal_tversky, metrics = [tversky])
  • 现在,你知道我们在分类器模型中使用的回调。我们将使用相同的方法来获得更好的性能。最后,我们训练我们的分割模型。

# use early stopping to exit training if validation loss is not decreasing even after certain epochs.
earlystopping = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=20)# save the best model with lower validation loss
checkpointer = ModelCheckpoint(filepath="ResUNet-weights.hdf5", verbose=1, save_best_only=True)model_seg.fit(training_generator, epochs = 1, validation_data = validation_generator, callbacks = [checkpointer, earlystopping])
  • 预测测试数据集的蒙版。这里,model是前面训练的分类器模型,model_seg是上面训练的分割模型。

from utilities import prediction# making prediction
image_id, mask, has_mask = prediction(test, model, model_seg)

输出将为我们提供图像路径、预测蒙版和类标签。

  • 根据预测结果创建数据帧并与 image_path 上的测试数据帧合并。

# creating a dataframe for the result
df_pred = pd.DataFrame({'image_path': image_id,'predicted_mask': mask,'has_mask': has_mask})# Merge the dataframe containing predicted results with the original test data.
df_pred = test.merge(df_pred, on = 'image_path')
df_pred.head()

正如你在输出中看到的那样,我们现在已将最终预测的蒙版合并到我们的数据帧中。

  • 最后,将原始图像、原始蒙版和预测蒙版一起可视化,以分析我们的分割模型的准确性。

count = 0 
fig, axs = plt.subplots(10, 5, figsize=(30, 50))
for i in range(len(df_pred)):if df_pred['has_mask'][i] == 1 and count < 5:# read the images and convert them to RGB formatimg = io.imread(df_pred.image_path[i])img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)axs[count][0].title.set_text("Brain MRI")axs[count][0].imshow(img)# Obtain the mask for the image mask = io.imread(df_pred.mask_path[i])axs[count][1].title.set_text("Original Mask")axs[count][1].imshow(mask)# Obtain the predicted mask for the image predicted_mask = np.asarray(df_pred.predicted_mask[i])[0].squeeze().round()axs[count][2].title.set_text("AI Predicted Mask")axs[count][2].imshow(predicted_mask)# Apply the mask to the image 'mask==255'img[mask == 255] = (255, 0, 0)axs[count][3].title.set_text("MRI with Original Mask (Ground Truth)")axs[count][3].imshow(img)img_ = io.imread(df_pred.image_path[i])img_ = cv2.cvtColor(img_, cv2.COLOR_BGR2RGB)img_[predicted_mask == 1] = (0, 255, 0)axs[count][4].title.set_text("MRI with AI Predicted Mask")axs[count][4].imshow(img_)count += 1fig.tight_layout()

输出显示我们的分割模型非常好地定位了肿瘤。做得好!

此外,你可以尝试向目前训练的模型添加更多层并分析性能。还可以将类似的解决方案应用于其他问题陈述,因为图像分割是当今非常感兴趣的领域。

☆ END ☆

如果看到这里,说明你喜欢这篇文章,请转发、点赞。微信搜索「uncle_pn」,欢迎添加小编微信「 woshicver」,每日朋友圈更新一篇高质量博文。

扫描二维码添加小编↓

这篇关于使用深度学习进行脑肿瘤检测和定位:第 2 部分的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/173760

相关文章

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el

C#中Trace.Assert的使用小结

《C#中Trace.Assert的使用小结》Trace.Assert是.NET中的运行时断言检查工具,用于验证代码中的关键条件,下面就来详细的介绍一下Trace.Assert的使用,具有一定的参考价值... 目录1、 什么是 Trace.Assert?1.1 最简单的比喻1.2 基本语法2、⚡ 工作原理3

C# IPAddress 和 IPEndPoint 类的使用小结

《C#IPAddress和IPEndPoint类的使用小结》本文主要介绍了C#IPAddress和IPEndPoint类的使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录一、核心作用网络编程基础类二、IPAddress 类详解三种初始化方式1. byte 数组初始化2. l

C语言逗号运算符和逗号表达式的使用小结

《C语言逗号运算符和逗号表达式的使用小结》本文详细介绍了C语言中的逗号运算符和逗号表达式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 在C语言中逗号“,”也是一种运算符,称为逗号运算符。 其功能是把两个表达式连接其一般形式为:表达