深度学习之CNN神经网络图像分类cifar10数据集

使用Alexnet模型做cifar10分类

import tensorflow as tf
import numpy as np
import pickle
import os

# 读取文件目录
# 自行下载数据集
cifar_dir = './cifar-10-batches-py'
print(os.listdir(cifar_dir))

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') ]


# 构建数据载入函数
def load_data(filename):
    with open(filename,'rb') as f:
        data = pickle.load(f,encoding='bytes')
        return data[b'data'],data[b'labels']


# 构建数据处理类
# 主要完成数据的提取,归一化,洗牌,分批次提取等
class CifarData:
    def __init__(self,filenames,need_shuffle):
        all_data = []
        all_labels = []
        for i in filenames:
            data,labels = load_data(i)
            all_data.append(data)
            all_labels.append(labels)

        self._data = np.vstack(all_data) / 127.5 - 1
        self._labels = np.hstack(all_labels)
        self._num_examples = self._data.shape[0]
        self._index = 0
        self._need_shuffle = need_shuffle
        if self._need_shuffle:
            self.shuffle_data()

    def shuffle_data(self):
        o = np.random.permutation(self._num_examples)
        self._data = self._data[o]
        self._labels = self._labels[o]

    def next_batch(self,batch_size):
        end_index = self._index + batch_size
        if end_index > self._num_examples:
            if self._need_shuffle:
                self.shuffle_data()
                self._index = 0
                end_index = batch_size
            else:
                raise Exception('没有过多样本')
        if end_index > self._num_examples:
            raise Exception('尺寸过大')

        batch_data = self._data[self._index:end_index]
        batch_labels = self._labels[self._index:end_index]
        self._index = end_index
        return batch_data,batch_labels


# 实例化
train_data = CifarData(train_filenames,True)
test_data = CifarData(test_filenames,False)

# 构建模型
X = tf.placeholder(tf.float32,shape=[None,3072])
Y = tf.placeholder(tf.int64,shape=[None])
X_img = tf.reshape(X,[-1,3,32,32])
X_img = tf.transpose(X_img,[0,2,3,1])

# 卷积部分
conv1_1 = tf.layers.conv2d(X_img,32,(3,3),padding='same',name='conv1_1')
pooling1 = tf.layers.max_pooling2d(conv1_1,(2,2),(2,2),name='pool1')

conv2_1 = tf.layers.conv2d(pooling1,32,(3,3),padding='same',name='conv2_1')
pooling2 = tf.layers.max_pooling2d(conv2_1,(2,2),(2,2),name='pool2')

conv3_1 = tf.layers.conv2d(pooling2,32,(3,3),padding='same',name='conv3_1')
conv3_2 = tf.layers.conv2d(conv3_1,32,(3,3),padding='same',name='conv3_2')
conv3_3 = tf.layers.conv2d(conv3_2,32,(3,3),padding='same',name='conv3_3')
pooling3 = tf.layers.max_pooling2d(conv3_3,(2,2),(2,2),name='pool3')

# 展平
flatten = tf.layers.flatten(pooling3,name='flatten')

# 全连接部分
fc6 = tf.layers.dense(flatten,64,activation=tf.nn.relu,name='fc6')
fc7 = tf.layers.dense(fc6,64,activation=tf.nn.relu,name='fc7')

y_ = tf.layers.dense(fc7,10,activation=None)

# 代价
loss = tf.losses.sparse_softmax_cross_entropy(logits=y_,labels=Y)

# 优化器
train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# 预测
predict = tf.argmax(y_,1)
# 准确率
accuracy = tf.reduce_mean(tf.cast(tf.equal(predict,Y),dtype=tf.float32))

# 超参数
batch_size = 20
train_steps = 10000
test_steps = 100

# 开启会话
with tf.Session() as sess:
	# 初始化所有变量
    sess.run(tf.global_variables_initializer())
    for i in range(train_steps):
        x_train,y_train = train_data.next_batch(batch_size)
        los,ac,_ = sess.run([loss,accuracy,train_op],feed_dict={X:x_train,Y:y_train})
        if (i + 1) % 500 == 0:
            print('代价:',los)
            print('准确率',ac)
        # 每5000次进行一次测试
        if (i + 1) % 5000 == 0:
            all_acc = []
            test_data = CifarData(test_filenames,False)
            for j in range(test_steps):
                x_test,y_test = test_data.next_batch(batch_size)
                acc = sess.run(accuracy,feed_dict={X:x_test,Y:y_test})
                all_acc.append(acc)
            print('测试集准去率: ',sess.run(tf.reduce_mean(all_acc)))

你可能感兴趣的:(深度学习项目练习)