kaggle之皮肤癌数据的深度学习测试

kaggle之皮肤癌数据的深度学习测试

近期一直在肝深度学习

很久之前,曾经上手搞过一段时间的深度学习,似乎是做轮胎花纹的识别,当初用的是TensorFlow,CPU版本的,但已经很长时间都没弄过了

现在因为各种原因,不得不重新开始。因为设备限制,深度学习的GPU环境一直没搭好,为了快速开始,不得不继续使用CPU版本

我用的是kaggle提供的皮肤癌的数据集,地址在这里,下载的话,需要注册kaggle,压缩包有5个多G,但是解压后只有2G

我的编译环境是本地Python,版本是3.7,编译器是pycharm

下面就正式开始深度学习测试

一、数据简介

介绍的原文:

About Dataset

Overview

Another more interesting than digit classification dataset to use to get biology and medicine students more excited about machine learning and image processing.

Original Data Source

  • Original Challenge: https://challenge2018.isic-archive.com
  • https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/DBW86T
    [1] Noel Codella, Veronica Rotemberg, Philipp Tschandl, M. Emre Celebi, Stephen Dusza, David Gutman, Brian Helba, Aadi Kalloo, Konstantinos Liopyris, Michael Marchetti, Harald Kittler, Allan Halpern: “Skin Lesion Analysis Toward Melanoma Detection 2018: A Challenge Hosted by the International Skin Imaging Collaboration (ISIC)”, 2018; https://arxiv.org/abs/1902.03368

[2] Tschandl, P., Rosendahl, C. & Kittler, H. The HAM10000 dataset, a large collection of multi-source dermatoscopic images of common pigmented skin lesions. Sci. Data 5, 180161 doi:10.1038/sdata.2018.161 (2018).

From Authors

Training of neural networks for automated diagnosis of pigmented skin lesions is hampered by the small size and lack of diversity of available dataset of dermatoscopic images. We tackle this problem by releasing the HAM10000 (“Human Against Machine with 10000 training images”) dataset. We collected dermatoscopic images from different populations, acquired and stored by different modalities. The final dataset consists of 10015 dermatoscopic images which can serve as a training set for academic machine learning purposes. Cases include a representative collection of all important diagnostic categories in the realm of pigmented lesions: Actinic keratoses and intraepithelial carcinoma / Bowen’s disease (akiec), basal cell carcinoma (bcc), benign keratosis-like lesions (solar lentigines / seborrheic keratoses and lichen-planus like keratoses, bkl), dermatofibroma (df), melanoma (mel), melanocytic nevi (nv) and vascular lesions (angiomas, angiokeratomas, pyogenic granulomas and hemorrhage, vasc).

More than 50% of lesions are confirmed through histopathology (histo), the ground truth for the rest of the cases is either follow-up examination (follow_up), expert consensus (consensus), or confirmation by in-vivo confocal microscopy (confocal). The dataset includes lesions with multiple images, which can be tracked by the lesion_id-column within the HAM10000_metadata file.

The test set is not public, but the evaluation server remains running (see the challenge website). Any publications written using the HAM10000 data should be evaluated on the official test set hosted there, so that methods can be fairly compared.

估计大伙儿和我一样,不太乐意看英文,那我大致翻译一下

首先,他说这是另一个比数字分类更有趣的数据集,用来让生物和医学学生对机器学习和图像处理更感兴趣,深度学习一般最先上手的就是手写数字分类了吧

然后,作者介绍说,神经网络用于色素皮肤病变自动诊断的训练由于皮肤镜图像的小尺寸和缺乏多样性数据集而受到阻碍。我们通过发布HAM 10000(“10000训练图像的人对机器”)数据集来解决这个问题。我们收集了不同人群的皮肤镜图像,并以不同的方式获取和存储。最后的数据集由10015张皮肤科图像组成,这些图像可以作为学习机器学习的训练集。病例包括色素沉着病变领域的所有重要诊断类别:光化角化病和上皮内癌/鲍温病(Akiec)、基底细胞癌(BCC)、良性角化样病变(太阳扁桃体/脂溢性角化角化症和扁平苔藓样角化物、BKL)、皮肤纤维瘤(DF)、黑色素瘤(MEL)、黑色素细胞痣(NV)和血管病变(血管瘤、血管瘤、化脓性肉芽肿和出血,VASc)。
超过50%的病变是通过组织病理学(组织病理学)确认的,其余病例的基本真相要么是随访检查(随访),要么是专家共识(共识),或者是活体共聚焦显微镜(共聚焦)确认。数据集包含具有多个图像的病变,这些图像可以由HAM 10000_元数据文件中的PAILE_id列跟踪。
测试集是不公开的,但是评估服务器保持运行(参见挑战赛网站)。任何使用HAM10000数据编写的出版物都应该在那里托管的官方测试集上进行评估,以便可以公平地比较各种方法。

全部的数据包括图像数据和文本标签数据,如下:

image-20240426110259167

图像数据在俩文件夹中,csv文件是针对不同的数据的特征数据,共有10015张图片

数据简介就这些了,要想看更详细的,可以直接去kaggle上看

二、深度学习建模

建模过程我参考了一个点赞数最多的commit,作者用的是CNN(卷积神经网络),准确率能达到77%

总共有这么几个步骤:

1、导入所需的库

import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
from glob import glob
import seaborn as sns

sns.set()
from PIL import Image

np.random.seed(123)
from sklearn.preprocessing import label_binarize
from sklearn.metrics import confusion_matrix
import itertools

import keras
from keras.utils.np_utils import to_categorical  # used for converting labels to one-hot-encoding
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras import backend as K
import itertools
from keras.layers.normalization import BatchNormalization
from keras.utils.np_utils import to_categorical  # convert to one-hot-encoding

from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from sklearn.model_selection import train_test_split

start_time = time.time()

程序跑下来,有些库没有用到,我在作者的基础上,添加了一个计时器,用来计算函数的运行时间

2、制作图像和标签的字典

这一步主要是用来读取数据的

看下面的代码

base_skin_dir = os.path.join('.', 'input')
# base_skin_dir = '.\\input'

# Merging images from both folders HAM10000_images_part1.zip and HAM10000_images_part2.zip into one dictionary

imageid_path_dict = {os.path.splitext(os.path.basename(x))[0]: x
                     for x in glob(os.path.join(base_skin_dir, '*', '*.jpg'))}

# This dictionary is useful for displaying more human-friendly labels later on

lesion_type_dict = {
    'nv': 'Melanocytic nevi',
    'mel': 'Melanoma',
    'bkl': 'Benign keratosis-like lesions ',
    'bcc': 'Basal cell carcinoma',
    'akiec': 'Actinic keratoses',
    'vasc': 'Vascular lesions',
    'df': 'Dermatofibroma'
}

注意看一下我的目录结构

image-20240426111057768

test_ham10000.py就是我的py文件,和input目录同一层级

3、读取特征数据

# 读取数据
skin_df = pd.read_csv(os.path.join(base_skin_dir, 'HAM10000_metadata.csv'))

# Creating New Columns for better readability

skin_df['path'] = skin_df['image_id'].map(imageid_path_dict.get)
skin_df['cell_type'] = skin_df['dx'].map(lesion_type_dict.get)
skin_df['cell_type_idx'] = pd.Categorical(skin_df['cell_type']).codes

这个数据应该是病例数据,包括病例id、图像ID、病例性别、年龄等,然后根据原始数据,创建了其他几个特征

  • 根据image_id设置病例的图片路径
  • 根据cell_type(细胞类型?)设置dx
  • 根据细胞类型设置索引

示例如下:

lesion_idimage_iddxdx_typeagesexlocalizationpathcell_typecell_type_idx
0HAM_0000118ISIC_0027419bklhisto80.0malescalp…/input/ham10000_images_part_1/ISIC_0027419.jpgBenign keratosis-like lesions2
1HAM_0000118ISIC_0025030bklhisto80.0malescalp…/input/ham10000_images_part_1/ISIC_0025030.jpgBenign keratosis-like lesions2
2HAM_0002730ISIC_0026769bklhisto80.0malescalp…/input/ham10000_images_part_1/ISIC_0026769.jpgBenign keratosis-like lesions2
3HAM_0002730ISIC_0025661bklhisto80.0malescalp…/input/ham10000_images_part_1/ISIC_0025661.jpgBenign keratosis-like lesions2
4HAM_0001466ISIC_0031633bklhisto75.0maleear…/input/ham10000_images_part_2/ISIC_0031633.jpgBenign keratosis-like lesions2

4、数据清洗

发现skin_df中的年龄存在空值,需要填充一下,用均值填充

skin_df['age'].fillna((skin_df['age'].mean()), inplace=True)

5、数据特征

主要是观察了一下数据的分布特征

# 数据特征
fig, ax1 = plt.subplots(1, 1, figsize=(10, 5))
skin_df['cell_type'].value_counts().plot(kind='bar', ax=ax1)
plt.xticks(rotation=0)
plt.show()
skin_df['dx_type'].value_counts().plot(kind='bar')
plt.show()

img

从上面的图中可以看出,与其他类型的细胞相比,黑色素细胞痣的细胞类型有非常多的实例。

img

dx_type,以查看其4个类别的分布,如下所示:

  1. 组织病理学(Histo):切除病灶的组织病理学诊断由专门的皮肤病理学家进行。
  2. 共焦(confocal):反射共焦显微术是一种在体成像技术,其分辨率在近细胞水平,有些面部良性,在人工直方图变化前后,所有训练集图像在实验室颜色空间中都有灰色世界的假设。
  3. 随访(follow_up):如果数字皮肤镜监测的痣在3次随访或1.5年的随访中没有显示出任何变化,生物学家接受这一检查作为生物善意的证据。只有痣,但没有其他良性诊断被标记为这种基础真相,因为皮肤科医生通常不监测皮肤病纤维瘤,脂溢性角化,或血管病变。
  4. 共识(consensus):对于典型的无组织病理学或随访的良性病例,生物学家提供作者PT和HK的专家共识评级。只有当两位作者独立地给出相同的明确的良性诊断时,他们才会使用一致的标签。有这种真相的病变通常是出于教育原因而拍摄的,不需要进一步的随访或活检来确认。

非医学专业,看起来实在费劲

还有很多其他的特征,不一一展开了,展开了也看的云里雾里

6、加载并调整图像大小

skin_df['image'] = skin_df['path'].map(lambda x: np.asarray(Image.open(x).resize((100, 75))))

# Checking the image size distribution
# skin_df['image'].map(lambda x: x.shape).value_counts()
features = skin_df.drop(columns=['cell_type_idx'], axis=1)
target = skin_df['cell_type_idx']

在此步骤中,图像将从image文件夹中的图像路径加载到名为image的列中。我们还调整图像的大小,图像的原始尺寸为450x600x3,TensorFlow无法处理,所以这就是把它的大小调整为100×75。这里比较耗时,得耐心等待

后面两行就是准备特征数据和目标数据

7、划分训练集和测试集

x_train_o, x_test_o, y_train_o, y_test_o = train_test_split(features, target, test_size=0.20, random_state=1234)

常规操作,机器学习也这么搞

8、数据标准化

x_train = np.asarray(x_train_o['image'].tolist())
x_test = np.asarray(x_test_o['image'].tolist())

x_train_mean = np.mean(x_train)
x_train_std = np.std(x_train)

x_test_mean = np.mean(x_test)
x_test_std = np.std(x_test)

x_train = (x_train - x_train_mean) / x_train_std
x_test = (x_test - x_test_mean) / x_test_std

也是常规操作,计算数据与均值的差,然后除以标准差

9、标签独热编码

这一步我感觉必要性不是特别大

y_train = to_categorical(y_train_o, num_classes=7)
y_test = to_categorical(y_test_o, num_classes=7)

其实就是把数字0-6变成了一个向量,如0->[1,0,0,0,0,0,0],1->[0,1,0,0,0,0,0]

10、分割训练集和验证集

x_train, x_validate, y_train, y_validate = train_test_split(x_train, y_train, test_size=0.1, random_state=2)

常规操作

11、建模

进入主题,,,

前面就提到了,使用的是CNN

# my CNN architechture is In -> [[Conv2D->relu]*2 -> MaxPool2D -> Dropout]*2 -> Flatten -> Dense -> Dropout -> Out
input_shape = (75, 100, 3)
num_classes = 7

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', padding='Same', input_shape=input_shape))
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', padding='Same', ))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation='relu', padding='Same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='Same'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.40))

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.summary()

输入size就是图像的size

分类结果是7个

使用的是2D卷积,卷积核大小是3*3

当然,也设置了多种卷积形式

12、设置优化器和退火器

在深度学习中,设置优化器和退火器的作用是优化模型的训练过程。优化器可以帮助调整模型参数以最小化损失函数,而退火器可以逐渐减小学习率以优化模型训练的速度和效果。通过合理选择和调整优化器和退火器,可以提高模型的性能和收敛速度。

optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
# Compile the model
model.compile(optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"])
# Set a learning rate annealer
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
                                            patience=3,
                                            verbose=1,
                                            factor=0.5,
                                            min_lr=0.00001)

数据增强。数据增强的目的是通过对原始数据进行一系列变换或操作,生成更多、更多样化的训练样本,以增加模型在数据多样性和复杂性方面的鲁棒性和泛化能力。数据增强可以帮助模型更好地理解数据的特征和模式,并减少过拟合的风险,提高模型在未知数据上的表现。

datagen = ImageDataGenerator(
    featurewise_center=False,  # set input mean to 0 over the dataset
    samplewise_center=False,  # set each sample mean to 0
    featurewise_std_normalization=False,  # divide inputs by std of the dataset
    samplewise_std_normalization=False,  # divide each input by its std
    zca_whitening=False,  # apply ZCA whitening
    rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)
    zoom_range=0.1,  # Randomly zoom image
    width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
    height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
    horizontal_flip=False,  # randomly flip images
    vertical_flip=False)  # randomly flip images
datagen.fit(x_train)

以上代码使用了ImageDataGenerator来进行数据增强操作,包括旋转、缩放、水平和垂直平移、水平和垂直翻转等多种操作,以生成更多样化的训练样本,提高模型的泛化能力。具体来说,这段代码设置了不进行数据标准化和ZCA白化,但进行了图片旋转、随机缩放、水平和垂直平移以及随机翻转等操作。

13、拟合模型

其实就是用增强后的数据来训练模型

epochs = 50 
batch_size = 10
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
                              epochs = epochs, validation_data = (x_validate,y_validate),
                              verbose = 1, steps_per_epoch=x_train.shape[0] // batch_size
                              , callbacks=[learning_rate_reduction])

在每个epoch内,从生成的增强数据流(datagen.flow)中获取批量大小为10的数据进行训练,训练50个epoch。

同时,在训练过程中使用验证集(x_validate, y_validate)对模型进行验证,以便监控模型性能。steps_per_epoch参数指定每个epoch包含的训练步数,这里是整个训练集的样本数除以批量大小。callbacks参数中传入了learning_rate_reduction回调函数,用于动态调整学习率,以提高模型的性能和收敛速度。

模型跑的很忙,而且直接跑满了我的CPU,看来没有GPU确实不太行,程序运行过程中,我看了一下我的CPU运行情况,几乎一直处于拉满状态

image-20240426115554712

14、模型评估

经过漫长的计算,耗时大概俩小时,终于计算出结果了,赶紧计算一下模型的准确率

loss, accuracy = model.evaluate(x_test, y_test, verbose=1)
loss_v, accuracy_v = model.evaluate(x_validate, y_validate, verbose=1)
print("Validation: accuracy = %f  ;  loss_v = %f" % (accuracy_v, loss_v))
print("Test: accuracy = %f  ;  loss = %f" % (accuracy, loss))
model.save("model.h5")

计算结果:

2003/2003 [==============================] - 1s 694us/step
802/802 [==============================] - 0s 527us/step
Validation: accuracy = 0.785536  ;  loss_v = 0.586728
Test: accuracy = 0.764853  ;  loss = 0.616134

当然也可以看一下模型的学习曲线和损失函数以及混淆矩阵,这里我直接用作者提供的了

img

img

三、结语

模型对基底细胞癌的错误预测最多,编码为3,其次是血管病变编码为5。黑素细胞痣编码为0,光化性角化病编码为4,误分率最低。
还可以进一步调整我们的模型,使之更容易达到80%以上的精度。不过77.0344%的预测结果与人眼相比,该模型仍然是有效的。

四、完整代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:HP
# datetime:2024/4/26 9:40
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
from glob import glob
import seaborn as sns

sns.set()
from PIL import Image

np.random.seed(123)
from sklearn.preprocessing import label_binarize
from sklearn.metrics import confusion_matrix
import itertools

import keras
from keras.utils.np_utils import to_categorical  # used for converting labels to one-hot-encoding
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras import backend as K
import itertools
from keras.layers.normalization import BatchNormalization
from keras.utils.np_utils import to_categorical  # convert to one-hot-encoding

from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from sklearn.model_selection import train_test_split

start_time = time.time()


def plot_model_history(model_history):
    fig, axs = plt.subplots(1, 2, figsize=(15, 5))
    # summarize history for accuracy
    axs[0].plot(range(1, len(model_history.history['acc']) + 1), model_history.history['acc'])
    axs[0].plot(range(1, len(model_history.history['val_acc']) + 1), model_history.history['val_acc'])
    axs[0].set_title('Model Accuracy')
    axs[0].set_ylabel('Accuracy')
    axs[0].set_xlabel('Epoch')
    axs[0].set_xticks(np.arange(1, len(model_history.history['acc']) + 1), len(model_history.history['acc']) / 10)
    axs[0].legend(['train', 'val'], loc='best')
    # summarize history for loss
    axs[1].plot(range(1, len(model_history.history['loss']) + 1), model_history.history['loss'])
    axs[1].plot(range(1, len(model_history.history['val_loss']) + 1), model_history.history['val_loss'])
    axs[1].set_title('Model Loss')
    axs[1].set_ylabel('Loss')
    axs[1].set_xlabel('Epoch')
    axs[1].set_xticks(np.arange(1, len(model_history.history['loss']) + 1), len(model_history.history['loss']) / 10)
    axs[1].legend(['train', 'val'], loc='best')
    plt.show()


base_skin_dir = os.path.join('.', 'input')
# base_skin_dir = '.\\input'

# Merging images from both folders HAM10000_images_part1.zip and HAM10000_images_part2.zip into one dictionary

imageid_path_dict = {os.path.splitext(os.path.basename(x))[0]: x
                     for x in glob(os.path.join(base_skin_dir, '*', '*.jpg'))}

# This dictionary is useful for displaying more human-friendly labels later on

lesion_type_dict = {
    'nv': 'Melanocytic nevi',
    'mel': 'Melanoma',
    'bkl': 'Benign keratosis-like lesions ',
    'bcc': 'Basal cell carcinoma',
    'akiec': 'Actinic keratoses',
    'vasc': 'Vascular lesions',
    'df': 'Dermatofibroma'
}

# 读取数据
skin_df = pd.read_csv(os.path.join(base_skin_dir, 'HAM10000_metadata.csv'))

# Creating New Columns for better readability

skin_df['path'] = skin_df['image_id'].map(imageid_path_dict.get)
skin_df['cell_type'] = skin_df['dx'].map(lesion_type_dict.get)
skin_df['cell_type_idx'] = pd.Categorical(skin_df['cell_type']).codes

# 数据清洗
skin_df['age'].fillna((skin_df['age'].mean()), inplace=True)

# 数据特征
fig, ax1 = plt.subplots(1, 1, figsize=(10, 5))
skin_df['cell_type'].value_counts().plot(kind='bar', ax=ax1)
plt.xticks(rotation=0)
plt.show()
skin_df['dx_type'].value_counts().plot(kind='bar')
plt.show()

# 加载并调整图像大小
skin_df['image'] = skin_df['path'].map(lambda x: np.asarray(Image.open(x).resize((100, 75))))

# Checking the image size distribution
# skin_df['image'].map(lambda x: x.shape).value_counts()
features = skin_df.drop(columns=['cell_type_idx'], axis=1)
target = skin_df['cell_type_idx']

# 划分训练集和测试集
x_train_o, x_test_o, y_train_o, y_test_o = train_test_split(features, target, test_size=0.20, random_state=1234)

# 数据标准化
x_train = np.asarray(x_train_o['image'].tolist())
x_test = np.asarray(x_test_o['image'].tolist())

x_train_mean = np.mean(x_train)
x_train_std = np.std(x_train)

x_test_mean = np.mean(x_test)
x_test_std = np.std(x_test)

x_train = (x_train - x_train_mean) / x_train_std
x_test = (x_test - x_test_mean) / x_test_std

# 标签编码
# Perform one-hot encoding on the labels
y_train = to_categorical(y_train_o, num_classes=7)
y_test = to_categorical(y_test_o, num_classes=7)

# 分割训练和验证
x_train, x_validate, y_train, y_validate = train_test_split(x_train, y_train, test_size=0.1, random_state=2)

# !!建模
# Set the CNN model
# my CNN architechture is In -> [[Conv2D->relu]*2 -> MaxPool2D -> Dropout]*2 -> Flatten -> Dense -> Dropout -> Out
input_shape = (75, 100, 3)
num_classes = 7

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', padding='Same', input_shape=input_shape))
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', padding='Same', ))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation='relu', padding='Same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='Same'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.40))

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.summary()

# 设置优化器和退火器
# Define the optimizer
optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
# Compile the model
model.compile(optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"])
# Set a learning rate annealer
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
                                            patience=3,
                                            verbose=1,
                                            factor=0.5,
                                            min_lr=0.00001)

# 数据增强
datagen = ImageDataGenerator(
    featurewise_center=False,  # set input mean to 0 over the dataset
    samplewise_center=False,  # set each sample mean to 0
    featurewise_std_normalization=False,  # divide inputs by std of the dataset
    samplewise_std_normalization=False,  # divide each input by its std
    zca_whitening=False,  # apply ZCA whitening
    rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)
    zoom_range=0.1,  # Randomly zoom image
    width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
    height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
    horizontal_flip=False,  # randomly flip images
    vertical_flip=False)  # randomly flip images

datagen.fit(x_train)

# 模型拟合训练
# Fit the model
epochs = 50
batch_size = 10
history = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
                              epochs=epochs, validation_data=(x_validate, y_validate),
                              verbose=1, steps_per_epoch=x_train.shape[0] // batch_size
                              , callbacks=[learning_rate_reduction])

# 模型评估
loss, accuracy = model.evaluate(x_test, y_test, verbose=1)
loss_v, accuracy_v = model.evaluate(x_validate, y_validate, verbose=1)
print("Validation: accuracy = %f  ;  loss_v = %f" % (accuracy_v, loss_v))
print("Test: accuracy = %f  ;  loss = %f" % (accuracy, loss))
model.save("model.h5")
plot_model_history(history)

end_time = time.time()
run_time = end_time - start_time
print(f"程序运行时间为: {run_time} 秒")

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/584916.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【论文阅读】ELAN-Efficient Long-Range Attention Network for Image Super-resolution

ELAN-Efficient Long-Range Attention Network for Image Super-resolution 论文地址简介1 引言2相关工作2.1 基于 CNN 的 SR 方法2.2 基于 Transformer 的 SR 方法 3 方法论3.1 ELAN 的整体流程3.2 Efficient Long-range Attention Block (ELAB) 4实验4.1实验设置4.2 与轻量级…

Gray Zone Warfare灰区战争无法启动、登不上、加载失败解决办法

《灰区战争》是由捷克独立工作室开发、虚幻5引擎打造的开放世界FPS游戏,现已上线Steam页面,游戏将于2024年推出,暂不支持中文。当行动者 在对手做出反应之前迅速取得小规模胜利时,就出现了既成事实。例如:没收有争议的…

如何利用AI智能名片B2B2C商城系统将关键客户转化为数据驱动的会员

在数字化浪潮席卷全球的今天,数据无疑是企业发展的核心驱动力。然而,如何将这些无形的数字转化为企业实际增长的动力,却是许多企业面临的难题。特别是对于关键客户的管理,如何深入挖掘他们的价值,并转化为企业的忠实会…

【论文笔记】Training language models to follow instructions with human feedback A部分

Training language models to follow instructions with human feedback A 部分 回顾一下第一代 GPT-1 : 设计思路是 “海量无标记文本进行无监督预训练少量有标签文本有监督微调” 范式;模型架构是基于 Transformer 的叠加解码器(掩码自注意…

力扣:61. 旋转链表(Java,双指针)

目录 题目描述:输入:输出:代码描述: 题目描述: 给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。 输入: head [1,2,3,4,5], k 2 输出: [4,5,1,…

Java项目:基于SSM框架实现的视康眼镜网店销售管理系统眼镜商城(ssm+B/S架构+源码+数据库+毕业论文+PPT)

一、项目简介 本项目是一套基于SSM框架实现的视康眼镜网店销售管理系统 包含:项目源码、数据库脚本等,该项目附带全部源码可作为毕设使用。 项目都经过严格调试,eclipse或者idea 确保可以运行! 该系统功能完善、界面美观、操作简…

Android4.4真机移植过程笔记(三)

如果文章字体看得不是很清楚,大家可以下载pdf文档查看,文档已上传~oo~ 7、安装加密APK 需要修改文件如下: 相对Android4.2改动还是蛮大的,有些文件连路径都变了: //Android4.2 1、frameworks/native/libs…

Java字符缓冲区

字符缓冲区是在计算机编程中非常重要的一种数据结构,它主要用于存储和高效地操作字符序列。 在 Java 中,StringBuffer类就是典型的字符缓冲区实现。与String类不同,StringBuffer具有动态可变性,这意味着我们可以在原有的字符序列…

【统计推断】-01 抽样原理之(四):中心极限定律(1)

文章目录 一、说明二、样本均值的抽样分布三、两个重要公理四、中心极限定理4.1 定义4.2 中心极限定理的特点4.3 中心极限定理的条件 五、一个举例5.1 一个连续分布示例5.2 样本容量变化的对比 六、结论 关键词:    Central Limit Theorem    Law of Large Numb…

机器学习 | 准确率、召回率、精准率、特异度傻傻分不清?ROC曲线怎么看?一篇文章帮你搞定

一、真正类、假负类、假正类与真负类 二、准确率、召回率、精准率、特异度与假正率 1. 准确率 (Accuracy) 准确率表明成功预测(预测为负或为正)的结果占总样本的百分比。 准确率 , 2. 召回率/查全率/灵敏度/真正率(Recall&a…

新华三李玉涛:智算网络是解决AI算力需求的关键

近年来,人工智能领域呈现爆发式增长,尤其在OpenAI、文心一言等大模型的不断推出,参数规模实现了飞跃式增长。同时,Character AI、谷歌Bard等应用已经逐渐渗透至日常生活和工作当中,越来越多的人开始借助AIGC工具来提升…

最优二叉搜索树

一、二叉搜索树(二叉查找树) 所有根节点大于左子树的节点,小于右子树的节点的二叉树 满足以下性质: 1.如果左子树不为空,则左子树上的所有节点都小于根节点 2.如果右子树不为空,则右子树上的所有节点都大于…

Web-SpringBootWeb

创建项目 后面因为报错,所以我把jdk修改成22,仅供参考。 定义类,创建方法 package com.start.springbootstart.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotati…

实验8 NAT配置

实验8 NAT配置 一、 原理描述二、 实验目的三、 实验内容1.实验场景2.实验要求 四、 实验配置五、 实验步骤2.静态NAT配置3.NAT Outbound配置4.NAT Easy-IP配置 一、 原理描述 2019年11月26日,全球43亿个IPv4地址正式耗尽,这意味着没有更多的IPv4地址可…

基于SSM的“航空机票预订系统”的设计与实现(源码+数据库+文档+PPT)

基于SSM的“航空机票预订系统”的设计与实现(源码数据库文档PPT) 开发语言:Java 数据库:MySQL 技术:SSM 工具:IDEA/Ecilpse、Navicat、Maven 系统展示 系统首页 公告管理 用户注册 留言评论 会员管理 航班管理 订…

uniApp+Vue3+vite+Element UI或者Element Plus开发学习,使用vite构建管理项目,HBuilderX做为开发者工具

我们通常给小程序或者app开发后台时,不可避免的要用到可视化的数据管理后台,而vue和Element是我们目前比较主流的开发管理后台的主流搭配。所以今天石头哥就带大家来一起学习下vue3和Element plus的开发。 准备工作 1,下载HBuilderX 开发者…

IDEA插件-通义灵码 VS ChatGPT-EasyCode

智能编码助手新时代:通义灵码 vs ChatGPT-EasyCode 随着人工智能技术的飞速发展,智能编码助手逐渐成为程序员的必备工具。它们可以帮助程序员提高编码效率,降低代码缺陷率,并解放创造力。 目前市场上涌现出了众多智能编码助手&a…

npm install 卡住不动不执行解决方法

npm install 卡住不动不执行解决方法,先是想到的切淘宝镜像,于是》》》 走淘宝镜像,结果淘宝镜像挂了,于是》》》》》 切成这个 https://registry.npmmirror.com/ 大功告成!

访问公共盘时提示:你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问。这些策略可帮助保护你的电脑免受网络上不安全设备或恶意设备的威胁。

原因:未启动启用策略:不安全的来宾登录 办法: 1,WindowsR键,打开运行,输入gpedit.msc,打开本地组策略编辑器;2,计算机配置>管理模板>网络>Lanman 工作站>启…

KUKA机器人如何给IO信号或寄存器添加中文注释信息?

KUKA机器人如何给IO信号或寄存器添加中文注释信息? 如下图所示,首先,我们需要登录专家以上用户权限(默认密码KUKA), 如下图所示,点击“投入运行”—“网络配置”, 如下图所示,此时机器人的IP地址为192.168.1.10, 如下图所示,用一根网线连接机器人控制柜到笔记…
最新文章