(二)为COVID-19诊断准备深度学习环境

目录

安装TensorFlow和其他库

数据集

加载预训练的ResNet50模型

使用ImageDataGenerator加载数据

下一步


在本系列文章中,我们将应用深度学习网络ResNet50来诊断胸部X射线图像中的Covid-19。我们将使用PythonTensorFlow库在Jupyter Notebook上训练神经网络。

此项目所需的工具和库是:

IDE

库:

我们假设您熟悉使用PythonJupyter notebook进行深度学习。如果您不熟悉Python,请从本教程开始。如果您还不熟悉Jupyter,请从这里开始

上一篇文章中,我们介绍了迁移学习和 ResNet50。在本文中,除了安装TensorFlow和启动网络训练所需的其他库之外,我们还将讨论用于训练ResNet50的数据集。

安装TensorFlow和其他库

在这个项目中,我们将在Jupyter Notebook上使用Python 3.7。我们将使用TensorFlow 2.0作为深度学习库来构建我们的模型。要安装TensorFlow,请打开Anaconda并运行以下GPU CUDA命令:

conda create -n tf-gpu-cuda8 tensorflow-gpu cudatoolkit=10.0
conda activate tf-gpu-cuda8

要检查TensorFlow是否安装正确,请打开Jupyter Notebook并键入:

Import Tensorflow as tf

如果您没有收到任何错误,则TensorFlow已正确安装。

现在我们需要安装一些基本的库,比如NumPyMatplotlib。打开Anaconda并输入以下内容:

conda install numpy
conda install -c conda-forge matplotlib

打开您的Jupyter Notebook,添加这两个命令,并确保它们不会产生错误。

Import numpy as np
Import matplotlib.pyplot as plt

一旦我们安装了所有必需的库,我们将它们与一些我们将在这个项目中使用的附加包一起导入:

# Import required libraries
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import random
from keras.applications.imagenet_utils import preprocess_input
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

from keras.models import Model
from keras.applications import ResNet50
from keras.preprocessing.image import ImageDataGenerator

数据集

在开始我们的网络编码之前,我们需要一组图像来训练和测试网络。在这个项目中,我们将使用Covid-19胸部X射线图像公共数据集。该数据集包含三类图像:Covid-19NormalPneumonia。我们的目标是对Covid-19正面负面图像进行分类;为此,我们只需要Covid-19Normal类。因此,在下载数据集后,我们从中删除了肺炎类。该数据集包含1,143COVID-19阳性图像和1,341张正常图像(冠状病毒阴性)。

图像应下载并预处理以适合por网络的输入格式——调整为224x224x3。您可以使用 TensorFlowImageDataGenerator加载和调整图像大小。

加载预训练的ResNet50模型

首先,我们需要加载预训练模型并冻结其权重。在我们的项目中,我们将使用 ResNet50 作为Keras内置神经网络模型(包括ResNetInceptionGoogleNet 等)的预定义网络架构。

由于我们想使用迁移学习而不是从头开始,因此我们要求Keras加载已经在ImageNet图像上训练过的ResNet 50副本。该选项 include_top=False 允许通过删除最后的密集层来提取特征。这有助于我们控制模型的输出和输入。

model = tf.keras.applications.ResNet50(weights='imagenet')
base_model = tf.keras.applications.ResNet50(weights='imagenet', include_top = False)
print(base_model.summary())

3ResNet50基础模型的快照

然后我们可以显示网络层的名称和编号,以便在以后的阶段可以轻松地将它们设置为可训练的。

for i, layer in enumerate(base_model.layers):
  print(i, layer.name)

使用ImageDataGenerator加载数据

TensorFlowKeras提供了一种使用ImageDataGenerator 。此功能允许您在一次操作中预处理您的数据——调整大小、重新缩放和洗牌。

首先,我们从预训练的ResNet50模型中调用预处理函数。

train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=tf.keras.applications.resnet50.preprocess_input)

接下来,我们将批量从我们的项目目录中收集训练和测试图像,并将它们分别存储在train_datagentest_datagen目录中。

train_datagen = ImageDataGenerator(preprocessing_function = preprocess_input)
test_datagen = ImageDataGenerator(preprocessing_function = preprocess_input)
train_generator = train_datagen.flow_from_directory(r'C:\Users\abdul\Desktop\Research\Covid=19\COVDATA\train', 
                                                   target_size = (224, 224),
                                                   color_mode = 'rgb',
                                                   batch_size = 3,
                                                   class_mode = 'binary',
                                                   shuffle = True)
test_generator = test_datagen.flow_from_directory(r'C:\Users\abdul\Desktop\Research\Covid=19\COVDATA\test', 
                                                   target_size = (224, 224),
                                                   color_mode = 'rgb',
                                                   batch_size = 3,
                                                   class_mode = 'binary',
                                                   shuffle = True)

注意上面的函数包含One-hot编码,用于标记我们在这个项目中的两个类别:Covid-19Normal。要检查图像的标签,请键入:

train_datagen.label

正如您在代码中看到的,我们将图像大小调整为224x224x3以适应ResNet50的输入格式。我们使用二元类模式,因为我们的分类任务是二元任务;它只处理两个类。

然后,我们可以可视化一些将用于训练网络的数据图像。我们可以使用OpenCV一张一张地显示图像,如下例所示:

imageformat=".png"
path=r'C:\Users\abdul\Desktop\ContentLab\ContentLab[Abdulkader_Helwan]\test2\COVID-19'
imfilelist=[os.path.join(path,f) for f in os.listdir(path) if f.endswith(imageformat)]
for el in imfilelist:
        print(el)
        image = cv2.imread(el, cv2.IMREAD_COLOR)
        cv2.imshow('Image', image) #Show the image
        cv2.waitKey(1000)

这将依次显示图像,如图4所示:

4:使用 cv2 读取和显示所有图像

下一步

接下来的文章中,我们将在重组ResNet50执行新的分类任务工作。敬请关注!

https://www.codeproject.com/Articles/5294461/Preparing-a-Deep-Learning-Environment-for-COVID-19

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值