x射线图像处理_5行代码中的medicalai教程x射线图像分类

x射线图像处理

What is MedicalAI Framework?

什么是MedicalAI框架?

Medical AI is a High-level Tensorflow 2.0 wrapper library/framework to speed up experiments from training to deployment. How Keras was to Tensorflow, MedicalAI is to Tensorflow 2.0.

Medical AI是高级Tensorflow 2.0包装器库/框架,可加快从培训到部署的实验。 Keras对Tensorflow的影响如何,MedicalAI对Tensorflow 2.0的影响。

Medical AI aims to reduce the entry-barrier for starters including doctors/researchers/medical professionals from multiple backgrounds to focus more on experimenting using AI/ML for medical applications. Getting into AI/ML development can be intimidating as the learning curve can be very steep to many individuals. This framework follows a template structure, where the users will set a few parameters and can use the templates to get started with AI/ML experimentation. Of course, veterans and advanced users can outright use their domain and framework knowledge to some amazing advanced stuff. Currently, this framework supports all Tensorflow 2.0 methods. We do plan to bring in PyTorch support in the later releases.

医学AI旨在减少来自多个背景的初学者(包括医生/研究人员/医学专业人员)的进入壁垒,而将更多精力放在使用AI / ML进行医学应用的实验上。 由于许多人的学习曲线可能非常陡峭,因此进入AI / ML开发可能会令人生畏。 该框架遵循模板结构,用户可以在其中设置一些参数,并可以使用模板开始AI / ML实验。 当然,退伍军人和高级用户可以完全使用他们的领域和框架知识来学习一些惊人的高级知识。 当前,该框架支持所有Tensorflow 2.0方法。 我们确实计划在以后的版本中引入PyTorch支持。

The most useful feature is the model evaluation report generation. The framework can be used to generate a comprehensive evaluation PDF report with model sensitivity, specificity, accuracy, confidence intervals, ROC Curve Plot, Precision-Recall Curve Plot, and Confusion Matrix Plot for each class. This makes experiment management and model management easier for publishing results, moving models to productions, and keeping track of the progress of your experimentation.

最有用的功能是模型评估报告的生成。 该框架可用于为每个类别生成具有模型敏感性,特异性,准确性,置信区间,ROC曲线图,精确召回曲线图和混淆矩阵图的综合评估PDF报告。 这使得实验管理和模型管理更容易发布结果,将模型移至生产环境并跟踪实验进度。

让我们开始使用图书馆 (Let’s get started with using the Library)

0.要求 (0. Requirements)

Python 3.5-3.7 (Not Tested on 3.8 Since Tensorflow does not support 3.8 yet)
Dependencies: Numpy, Tensorflow, Seaborn, Matplotlib, PandasNOTE: Dependency libraries are automatically installed. No need for user to install them manually.

1.通过PyPI安装库 (1. Installation of Library via PyPI)

pip install medicalai
Image for post

2.开始编码..! (2. Start Coding..!)

A. Import the library

A.导入库

import medicalai as ai

B. Prepare and Process Dataset

B.准备和处理数据集

Image for post
Fig 1: Folder Organization of Dataset
图1:数据集的文件夹组织

To easily process your dataset using builtin data processor functions, organize your data as shown in Fig 1. The main_folder should contain test and train folder. Train folder samples will be used for training the AI model and Test folder will be used for validation/testing the trained model.

为了使用内置的数据处理器功能轻松处理数据集,请如图1所示组织数据 。main_folder 应该包含 test train 文件夹。 训练文件夹样本将用于训练AI模型,而测试文件夹将用于验证/测试训练后的模型。

# Set the below parameters for dataset processor
datasetFolderPath = <main_folder_containing_test_and_train_folder>IMG_HEIGHT=224, IMG_WIDTH =224# Call the dataset processor to preprocess your dataset
trainSet,testSet,labelNames=ai.datasetFromFolder(datasetFolderPath, targetDim = (IMG_HEIGHT,IMG_WIDTH), normalize=True).load_dataset()

IMG_HEIGHT, IMG_WIDTH are the parameters that define the input size for the AI model we will be training. You can play around with this number to tune your AI model.

IMG_HEIGHT,IMG_WIDTH是定义我们将要训练的AI模型的输入大小的参数。 您可以尝试使用此数字来调整您的AI模型。

The trainSet and testSet variable consist of data and associated class labels. These can be accessed using trainSet.data and trainSet.labels. labelNames contains the target class names of your dataset.

trainSettestSet变量由数据和关联的类标签组成 这些可以使用 trainSet.datatrainSet.labels。 labelNames包含数据集的目标类名称。

To get started, you can download a sample dataset of Pneumonia, XRAY, and COVID Dataset and get familiar with the framework.

首先,您可以下载肺炎,XRAY和COVID数据集的样本数据集,并熟悉该框架。

# Download a sample dataset and Set the below parameters for dataset processor
datasetDWLD = ai.getFile('https://github.com/aibharata/covid19-dataset/archive/v1.0.zip')datasetFolderPath = datasetDWLD+'/covid19-dataset-1.0/chest-xray-pnumonia-covid19/'IMG_HEIGHT=224, IMG_WIDTH =224# Call the dataset processor to preprocess your datasettrainSet,testSet,labelNames = ai.datasetFromFolder(datasetFolderPath, targetDim = (IMG_HEIGHT,IMG_WIDTH), normalize=True).load_dataset()

C. Set Hyperparameters of Training

C.设置训练的超参数

epochs = 10 #<-- No of passes to make on dataset
RETRAIN_MODEL = True #<-- Whether to improve the model if model exists
MODEL_SAVE_NAME = 'medicalai_test_model_1' #<-- Model Output Name#Select Inbuilt Networks to create and train the model
AI_NAME = 'tinyMedNet'
OUTPUT_CLASSES = 3 #<-- No of target classes in your dataset

AI_NAME is the parameter that tells the framework which inbuilt network to use. Inbuilt Networks Available:

AI_NAME是告诉框架使用哪个内置网络的参数。 可用的内置网络:

DenseNet121, InceptionResNetV2, InceptionV3, MobileNet, MobileNetV2, VGG16, Xception, megaNet (COVIDNet), resNet110, resNet20, resNet32, resNet56, tinyMedNet, tinyMedNet_v2, tinyMedNet_v3

DenseNet121,InceptionResNetV2,InceptionV3,MobileNet,MobileNetV2,VGG16,Xception,megaNet(COVIDNet),resNet110,resNet20,resNet32,resNet56,tinyMedNet,tinyMedNet_v2,tinyMedNet_v3

Additionally you can create your own custom network using NetworkInit class.

另外,您可以使用NetworkInit类创建自己的自定义网络。

D. Initialize Train Engine and Train

D.初始化火车引擎和火车

trainer = ai.TRAIN_ENGINE()
trainer.train_and_save_model(AI_NAME=AI_NAME,
MODEL_SAVE_NAME = MODEL_SAVE_NAME,
trainSet=trainSet,testSet=testSet,
OUTPUT_CLASSES=OUTPUT_CLASSES,
RETRAIN_MODEL= RETRAIN_MODEL,
EPOCHS= epochs,
showModel = True # Set this True if you want to see model summary)

The above 2 codes initialize the training engine and begin training until the epochs are completed. You can also set the learning rate and batch size for the train_and_save_model function.

上面的2个代码初始化训练引擎,并开始训练,直到纪元完成为止。 您还可以为train_and_save_model 函数设置学习率和批量大小

This automatically builds a new model for given networks/AI or reload an existing AI model. This function can be used to retrain existing models or create new models.

这将自动为给定的网络/ AI构建新模型或重新加载现有的AI模型。 此功能可用于重新训练现有模型或创建新模型。

E. Plot Training Accuracy and Loss vs Epochs

E.情节训练的准确性和损失与时期

# Plot Training Metrics
trainer.plot_train_acc_loss()

This plot_train_acc_loss can be used to view how loss and accuracy varied over each epoch. Generates an interactive graph for inspection. The sample output is as shown below.

plot_train_acc_loss可用于查看每个时期的损耗和准确性如何变化。 生成一个交互式图形进行检查。 示例输出如下所示。

Image for post

生成全面的模型评估报告。 (Generate a comprehensive Model Evaluation report.)

仅使用1个代码,即可为每个类别生成具有模型敏感性,特异性,准确性,置信区间,ROC曲线图,精确调用曲线图和混淆矩阵图的PDF。 当使用“测试或验证数据集”评估模型时,可以使用此功能。 (With just 1 of code, Generate a PDF with model sensitivity, specificity, accuracy, confidence intervals, ROC Curve Plot, Precision-Recall Curve Plot, and Confusion Matrix Plot for each class. This function can be used when evaluating a model with a Test or Validation Data Set.)

A sample report is shown below.

样本报告如下所示。

Image for post

在样本上解释模型(可解释的AI) (Explain the Model on a sample (Explainable AI))

trainer.explain(testSet.data[0:1], layer_to_explain=’CNN3')

Just set the sample to be explained in the first parameter and the required layer to explain the model. The layer names can be obtained by trainer.getLayerNames()

只需在第一个参数中设置要解释的样本,然后在解释模型所需的层中进行设置即可。 图层名称可以通过trainer.getLayerNames()获得

This Explains a model layer with respect to Input and Output using Grad-cam. Basically, see what the AI is seeing to arrive at a certain prediction. More methods to be updated in the next versions.

这使用Grad-cam解释了有关输入和输出的模型层。 基本上,请看AI在达到某个预测时会看到什么。 更多方法将在下一版本中更新。

用于预测和生产的推理引擎 (INFERENCE ENGINE FOR PREDICTION AND PRODUCTION)

仅需两行代码即可对经过训练的模型进行推断/预测 (Perform inference/prediction on the trained model with just 2 lines of code)

#Initialize Inference Engine
infEngine = ai.INFERENCE_ENGINE(MODEL_OUTPUT_NAME_USED_IN_TRAINING)#Peform Prediction on Input
infEngine.predict_with_labels(<INPUT_DATA_OR_FILE_PATH>)

If MedicalAI framework is used during training, then it will generate all the necessary files to be used during prediction or production. MedicalAI automatically initializes data preprocessors to apply the same preprocessing parameters used in training such as Normalization or Scaling.

如果在培训期间使用了MedicalAI框架,则它将生成在预测或生产期间使用的所有必要文件。 MedicalAI自动初始化数据预处理器,以应用训练中使用的相同预处理参数,例如归一化或缩放。

You can also download image from the web and perform predictions easily.

您还可以从网上下载图像并轻松进行预测。

infEngine = ai.INFERENCE_ENGINE(MODEL_OUTPUT_NAME_USED_IN_TRAINING)
# Download image from web and perform prediction
testImg = ai.getFile(‘<DATA_URL>')#the below 2 functions achieve the same functionality of predict_with_labels
preds = infEngine.predict(testImg)
infEngine.decode_predictions(preds)

Google Colab Notebook Link:

Google Colab笔记本链接:

Image for post
Youtube Link to Tutorial: https://www.youtube.com/watch?v=V4nCX-kLACg
Youtube链接至教程: https//www.youtube.com/watch?v = V4nCX- kLACg

普通纸笔记 (A note In Plain English)

Did you know that we have four publications and a YouTube channel? You can find all of this from our homepage at plainenglish.io — show some love by giving our publications a follow and subscribing to our YouTube channel!

您知道我们有四个出版物和一个YouTube频道吗? 您可以在我们的主页plainenglish.io上找到所有这些内容 -通过对我们的出版物进行关注并订阅我们的YouTube频道来表达爱意

翻译自: https://medium.com/ai-in-plain-english/medicalai-tutorial-x-ray-image-classification-in-5-lines-of-code-58c23859fef2

x射线图像处理

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值