使用AlexNet训练自己的数据集

前言:

前两篇分别介绍两个图像识别的模型,第一个是mnist手写体图像识别,第二个是在第一个代码的基础上增加了一些优化技巧,例如正则化、dropout等,并且比较加上各种优化技巧之后图像识别的结果。 接下来介绍几个图像识别中比较经典的算法。具体的就介绍AlexNet 、VGG19 、 ResNet_152  、 InceptionV4 、DenseNet这五个模型,并且测试一下这些模型在卫星图像分类上的效果。

数据集介绍:

本次数据集是百度点石城市功能分类初赛的数据集,数据集的具体介绍见链接,在本代码中我们仅仅只使用数据集的图像,对于数据集中文本的使用等在一个专栏在讲解。

模型一: AlexNet

模型的介绍:

Alexnet是人工智能三大教父之一Hinton的学生Alex Krizhevsky发明的一个Deep Learning模型,一举摘下了视觉领域竞赛ILSVRC 2012的桂冠,在百万量级的ImageNet数据集合上,效果大幅度超过传统的方法,从传统的70%多提升到80%多。Alexnet成功的原因有五个:

原因一:  数据增强  常用的方法有  水平翻转 随机裁剪、平移变换 颜色、光照变换

原因二:   使用Dropout防止过拟合

原因三:   使用ReLU激活函数代替传统的Tanh或者Logistic

原因四:  Local Response Normalization 实际就是利用临近的数据做归一化。

原因五:  Overlapping的意思是有重叠,即Pooling的步长比Pooling Kernel的对应边要小。

模型的架构图:

这个图虽然比较直观的展示了Alexnet的架构,五层卷积层加上三层全连接层,但是在我们写代码的我们要知道每层卷积的步长,卷积核大小一类的信息,怎么办,okay下面一幅图就比较适合解释这个问题。okay这样的话,我们就能知道每一层卷积包含那些操作了。在我们Alexnet每一层的架构有了一定了解之后,我们开始我们的代码,从代码中分析具体过程。

# 为了能够更好的理解Alexnet网络, 我写了两个python文件,一个是主函数文件AlexNet_Train.py,这里面我们主要看怎么加载数据,调用Alexnet网络,以及优化器的设置。而Alexnet.py中存放的是Alexnet的网络架构。

首先是主程序AlexNet_Train.py:

主函数文件比较简单,可以分成以下几个步骤,定义一些占位符(挖坑),调用Alexnet网络,并且返回一个大小为[batch_size, label]的隐层特征,再然后使用选择softmax以及adam优化器计算隐层特征和真实label的差距,并更新数据。最后即是feed数据(填坑)。 之所以写的很简单是,没有加上一些模型保存和加载,因为比较麻烦。

# -*- coding: utf-8 -*-
# @Time    : 2019/7/2 14:37
# @Author  : YYLin
# @Email   : [email protected]
# @File    : AlexNet_Train.py
from Alexnet import alexNet
import tensorflow as tf
import os
import cv2
import numpy as np
from keras.utils import to_categorical

# 定义一下训练中所需要的一些变量
# 站在现在的角度来看AlexNet模型的话 比较简单 所以可以用较大的batch_size
batch_size = 128
img_high = 100
img_width = 100
Channel = 3
label = 9

# 定义输入图像的占位符
inputs = tf.placeholder(tf.float32, [batch_size, img_high, img_width, Channel], name='inputs')
y = tf.placeholder(dtype=tf.float32, shape=[batch_size, label], name='label')
keep_prob = tf.placeholder("float")

# 调用alexNet训练数据  model.fc3 最后分类的结果为[batch_size, label]
model = alexNet(inputs, keep_prob, label)
score = model.fc3
softmax_result = tf.nn.softmax(score)

# 定义损失函数 以及相对应的优化器
cross_entropy = -tf.reduce_sum(y*tf.log(softmax_result))
Optimizer = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

# 用于判断验证集的结果是否正确
correct_prediction = tf.equal(tf.argmax(softmax_result, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))


# 定义一个读取训练集和验证集的函数
def load_satetile_image(batch_size=128, dataset='train'):
    img_list = []
    label_list = []
    dir_counter = 0

    if dataset == 'train':
        path = '../Dataset/baidu/train_image/train'

        # 对路径下的所有子文件夹中的所有jpg文件进行读取并存入到一个list中
        for child_dir in os.listdir(path):
            child_path = os.path.join(path, child_dir)
            for dir_image in os.listdir(child_path):
                img = cv2.imread(os.path.join(child_path, dir_image))
                img_list.append(img)
                label_list.append(dir_counter)

            dir_counter += 1
    else:
        path = '../Dataset/baidu/valid_image/valid'

        # 对路径下的所有子文件夹中的所有jpg文件进行读取并存入到一个list中
        for child_dir in os.listdir(path):
            child_path = os.path.join(path, child_dir)
            for dir_image in os.listdir(child_path):
                img = cv2.imread(os.path.join(child_path, dir_image))
                img_list.append(img)
                label_list.append(dir_counter)

            dir_counter += 1

    # 返回的img_list转成了 np.array的格式
    X_train = np.array(img_list)
    Y_train = to_categorical(label_list, 9)
    # print('to_categorical之后Y_train的类型和形状:', type(Y_train), Y_train.shape)

    # 加载数据的时候 重新排序
    # print('X_train.shape, Y_train.shape:', X_train.shape, Y_train.shape)
    data_index = np.arange(X_train.shape[0])
    np.random.shuffle(data_index)
    data_index = data_index[:batch_size]
    x_batch = X_train[data_index, :, :, :]
    y_batch = Y_train[data_index, :]

    return x_batch, y_batch


# 开始feed 数据并且训练数据
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(500000//batch_size):
        # 加载训练集和验证集
        img, img_label = load_satetile_image(batch_size, dataset='train')
        img_valid, img_valid_label = load_satetile_image(batch_size, dataset='vaild')
        # print('使用 mnist.train.next_batch加载的数据集形状', img.shape, type(img))

        # print('模型使用的是dropout的模型')
        dropout_rate = 0.5
        Optimizer.run(feed_dict={inputs: img, y: img_label, keep_prob: dropout_rate})

        # print('经过 tf.reshape之后数据的形状以及类型是:', img.shape, type(img))
        if i % 20 == 0:
            train_accuracy = accuracy.eval(feed_dict={inputs: img, y: img_label, keep_prob: dropout_rate})
            print("step %d, training accuracy %g" % (i, train_accuracy))

        # 输出验证集上的结果
        if i % 50 == 0:
            dropout_rate = 1
            valid_socre = accuracy.eval(feed_dict={inputs: img_valid, y: img_valid_label, keep_prob: dropout_rate})
            print("step %d, valid accuracy %g" % (i, valid_socre))

然后是本程序核心,AlexNet模型:

在这里讲一下我复现这些经典模型的方法,因为我不是专门研究图像是别的,所以我不是直接上GitHub上找别人的源码复现一下看看效果,而是首先上知乎上了解以下这些模型的基本架构,等我明白了具体架构的时候,我才会去上GitHub上找和我模型相关的源码。因为你不能保证别人开源的代码的架构是不是正确的,当你理解了架构之后,在看别人模型的时候你就有了一个粗浅的判断。okay!!!!!!!!话不多说看一下大佬们的代码结构

由AlexNet的架构图我们可以知道,AlexNet总共有5层卷积层,

第一层: 卷积核大小是11*11  步长是4*4  通道数为96  紧接着是一个relu激活函数 局部正则化 最后是一个卷积核大小为3*3 步长为2*2 的最大池化层。  代码中conv1 在卷积最后没有使用relu激活函数   其他均满足要求

第二层: 卷积核大小是5*5 步长是1*1 通道数为256 紧接着是一个relu激活函数 局部正则化 最后是一个卷积核大小为3*3 步长为2*2 的最大池化层。  代码中conv2除了没使用relu激活函数 其他均满足

第三层、第四层、第五层:   卷积核大小为 3 * 3,  步长为 1*1,通道数分别是384 384 256,同样没有增加relu激活函数。

最后全连接层是我自己根据实际需求修改的。因为我加载图像的大小是100*100,这个是不会对程序有影响的。而且上面说的没有增加relu是我专门拿出来的,意思是不要相信任何人的公开的源码,毕竟不收费你也不确定人家对不对。所以一定要好好看论文,简单理解模型之后再复现代码。

# -*- coding: utf-8 -*-
# @Time    : 2019/7/1 20:40
# @Author  : YYLin
# @Email   : [email protected]
# @File    : Alexnet.py
import tensorflow as tf


# 定义一个最大池化层
def maxPoolLayer(x, kHeight, kWidth, strideX, strideY, name, padding="SAME"):
    return tf.nn.max_pool(x, ksize=[1, kHeight, kWidth, 1],
                          strides=[1, strideX, strideY, 1], padding=padding, name=name)


# 使用dropout 随机丢包
def dropout(x, keepPro, name=None):
    return tf.nn.dropout(x, keepPro, name)


# 定义一个局部正则化
def LRN(x, r, alpha, beta, name=None, bias=1.0):
    return tf.nn.local_response_normalization(x, depth_radius=r, alpha=alpha, beta=beta, bias=bias, name=name)


# 定义一个全连接层的函数 其实可以修改成之前我写的那个
def fcLayer(x, inputD, outputD, reluFlag, name):
    with tf.variable_scope(name) as scope:
        w = tf.get_variable("w", shape=[inputD, outputD], dtype="float")
        b = tf.get_variable("b", [outputD], dtype="float")
        out = tf.nn.xw_plus_b(x, w, b, name=scope.name)
        if reluFlag:
            return tf.nn.relu(out)
        else:
            return out


def convLayer(x, kHeight, kWidth, strideX, strideY, featureNum, name, padding="SAME", groups=1):
              
    channel = int(x.get_shape()[-1])
    conv = lambda a, b: tf.nn.conv2d(a, b, strides=[1, strideY, strideX, 1], padding=padding)
    with tf.variable_scope(name) as scope:
        w = tf.get_variable("w", shape=[kHeight, kWidth, channel/groups, featureNum])
        b = tf.get_variable("b", shape=[featureNum])

        xNew = tf.split(value=x, num_or_size_splits=groups, axis=3)
        wNew = tf.split(value=w, num_or_size_splits=groups, axis=3)

        featureMap = [conv(t1, t2) for t1, t2 in zip(xNew, wNew)]
        mergeFeatureMap = tf.concat(axis=3, values=featureMap)
        # print mergeFeatureMap.shape
        out = tf.nn.bias_add(mergeFeatureMap, b)
        return tf.nn.relu(tf.reshape(out, mergeFeatureMap.get_shape().as_list()), name = scope.name)


# 定义一个alexNet类别 方便之后调用
class alexNet(object):
    def __init__(self, x, keepPro, classNum):
        self.X = x
        self.KEEPPRO = keepPro
        self.CLASSNUM = classNum
        # 用于加载alexNet 所需要的各种操作
        self.begin_alexNet()

    def begin_alexNet(self):
        conv1 = convLayer(self.X, 11, 11, 4, 4, 96, "conv1", "VALID")
        lrn1 = LRN(conv1, 2, 2e-05, 0.75, "norm1")
        pool1 = maxPoolLayer(lrn1, 3, 3, 2, 2, "pool1", "VALID")

        conv2 = convLayer(pool1, 5, 5, 1, 1, 256, "conv2", groups = 2)
        lrn2 = LRN(conv2, 2, 2e-05, 0.75, "lrn2")
        pool2 = maxPoolLayer(lrn2, 3, 3, 2, 2, "pool2", "VALID")

        conv3 = convLayer(pool2, 3, 3, 1, 1, 384, "conv3")

        conv4 = convLayer(conv3, 3, 3, 1, 1, 384, "conv4", groups = 2)

        conv5 = convLayer(conv4, 3, 3, 1, 1, 256, "conv5", groups = 2)
        pool5 = maxPoolLayer(conv5, 3, 3, 2, 2, "pool5", "VALID")
        print('alexNet中最后一层卷积层的形状是:', pool5.shape)

        fcIn = tf.reshape(pool5, [-1, 256 * 2 * 2])
        fc1 = fcLayer(fcIn, 256 * 2 * 2, 4096, True, "fc6")
        dropout1 = dropout(fc1, self.KEEPPRO)

        fc2 = fcLayer(dropout1, 4096, 4096, True, "fc7")
        dropout2 = dropout(fc2, self.KEEPPRO)

        self.fc3 = fcLayer(dropout2, 4096, self.CLASSNUM, True, "fc8")

okay 最后看一下AlexNet在卫星图像分类上的效果吧!!!!!!!!!!!!!! 

训练时间比较短,不能说明问题,先让程序跑着,跑个差不多的时候在分析一下效果好或者不好的原因。

猜你喜欢

转载自blog.csdn.net/qq_41776781/article/details/94437671