机器学习笔记7 CIFAR 10 图像识别 ResNet with Keras

ResNet

在之前的文章中,我们利用了卷积神经网络构造了一个图像识别模型,这个模型在CIFAR 10 数据集上的准确率在70%以上。 在这篇文章中,我们将利用keras构造ResNet模型,使用这个模型与之前的模型进行比较。
ResNet 模型是在这篇文章中给出的 Deep Residual Learning for Image Recognition
在进行CNN模型搭建的时候,我们回尽可能的想要搭建更多的层, 但是当层数递增的时候,我们会遇到梯度湮灭的问题。例如,我们有一个卷积神经网络模型,在训练的过程中,这个模型即将要收敛到极值点,但是在这个过程中,梯度将会很快的衰减。 由于卷积层的深度,在利用链式法则求偏导数的时候,会使得梯度趋近于0。
为了解决这个问题,在这篇文章中,作者给出了一个简单的方法,即增加一个残差模块, 如下图所示:
ResNet
这时候我们进行求偏导的时候,结果会自动的加上1,梯度湮灭的问题也就很好的解决了。在原文中,作者甚至可以训练一个超过1000层的神经网络。

数据处理

这里的数据处理和之前一样, 就不赘述了。

模型搭建

首先,我们先搭建残差模块。

导入keras
import keras as keras
import os
os.environ['KERAS_BACKEND']='tensorflow'
import numpy as np
from keras.datasets import cifar10
from keras.layers import Conv2D, MaxPooling2D, Input, Add, AveragePooling2D
from keras.layers import Dropout, Dense, Flatten, BatchNormalization,Activation
from keras.models import Model, Sequential
from keras.utils import plot_model
import matplotlib.pyplot as plt
预处理
def block_prepare(Input_tensor, Layer):
    x = Input_tensor
    x = Conv2D(Layer, (3,3), padding = "same", activation = "relu")(x)
    x = BatchNormalization()(x)
    x = MaxPooling2D((2,2))(x)
    return x
残差模块
def block_conv(Input_tensor, Layer, Last, Avg):
    x = Input_tensor
    x_identity = x
    x = Conv2D(Layer, (3,3), padding = "same", activation = "relu")(x)
    x = BatchNormalization()(x)
    x = Conv2D(Layer, (3,3), padding = "same", activation = None)(x)
    x = BatchNormalization()(x)
    x = Add()([x,x_identity])
    x = Activation("relu")(x)
    
    if Last == True:
        x = MaxPooling2D(pool_size = (3,3), strides = (2,2), padding = "same")(x)
        x = Conv2D(Layer * 2, (1,1), padding = "same")(x)
    
    if Avg == True:
        x = AveragePooling2D(pool_size = (2,2), strides = (2,2), padding = "same")(x)
    return x

在残差模块中,我们使用的如下方案:
CONV >> Activation >> BN >> Conv >> Activation >> BN >> Residual >> Activation >> Pooling

模型拼接
x = Input(shape = (32,32,3))
y = block_prepare(Input_tensor=x, Layer=64)
########################################
for i in range(4):
    Last = True if i == 3 else False
    y = block_conv(Input_tensor=y, Layer = 64, Last=Last, Avg = False)
for i in range(4):
    Last = True if i == 3 else False
    y = block_conv(Input_tensor=y, Layer = 128, Last=Last, Avg = False)
for i in range(6):
    Last = True if i == 5 else False
    y = block_conv(Input_tensor=y, Layer = 256, Last=Last, Avg = False)
for i in range(3):
    Avg = True if i == 2 else False
    y = block_conv(Input_tensor=y, Layer = 512, Last=False, Avg = Avg)

y = Flatten()(y)
y = Dense(10)(y)
out = Activation("softmax")(y)
ResNet = Model(inputs=x, outputs=out)

ResNet.summary()

这里我们使用了四次残差模块,每次使用时,分别作用4次、4次、6次以及3次。
接着便可以进行训练。 由于层数很高,训练十分的缓慢。

ResNet.compile(optimizer = "adam", loss = 'categorical_crossentropy', metrics = ['accuracy'])
history = ResNet.fit(images, labels_onehot, batch_size = 64, epochs = 50, 
                    validation_data = [images_test, labels_test_onehot])

这里我们的模型出现了过拟合的现象,之后需要进行调整。

Epoch 50/50
50000/50000 [==============================] - 3603s 72ms/step - loss: 0.0472 - acc: 0.9838 - val_loss: 1.2769 - val_acc: 0.7804
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值