本文介绍三种卷积神经网络结构,分别是CNN、ResNet、DenseNet,以及利用python和tensorflow分别实现这三种神经网络结构,在MNIST手写数字集实现图像分类,
一. CNN(卷积神经网络)
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets('MNIST_data', one_hot=True) #import MNIST
print("start")
inputs_ = tf.placeholder(tf.float32, (None, 28, 28, 1), name='inputs_')#train data
y = tf.placeholder(tf.float32, [None, 10])#train label
sess = tf.InteractiveSession()
def weight_variable(shape):
#initial variable
initial = tf.truncated_normal(shape, mean=0,stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
x = tf.reshape(inputs_, (-1,28,28,1)) #x为-1*28*28*1
conv1 = tf.layers.conv2d(x, 32, (3,3), padding='same', activation=tf.nn.relu)
pooling1 = tf.layers.max_pooling2d(conv1, (2,2), (2,2), padding='same')
#conv1 turn to 14*14*32
conv2 = tf.layers.conv2d(pooling1, 64, (3,3), padding='same', activation=tf.nn.relu)
pooling2 = tf.layers.max_pooling2d(conv2, (2,2), (2,2), padding='same')
#7*7*64
conv3 = tf.layers.conv2d(pooling2, 64, (3,3), padding='same', activation=tf.nn.relu)
pooling3 = tf.layers.max_pooling2d(conv3, (2,2), (2,2), padding='same')
#4*4*64
#fully connected network
flat = tf.reshape(pooling3, [-1,4*4*64]) #turn to 1 dim
w_fc1 = weight_variable([4 * 4 *64, 1024])
b_fc1 = bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(flat, w_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
w_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, w_fc2) + b_fc2
prediction = tf.nn.softmax(y_conv,name='prediction') #softmax返回一组概率向量
#建立损失函数,在这里采用交叉熵函数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_conv))
#最小化损失
train_step = tf.train.AdamOptimizer(1e-3).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y,1)) #返回bool型(结果,标签)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #求平均,计算正确率(tf.cast:bool-float转换)
#初始化变量
#sess = tf.Session()
sess.run(tf.global_variables_initializer())
pos = 0
trains = []
print("houyu")
for i in range(6001):
batch = mnist.train.next_batch(100)
images = batch[0].reshape((-1,28,28,1)) #像素归一化为[0,1]之间的值
#print("images shape:",np.shape(images))
labels = batch[1]