import tensorflow as tf
import numpy as np
import os
#######################################################################
# 读取数据和标签
finaldata = np.load("data/单张一百分类训练集.npy")
finalval = np.load("data/单张一百分类测试集.npy")
np.random.seed(1000)
np.random.shuffle(finaldata)
np.random.shuffle(finalval)
########################################################################
# 超参数
batch_size = 100
learning_rate = 0.001
epochs = 10000
#######################################################################
# 用numpy进行one_hot编码
def np_one_hot(labels):
n_labels = np.max(labels) + 1
one_hot = np.eye(n_labels)[labels]
return one_hot
######################################################################
# 将数据与标签分离,并对标签进行one_hot编码
def input_data(finaldata):
data = []
label = []
for i in range(len(finaldata)):
data.append(finaldata[i][0])
label.append(finaldata[i][1])
data = np.array(data)
data = data.reshape(-1,16,16,1)
label = np.array(label)
label = np_one_hot(label)
return data, label
# 训练集
data, label = input_data(finaldata)
# 测试集
data_val, label_val = input_data(finalval)
###########################################################################
# 排除管径对分类的影响
ones = np.ones((len(data),10,16,1))
data[:,6::,:,:] = ones
val_ones = np.ones((len(data_val),10,16,1))
data_val[:,6::,:,:] = val_ones
data_val = data_val[0:100,:,:,:]
label_val = label_val[0:100,:]
###########################################################################
# 得到batch个数据
# 有数据有label时
def get_batch(inputs=None, labels=None, batch_size=None, shuffle=True):
assert len(inputs) == len(labels)
indices = np.arange(len(inputs))
if shuffle:
np.random.shuffle(indices)
# start_idx为batch_size个数
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 = indices[start_idx:start_idx + batch_size]
yield inputs[excerpt] , labels[excerpt]
#####################################################################
def inference(images):
# conv1 16x16x1->16x16x96
with tf.variable_scope("conv1") as scope:
weights = tf.get_variable("weights",
shape = [3, 3, 1, 96],
dtype = tf.float32,
initializer = tf.truncated_normal_initializer(stddev=0.05,dtype=tf.float32))
biases = tf.get_variable("biases",
shape = [96],
dtype = tf.float32,
initializer = tf.constant_initializer(0.0))
conv = tf.nn.conv2d(images, weights, strides=[1, 1, 1, 1], padding="SAME")
pre_activation = tf.nn.bias_add(conv, biases)
conv1 = tf.nn.relu(pre_activation, name=scope.name)
# pool1 and norm1 16x16x96->8x8x96
with tf.variable_scope("pooling1_lrn") as scope:
pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2 ,1],
padding="SAME", name="pooing1")
norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0,
beta=0.75, name="norm1")
# conv2 8x8x96->8x8x64
with tf.variable_scope("conv2") as scope:
weights = tf.get_variable("weights",
shape = [3, 3, 96, 64],
dtype = tf.float32,
initializer = tf.truncated_normal_initializer(stddev=0.05,dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[64],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
conv = tf.nn.conv2d(norm1, weights, strides=[1,1,1,1],padding='SAME')
pre_activation = tf.nn.bias_add(conv, biases)
conv2 = tf.nn.relu(pre_activation, name='conv2')
#pool2 and norm2 8x8x64->4x4x64
with tf.variable_scope('pooling2_lrn') as scope:
norm2 = tf.nn.lrn(conv2, depth_radius=4, bias=1.0, alpha=0.001/9.0,
beta=0.75,name='norm2')
pool2 = tf.nn.max_pool(norm2, ksize=[1,3,3,1], strides=[1,1,1,1],
padding='SAME',name='pooling2')
print(pool2.shape)
# fc3
with tf.variable_scope("fc3") as scope:
reshape = tf.reshape(pool2, shape=[batch_size, -1])
dim = reshape.get_shape()[1].value
weights = tf.get_variable("weights",
shape=[dim, 512],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.004,dtype=tf.float32))
biases = tf.get_variable("biases",
shape=[512],
dtype = tf.float32,
initializer = tf.constant_initializer(0.1) )
fc3 = tf.nn.relu(tf.matmul(reshape,weights) + biases, name=scope.name)
# fc4
with tf.variable_scope('fc4') as scope:
weights = tf.get_variable('weights',
shape=[512,256],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.004,dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[256],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
local4 = tf.nn.relu(tf.matmul(fc3, weights) + biases, name='fc4')
# softmax
with tf.variable_scope('softmax_linear') as scope:
weights = tf.get_variable('softmax_linear',
shape=[256, 100],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.004,dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[100],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
softmax_linear = tf.add(tf.matmul(local4, weights), biases, name='softmax_linear')
return softmax_linear
######################################################################
# loss函数
def losses(logits, labels):
with tf.variable_scope('loss') as scope:
# to use this loss fuction, one-hot encoding is needed!
cross_entropy = tf.nn.softmax_cross_entropy_with_logits\
(logits=logits, labels=labels, name='xentropy_per_example')
loss = tf.reduce_mean(cross_entropy, name='loss')
if not os.path.exists('loss'):
os.makedirs('loss')
tf.summary.scalar(scope.name+'/loss', loss)
return loss
###################################################################################
images = tf.placeholder(tf.float32,[batch_size,16, 16, 1])
labels = tf.placeholder(tf.float32,[batch_size,100])
####################################################################################
# 训练模型
def train():
my_global_step = tf.Variable(0, name='global_step', trainable=False)
logits = inference(images)
prediction = tf.nn.softmax(logits)
correct_prediction = tf.equal(tf.argmax(prediction,1),tf.argmax(labels,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
loss = losses(logits, labels)
train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step = my_global_step)
saver = tf.train.Saver(tf.global_variables())
summary_op = tf.summary.merge_all()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
if not os.path.exists('logs'):
os.makedirs('logs')
summary_writer = tf.summary.FileWriter('logs',sess.graph)
for e in range(epochs):
for data_batch,label_batch in get_batch(data,label,batch_size):
_, loss_value ,train_accuracy_value= sess.run([train_op, loss, accuracy], feed_dict={images:data_batch,labels:label_batch})
test_accuraccy_value = sess.run(accuracy,feed_dict={images:data_val,labels:label_val})
print("e: %d, loss: %.4f, train_accuracy: %.4f, test_accuracy: %.4f" % (e, loss_value, train_accuracy_value,test_accuraccy_value))
if e % 10 ==0:
summary_str = sess.run(summary_op, feed_dict={images:data_batch,labels:label_batch})
summary_writer.add_summary(summary_str, e)
saver.save(sess,"checkpoints/model.ckpt", global_step=e)
sess.close()
train()