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


    数据来源

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

    数据划分

    写了个脚本划分

    1. for line in open('ISIC/labels.csv').readlines()[1:]:
    2. split_line = line.split(',')
    3. img_file = split_line[0]
    4. benign_malign = split_line[1]
    5. # 0.8 for train, 0.1 for test, 0.1 for validation
    6. random_num = random.random()
    7. if random_num < 0.8:
    8. location = train
    9. train_examples += 1
    10. elif random_num < 0.9:
    11. location = validation
    12. validation_examples += 1
    13. else:
    14. location = test
    15. test_examples += 1
    16. if int(float(benign_malign)) == 0:
    17. shutil.copy(
    18. 'ISIC/images/' + img_file + '.jpg',
    19. location + 'benign/' + img_file + '.jpg'
    20. )
    21. elif int(float(benign_malign)) == 1:
    22. shutil.copy(
    23. 'ISIC/images/' + img_file + '.jpg',
    24. location + 'malignant/' + img_file + '.jpg'
    25. )
    26. print(f'Number of training examples {train_examples}')
    27. print(f'Number of test examples {test_examples}')
    28. print(f'Number of validation examples {validation_examples}')

    数据生成模块

    1. train_datagen = ImageDataGenerator(
    2. rescale=1.0 / 255,
    3. rotation_range=15,
    4. zoom_range=(0.95, 0.95),
    5. horizontal_flip=True,
    6. vertical_flip=True,
    7. data_format='channels_last',
    8. dtype=tf.float32,
    9. )
    10. train_gen = train_datagen.flow_from_directory(
    11. 'data/train/',
    12. target_size=(img_height, img_width),
    13. batch_size=batch_size,
    14. color_mode='rgb',
    15. class_mode='binary',
    16. shuffle=True,
    17. seed=123,
    18. )

     模型加载和运行

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

    1. # NasNet
    2. model = keras.Sequential([
    3. hub.KerasLayer(r'C:\\Users\\32573\\Desktop\\tools\py\\cancer_classification_project\\saved_model',
    4. trainable=True),
    5. layers.Dense(1, activation='sigmoid'),
    6. ])
    1. model.compile(
    2. optimizer=keras.optimizers.Adam(3e-4),
    3. loss=[keras.losses.BinaryCrossentropy(from_logits=False)],
    4. metrics=['accuracy']
    5. )
    6. model.fit(
    7. train_gen,
    8. epochs=1,
    9. steps_per_epoch=train_examples // batch_size,
    10. validation_data=validation_gen,
    11. validation_steps=validation_examples // batch_size,
    12. )

    运行结果 

     模型其他评估指标

    1. METRICS = [
    2. keras.metrics.BinaryAccuracy(name='accuracy'),
    3. keras.metrics.Precision(name='precision'),
    4. keras.metrics.Recall(name='Recall'),
    5. keras.metrics.AUC(name='AUC'),
    6. ]

     绘制roc

    1. def plot_roc(label, data):
    2. predictions = model.predict(data)
    3. fp, tp, _ = roc_curve(label, predictions)
    4. plt.plot(100*fp, 100*tp)
    5. plt.xlabel('False Positives [%]')
    6. plt.ylabel('True Positives [%]')
    7. plt.show()
    8. test_labels = np.array([])
    9. num_batches = 0
    10. for _, y in test_gen:
    11. test_labels = np.append(test_labels, y)
    12. num_batches = 1
    13. if num_batches == math.ceil(test_examples / batch_size):
    14. break
    15. plot_roc(test_labels, test_gen)

  • 相关阅读:
    vue2+elementUI,vue3+elementPlus解决form中的下拉列表回写显示id,不显示label
    GraphQL 入门
    车间调度|基于遗传算法的柔性车间调度(Matlab代码实现)
    迭代器的封装与反向迭代器
    【pytorch09】数学运算
    前端面试官会问的问题
    R reason ‘拒绝访问‘的解决方案
    day13:MyBatis映射文件深入
    几种反序列化漏洞
    什么是 mapState 助手?
  • 原文地址:https://blog.csdn.net/2401_82787858/article/details/139338035