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

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

1、下载数据集
本期学习需要使用的数据集是17 Category Flower Dataset。17flowers是牛津大学Visual Geometry Group选取的在英国比较常见的17种花。其中每种花有80张图片,整个数据及有1360张图片,可以在官网下载。
下载路径:http://www.robots.ox.ac.uk/~vgg/data/flowers/17/
或者在tensorflow的官网下载:https://github.com/ck196/tensorflow-alexnet/blob/master/17flowers.tar.gz

2、了解lenet模型
LeNet-5:是Yann LeCun在1998年设计的用于手写数字识别的卷积神经网络,当年美国大多数银行就是用它来识别支票上面的手写数字的,它是早期卷积神经网络中最有代表性的实验系统之一。

LenNet-5共有7层(不包括输入层),每层都包含不同数量的训练参数。LeNet-5中主要有2个卷积层、2个下抽样层(池化层)、3个全连接层,3种连接方式如下图所示。
在这里插入图片描述
3、使用Lenet-5构建自己的图像识别模型
(1)首先得先把文件夹的图片数据读入模型,数据集图片大小是2828,现在需要把它变成100100.具体代码如下所示:

#coding=utf-8
from skimage import io,transform
import glob
import os
import tensorflow as tf
import numpy as np
import time



#数据集地址
path='D:/biancheng/'

#模型保存地址
model_path='D:/biancheng/LeNet_model/model.ckpt'

#将所有的图片resize成100*100
w=28
h=28
c=3

#读取图片
def read_img(path):
    #
    cate=[path+x for x in os.listdir(path) if os.path.isdir(path+x)]
    imgs=[]
    labels=[]
    for idx,folder in enumerate(cate):
        for im in glob.glob(folder+'/*.jpg'):
            print('reading the images:%s'%(im))
            img=io.imread(im)
            img=transform.resize(img,(w,h,c))
            imgs.append(img)
            labels.append(idx)
    return np.asarray(imgs,np.float32),np.asarray(labels,np.int32)
data,label=read_img(path)

#参数概要
def variable_summaries(var):
    with tf.name_scope("summaries"):
        mean = tf.reduce_mean(var)
        tf.summary.scalar("mean",mean)#平均值
        with tf.name_scope("stddev"):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var-mean)))
        tf.summary.scalar("stddev",stddev)#标准差
        tf.summary.scalar("max",tf.reduce_max(var))#最大值
        tf.summary.scalar("min",tf.reduce_min(var))#最小值
        tf.summary.histogram("histogram",var)#直方图

#打乱顺序
num_example=data.shape[0]
arr=np.arange(num_example)
np.random.shuffle(arr)
data=data[arr]
label=label[arr]

#将所有数据分为训练集和验证集
ratio=0.7
s=np.int(num_example*ratio)
x_train=data[:s]
y_train=label[:s]
x_val=data[s:]
y_val=label[s:]

#-----------------构建网络----------------------
#占位符
x=tf.placeholder(tf.float32,shape=[None,w,h,c],name='x')
y_=tf.placeholder(tf.int32,shape=[None,],name='y_')
lr = tf.Variable(0.0001,dtype=tf.float32)

# 构建CNN网络
def inference(input_tensor, train, regularizer):
    with tf.variable_scope('layer1-conv1'):
        conv1_weights = tf.get_variable("weight",[5,5,3,32],
                                        initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv1_biases = tf.get_variable("bias", [32],
                                       initializer=tf.constant_initializer(0.0))
        conv1 = tf.nn.conv2d(input_tensor, conv1_weights,
                             strides=[1, 1, 1, 1], padding='SAME')
        relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))


    with tf.name_scope("layer1-pool1"):
        pool1 = tf.nn.max_pool(relu1, ksize = [1,2,2,1],
                               strides=[1,2,2,1],padding="VALID")


    with tf.variable_scope("layer2-conv2"):
        conv2_weights = tf.get_variable("weight",[5,5,32,64],
                                       initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv2_biases = tf.get_variable("bias", [64],
                                       initializer=tf.constant_initializer(0.0))
        conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
        relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))

    with tf.name_scope("layer2-pool2"):
        pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1], padding='VALID')

        nodes = 7*7*64
        reshaped = tf.reshape(pool2,[-1,nodes])

    with tf.variable_scope('layer3-fc1'):
        fc1_weights = tf.get_variable("weight", [nodes, 512],
                                      initializer=tf.truncated_normal_initializer(stddev=0.1))

        if regularizer != None:
            tf.add_to_collection('losses', regularizer(fc1_weights))

        fc1_biases = tf.get_variable("bias", [512],
                                     initializer=tf.constant_initializer(0.1))
        fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_weights) + fc1_biases)

        if train:
            fc1 = tf.nn.dropout(fc1, 0.5)

    with tf.variable_scope('layer4-fc2'):
        fc2_weights = tf.get_variable("weight", [512, 64],
                                      initializer=tf.truncated_normal_initializer(stddev=0.1))

        if regularizer != None:
            tf.add_to_collection('losses', regularizer(fc2_weights))

        fc2_biases = tf.get_variable("bias", [64],
                                     initializer=tf.constant_initializer(0.1))

        fc2 = tf.nn.relu(tf.matmul(fc1, fc2_weights) + fc2_biases)
        if train:
            fc2 = tf.nn.dropout(fc2, 0.5)


    with tf.variable_scope('layer5-fc3'):
        fc3_weights = tf.get_variable("weight", [64, 5],
                                      initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None:
            tf.add_to_collection('losses', regularizer(fc3_weights))
        fc3_biases = tf.get_variable("bias", [5], initializer=tf.constant_initializer(0.1))
        logit = tf.matmul(fc2, fc3_weights) + fc3_biases

    return logit

#---------------------------网络结束---------------------------
regularizer = tf.contrib.layers.l2_regularizer(0.0001)
logits = inference(x,False,regularizer)

#(小处理)将logits乘以1赋值给logits_eval,定义name,方便在后续调用模型时通过tensor名字调用输出tensor
b = tf.constant(value=1,dtype=tf.float32)
logits_eval = tf.multiply(logits,b,name='logits_eval')

with tf.name_scope("loss"):
    loss=tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y_)
    tf.summary.scalar("loss", loss)

with tf.name_scope("train"):
    train_op=tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss)
correct_prediction = tf.equal(tf.cast(tf.argmax(logits,1),tf.int32), y_)
acc= tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

#定义一个函数,按批次取数据
def minibatches(inputs=None, targets=None, batch_size=None, shuffle=False):
    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batch_size + 1, batch_size):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batch_size]
        else:
            excerpt = slice(start_idx, start_idx + batch_size)
        yield inputs[excerpt], targets[excerpt]

print("--------------------------数据集训练开始---------------------------------")
#训练和测试数据,可将n_epoch设置更大一些
n_epoch=3000
batch_size=16
saver=tf.train.Saver()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(n_epoch):
        start_time = time.time()
        sess.run(tf.assign(lr, 0.001 * (0.95 ** epoch)))


        #training
        train_loss, train_acc, n_batch = 0, 0, 0
        for x_train_a, y_train_a in minibatches(x_train, y_train, batch_size, shuffle=True):
            _,err,ac=sess.run([train_op,loss,acc], feed_dict={x: x_train_a, y_: y_train_a})
            train_loss += err;
            train_acc += ac;
            n_batch += 1

            print("   train loss: %f" % (np.sum(train_loss)/ n_batch),"   train acc: %f" % (np.sum(train_acc)/ n_batch))


        #validation
        val_loss, val_acc, n_batch = 0, 0, 0
        for x_val_a, y_val_a in minibatches(x_val, y_val, batch_size, shuffle=False):
            err, ac = sess.run([loss,acc], feed_dict={x: x_val_a, y_: y_val_a})
            val_loss += err; val_acc += ac; n_batch += 1
            count = 0

            print("   validation loss: %f" % (np.sum(val_loss)/ n_batch),"   validation acc: %f" % (np.sum(val_acc)/ n_batch))

    saver.save(sess,model_path)
    print("--------------------------数据集训练结束---------------------------------")


4、使用构建的lenet网络模型进行花卉识别
前面对lenet网络进行训练,以及保存了网络模型,现在我们要进行调用模型进行图像的测试识别,因此写了一个test.py文件进行测试。

test.py代码如下:

#coding=utf-8
from skimage import io, transform
import tensorflow as tf
import numpy as np
import os  # os 处理文件和目录的模块
import glob  # glob 文件通配符模块

#待测试数据集的路径
path = 'D:/biancheng/'
# 类别代表字典
flower_dict = {0:'花卉1',1:'花卉2',2:'花卉3',3:'花卉4',4:'花卉5'}

w = 28
h = 28
c = 3


# 读取图片+数据处理
def read_img(path):
    # os.listdir(path) 返回path指定的文件夹包含的文件或文件夹的名字的列表
    # os.path.isdir(path)判断path是否是目录
    # b = [x+x for x in list1 if x+x<15 ]  列表生成式,循环list1,当if为真时,将x+x加入列表b
    cate = [path + x for x in os.listdir(path) if os.path.isdir(path + x)]
    imgs = []

    for idx, folder in enumerate(cate):
        # glob.glob(s+'*.py') 从目录通配符搜索中生成文件列表
        for im in glob.glob(folder + '/*.jpg'):
            # 输出读取的图片的名称
            print('reading the images:%s' % (im))
            # io.imread(im)读取单张RGB图片 skimage.io.imread(fname,as_grey=True)读取单张灰度图片
            # 读取的图片
            img = io.imread(im)
            # skimage.transform.resize(image, output_shape)改变图片的尺寸
            img = transform.resize(img, (w, h, c))
            # 将读取的图片数据加载到imgs[]列表中
            imgs.append(img)
            # 将图片的label加载到labels[]中,与上方的imgs索引对应
            # labels.append(idx)
            # 将读取的图片和labels信息,转化为numpy结构的ndarr(N维数组对象(矩阵))数据信息
    return np.asarray(imgs, np.float32)
    # 调用读取图片的函数,得到图片和labels的数据集

data = read_img(path)
with tf.Session() as sess:
    saver = tf.train.import_meta_graph('D:/biancheng/LeNet_model/model.ckpt.meta')
    saver.restore(sess,tf.train.latest_checkpoint('D:/biancheng/LeNet_model/'))
    # sess:表示当前会话,之前保存的结果将被加载入这个会话
    # 设置每次预测的个数
    graph = tf.get_default_graph()
    x = graph.get_tensor_by_name("x:0")
    feed_dict = {x: data}

    logits = graph.get_tensor_by_name("logits_eval:0")  # eval功能等同于sess(run)

    classification_result = sess.run(logits, feed_dict)

    # 打印出预测矩阵
    print(classification_result)
    # 打印出预测矩阵每一行最大值的索引
    print(tf.argmax(classification_result, 1).eval())
    # 根据索引通过字典对应花卉的分类
    output = []
    output = tf.argmax(classification_result, 1).eval()
    for i in range(len(output)):
        print("第", i + 1, "朵花卉预测:" + flower_dict[output[i]])

本期作业:
1、了解dropout、正则化、批量归一化等
2、继续修改Lenet网络,提高准确率
3、改写VGG16网络,达到可以初步识别花卉数据集
4、继续tensorflow视频课程(P24-30):https://www.bilibili.com/video/av20542427/?p=8&t=1221

本期学习就到这里,欢迎大家关注、批评指正。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值