暂且贴出代码记录下,省的弄丢了,后面我有时间再修改,哈哈
分析:
1. reduce_sum/reduce_mean
表示在某一维度进行reduce,就是计算压缩的意思。所以reduce_sum就是计算沿某一维度计算均值,使其变成1。tf.reduce_sum(Tensor, reduction_indices = [1])) 就是让沿第二维求和,这样就把一张图片784个值的每个值都与计算得到的784个值一一对应计算 −∑iy′ilog(yi), 当然了,最后还要对一个batch的所有100张图片计算平均交叉熵。
2. argmax
表示求某一维的最大值。很简单吧。这里 tf.argmax(y_, 1) 就是查看预测的最大值呗。因为预测的label是以(100, 10)显示的。对于每一张图,有10个数,数字最大的则为其预测标签。
3. cast
强制转换数据类型,没啥好说的。
4tf.truncated_normal与tf.random_normal的区别
作为tensorflow里的正态分布产生函数,这两个函数的输入参数几乎完全一致,
而其主要的区别在于,tf.truncated_normal的输出如字面意思是截断的,而截断的标准是2倍的stddev。
import tensorflow as tf
import os
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
'''
#当制作数据集取消注释,将读数据给注释掉
cwd='/home/xiaorun/deeplerning/classification_keras/images/'
classes={'not_santa','santa'}
writer= tf.python_io.TFRecordWriter("santa_class.tfrecords")
for index,name in enumerate(classes):
class_path=cwd+name+'/'
for img_name in os.listdir(class_path):
img_path=class_path+img_name
img=Image.open(img_path)
img= img.resize((224,224))
img_raw=img.tobytes()
#plt.imshow(img) # if you want to check you image,please delete '#'
#plt.show()
example = tf.train.Example(features=tf.train.Features(feature={
"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
}))
writer.write(example.SerializeToString())
writer.close()
'''
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={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [224, 224, 3])
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
print(img,label)
return img, label
batch_size = 8
#initial weights
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev = 0.1)
return tf.Variable(initial)
#initial bias
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
#convolution layer
def conv2d(x,W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
#max_pool layer
def max_pool_4x4(x):
return tf.nn.max_pool(x, ksize=[1,4,4,1], strides=[1,4,4,1], padding='SAME')
x = tf.placeholder(tf.float32, [batch_size,224,224,3])
y_ = tf.placeholder(tf.float32, [batch_size,1])
#first convolution and max_pool layer
W_conv1 = weight_variable([5,5,3,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1)
h_pool1 = max_pool_4x4(h_conv1)
#second convolution and max_pool layer
W_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_4x4(h_conv2)
#变成全连接层,用一个MLP处理
reshape = tf.reshape(h_pool2,[batch_size, -1])
dim = reshape.get_shape()[1].value
W_fc1 = weight_variable([dim, 1024])
b_fc1 = bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(reshape, W_fc1) + b_fc1)
#dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024,2])
b_fc2 = bias_variable([2])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
#损失函数及优化算法
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
image, label = read_and_decode("santa_class.tfrecords")
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
coord=tf.train.Coordinator()
threads= tf.train.start_queue_runners(coord=coord)
example = np.zeros((batch_size,224,224,3))
l = np.zeros((batch_size,1))
saver=tf.train.Saver()
try:
for i in range(25):
print(i)
for epoch in range(batch_size):
example[epoch], l[epoch] = sess.run([image,label])#在会话中取出image和label
train_step.run(feed_dict={x: example, y_: l, keep_prob: 0.5})
print('hello')
print(accuracy.eval(feed_dict={x: example, y_: l, keep_prob: 0.5})) #eval函数类似于重新run一遍,验证,同时修正
saver.save(sess, './')
except tf.errors.OutOfRangeError:
print('done!')
finally:
coord.request_stop()
coord.join(threads)