📜  使用深度学习进行肺炎检测

📅  最后修改于: 2022-05-13 01:54:23.262000             🧑  作者: Mango

使用深度学习进行肺炎检测

在这篇文章中,我们将讨论解决一个医学问题,即肺炎,这是一种危险的疾病,可能发生在一个或两个肺部,通常由病毒、真菌或细菌引起。我们将根据我们拥有的 X 射线检测这种肺部疾病。胸部 X 射线数据集取自 Kaggle,其中包含按“肺炎”“正常”两个类别区分的各种 X 射线图像。我们将创建一个深度学习模型,它实际上会告诉我们这个人是否患有肺炎。

工具和技术:

VGG16:它是一种简单且广泛使用的卷积神经网络 (CNN) 架构,用于 ImageNet,这是一个用于视觉对象识别软件研究的巨大可见数据库任务。

迁移学习 (TL):它是一种深度学习技术,专注于采用预训练的神经网络并存储在解决一个问题时获得的知识并将其应用于新的不同数据集。在本文中,在学习识别 ImageNet 中的 1000 个不同类别时获得的知识可以应用于尝试识别疾病。

模型架构:

所需模块:

  • Keras:它是一个运行在 TensorFlow 库之上的用于深度学习的Python模块。它的创建是为了使深度学习模型的实施尽可能容易和快速,以进行研究和开发。由于 Keras 在 Keras 之上运行,我们必须先安装 TensorFlow。要安装此库,请在 IDE/终端中键入以下命令。
pip install tensorflow
    pip install keras
  • SciPy: SciPy 是一个免费的开源Python模块,用于技术和科学计算。由于我们在本文中需要图像转换,因此我们必须安装 SciPy 模块。要安装此库,请在 IDE/终端中键入以下命令。
pip install scipy
  • glob:在Python中,glob 模块用于检索与指定模式匹配的文件/路径名。为了找出我们的训练数据集文件夹中有多少类,我们在本文中使用了这个模块。
pip install glob2

逐步实施:

第 1 步:从此 url 下载数据集。数据集包含测试、训练、验证文件夹。我们将使用测试和训练数据集来训练我们的模型。然后我们将使用验证数据集验证我们的模型。

第 2 步:导入 keras 中可用的所有必要模块,如 ImageDataGenerator、Model、Dense、Flatten 等。我们将创建一个通用代码,这意味着我们只需更改库名称,然后我们的代码将自动适用于 VGG16、VGG19 和 resnet50。

Python3
from keras.models import Model
from keras.layers import Flatten,Dense
from keras.applications.vgg16 import VGG16
import matplotlib.pyplot as plot
from glob import glob


Python3
IMAGESHAPE = [224, 224, 3] 
training_data = 'chest_xray/train'
testing_data = 'chest_xray/test'


Python3
vgg_model = VGG16(input_shape=IMAGESHAPE, weights='imagenet', include_top=False)


Python3
for each_layer in vgg_model.layers:
    each_layer.trainable = False


Python3
classes = glob('chest_xray/train/*')


Python3
flatten_layer = Flatten()(vgg_model.output)
prediction = Dense(len(classes), activation='softmax')(flatten_layer)


Python3
final_model = Model(inputs=vgg_model.input, outputs=prediction) 
final_model.summary()


Python3
final_model.compile( 
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)


Python3
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255, 
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
testing_datagen = ImageDataGenerator(rescale =1. / 255)


Python3
training_set = train_datagen.flow_from_directory('chest_xray/train', 
                                                 target_size = (224, 224),
                                                 batch_size = 4,
                                                 class_mode = 'categorical')


Python3
test_set = testing_datagen.flow_from_directory('chest_xray/test',
                                               target_size = (224, 224),
                                               batch_size = 4,
                                               class_mode = 'categorical')


Python3
fitted_model = final_model.fit_generator( 
  training_set,
  validation_data=test_set,
  epochs=5,
  steps_per_epoch=len(training_set),
  validation_steps=len(test_set)
)


Python3
final_model.save('our_model.h5')


Python3
from keras_preprocessing import image
from keras.models import load_model
from keras.applications.vgg16 import preprocess_input
import numpy as np
model=load_model('our_model.h5') #Loading our model
img=image.load_img('D:/Semester - 6/PneumoniaGFG/chest_xray/val/PNEUMONIA/person1954_bacteria_4886.jpeg',target_size=(224,224))
imagee=image.img_to_array(img) #Converting the X-Ray into pixels
imagee=np.expand_dims(imagee, axis=0)
img_data=preprocess_input(imagee)
prediction=model.predict(img_data)
if prediction[0][0]>prediction[0][1]:  #Printing the prediction of model.
    print('Person is safe.')
else:
    print('Person is affected with Pneumonia.')
print(f'Predictions: {prediction}')


Python3
from keras.models import Model
from keras.layers import Flatten,Dense
from keras.applications.vgg16 import VGG16  #Import all the necessary modules
import matplotlib.pyplot as plot
from glob import glob
  
IMAGESHAPE = [224, 224, 3] #Provide image size as 224 x 224 this is a fixed-size for VGG16 architecture
vgg_model = VGG16(input_shape=IMAGESHAPE, weights='imagenet', include_top=False)
#3 signifies that we are working with RGB type of images.
training_data = 'chest_xray/train'
testing_data = 'chest_xray/test' #Give our training and testing path
  
for each_layer in vgg_model.layers:
    each_layer.trainable = False #Set the trainable as False, So that all the layers would not be trained.
classes = glob('chest_xray/train/*') #Finding how many classes present in our train dataset.
flatten_layer = Flatten()(vgg_model.output)
prediction = Dense(len(classes), activation='softmax')(flatten_layer)
final_model = Model(inputs=vgg_model.input, outputs=prediction) #Combine the VGG output and prediction , this all together will create a model.
final_model.summary() #Displaying the summary
final_model.compile( #Compiling our model using adam optimizer and optimization metric as accuracy.
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255, #importing our dataset to keras using ImageDataGenerator in keras.
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
testing_datagen = ImageDataGenerator(rescale =1. / 255)
training_set = train_datagen.flow_from_directory('chest_xray/train', #inserting the images.
                                                 target_size = (224, 224),
                                                 batch_size = 4,
                                                 class_mode = 'categorical')
test_set = testing_datagen.flow_from_directory('chest_xray/test',
                                               target_size = (224, 224),
                                               batch_size = 4,
                                               class_mode = 'categorical')
fitted_model = final_model.fit_generator( #Fitting the model.
  training_set,
  validation_data=test_set,
  epochs=5,
  steps_per_epoch=len(training_set),
  validation_steps=len(test_set)
)
plot.plot(fitted_model.history['loss'], label='training loss') #Plotting the accuracies
plot.plot(fitted_model.history['val_loss'], label='validation loss')
plot.legend()
plot.show()
plot.savefig('LossVal_loss')
plot.plot(fitted_model.history['acc'], label='training accuracy')
plot.plot(fitted_model.history['val_acc'], label='validation accuracy')
plot.legend()
plot.show()
plot.savefig('AccVal_acc')
final_model.save('our_model.h5') #Saving the model file.


Python3
from keras_preprocessing import image
from keras.models import load_model
from keras.applications.vgg16 import preprocess_input
import numpy as np
model=load_model('our_model.h5') #Loading our model
img=image.load_img('D:/Semester - 6/PneumoniaGFG/chest_xray/val/PNEUMONIA/person1954_bacteria_4886.jpeg',target_size=(224,224))
imagee=image.img_to_array(img) #Converting the X-Ray into pixels
imagee=np.expand_dims(imagee, axis=0)
img_data=preprocess_input(imagee)
prediction=model.predict(img_data)
if prediction[0][0]>prediction[0][1]:  #Printing the prediction of model.
    print('Person is safe.')
else:
    print('Person is affected with Pneumonia.')
print(f'Predictions: {prediction}')


第 3 步:在此之后,我们将提供我们的图像尺寸,即 224 x 224,这是 VGG16 架构的固定尺寸。 3 表示我们正在处理 RGB 类型的图像。然后我们将提供我们的训练和测试数据路径。

Python3

IMAGESHAPE = [224, 224, 3] 
training_data = 'chest_xray/train'
testing_data = 'chest_xray/test'

Step4:现在,我们将导入我们的 VGG16 模型。在导入时,我们将使用imageNet 的权重 & include_top=False表示我们不想对imageNet中存在的 1000 个不同类别进行分类,我们的问题是关于肺炎和正常这两个类别,这就是为什么我们只删除第一层和最后一层的原因我们将只设计我们自己的层并将其添加到 VGG16 中。

Python3

vgg_model = VGG16(input_shape=IMAGESHAPE, weights='imagenet', include_top=False)

Step5:导入VGG16模型后,我们要做这个重要的改变。通过使用 for 循环遍历所有层并将可训练设置为 False,这样所有层都不会被训练。

Python3

for each_layer in vgg_model.layers:
    each_layer.trainable = False

第 6 步:我们将尝试查看我们的训练数据集中有多少类,以了解我们应该有多少输出标签。

Python3

classes = glob('chest_xray/train/*') 

Step7:由于我们在上一步中删除了第一列和最后一列,我们将只制作一个扁平层,最后我们只需添加带有softmax激活函数的最后一层。 len(classes)表示我们在输出层中有多少类别。

Python3

flatten_layer = Flatten()(vgg_model.output)
prediction = Dense(len(classes), activation='softmax')(flatten_layer)

Step8:现在我们将VGG输出和预测结合起来,这一切将创建一个模型。当我们检查模型摘要时,我们可以观察到最后一层只有两个类别。

Python3

final_model = Model(inputs=vgg_model.input, outputs=prediction) 
final_model.summary()

Step9:现在我们将使用亚当优化器和优化指标作为准确度来编译我们的模型。

Python3

final_model.compile( 
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)

Step10:编译模型后,我们必须使用 Keras 中的 ImageDataGenerator 将数据集导入 Keras。为了创建其他功能,我们使用 rescale、shear_range、zoom_range 等指标,这些将在训练和测试阶段帮助我们。

Python3

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255, 
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
testing_datagen = ImageDataGenerator(rescale =1. / 255)

Step11:现在我们将使用flow_from_directory()函数插入图像。确保在这里我们必须传递与我们之前启动的相同大小的图像。批量大小 4 表示一次将给出 4 张图像进行训练。 Class_mode 是分类模式,即肺炎或非肺炎。

Python3

training_set = train_datagen.flow_from_directory('chest_xray/train', 
                                                 target_size = (224, 224),
                                                 batch_size = 4,
                                                 class_mode = 'categorical')

Step12:同样,我们将对测试数据集执行与训练数据集相同的操作。

Python3

test_set = testing_datagen.flow_from_directory('chest_xray/test',
                                               target_size = (224, 224),
                                               batch_size = 4,
                                               class_mode = 'categorical')

第 13步:最后,我们使用fit_generator()函数拟合模型,并将有关我们的训练和测试数据集的所有必要细节作为参数传递。这将需要一些时间来执行。

Python3

fitted_model = final_model.fit_generator( 
  training_set,
  validation_data=test_set,
  epochs=5,
  steps_per_epoch=len(training_set),
  validation_steps=len(test_set)
)

Step14:创建模型文件并存储该模型。这样我们就不需要在每次输入时都训练模型。

Python3

final_model.save('our_model.h5')

Step15:加载我们创建的模型。现在读取图像并预处理图像,最后我们使用model.predict()函数检查模型给出的输出。

Python3

from keras_preprocessing import image
from keras.models import load_model
from keras.applications.vgg16 import preprocess_input
import numpy as np
model=load_model('our_model.h5') #Loading our model
img=image.load_img('D:/Semester - 6/PneumoniaGFG/chest_xray/val/PNEUMONIA/person1954_bacteria_4886.jpeg',target_size=(224,224))
imagee=image.img_to_array(img) #Converting the X-Ray into pixels
imagee=np.expand_dims(imagee, axis=0)
img_data=preprocess_input(imagee)
prediction=model.predict(img_data)
if prediction[0][0]>prediction[0][1]:  #Printing the prediction of model.
    print('Person is safe.')
else:
    print('Person is affected with Pneumonia.')
print(f'Predictions: {prediction}')

下面是完整的实现:

肺炎.py:

Python3

from keras.models import Model
from keras.layers import Flatten,Dense
from keras.applications.vgg16 import VGG16  #Import all the necessary modules
import matplotlib.pyplot as plot
from glob import glob
  
IMAGESHAPE = [224, 224, 3] #Provide image size as 224 x 224 this is a fixed-size for VGG16 architecture
vgg_model = VGG16(input_shape=IMAGESHAPE, weights='imagenet', include_top=False)
#3 signifies that we are working with RGB type of images.
training_data = 'chest_xray/train'
testing_data = 'chest_xray/test' #Give our training and testing path
  
for each_layer in vgg_model.layers:
    each_layer.trainable = False #Set the trainable as False, So that all the layers would not be trained.
classes = glob('chest_xray/train/*') #Finding how many classes present in our train dataset.
flatten_layer = Flatten()(vgg_model.output)
prediction = Dense(len(classes), activation='softmax')(flatten_layer)
final_model = Model(inputs=vgg_model.input, outputs=prediction) #Combine the VGG output and prediction , this all together will create a model.
final_model.summary() #Displaying the summary
final_model.compile( #Compiling our model using adam optimizer and optimization metric as accuracy.
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255, #importing our dataset to keras using ImageDataGenerator in keras.
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
testing_datagen = ImageDataGenerator(rescale =1. / 255)
training_set = train_datagen.flow_from_directory('chest_xray/train', #inserting the images.
                                                 target_size = (224, 224),
                                                 batch_size = 4,
                                                 class_mode = 'categorical')
test_set = testing_datagen.flow_from_directory('chest_xray/test',
                                               target_size = (224, 224),
                                               batch_size = 4,
                                               class_mode = 'categorical')
fitted_model = final_model.fit_generator( #Fitting the model.
  training_set,
  validation_data=test_set,
  epochs=5,
  steps_per_epoch=len(training_set),
  validation_steps=len(test_set)
)
plot.plot(fitted_model.history['loss'], label='training loss') #Plotting the accuracies
plot.plot(fitted_model.history['val_loss'], label='validation loss')
plot.legend()
plot.show()
plot.savefig('LossVal_loss')
plot.plot(fitted_model.history['acc'], label='training accuracy')
plot.plot(fitted_model.history['val_acc'], label='validation accuracy')
plot.legend()
plot.show()
plot.savefig('AccVal_acc')
final_model.save('our_model.h5') #Saving the model file.

测试.py:

Python3

from keras_preprocessing import image
from keras.models import load_model
from keras.applications.vgg16 import preprocess_input
import numpy as np
model=load_model('our_model.h5') #Loading our model
img=image.load_img('D:/Semester - 6/PneumoniaGFG/chest_xray/val/PNEUMONIA/person1954_bacteria_4886.jpeg',target_size=(224,224))
imagee=image.img_to_array(img) #Converting the X-Ray into pixels
imagee=np.expand_dims(imagee, axis=0)
img_data=preprocess_input(imagee)
prediction=model.predict(img_data)
if prediction[0][0]>prediction[0][1]:  #Printing the prediction of model.
    print('Person is safe.')
else:
    print('Person is affected with Pneumonia.')
print(f'Predictions: {prediction}')

输出:

参考:

  • https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia