1.數(shù)據(jù)集
CIFAR_10
192.168.9.5:/DATACENTER1/zhiwen.wang/tensorflow-wzw/tensorflow_learn/CIFAR_10/cifar-10-batches
2.代碼
192.168.9.5:/DATACENTER1/zhiwen.wang/tensorflow-wzw/tensorflow_learn/CIFAR_10/cifar10
wzwtrain13.py
# -*- coding: utf-8 -*-
"""
Created on 2019
@author: wzw
"""
'''
建立一個(gè)帶有全局平均池化層的卷積神經(jīng)網(wǎng)絡(luò)? 并對(duì)CIFAR-10數(shù)據(jù)集進(jìn)行分類
1.使用3個(gè)卷積層的同卷積操作瘾杭,濾波器大小為5x5蹬耘,每個(gè)卷積層后面都會(huì)跟一個(gè)步長(zhǎng)為2x2的池化層台汇,濾波器大小為2x2
2.對(duì)輸出的10個(gè)feature map進(jìn)行全局平均池化,得到10個(gè)特征
3.對(duì)得到的10個(gè)特征進(jìn)行softmax計(jì)算丛肢,得到分類
'''
import cifar10_input
import tensorflow as tf
import numpy as np
'''
一 引入數(shù)據(jù)集
'''
batch_size = 256
learning_rate = 1e-4
training_step = 100000
display_step = 200
#數(shù)據(jù)集目錄
data_dir = './cifar-10-batches/cifar-10-batches-bin'
print('begin')
#獲取訓(xùn)練集數(shù)據(jù)
images_train,labels_train = cifar10_input.inputs(eval_data=False,data_dir = data_dir,batch_size=batch_size)
print('begin data')
'''
二 定義網(wǎng)絡(luò)結(jié)構(gòu)
'''
def weight_variable(shape):
? ? '''
? ? 初始化權(quán)重
? ? args:
? ? ? ? shape:權(quán)重shape
? ? '''
? ? initial = tf.truncated_normal(shape=shape,mean=0.0,stddev=0.1)
? ? return tf.Variable(initial)
def bias_variable(shape):
? ? '''
? ? 初始化偏置
? ? args:
? ? ? ? shape:偏置shape
? ? '''
? ? initial =tf.constant(0.1,shape=shape)
? ? return tf.Variable(initial)
def conv2d(x,W):
? ? '''
? ? 卷積運(yùn)算 撩满,使用SAME填充方式? 池化層后
? ? ? ? out_height = in_hight / strides_height(向上取整)
? ? ? ? out_width = in_width / strides_width(向上取整)
? ? args:
? ? ? ? x:輸入圖像 形狀為[batch,in_height,in_width,in_channels]
? ? ? ? W:權(quán)重 形狀為[filter_height,filter_width,in_channels,out_channels]? ? ? ?
? ? '''
? ? return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
def max_pool_2x2(x):
? ? '''
? ? 最大池化層,濾波器大小為2x2,'SAME'填充方式? 池化層后
? ? ? ? out_height = in_hight / strides_height(向上取整)
? ? ? ? out_width = in_width / strides_width(向上取整)
? ? args:
? ? ? ? x:輸入圖像 形狀為[batch,in_height,in_width,in_channels]
? ? '''
? ? return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
def avg_pool_6x6(x):
? ? '''
? ? 全局平均池化層习蓬,使用一個(gè)與原有輸入同樣尺寸的filter進(jìn)行池化旺隙,'SAME'填充方式? 池化層后
? ? ? ? out_height = in_hight / strides_height(向上取整)
? ? ? ? out_width = in_width / strides_width(向上取整)
? ? args;
? ? ? ? x:輸入圖像 形狀為[batch,in_height,in_width,in_channels]
? ? '''
? ? return tf.nn.avg_pool(x,ksize=[1,6,6,1],strides=[1,6,6,1],padding='SAME')
def print_op_shape(t):
? ? '''
? ? 輸出一個(gè)操作op節(jié)點(diǎn)的形狀
? ? '''
? ? print(t.op.name,'',t.get_shape().as_list())
#定義占位符
input_x = tf.placeholder(dtype=tf.float32,shape=[None,24,24,3])? #圖像大小24x24x
input_y = tf.placeholder(dtype=tf.float32,shape=[None,10])? ? ? ? #0-9類別
x_image = tf.reshape(input_x,[-1,24,24,3])
#1.卷積層 ->池化層
W_conv1 = weight_variable([5,5,3,64])
b_conv1 = bias_variable([64])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1)? ? #輸出為[-1,24,24,64]
print_op_shape(h_conv1)
h_pool1 = max_pool_2x2(h_conv1)? ? ? ? ? ? ? ? ? ? ? ? ? ? #輸出為[-1,12,12,64]
print_op_shape(h_pool1)
#2.卷積層 ->池化層
W_conv2 = weight_variable([5,5,64,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2) + b_conv2)? ? #輸出為[-1,12,12,64]
print_op_shape(h_conv2)
h_pool2 = max_pool_2x2(h_conv2)? ? ? ? ? ? ? ? ? ? ? ? ? ? #輸出為[-1,6,6,64]
print_op_shape(h_pool2)
#3.卷積層 ->全局平均池化層
W_conv3 = weight_variable([5,5,64,10])
b_conv3 = bias_variable([10])
h_conv3 = tf.nn.relu(conv2d(h_pool2,W_conv3) + b_conv3)? #輸出為[-1,6,6,10]
print_op_shape(h_conv3)
nt_hpool3 = avg_pool_6x6(h_conv3)? ? ? ? ? ? ? ? ? ? ? ? #輸出為[-1,1,1,10]
print_op_shape(nt_hpool3)
nt_hpool3_flat = tf.reshape(nt_hpool3,[-1,10])? ? ? ? ? ?
y_conv = tf.nn.softmax(nt_hpool3_flat)
'''
三 定義求解器
'''
#softmax交叉熵代價(jià)函數(shù)
cost = tf.reduce_mean(-tf.reduce_sum(input_y * tf.log(y_conv),axis=1))
#求解器
train = tf.train.AdamOptimizer(learning_rate).minimize(cost)
#返回一個(gè)準(zhǔn)確度的數(shù)據(jù)
correct_prediction = tf.equal(tf.arg_max(y_conv,1),tf.arg_max(input_y,1))
#準(zhǔn)確率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,dtype=tf.float32))
'''
四 開始訓(xùn)練
'''
sess = tf.Session();
sess.run(tf.global_variables_initializer())
tf.train.start_queue_runners(sess=sess)
for step in range(training_step):
? ? with tf.device('/cpu:0'):
? ? ? ? image_batch,label_batch = sess.run([images_train,labels_train])
? ? ? ? label_b = np.eye(10,dtype=np.float32)[label_batch]
? ? with tf.device('/gpu:0'):
? ? ? ? train.run(feed_dict={input_x:image_batch,input_y:label_b},session=sess)
? ? if step % display_step == 0:
? ? ? ? train_accuracy = accuracy.eval(feed_dict={input_x:image_batch,input_y:label_b},session=sess)
? ? ? ? print('Step {0} tranining accuracy {1}'.format(step,train_accuracy))
3.運(yùn)行
CUDA_VISIBLE_DEVICES=1 python wzwtrain13.py
4.運(yùn)行結(jié)果