基于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)

相关推荐
B站计算机毕业设计之家8 分钟前
深度学习:YOLOv8人体行为动作识别检测系统 行为识别检测识系统 act-dataset数据集 pyqt5 机器学习✅
人工智能·python·深度学习·qt·yolo·机器学习·计算机视觉
on_pluto_9 分钟前
GAN生成对抗网络学习-例子:生成逼真手写数字图
人工智能·深度学习·神经网络·学习·算法·机器学习·生成对抗网络
Q741_14722 分钟前
C++ 分治 快速排序优化 三指针快排 力扣 面试题 17.14. 最小K个数 题解 每日一题
c++·算法·leetcode·快排·topk问题
sun༒22 分钟前
递归经典例题
java·算法
渡我白衣25 分钟前
AI 应用层革命(一)——软件的终结与智能体的崛起
人工智能·opencv·机器学习·语言模型·数据挖掘·人机交互·集成学习
Lear28 分钟前
【数组】代码随想录 44.开发商购买土地
算法
CoovallyAIHub34 分钟前
OmniNWM:突破自动驾驶世界模型三大瓶颈,全景多模态仿真新标杆(附代码地址)
深度学习·算法·计算机视觉
刘孬孬沉迷学习1 小时前
AI+通信+多模态应用分类与核心内容总结
人工智能·机器学习·分类·数据挖掘·信息与通信
TU^1 小时前
C语言习题~day27
c语言·数据结构·算法
Allenlzcoder1 小时前
掌握机器学习算法及其关键超参数
人工智能·机器学习·超参数