Tensorflow学习教程------lenet多标签分类

本文在上篇的基础上利用lenet进行多标签分类。五个分类标准,每个标准分两类。实际来说,本文所介绍的多标签分类属于多任务学习中的联合训练,具体代码如下。

#coding:utf-8
import tensorflow as tf
import os
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={
                                           ‘label1‘: tf.FixedLenFeature([], tf.int64),
                                           ‘label2‘: tf.FixedLenFeature([], tf.int64),
                                           ‘label3‘: tf.FixedLenFeature([], tf.int64),
                                           ‘label4‘: tf.FixedLenFeature([], tf.int64),
                                           ‘label5‘: tf.FixedLenFeature([], tf.int64),
                                           ‘img_raw‘ : tf.FixedLenFeature([], tf.string),
                                       })

    img = tf.decode_raw(features[‘img_raw‘], tf.uint8)
    img = tf.reshape(img, [227, 227, 3])
    img = (tf.cast(img, tf.float32) * (1. / 255) - 0.5)*2
    label1 = tf.cast(features[‘label1‘], tf.int32)
    label2 = tf.cast(features[‘label2‘], tf.int32)
    label3 = tf.cast(features[‘label3‘], tf.int32)
    label4 = tf.cast(features[‘label4‘], tf.int32)
    label5 = tf.cast(features[‘label5‘], tf.int32)
    #print img,label
    return img, label1,label2,label3,label4,label5

def get_batch(image, label1,label2,label3,label4,label5, batch_size,crop_size):
    #数据扩充变换
    distorted_image = tf.random_crop(image, [crop_size, crop_size, 3])#随机裁剪
    distorted_image = tf.image.random_flip_up_down(distorted_image)#上下随机翻转
    distorted_image = tf.image.random_brightness(distorted_image,max_delta=63)#亮度变化
    distorted_image = tf.image.random_contrast(distorted_image,lower=0.2, upper=1.8)#对比度变化  

    #生成batch
    #shuffle_batch的参数:capacity用于定义shuttle的范围,如果是对整个训练数据集,获取batch,那么capacity就应该够大
    #保证数据打的足够乱
    images, label1_batch,label2_batch,label3_batch,label4_batch,label5_batch = tf.train.shuffle_batch([distorted_image,      label1,label2,label3,label4,label5],batch_size=batch_size,
                                                 num_threads=1,capacity=20000,min_after_dequeue=1000) 

    return images, label1_batch,label2_batch,label3_batch,label4_batch,label5_batch

class network(object):  

    def lenet(self,images,keep_prob):

        ‘‘‘
        根据tensorflow中的conv2d函数,我们先定义几个基本符号
        输入矩阵 W×W,这里只考虑输入宽高相等的情况,如果不相等,推导方法一样,不多解释。
        filter矩阵 F×F,卷积核
        stride值 S,步长
        输出宽高为 new_height、new_width
        在Tensorflow中对padding定义了两种取值:VALID、SAME。下面分别就这两种定义进行解释说明。
        VALID
        new_height = new_width = (W – F + 1) / S  #结果向上取整
        SAME
        new_height = new_width = W / S    #结果向上取整
        ‘‘‘

        images = tf.reshape(images,shape=[-1,32,32,3])
        #images = (tf.cast(images,tf.float32)/255.0-0.5)*2
        #第一层,卷积层 32,32,3--->5,5,3,6--->28,28,6
        #卷积核大小为5*5 输入层深度为3即三通道图像 卷积核深度为6即卷积核的个数
        conv1_weights = tf.get_variable("conv1_weights",[5,5,3,6],initializer = tf.truncated_normal_initializer(stddev=0.1))
        conv1_biases = tf.get_variable("conv1_biases",[6],initializer = tf.constant_initializer(0.0))
        #移动步长为1 不使用全0填充
        conv1 = tf.nn.conv2d(images,conv1_weights,strides=[1,1,1,1],padding=‘VALID‘)
        #激活函数Relu去线性化
        relu1 = tf.nn.relu(tf.nn.bias_add(conv1,conv1_biases))

        #第二层 最大池化层  28,28,6--->1,2,2,1--->14,14,6
        #池化层过滤器大小为2*2 移动步长为2 使用全0填充
        pool1 = tf.nn.max_pool(relu1, ksize=[1,2,2,1],strides=[1,2,2,1],padding=‘SAME‘)

        #第三层 卷积层   14,14,6--->5,5,6,16--->10,10,16
        #卷积核大小为5*5 当前层深度为6 卷积核的深度为16
        conv2_weights = tf.get_variable("conv_weights",[5,5,6,16],initializer = tf.truncated_normal_initializer(stddev=0.1))
        conv2_biases = tf.get_variable("conv2_biases",[16],initializer = tf.constant_initializer(0.0))

        conv2 = tf.nn.conv2d(pool1,conv2_weights,strides=[1,1,1,1],padding=‘VALID‘) #移动步长为1 不使用全0填充
        relu2 = tf.nn.relu(tf.nn.bias_add(conv2,conv2_biases))

        #第四层 最大池化层 10,10,16--->1,2,2,1--->5,5,16
        #池化层过滤器大小为2*2 移动步长为2 使用全0填充
        pool2 = tf.nn.max_pool(relu2,ksize = [1,2,2,1],strides=[1,2,2,1],padding=‘SAME‘)

        #第五层 全连接层
        fc1_weights = tf.get_variable("fc1_weights",[5*5*16,1024],initializer = tf.truncated_normal_initializer(stddev=0.1))
        fc1_biases = tf.get_variable("fc1_biases",[1024],initializer = tf.constant_initializer(0.1)) #[1,1024]
        pool2_vector = tf.reshape(pool2,[-1,5*5*16]) #特征向量扁平化 原始的每一张图变成了一行9×9*64列的向量
        fc1 = tf.nn.relu(tf.matmul(pool2_vector,fc1_weights)+fc1_biases)

        #为了减少过拟合 加入dropout层

        fc1_dropout = tf.nn.dropout(fc1,keep_prob)

        #第六层 全连接层
        #神经元节点数为1024  分类节点2
        fc2_weights = tf.get_variable("fc2_weights",[1024,2],initializer=tf.truncated_normal_initializer(stddev=0.1))
        fc2_biases = tf.get_variable("fc2_biases",[2],initializer = tf.constant_initializer(0.1))
        fc2 = tf.matmul(fc1_dropout,fc2_weights) + fc2_biases

        return fc2
    def lenet_loss(self,fc2,y1_,y2_,y3_,y4_,y5_):

        #第七层 输出层
        #softmax
        y1_conv = tf.nn.softmax(fc2)
        labels1=tf.one_hot(y1_,2)
        #定义交叉熵损失函数
        #cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv),reduction_indices=[1]))
        loss1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = y1_conv, labels =labels1))

        y2_conv = tf.nn.softmax(fc2)
        labels2=tf.one_hot(y2_,2)
        #定义交叉熵损失函数
        #cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv),reduction_indices=[1]))
        loss2 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = y2_conv, labels =labels2))

        y3_conv = tf.nn.softmax(fc2)
        labels3=tf.one_hot(y3_,2)
        #定义交叉熵损失函数
        #cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv),reduction_indices=[1]))
        loss3 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = y3_conv, labels =labels3))

        y4_conv = tf.nn.softmax(fc2)
        labels4=tf.one_hot(y4_,2)
        #定义交叉熵损失函数
        #cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv),reduction_indices=[1]))
        loss4 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = y4_conv, labels =labels4))

        y5_conv = tf.nn.softmax(fc2)
        labels5=tf.one_hot(y5_,2)
        #定义交叉熵损失函数
        #cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv),reduction_indices=[1]))
        loss5 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = y5_conv, labels =labels5))

        loss = (loss1 + loss2 + loss3 + loss4 + loss5)/5
        self.cost = loss
        return self.cost

    def lenet_optimer(self,loss):
        train_optimizer = tf.train.GradientDescentOptimizer(lr).minimize(loss)
        return train_optimizer

def train():
    image,label1,label2,label3,label4,label5=read_and_decode("./train.tfrecords")
    testimage,testlabel1,testlabel2,testlabel3,testlabel4,testlabel5=read_and_decode("./test.tfrecords")
    batch_image,batch_label1,batch_label2,batch_label3,batch_label4,batch_label5=get_batch(image,label1,label2,label3,label4,label5,batch_size=30,crop_size=32)
    testbatch_image,testbatch_label1,testbatch_label2,testbatch_label3,testbatch_label4,testbatch_label5=get_batch(testimage,testlabel1,testlabel2,testlabel3,testlabel4,testlabel5,batch_size=30,crop_size=32)
    #测试数据集

   #建立网络,训练所用
    x = tf.placeholder("float",shape=[None,32,32,3],name=‘x-input‘)
    y1_ = tf.placeholder("int32",shape=[None])
    y2_ = tf.placeholder("int32",shape=[None])
    y3_ = tf.placeholder("int32",shape=[None])
    y4_ = tf.placeholder("int32",shape=[None])
    y5_ = tf.placeholder("int32",shape=[None])

    keep_prob = tf.placeholder(tf.float32)

    net=network()
    #inf=net.buildnet(batch_image)
    inf = net.lenet(x,keep_prob)
    loss=net.lenet_loss(inf,y1_,y2_,y3_,y4_,y5_)  #计算loss
    opti=net.optimer(loss)  #梯度下降

    correct_prediction1 = tf.equal(tf.cast(tf.argmax(inf,1),tf.int32),testbatch_label1)
    accuracy1 = tf.reduce_mean(tf.cast(correct_prediction1,tf.float32))

    correct_prediction2 = tf.equal(tf.cast(tf.argmax(inf,1),tf.int32),testbatch_label2)
    accuracy2 = tf.reduce_mean(tf.cast(correct_prediction2,tf.float32))

    correct_prediction3 = tf.equal(tf.cast(tf.argmax(inf,1),tf.int32),testbatch_label3)
    accuracy3 = tf.reduce_mean(tf.cast(correct_prediction3,tf.float32))

    correct_prediction4 = tf.equal(tf.cast(tf.argmax(inf,1),tf.int32),testbatch_label4)
    accuracy4 = tf.reduce_mean(tf.cast(correct_prediction4,tf.float32))

    correct_prediction5 = tf.equal(tf.cast(tf.argmax(inf,1),tf.int32),testbatch_label5)
    accuracy5 = tf.reduce_mean(tf.cast(correct_prediction5,tf.float32))

    accuracy = (accuracy1+accuracy2+accuracy3+accuracy4+accuracy5)/5

    init=tf.global_variables_initializer()
    with tf.Session() as session:
        with tf.device("/gpu:0"):
            session.run(init)
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)
            max_iter=10000
            iter=0
            if os.path.exists(os.path.join("model",‘model.ckpt‘)) is True:
                tf.train.Saver(max_to_keep=None).restore(session, os.path.join("model",‘model.ckpt‘))
            while iter<max_iter:
                #loss_np,_,label_np,image_np,inf_np=session.run([loss,opti,batch_image,batch_label,inf])
                b_batch_image,b_batch_label1,b_batch_label2,b_batch_label3,b_batch_label4,b_batch_label5 = session.run([batch_image,batch_label1,batch_label2,batch_label3,batch_label4,batch_label5])
                testb_batch_image,testb_batch_label1,testb_batch_label2,testb_batch_label3,testb_batch_label4,testb_batch_label5 = session.run([testbatch_image,testbatch_label1,testbatch_label2,testbatch_label3,testbatch_label4,testbatch_label5])
                loss_np,_=session.run([loss,opti],feed_dict={x:b_batch_image,y1_:b_batch_label1,y2_:b_batch_label2,y3_:b_batch_label3,y4_:b_batch_label4,y5_:b_batch_label5,keep_prob:0.6})
                if iter%50==0:
                    print ‘trainloss:‘,loss_np
                if iter%500==0:
                    #accuracy_np = session.run([accuracy])
                    accuracy_np = session.run([accuracy],feed_dict={x:testb_batch_image,y1_:testb_batch_label1,y2_:testb_batch_label2,y3_:testb_batch_label3,y4_:testb_batch_label4,y5_:testb_batch_label5,keep_prob:1.0})
                    print ‘测试集准确率为:‘,accuracy_np
                iter+=1
            coord.request_stop()#queue需要关闭,否则报错
            coord.join(threads)
if __name__ == ‘__main__‘:
    train()

原文地址:https://www.cnblogs.com/cnugis/p/8428277.html

时间: 2024-08-26 03:18:45

Tensorflow学习教程------lenet多标签分类的相关文章

Tensorflow学习教程------普通神经网络对mnist数据集分类

首先是不含隐层的神经网络, 输入层是784个神经元 输出层是10个神经元 代码如下 #coding:utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist = input_data.read_data_sets("MNIST_data", one_hot=True) #每个批次的大小 batch_size = 100 #计算一共有多少个批次

Tensorflow学习教程------tfrecords数据格式生成与读取

首先是生成tfrecords格式的数据,具体代码如下: #coding:utf-8 import os import tensorflow as tf from PIL import Image cwd = os.getcwd() ''' 此处我加载的数据目录如下: bt -- 14018.jpg 14019.jpg 14020.jpg nbt -- 1_ddd.jpg 1_dsdfs.jpg 1_dfd.jpg 这里的bt nbt 就是类别,也就是代码中的classes ''' writer

Tensorflow学习教程------利用卷积神经网络对mnist数据集进行分类_训练模型

原理就不多讲了,直接上代码,有详细注释. #coding:utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data',one_hot=True) #每个批次的大小 batch_size = 100 n_batch = mnist.train._num_examples // batch_

第十二天学习:html标签分类

关键字:html标签分类 晨跑计划: 学习计划: 对html标签进行更详细的分类 学习记录: 扩展阅读: 问题的记录与解答:

几种分类问题的区别:多类分类,多标签分类,多示例学习,多任务学习

多类分类(Multiclass Classification) 一个样本属于且只属于多个类中的一个,一个样本只能属于一个类,不同类之间是互斥的. 典型方法: One-vs-All or One-vs.-rest: 将多类问题分成N个二类分类问题,训练N个二类分类器,对第i个类来说,所有属于第i个类的样本为正(positive)样本,其他样本为负(negative)样本,每个二类分类器将属于i类的样本从其他类中分离出来. one-vs-one or All-vs-All: 训练出N(N-1)个二类

Deep Learning 十_深度学习UFLDL教程:Convolution and Pooling_exercise(斯坦福大学深度学习教程)

前言 理论知识:UFLDL教程和http://www.cnblogs.com/tornadomeet/archive/2013/04/09/3009830.html 实验环境:win7, matlab2015b,16G内存,2T机械硬盘 实验内容:Exercise:Convolution and Pooling.从2000张64*64的RGB图片(它是the STL10 Dataset的一个子集)中提取特征作为训练数据集,训练softmax分类器,然后从3200张64*64的RGB图片(它是th

(转)干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码)

干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码) 该博客来源自:https://mp.weixin.qq.com/s?__biz=MzA4NzE1NzYyMw==&mid=2247492203&idx=5&sn=3020c3a43bd4dd678782d8aa24996745&chksm=903f1c73a74895652ee688d070fd807771e3fe6a8947f77f3a15a44a65557da0313ac5ad592c

Makefile学习教程 跟我一起写 Makefile

https://github.com/zhangliyong/myknowledge/blob/master/Makefile%E5%AD%A6%E4%B9%A0%E6%95%99%E7%A8%8B%20%E8%B7%9F%E6%88%91%E4%B8%80%E8%B5%B7%E5%86%99%20Makefile.wiki ++ Makefile学习教程 跟我一起写 Makefile 0 Makefile概述 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Wi

tensorflow实现基于LSTM的文本分类方法

tensorflow实现基于LSTM的文本分类方法 作者:u010223750 引言 学习一段时间的tensor flow之后,想找个项目试试手,然后想起了之前在看Theano教程中的一个文本分类的实例,这个星期就用tensorflow实现了一下,感觉和之前使用的theano还是有很大的区别,有必要总结mark一下 模型说明 这个分类的模型其实也是很简单,主要就是一个单层的LSTM模型,当然也可以实现多层的模型,多层的模型使用Tensorflow尤其简单,下面是这个模型的图  简单解释一下这个图