用深度学习对皮肤病变进行分类

691 阅读3分钟

image.png

皮肤癌是世界上最常见的癌症类型,早期发现对于成功治疗至关重要。帮助早期检测的一个方法是通过使用自动皮肤病变分类系统,它可以根据数字图像准确地将皮肤病变分为良性或恶性。在本教程中,我们将使用深度学习来建立一个皮肤病变分类模型。

数据集

我们将使用HAM10000数据集,它由10,015张皮肤病变的皮肤镜图像组成。每张图像都被分类为七种不同类型的皮肤病变之一:黑色素细胞痣、黑色素瘤、基底细胞癌、光化性角化病、良性角化病、皮纤维瘤和血管病变。

预处理数据

在建立我们的分类模型之前,我们需要对数据进行预处理。我们将把所有的图像调整为标准尺寸,并将像素值归一化为0和1之间,我们还将对目标标签进行一次编码。

import pandas as pdimport numpy as npfrom keras.preprocessing.image import load_img, img_to_arrayfrom keras.utils import to_categorical# Load the datadata = pd.read_csv('HAM10000_metadata.csv')# Preprocess the images and labelsimages = []labels = []for i in range(len(data)):    # Load the image and resize it to 224x224    img = load_img('HAM10000_images/' + data['image_id'][i] + '.jpg', target_size=(224, 224))    img_array = img_to_array(img)    images.append(img_array)    # One-hot encode the label    label = to_categorical(data['dx'][i], num_classes=7)    labels.append(label)    # Convert the data to arraysimages = np.array(images)labels = np.array(labels)

建立模型

对于我们的皮损分类模型,我们将使用一个预先训练好的卷积神经网络(CNN),称为VGG16,作为基础模型。我们将在基础模型的基础上增加一些额外的层来进行微调。

from keras.applications.vgg16 import VGG16from keras.models import Sequentialfrom keras.layers import Dense, Flatten# Load the VGG16 model without the top layerbase_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))# Freeze the base model layersfor layer in base_model.layers:    layer.trainable = False# Add additional layersmodel = Sequential()model.add(base_model)model.add(Flatten())model.add(Dense(256, activation='relu'))model.add(Dense(7, activation='softmax'))# Compile the modelmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

训练模型

我们将使用32个批处理量对模型进行10个epochs的训练。

model.fit(images, labels, epochs=10, batch_size=32, validation_split=0.2)

评估模型

一旦模型训练完成,我们就可以在测试的图像集上评估它的性能。

# Load the test datatest_data = pd.read_csv('test_metadata.csv')test_images = []test_labels = []for i in range(len(test_data)):    # Load the image and resize it to 224x224    img = load_img('test_images/' + test_data['image_id'][i] + '.jpg', target_size=(224, 224))    img_array = img_to_array(img)    test_images.append(img_array)    # One-hot encode the label    label = to_categorical(test_data['dx'][i], num_classes=7)    test_labels.append(label)    # Convert the data to arraystest_images = np.array(test_images)test_labels = np.array(test_labels)# Evaluate the model on the test dataloss, accuracy = model.evaluate(test_images, test_labels)print('Test accuracy:', accuracy)

在本教程中,我们使用深度学习来建立一个使用HAM10000数据集的皮肤病变分类模型。我们使用转移学习和微调来建立一个在测试图像集上取得高准确率的模型。这个模型有可能帮助早期检测皮肤癌并改善病人的治疗效果。

参考文献

  1. Tschandl, P., Rosendahl, C., & Kittler, H. (2018).HAM10000数据集,常见色素性皮肤病的多源皮肤镜图像大集合。科学数据,5,180161。https://doi.org/10.1038/sdata.2018.161
  2. Simonyan, K., & Zisserman, A. (2015).用于大规模图像识别的极深卷积网络。In International Conference on Learning Representations.arxiv.org/abs/1409.15…