基于tensorflow和NasNet的皮肤癌分类项目

数据来源

https://challenge.isic-archive.com/data/#2019

数据划分

写了个脚本划分

python 复制代码
for line in open('ISIC/labels.csv').readlines()[1:]:
    split_line = line.split(',')
    img_file = split_line[0]
    benign_malign = split_line[1]

    # 0.8 for train, 0.1 for test, 0.1 for validation
    random_num = random.random()

    if random_num < 0.8:
        location = train
        train_examples += 1

    elif random_num < 0.9:
        location = validation
        validation_examples += 1

    else:
        location = test
        test_examples += 1

    if int(float(benign_malign)) == 0:
        shutil.copy(
            'ISIC/images/' + img_file + '.jpg',
            location + 'benign/' + img_file + '.jpg'
        )

    elif int(float(benign_malign)) == 1:
        shutil.copy(
            'ISIC/images/' + img_file + '.jpg',
            location + 'malignant/' + img_file + '.jpg'
        )

print(f'Number of training examples {train_examples}')
print(f'Number of test examples {test_examples}')
print(f'Number of validation examples {validation_examples}')

数据生成模块

python 复制代码
train_datagen = ImageDataGenerator(
    rescale=1.0 / 255,
    rotation_range=15,
    zoom_range=(0.95, 0.95),
    horizontal_flip=True,
    vertical_flip=True,
    data_format='channels_last',
    dtype=tf.float32,
)


train_gen = train_datagen.flow_from_directory(
    'data/train/',
    target_size=(img_height, img_width),
    batch_size=batch_size,
    color_mode='rgb',
    class_mode='binary',
    shuffle=True,
    seed=123,
)

模型加载和运行

由于数据量较大,本次使用NasNet, 来源于nasnet | Kaggle

python 复制代码
# NasNet
model = keras.Sequential([
    hub.KerasLayer(r'C:\\Users\\32573\\Desktop\\tools\py\\cancer_classification_project\\saved_model',
                   trainable=True),
    layers.Dense(1, activation='sigmoid'),
])
python 复制代码
model.compile(
    optimizer=keras.optimizers.Adam(3e-4),
    loss=[keras.losses.BinaryCrossentropy(from_logits=False)],
    metrics=['accuracy']
)

model.fit(
    train_gen,
    epochs=1,
    steps_per_epoch=train_examples // batch_size,
    validation_data=validation_gen,
    validation_steps=validation_examples // batch_size,
)

运行结果

模型其他评估指标

python 复制代码
METRICS = [
    keras.metrics.BinaryAccuracy(name='accuracy'),
    keras.metrics.Precision(name='precision'),
    keras.metrics.Recall(name='Recall'),
    keras.metrics.AUC(name='AUC'),
]

绘制roc图

python 复制代码
def plot_roc(label, data):
    predictions = model.predict(data)
    fp, tp, _ = roc_curve(label, predictions)

    plt.plot(100*fp, 100*tp)
    plt.xlabel('False Positives [%]')
    plt.ylabel('True Positives [%]')
    plt.show()


test_labels = np.array([])
num_batches = 0

for _, y in test_gen:
    test_labels = np.append(test_labels, y)
    num_batches = 1
    if num_batches == math.ceil(test_examples / batch_size):
        break

plot_roc(test_labels, test_gen)

相关推荐
谭欣辰2 分钟前
详细讲解 C++ 有向无环图(DAG)及拓扑排序
c++·算法·图论
Lsk_Smion4 分钟前
【类增量学习之2025ICCV】TUNA : 让AI像搭积木一样学习新知识,TUNA的适配器融合之道
人工智能·深度学习·机器学习·论文笔记
承渊政道7 分钟前
【递归、搜索与回溯算法】(掌握记忆化搜索的核心套路)
数据结构·c++·算法·leetcode·macos·动态规划·宽度优先
闻缺陷则喜何志丹9 分钟前
【 线性筛 调和级数】P7281 [COCI 2020/2021 #4] Vepar|普及+
c++·算法·洛谷·线性筛·调和级数
zzzsde11 分钟前
【Linux】线程概念与控制(1)线程基础与分页式存储管理
linux·运维·服务器·开发语言·算法
穿条秋裤到处跑12 分钟前
每日一道leetcode(2026.04.23):等值距离和
算法·leetcode·职场和发展
少许极端16 分钟前
算法奇妙屋(四十九)-贡献法
java·算法·leetcode·贡献法
武帝为此20 分钟前
【特征选择方法】
算法·数学建模
FL162386312921 分钟前
红外热成像建筑墙面缺陷裂缝掉皮空洞漏水检测数据集VOC+YOLO格式463张4类别
人工智能·yolo·机器学习
隔壁大炮21 分钟前
第一章_机器学习概述_01.机器学习_AI_ML_DL介绍
人工智能·深度学习·机器学习