Keras【极简】CNN

文章目录

  • 1、极简示例
  • 2、详细示例
    • 2.1、内置数据集
    • 2.2、图像生成器
    • 2.3、模型可视化
    • 2.4、损失曲线和精度曲线
  • 3、EarlyStopping设置自动结束

1、极简示例

from keras.datasets import cifar10  # 小图像10分类
from keras.utils import to_categorical  # 独热码
from keras.models import Sequential  # 顺序模型
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D

"""数据读取、预处理"""
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train / 255, x_test / 255  # 像素值→[0,1]
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

"""创建顺序模型"""
model = Sequential()
# 卷积层:30个3x3的卷积滤波器、ReLU激活
model.add(Conv2D(30, (3, 3), padding='same', activation='relu'))
# 池化层
model.add(MaxPooling2D(pool_size=(2, 2)))
# 平坦化
model.add(Flatten())
# 全连接层:神经元个数、ReLU激活
model.add(Dense(100, activation='relu'))
# 输出层(10分类)
model.add(Dense(10, activation='softmax'))

"""模型编译"""
# 优化器【随机梯度下降】;损失函数【分类交叉熵】;添加度量【精准度】
model.compile('sgd', 'categorical_crossentropy', ['accuracy'])

"""训练"""
model.fit(x_train, y_train, 1000, 2)  # 批量、训练次数

"""模型评估"""
loss, acc = model.evaluate(x_test, y_test, 1000)
print('loss %g  acc %g' % (loss, acc))

2、详细示例

from keras.datasets import cifar10
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
from keras.utils import plot_model  # conda install pydot
from keras.optimizers import RMSprop  # 优化器
from keras.preprocessing.image import ImageDataGenerator  # 图像生成器
import matplotlib.pyplot as mp

"""数据读取、预处理"""
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
input_shape = x_train.shape[1:]
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
# 数据增强
batch_size = 100
steps_per_epoch = len(y_train) / batch_size
epochs = 2
idg_train = ImageDataGenerator(
    horizontal_flip=True,
    rescale=1/255).flow(x_train, y_train, batch_size)
idg_test = ImageDataGenerator(
    rescale=1/255).flow(x_test, y_test, batch_size)
del x_train, y_train, x_test, y_test  # 释放内存

"""建模"""
model = Sequential()
model.add(Conv2D(30, (3, 3), activation='relu',
                 input_shape=input_shape))  # 输入尺寸使编译成功))
model.add(MaxPooling2D(pool_size=(2, 2), name='MaxPooling2D'))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(10, activation='softmax'))
optimizer = RMSprop(lr=1e-3, decay=1e-6)
model.compile(optimizer, 'categorical_crossentropy', ['acc'])

"""训练"""
history = model.fit_generator(
    idg_train, steps_per_epoch, epochs,
    validation_data=idg_test,
    validation_steps=batch_size)

"""模型可视化"""
model.summary()  # 网络维度
plot_model(model, 'model.png', show_shapes=True,
           show_layer_names=False)  # 网络结构(存图片)

"""损失曲线和精度曲线"""
ls = ['loss', 'val_loss', 'acc', 'val_acc']
hs = [history.history[i] for i in ls]
for i in (0, 1):
    mp.subplot(1, 2, i + 1)
    mp.plot(range(1, epochs + 1), hs[i * 2], 'g', label=ls[i * 2])
    mp.plot(range(1, epochs + 1), hs[i * 2 + 1], label=ls[i * 2 + 1])
    mp.legend()
mp.show()

2.1、内置数据集

from keras.datasets import cifar10  # 10分类小图像
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print(x_train.shape)  # (50000, 32, 32, 3)
print(y_train.shape)  # (50000, 1)
print(x_test.shape)  # (10000, 32, 32, 3)
print(y_test.shape)  # (10000, 1)
import matplotlib.pyplot as mp, numpy as np
images = np.concatenate([x_train[i] for i in range(9)], axis=1)
mp.imshow(images)
mp.show()

在这里插入图片描述

2.2、图像生成器

from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as mp, numpy as np
# 数据读取
(x_train, y_train), _ = cifar10.load_data()
imgs = x_train[:3]
# 图像生成器
idg = ImageDataGenerator(
    rescale=1./255,  # 像素值→[0,1]
    rotation_range=30,  # 旋转角度
    horizontal_flip=True,  # 水平翻转
    width_shift_range=.3,  # 水平偏移
    height_shift_range=.3)  # 垂直偏移
# 可视化
for e, i in enumerate(idg.flow(imgs, shuffle=False), 1):
    if e > 9: break  # 打断死循环
    mp.subplot(1, 9, e)
    mp.axis('off')
    img = np.concatenate(i)
    mp.imshow(img)
mp.show()

Keras【极简】CNN_第1张图片

2.3、模型可视化

.summary

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 30, 30, 30)        840       
_________________________________________________________________
MaxPooling2D (MaxPooling2D)  (None, 15, 15, 30)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 6750)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 100)               675100    
_________________________________________________________________
dense_2 (Dense)              (None, 10)                1010      
=================================================================
Total params: 676,950
Trainable params: 676,950
Non-trainable params: 0

plot_model(model, show_shapes=True, show_layer_names=False)
Keras【极简】CNN_第2张图片

2.4、损失曲线和精度曲线

Keras【极简】CNN_第3张图片

3、EarlyStopping设置自动结束

from keras.datasets import cifar10
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
from keras import optimizers
from keras.callbacks import EarlyStopping  # 提早停止训练
import matplotlib.pyplot as mp

"""训练批量和轮数"""
batch_size = 500
epochs = 50  # 总训练轮数
min_delta = .01  # 最小进步,低于该值将被认为没有进步
patience = 5  # 没有进步的训练轮数

"""读数据、预处理"""
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train / 255, x_test / 255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

"""建模"""
model = Sequential()
for i in (32, 64, 128):
    model.add(Conv2D(i, (3, 3), padding='same', activation='relu'))
    model.add(Conv2D(i, (3, 3), padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(.25))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(.5))
model.add(Dense(10, activation='softmax'))
optimizer = optimizers.RMSprop(lr=1e-3, decay=1e-6)
# 模型编译
model.compile(optimizer, 'categorical_crossentropy', ['acc'])
# 拟合(取1%样本用于验证)、自动结束
callback = EarlyStopping('val_acc', min_delta, patience)
history = model.fit(x_train, y_train, batch_size, epochs, verbose=2,
                    callbacks=[callback], validation_split=.01)

"""评估"""
loss, acc = model.evaluate(x_test, y_test, batch_size, verbose=2)
print('loss %g  acc %g' % (loss, acc))
acc = history.history['acc']
val_acc = history.history['val_acc']
mp.plot(range(len(acc)), acc, 'g', label='acc')
mp.plot(range(len(val_acc)), val_acc, label='val_acc')
mp.legend()
mp.show()
en cn en cn
convolution 卷积;盘旋 Conv2D 二维卷积
util 跑龙套;工具 regularizer 正则化矩阵
dense 稠密的 flatten 铲平vt;变平vi
optimize 优化 epoch 新纪元;时间上的一点
categorical 绝对的;分类的 sequential 连续的
pooling [贸易]联营;池化 drop out 退学;脱离
validation 确认;批准;生效 summary 摘要
rotation 旋转 flip 筋斗
rescale 重新调节 shift 移动
patience 耐性 monitor 监视器;显示屏;监测

你可能感兴趣的:(深度学习)