这个代码与前面代码类似,只是用的模型不一样,这里运用的是VGG-9模型,详情注释请看 深度学习之cifar-10④——十分类(cifar_10_Alexnet)
import tensorflow as tf
import numpy as np
import pickle
import os
cifar_dir = '../../datas/cifar-10-batches-py'
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
self.lables = np.hstack(all_labels)
print(self.data.shape)
print(self.lables.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.lables = self.lables[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_lables = self.lables[self.indicator:end_indicator]
self.indicator = end_indicator
return batch_data,batch_lables
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 = tf.layers.conv2d(x_img,8,(3,3),padding='same',activation=tf.nn.relu)
pooling1 = tf.layers.max_pooling2d(conv1,(2,2),(2,2))
conv2 = tf.layers.conv2d(pooling1,16,(3,3),padding='same',activation=tf.nn.relu)
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)
pooling3 = tf.layers.max_pooling2d(conv3_2,(2,2),(2,2))
conv4_1 = tf.layers.conv2d(pooling3,64,(3,3),padding='same',activation=tf.nn.relu)
conv4_2 = tf.layers.conv2d(conv4_1,64,(3,3),padding='same',activation=tf.nn.relu)
pooling4 = tf.layers.max_pooling2d(conv4_2,(2,2),(2,2))
flatten = tf.layers.flatten(pooling4)
fc7 = tf.layers.dense(flatten,64,activation=tf.nn.tanh)
fc8 = tf.layers.dense(fc7,64,activation=tf.nn.tanh)
a = tf.layers.dense(fc8,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.float32))
sess = tf.Session()
sess.run(tf.global_variables_initializer())
batch_size = 20
for i in range(1,20001):
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 % 1000 == 0:
print(i,c,a)
if i % 5000 == 0:
test_data = Cifar_Data(test_filenames, False)
all_acc = []
for k in range(1,201):
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))
效果
1000 1.386991 0.55
2000 0.9123479 0.6
3000 1.5659837 0.45
4000 1.2288189 0.6
5000 0.83473223 0.7
(10000, 3072)
(10000,)
0.61325
6000 1.0807028 0.55
7000 0.8725523 0.7
8000 1.0735404 0.65
9000 1.401082 0.6
10000 0.85721874 0.65
(10000, 3072)
(10000,)
0.66
11000 0.6657385 0.8
12000 0.78654975 0.7
13000 1.1124375 0.45
14000 1.0252459 0.65
15000 0.904095 0.65
(10000, 3072)
(10000,)
0.6795
16000 0.59674776 0.9
17000 0.560689 0.75
18000 0.56586057 0.85
19000 0.9847535 0.7
20000 0.44856086 0.9
(10000, 3072)
(10000,)
0.68574995