Tensorflow化骨绵掌第5式-LeNet、AlexNet、VGG16、VGG19打造自己的图像识别模型(2)

Tensorflow化骨绵掌第5式-LeNet、AlexNet、VGG16、VGG19打造自己的图像识别模型(2)

1、了解AlexNet网络结构
这篇博文中讲到的是AlexNet,这个网络应该是目前最广为人知的卷积神经网络。它在2012年以巨大的优势赢得了ILSVRC比赛,使得深度学习再次进入人们的视野,也确立了深度学习在计算机视觉领域的统治地位。

AlexNet有5个卷积层,3个全连接层,其中第三个全连接层(最后一层)是有1000类输出的Softmax层作为分类。
在这里插入图片描述

AlexNet每层的超参数如图1所示。输入图片大小为224x224个像素。第一个卷积层卷积核尺寸是11x11,步长为4,有96个卷积核,紧接着的是一个LRN层和一个大小为3x3,步长为2的最大池化层。第二个卷积层中卷积核的大小是5x5,后面三个卷积层中卷积核的大小是3x3,步长都为1。

在这里插入图片描述

图2是AlexNet每层的参数量和计算量统计图,可以发现,前面5个卷积层虽然计算量大但是参数量很小,这就是卷积层的优势所在。熟悉图像处理的朋友都知道卷积操作往往用于提取图像中的某一特征,例如Sobel算子可以用于提取图像的边缘信息,卷积神经网络中卷积核同样被用于提取图像的特征,通过不断提取特征获得某一事物的特性,进而对其进行识别。卷积神经网络中的卷积层通过局部连接、权值共享两个重要属性大大的缩小了参数量。也就是说,不管图像尺寸如何,网络中需要训练的权值数量只跟卷积和大小和数目有关,所以可以使用少量的参数训练任意数目、任意大小的图像。
在这里插入图片描述AlexNet模型与LeNet-5模型类似,只是要复杂一些,总共包含了大约6千万个参数。同样可以根据实际情况使用激活函数ReLU。原作者还提到了一种优化技巧,叫做Local Response Normalization(LRN)。 而在实际应用中,LRN的效果并不突出。

2、数据准备(制作Tfrecord数据集)
在本次学习中需要用到的网络结构采用的是tfrecord格式,所以我们需要先把花卉数据集转换成tfrecord格式的数据集,生成train.tfrecord文件。具体的解决方案代码如下所示:
To_tfrecord.py

#coding=utf-8

import os
import tensorflow as tf
from PIL import Image
import sys

def creat_tf(imgpath):

    # cwd = os.getcwd()
    classes = ("0","1","2","3","4")

    writer = tf.python_io.TFRecordWriter("train.tfrecords")
    for index, name in enumerate(classes):
        class_path = imgpath + name + "/"
        print(class_path)
        if os.path.isdir(class_path):
            for img_name in os.listdir(class_path):
                img_path = class_path + img_name
                img = Image.open(img_path)
                img = img.resize((224, 224))
                img_raw = img.tobytes()              #将图片转化为原生bytes
                example = tf.train.Example(features=tf.train.Features(feature={
                'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[int(name)])),
                'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
            }))
                writer.write(example.SerializeToString())  #序列化为字符串
                print(img_name)
    writer.close()

def read_example():

    #简单的读取例子:
    for serialized_example in tf.python_io.tf_record_iterator("train.tfrecords"):
        example = tf.train.Example()
        example.ParseFromString(serialized_example)
    
        #image = example.features.feature['img_raw'].bytes_list.value
        label = example.features.feature['label'].int64_list.value
        # 可以做一些预处理之类的
        print(label)

if __name__ == '__main__':
    imgpath = 'D:/biancheng/flowers/'
    creat_tf(imgpath)
    #read_example()

运行代码,生成train.tfrecord文件。
在这里插入图片描述

4、LRN(局部响应归一化)的介绍
作用:对局部神经元的活动创建竞争机制,使得其中响应较大的值变得更加大,并抑制其他反馈较小的神经元,增强模型的泛化能力。

如下图所示是LRN的公式:
在这里插入图片描述在公式中有:
在这里插入图片描述
总之是输入值除以一个数达到归一化的目的。N是总的通道数,不同通道累加的平方和,累加效果如下图。
在这里插入图片描述在Tensorflow中的API中已经内置了LRN的公式代码,如下所示:

sqr_sum[a, b, c, d] = sum(input[a,b, c, d - depth_radius : d + depth_radius + 1] ** 2)
output = input / (bias +alpha * sqr_sum) ** beta

其中[batch,height,width,channel]分别是:batch-批次数(每一批为一张图片),height、width分别为图片的高和宽,channel是一批图片中经过卷及操作后的输出神经元个数。

5、构建AlexNet网络模型
在前面了解了AlexNet的网络结构,在这个模型中应用到了LRN,接下来就是实现这个模型的代码展示:
alexnet.py

#coding=utf-8

from datetime import datetime
import tensorflow as tf
import math
import time
import numpy as np

def print_activations(t):
    print(t.op.name, ' ', t.get_shape().as_list())

def alexnet(images, _dropout):
    
    parameters = []
    "Conv1 layer"
    with tf.name_scope('conv1') as scope:
        weight = tf.Variable(tf.truncated_normal([11, 11, 3, 64], 
                            dtype=tf.float32, stddev=0.1), name='weights')
        conv = tf.nn.conv2d(images, weight, [1, 4, 4, 1], padding='SAME')
        bias = tf.Variable(tf.constant(0., shape=[64], dtype=tf.float32),
                            trainable=True, name='bias')
        conv1 = tf.nn.relu(conv + bias, name=scope)
        print_activations(conv1)
        parameters += [weight, bias]
    
    "LRN layer + Maxpool layer"
    lrn1 = tf.nn.lrn(conv1, 4, bias=1.0, alpha=0.001/9, beta=0.75, name='lrn1')
    pool1 = tf.nn.max_pool(lrn1, [1, 3, 3, 1], [1, 2, 2, 1],padding='VALID', name='pool1')
    print_activations(pool1)

    "Conv2 layer"
    with tf.name_scope('conv2') as scope:
        weight = tf.Variable(tf.truncated_normal([5, 5, 64, 192],
                            dtype=tf.float32, stddev=0.1), name='weights')
        conv = tf.nn.conv2d(pool1, weight, [1, 1, 1, 1], padding='SAME')
        bias = tf.Variable(tf.constant(0., shape=[192], dtype=tf.float32),
                            trainable=True, name='bias')
        conv2 = tf.nn.relu(conv + bias, name=scope)
        print_activations(conv2)
        parameters += [weight, bias]

    "LRN layer + Maxpool layer"
    lrn2 = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.001/9, beta=0.75, name='lrn2')
    pool2 = tf.nn.max_pool(lrn2, [1, 3, 3, 1], [1, 2, 2, 1], padding='VALID', name='pool2')
    print_activations(pool2)

    "Conv3 layer"
    with tf.name_scope('conv3') as scope:
        weight = tf.Variable(tf.truncated_normal([3, 3, 192, 384],
                            dtype=tf.float32, stddev=0.1), name='weights')
        conv = tf.nn.conv2d(pool2, weight, [1, 1, 1,1], padding='SAME')
        bias = tf.Variable(tf.constant(0., dtype=tf.float32, shape=[384]),
                            trainable=True, name='bias')
        conv3 = tf.nn.relu(conv + bias, name=scope)
        print_activations(conv3)
        parameters += [weight, bias]

    "Conv4 layer"
    with tf.name_scope('conv4') as scope:
        weight = tf.Variable(tf.truncated_normal([3, 3, 384, 256],
                            dtype=tf.float32, stddev=0.1), name='weights')
        conv = tf.nn.conv2d(conv3, weight, [1, 1, 1,1], padding='SAME')
        bias = tf.Variable(tf.constant(0., shape=[256], dtype=tf.float32),
                            trainable=True, name='bias')
        conv4 = tf.nn.relu(conv + bias, name=scope)
        print_activations(conv4)
        parameters +=[weight, bias]
        
    "Conv5 layer"
    with tf.name_scope('conv5') as scope:
        weight = tf.Variable(tf.truncated_normal([3, 3, 256, 256],
                            dtype=tf.float32, stddev=0.1), name='weights')
        conv = tf.nn.conv2d(conv4, weight, [1, 1, 1, 1], padding='SAME')
        bias = tf.Variable(tf.constant(0., shape=[256], dtype=tf.float32),
                            trainable=True, name='bias')
        conv5 = tf.nn.relu(conv + bias, name=scope)
        print_activations(conv5)
        parameters += [weight, bias]
    
    "Maxpool layer"
    pool5 = tf.nn.max_pool(conv5, [1, 3, 3, 1], [1, 2, 2, 1], padding='VALID', name='pool5')
    print_activations(pool5)

    "FC1 layer"
    flatten = tf.reshape(pool5, [-1, 6*6*256])
    with tf.name_scope('fc1') as scope:
        weight = tf.Variable(tf.truncated_normal([6*6*256, 4096], mean=0, stddev=0.01))
        bias   = tf.Variable(tf.constant(0., shape=[4096], dtype=tf.float32),
                            trainable=True, name='bias')
        fc1 = tf.nn.relu(tf.matmul(flatten, weight) + bias, name=scope)
        print_activations(fc1)
        parameters += [weight, bias]
    
    "dropout layer"
    dropout1 = tf.nn.dropout(fc1, _dropout)

    "FC2 layer"
    with tf.name_scope('fc2') as scope:
        weight = tf.Variable(tf.truncated_normal([4096,4096], mean=0, stddev=0.01))
        bias   = tf.Variable(tf.constant(0., shape=[4096], dtype=tf.float32),
                            trainable=True, name='bias')
        fc2 = tf.nn.relu(tf.matmul(dropout1, weight) + bias, name=scope)
        print_activations(fc2)
        parameters += [weight, bias]

    "dropout layer"
    dropout2 = tf.nn.dropout(fc2, _dropout)

    "FC3 layer"
    with tf.name_scope('fc3') as scope:
        weight = tf.Variable(tf.truncated_normal([4096, 5], mean=0, stddev=0.01))
        bias = tf.Variable(tf.constant(0., shape=[5], dtype=tf.float32),
                            trainable=True, name='bias')
        output = tf.nn.bias_add(tf.matmul(dropout2, weight), bias)
        print_activations(output)
        
    return output

6、训练网络模型并进行保存
将前面生成的train.tfrecord代入下面的网络中进行训练得到model.ckpt并进行保存。

alexnet_train.py

#coding=utf-8

import tensorflow as tf
import numpy as np
from datetime import datetime
from alexnet import *

def read_and_decode(filename):
    #根据文件名生成一个队列
    filename_queue = tf.train.string_input_producer([filename])

    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)   #返回文件名和文件
    features = tf.parse_single_example(serialized_example,
                                       features={
                                           'label': tf.FixedLenFeature([], tf.int64),
                                           'img_raw' : tf.FixedLenFeature([], tf.string),
                                       })

    img = tf.decode_raw(features['img_raw'], tf.uint8)
    img = tf.reshape(img, [224, 224, 3])
    # 转换为float32类型,并做归一化处理
    img = tf.cast(img, tf.float32) * (1. / 255)
    label = tf.cast(features['label'], tf.int64)
    #print 'label的样子是:', label
    return img, label

def train():

    x = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3], name='input')
    y = tf.placeholder(dtype=tf.float32, shape=[None, 5], name='label')
    keep_prob = tf.placeholder(tf.float32)
    output = alexnet(x, keep_prob)

    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y))
    train_step = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)

    pred = tf.argmax(output, 1)
    truth = tf.argmax(y, 1)
    accuracy = tf.reduce_mean(tf.cast(tf.equal(pred, truth), tf.float32))
    saver = tf.train.Saver()

    img, label = read_and_decode("./train.tfrecords")
    img_batch, label_batch = tf.train.shuffle_batch([img, label],
                                                batch_size=32, capacity=2000,
                                                min_after_dequeue=1000)
    labels = tf.one_hot(label_batch,5,1,0)

    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        for i in range(2500):
            #print '未转换的label样子是: ', sess.run(label)
            batch_xs, batch_ys = sess.run([img_batch, labels])
            #print '转换为onehot的label样子是: ', batch_ys, len(batch_ys), '个'
            _, loss_val = sess.run([train_step, loss], feed_dict={x:batch_xs, y:batch_ys, keep_prob:0.5})
            if i%10 == 0:
                train_arr = accuracy.eval(feed_dict={x:batch_xs, y: batch_ys, keep_prob: 1.0})
                print("%s: Step [%d]  Loss : %f, training accuracy :  %g" % (datetime.now(), i, loss_val, train_arr))

        coord.request_stop()
        coord.join()
        saver.save(sess,'./model/model.ckpt')
        #saver.restore(sess, './model.ckpt')读取参数
        #tf.train.write_graph(sess.graph_def,'.','pbtxt')
def main(argv=None):
    train()
if __name__ == '__main__':
    tf.app.run()

代码运行完生成如下文件:
在这里插入图片描述

7、调用模型进行图片测试
前面训练生成了网络模型文件,接下来需要我们对模型进行检验,验证模型的泛化能力。为此,将测试图片进行导入生成测试结果。具体的测试代码如下所示:
alexnet_test.py

#coding=utf-8

import tensorflow as tf
import numpy as np
from datetime import datetime
from alexnet import *
import cv2
import os

def test(path):

    x = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3], name='input')
    keep_prob = tf.placeholder(tf.float32)
    output = alexnet(x, keep_prob)
    preds = tf.argmax(output, 1)

    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, './model/model.ckpt')
    for i in os.listdir(path):
        imgpath = os.path.join(path, i)
        im = cv2.imread(imgpath)
        im = cv2.resize(im, (224 , 224)) * (1. / 255)
        #cv2.imshow("img", im)
        im = np.expand_dims(im, axis=0)
        pred = sess.run(preds, feed_dict={x:im, keep_prob:1.0})
        #_cls = pred.argmax()
        #print pred
        print("{} image flowers class is: {}".format(i, pred))

        
        #cv2.waitKey()
        #cv2.destroyWindow('img')
    sess.close()


# if __name__ == '__main__':
path = 'D:/biancheng/0'#测试图片文件夹
test(path)

我们看到测试结果如下图所示,发现模型的识别性能良好:
在这里插入图片描述
本期学习到此结束,欢迎大家进行关注、批评指正。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值