目录
安装TensorFlow和其他库
数据集
加载预训练的ResNet50模型
使用ImageDataGenerator加载数据
下一步
- 下载源 - 300.4 KB
在本系列文章中,我们将应用深度学习网络ResNet50来诊断胸部X射线图像中的Covid-19。我们将使用Python的TensorFlow库在Jupyter Notebook上训练神经网络。
此项目所需的工具和库是:
IDE:
- Jupyter Notebook
库:
- TensorFlow 2.0
- Keras
- NumPy
- Matplotlib
- CV2
我们假设您熟悉使用Python和Jupyter 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已正确安装。
现在我们需要安装一些基本的库,比如NumPy和Matplotlib。打开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-19、Normal和Pneumonia。我们的目标是对Covid-19的“正面”和“负面”图像进行分类;为此,我们只需要Covid-19和Normal类。因此,在下载数据集后,我们从中删除了肺炎类。该数据集包含1,143张COVID-19阳性图像和1,341张正常图像(冠状病毒阴性)。
图像应下载并预处理以适合por网络的输入格式——调整为224x224x3。您可以使用 TensorFlow的ImageDataGenerator加载和调整图像大小。
加载预训练的ResNet50模型首先,我们需要加载预训练模型并冻结其权重。在我们的项目中,我们将使用 ResNet50 作为Keras内置神经网络模型(包括ResNet、Inception、GoogleNet 等)的预定义网络架构。
由于我们想使用迁移学习而不是从头开始,因此我们要求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())
图 3:ResNet50基础模型的快照
然后我们可以显示网络层的名称和编号,以便在以后的阶段可以轻松地将它们设置为可训练的。
for i, layer in enumerate(base_model.layers):
print(i, layer.name)
TensorFlow和Keras提供了一种使用ImageDataGenerator 。此功能允许您在一次操作中预处理您的数据——调整大小、重新缩放和洗牌。
首先,我们从预训练的ResNet50模型中调用预处理函数。
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=tf.keras.applications.resnet50.preprocess_input)
接下来,我们将批量从我们的项目目录中收集训练和测试图像,并将它们分别存储在train_datagen和test_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-19和Normal。要检查图像的标签,请键入:
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