Getting depth study of the third chapter a fashion-mnist neural network image recognition network

Disclaimer: All belongs to the world proletariat all https://blog.csdn.net/qq_41776781/article/details/87884312

I read the foreword is introduced if train the neural network to do image recognition, just before a useful convolution neural networks in 21 deep learning project to do image recognition so tell us about it before I write! ! ! ! ! The main idea of ​​the book is to introduce the use of neural networks to do before mlp mlp but now both in the Image Recognition or GANs, the convolution neural networks are not good, gradually eliminated 

 

I ran into a problem before sharing the code is not a lie we look at the differences between Image Recognition and GANs

In the Image Recognition data softmax last layer output layer we find the greatest probability data and set it to 1

However, when doing data GANs cross entropy in the last layer output from the recognizer, we want a real data are all generated image are all 0

Remember the difference between the two methods! ! ! ! ! Less point the wrong way to go

# Image Recognition 交叉熵使用的两个参数是 真实标签和最后全连接层输出的数据


loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))


# GANs d_loss_real是真实数据的交叉熵 d_loss_fake是输入的是生成数据的交叉熵

d_loss_real = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=D_real_logits, labels=tf.ones_like(D_real)))



d_loss_fake = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=D_fake_logits, labels=tf.zeros_like(D_fake)))

 

 

For this fashion mnist image recognition program,

First, the operation of each layer is defined W (weight) and B (bias),

The image is then the convolution operation, and to make use relu activation function nonlinear data conversion

And then the maximum cell layer is 2 * 2

Finally, a full program of the connection layer data is divided into 10

 

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data


def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)


def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)


def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')


def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1], padding='SAME')

if __name__ == '__main__':
    # 读入数据
    mnist = input_data.read_data_sets("../../Dataset/fashion-mnist", one_hot=True)
    # x为训练图像的占位符、y_为训练图像标签的占位符
    x = tf.placeholder(tf.float32, [None, 784])
    y_ = tf.placeholder(tf.float32, [None, 10])

    # 将单张图片从784维向量重新还原为28x28的矩阵图片
    x_image = tf.reshape(x, [-1, 28, 28, 1])

    '''
    经过实验可以知道卷积运算的时候 
    卷积核的high width channel是能够随意修改的 
    但是最后表示经过该层之后神经元的输出数目 其要和bias以及以及下一层的卷积层通道数相对应即可
    最后重要的是tf.nn.conv2d第三个参数是不能随便修改的 不然会导致维度不对
    '''
    W_conv1 = weight_variable([6, 6, 1, 64])
    b_conv1 = bias_variable([64])
    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
    h_pool1 = max_pool_2x2(h_conv1)

    # 第二层卷积层
    W_conv2 = weight_variable([5, 5, 64, 128])
    b_conv2 = bias_variable([128])
    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
    h_pool2 = max_pool_2x2(h_conv2)

    # 全连接层,输出为1024维的向量
    W_fc1 = weight_variable([7 * 7 * 128, 1024])
    b_fc1 = bias_variable([1024])
    h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 128])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
    # 使用Dropout,keep_prob是一个占位符,训练时为0.5,测试时为1
    keep_prob = tf.placeholder(tf.float32)
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    # 把1024维的向量转换成10维,对应10个类别
    W_fc2 = weight_variable([1024, 10])
    b_fc2 = bias_variable([10])
    y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2

    # 我们不采用先Softmax再计算交叉熵的方法,而是直接用tf.nn.softmax_cross_entropy_with_logits直接计算
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
    # 同样定义train_step
    optimizer = tf.train.AdamOptimizer(1e-4).minimize(loss)

    # 定义测试的准确率
    correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))

    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

    #  定义保存相关数据文件的模型
    saver = tf.train.Saver(max_to_keep = 5)

    # 创建Session和变量初始化
    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    choose_way_train = "train"
    if choose_way_train == "train":
        # 训练20000步
        for i in range(20000):
            batch = mnist.train.next_batch(100)
            # 程序有个问题 无法确定程序进行到哪一步了

            '''
            用于测试单个程序是否能够跑的通
            train_accuracy = accuracy.eval(feed_dict={
                x: batch[0], y_: batch[1], keep_prob: 1.0})
            print("step %d, training accuracy %g" % (i, train_accuracy))
            '''

            #赋值的时候 等号前面的和等号后面的不能一样
            train_accuracy, _, val_loss= sess.run([accuracy, optimizer, loss],feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
            if i %100 ==0:
                print("step %d, val_loss %g" % (i, val_loss))
            if i % 1000 == 0:

                global_step = i
                print("i have save kpt/mnist_%3d.ckpt"%global_step)
                saver.save(sess, 'ckpt/mnist_%3d.ckpt'%global_step)
    else:
        model_file = tf.train.latest_checkpoint('ckpt/')
        saver.restore(sess, model_file)
        for i in range(1000):
            batch = mnist.test.next_batch(50)
            if i%100 ==0:
                val_acc, val_loss = sess.run([accuracy,loss],feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
                # 训练结束后报告在测试集上的准确度
                print("test accuracy %f"% val_acc)

The results: Using the GPU, then less than five minutes already finished training accuracy rate is 99.7% so now mnist almost too simple not in the papers Interested students can join a drawing program In this program the results drawn

'''
step 19100, val_loss 0.00158436
step 19200, val_loss 0.0086459
step 19300, val_loss 0.0232048
step 19400, val_loss 0.00513635
step 19500, val_loss 0.0242389
step 19600, val_loss 0.0272156
step 19700, val_loss 0.00483749
step 19800, val_loss 0.00234459
step 19900, val_loss 0.00342627
'''

 

Guess you like

Origin blog.csdn.net/qq_41776781/article/details/87884312