此代码在cifar_10的基础上用Alexnet-8进行卷积池化全连接来提高准确率
import tensorflow as tf
import os
import pickle
import numpy as np
# 路径
cifar_dir = '../../datas/cifar-10-batches-py'
# 查看目录
print(os.listdir(cifar_dir))
# 获取特征,标签
def load_data(filename):
with open(filename,'rb') as f:
data = pickle.load(f,encoding='bytes')
return data[b'data'],data[b'labels']
# 封装
class Cifar_Data:
# 构造方法
def __init__(self,filenames,need_shuffle):
all_data = []
all_labels = []
# 循环读取文件
for filename in filenames:
data,labels = load_data(filename)
all_data.append(data)
all_labels.append(labels)
self.data = np.vstack(all_data)
self.data = self.data / 127.5 - 1 #让标签在 -1~1之间
self.labels = np.hstack(all_labels)
print(self.data.shape)
print(self.labels.shape)
# 次数
self.num_examples = self.data.shape[0]
self.indicator = 0
self.need_shuffle = need_shuffle
# 洗牌
if self.need_shuffle:
self.shuffle_data()
# 随机洗牌
def shuffle_data(self):
p = np.random.permutation(self.num_examples)
self.data = self.data[p]
self.labels = self.labels[p]
# 批次
def next_batch(self,batch_size):
end_indicator = self.indicator + batch_size
if end_indicator > self.num_examples:
if self.need_shuffle:
self.shuffle_data()
self.indicator = 0
end_indicator = batch_size
else:
raise Exception('没有更多例子')
if end_indicator > self.num_examples:
raise Exception('批次大于所有例子')
batch_data = self.data[self.indicator:end_indicator]
batch_labels = self.labels[self.indicator:end_indicator]
self.indicator = end_indicator
return batch_data,batch_labels
# 获取文件
train_filenames = [os.path.join(cifar_dir,'data_batch_%d'%i) for i in range(1,6)]
test_filenames = [os.path.join(cifar_dir,'test_batch')]
# 实例化对象
train_data = Cifar_Data(train_filenames,True)
test_data = Cifar_Data(test_filenames,False)
# 站位
x = tf.placeholder(tf.float32,[None,3072])
y = tf.placeholder(tf.int64,[None,])
# 生成图片
x_img = tf.reshape(x,[-1,3,32,32])
# 32*32
x_img = tf.transpose(x_img,perm=[0,2,3,1])
# 卷积 # conv1: 神经元图, feature_map, 输出图像
conv1 = tf.layers.conv2d(x_img,32,(3,3),padding='same',activation=tf.nn.relu)
# 16 * 16
pooling1 = tf.layers.max_pooling2d(conv1,(2,2),(2,2))
conv2 = tf.layers.conv2d(pooling1,32,(3,3),padding='same',activation=tf.nn.relu)
# 8 * 8
pooling2 = tf.layers.max_pooling2d(conv2,(2,2),(2,2))
conv3_1 = tf.layers.conv2d(pooling2,32,(3,3),padding='same',activation=tf.nn.relu)
conv3_2 = tf.layers.conv2d(conv3_1,32,(3,3),padding='same',activation=tf.nn.relu)
conv3_3 = tf.layers.conv2d(conv3_2,32,(3,3),padding='same',activation=tf.nn.relu)
# 4 * 4 * 32
pooling3 = tf.layers.max_pooling2d(conv3_3,(2,2),(2,2))
# [None, 4 * 4 * 32]
flatten = tf.layers.flatten(pooling3)
# FC
fc6 = tf.layers.dense(flatten,64,activation=tf.nn.tanh)
fc7 = tf.layers.dense(fc6,64,activation=tf.nn.tanh)
a = tf.layers.dense(fc7,10)
# 代价
cost = tf.losses.sparse_softmax_cross_entropy(y,a)
# 优化器
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
# 准确率
pre = tf.argmax(a,1)
accuracy = tf.reduce_mean(tf.cast(tf.equal(pre,y),tf.float64))
# 开启会话
sess = tf.Session()
sess.run(tf.global_variables_initializer())
batch_size = 20
# 开始训练
for i in range(1,10001):
batch_x,batch_y = train_data.next_batch(batch_size)
c,a,_ = sess.run([cost,accuracy,optimizer],feed_dict={x:batch_x,y:batch_y})
if i % 500 == 0:
print(i,c,a)
if i % 5000 == 0:
all_acc = []
for k in range(1,101):
batch_x1,batch_y1 = test_data.next_batch(batch_size)
a1 = sess.run(accuracy,feed_dict={x:batch_x1,y:batch_y1})
all_acc.append(a1)
print(np.mean(all_acc))
效果
500 1.1759748 0.55
1000 1.3648958 0.5
1500 1.1858895 0.55
2000 0.9293791 0.7
2500 1.6354368 0.45
3000 0.7088274 0.75
3500 1.0402097 0.65
4000 1.170832 0.55
4500 0.6753399 0.8
5000 1.1852739 0.6
0.8499999999999995
5500 1.5266346 0.5
6000 0.6258583 0.8
6500 0.96164685 0.65
7000 0.80438775 0.85
7500 0.89651334 0.7
8000 0.7761994 0.7
8500 0.41731387 0.85
9000 0.4029478 0.8
9500 0.55531096 0.8
10000 0.47050935 0.95
0.7999999999999998