1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| import tensorflow as tf import numpy as np
def weight_variable(shape): # 产生截断正态分布随机数 initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial)
def bias_variable(shape): # 误差初始值定义为0.1 return tf.Variable(tf.constant(0.1, shape=shape))
def MLP(_IMAGE_SIZE=28, _IMAGE_CHANNELS = 1,_HIDDEN1 = 128,_HIDDEN2 = 64,_NUM_CLASSES = 10):
image_pixels = _IMAGE_SIZE * _IMAGE_SIZE * _IMAGE_CHANNELS
with tf.name_scope('main_params'): x = tf.placeholder(tf.float32, shape=[None, image_pixels], name='Input') y = tf.placeholder(tf.float32, shape=[None, _NUM_CLASSES], name='Output')
with tf.variable_scope('hidden1'): w_h = weight_variable([image_pixels, _HIDDEN1]) h = tf.nn.relu(tf.matmul(x, w_h)) # hidden layer1
with tf.variable_scope('hidden2'): w_h2 = weight_variable([_HIDDEN1, _HIDDEN2]) h2 = tf.nn.relu(tf.matmul(h, w_h2)) # hidden layer2
with tf.variable_scope('out'): w_out = weight_variable([_HIDDEN2, _NUM_CLASSES]) logit = tf.matmul(h2, w_out) # 输出层
return x, y, logit
def CNN(_IMAGE_SIZE = 32,_IMAGE_CHANNELS = 3,_NUM_CLASSES = 10):
with tf.name_scope('main_params'): x = tf.placeholder(tf.float32, shape=[None, _IMAGE_SIZE * _IMAGE_SIZE * _IMAGE_CHANNELS], name='Input') y = tf.placeholder(tf.float32, shape=[None, _NUM_CLASSES], name='Output') x_image = tf.reshape(x, [-1, _IMAGE_SIZE, _IMAGE_SIZE, _IMAGE_CHANNELS], name='images')
with tf.variable_scope('conv1') as scope: conv = tf.layers.conv2d( inputs=x_image, filters=32, kernel_size=[3, 3], padding='SAME', activation=tf.nn.relu ) pool = tf.layers.max_pooling2d(conv, pool_size=[2, 2], strides=2, padding='SAME')
with tf.variable_scope('conv2') as scope: conv = tf.layers.conv2d( inputs=pool, filters=64, kernel_size=[3, 3], padding='SAME', activation=tf.nn.relu ) pool = tf.layers.max_pooling2d(conv, pool_size=[2, 2], strides=2, padding='SAME')
with tf.variable_scope('fully_connected') as scope: flat = tf.layers.flatten(pool) # flat = tf.reshape(pool, [-1, 8 * 8 * 64]) # flat = tf.reshape(pool, [-1, 7 * 7 * 64]) fc = tf.layers.dense(inputs=flat, units=1500, activation=tf.nn.relu) drop = tf.layers.dropout(fc, rate=0.5) logit = tf.layers.dense(inputs=drop, units=_NUM_CLASSES, name=scope.name)
return x, y, logit
def LeNet(_IMAGE_SIZE = 32,_IMAGE_CHANNELS = 3,_NUM_CLASSES = 10):
with tf.name_scope('main_params'): x = tf.placeholder(tf.float32, shape=[None, _IMAGE_SIZE * _IMAGE_SIZE * _IMAGE_CHANNELS], name='Input') y = tf.placeholder(tf.float32, shape=[None, _NUM_CLASSES], name='Output') x_image = tf.reshape(x, [-1, _IMAGE_SIZE, _IMAGE_SIZE, _IMAGE_CHANNELS], name='images')
with tf.variable_scope('layer1-conv1'): W_conv1 = weight_variable([5, 5, 1, 6]) b_conv1 = bias_variable([6])
conv1 = tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], padding='VALID') relu1 = tf.nn.relu(tf.nn.bias_add(conv1, b_conv1))
with tf.name_scope('layer2-pool1'): pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
with tf.variable_scope('layer3-conv2'): W_conv2 = weight_variable([5, 5, 6, 16]) b_conv2 = bias_variable([16]) conv2 = tf.nn.conv2d(pool1, W_conv2, strides=[1, 1, 1, 1], padding='VALID') relu2 = tf.nn.relu(tf.nn.bias_add(conv2, b_conv2))
with tf.name_scope('layer4-pool2'): pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
with tf.variable_scope('layer5-fc1'): W_fc1 = weight_variable([400, 120]) b_fc1 = bias_variable([120])
flat = tf.layers.flatten(pool2) # flat = tf.reshape(pool2, [-1, 5 * 5 * 16])
fc1 = tf.nn.relu(tf.matmul(flat, W_fc1) + b_fc1)
with tf.variable_scope('layer6-fc2'): W_fc2 = weight_variable([120, 84]) b_fc2 = bias_variable([84])
fc2 = tf.matmul(fc1, W_fc2) + b_fc2
with tf.variable_scope('layer7-fc3'): W_fc3 = weight_variable([84, 10]) b_fc3 = bias_variable([10])
logit = tf.matmul(fc2, W_fc3) + b_fc3
return x,y,logit
def loss(logit, labels):
with tf.name_scope('cross_entropy'): # Operation to determine the cross entropy between logits and labels loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2( logits=logit, labels=labels, name='cross_entropy'))
return loss
def training(loss, learning_rate):
global_step = tf.Variable(0, name='global_step', trainable=False)
# 优化器 optimizer = tf.train.AdamOptimizer(learning_rate, 0.9)
# 参数更新 train_op = optimizer.minimize(loss, global_step=global_step)
return train_op,global_step
def evaluation(logits, labels):
with tf.name_scope('Accuracy'): # Operation comparing prediction with true label correct_prediction = tf.equal(tf.argmax(logits,1), tf.argmax(labels,1))
# Operation calculating the accuracy of the predictions accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# Summary operation for the accuracy # tf.summary.scalar('train_accuracy', accuracy)
return accuracy
def lr(epoch): learning_rate = 1e-3 if epoch > 80: learning_rate *= 0.5e-3 elif epoch > 60: learning_rate *= 1e-3 elif epoch > 40: learning_rate *= 1e-2 elif epoch > 20: learning_rate *= 1e-1 return learning_rate
|