欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

cifar数据集的预处理和训练

程序员文章站 2022-04-18 20:05:41
...

注:以下代码从mooc网<人工智能实践:Tensorflow笔记>课程作业给出参考代码和讨论区同学回复代码学习修改得到,侵权立删.

网址:https://www.icourse163.org/learn/PKU-1002536002?tid=1206591210#/learn/forumdetail?pid=1212984121

forward代码:

#coding:utf-8
import tensorflow as tf
IMAGE_SIZE = 32 #input:32*32
NUM_CHANNELS = 3 #color:3 
CONV1_SIZE = 5
CONV1_KERNEL_NUM = 32
CONV2_SIZE = 5
CONV2_KERNEL_NUM = 64
FC_SIZE = 512
OUTPUT_NODE = 10

def get_weight(shape, regularizer):
	w = tf.Variable(tf.truncated_normal(shape,stddev=0.1))
	if regularizer != None: tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w)) 
	return w

def get_bias(shape): 
	b = tf.Variable(tf.zeros(shape))  
	return b

def conv2d(x,w):  
	return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):  
	return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 

def forward(x, train, regularizer):
    conv1_w = get_weight([CONV1_SIZE, CONV1_SIZE, NUM_CHANNELS, CONV1_KERNEL_NUM], regularizer) 
    conv1_b = get_bias([CONV1_KERNEL_NUM]) 
    conv1 = conv2d(x, conv1_w) 
    relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_b)) 
    pool1 = max_pool_2x2(relu1) 

    conv2_w = get_weight([CONV2_SIZE, CONV2_SIZE, CONV1_KERNEL_NUM, CONV2_KERNEL_NUM],regularizer) 
    conv2_b = get_bias([CONV2_KERNEL_NUM])
    conv2 = conv2d(pool1, conv2_w) 
    relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_b))
    pool2 = max_pool_2x2(relu2)

    pool_shape = pool2.get_shape().as_list() 
    nodes = pool_shape[1] * pool_shape[2] * pool_shape[3] 
    reshaped = tf.reshape(pool2, [pool_shape[0], nodes]) 

    fc1_w = get_weight([nodes, FC_SIZE], regularizer) 
    fc1_b = get_bias([FC_SIZE]) 
    fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_w) + fc1_b) 
    if train: fc1 = tf.nn.dropout(fc1, 0.5)

    fc2_w = get_weight([FC_SIZE, OUTPUT_NODE], regularizer)
    fc2_b = get_bias([OUTPUT_NODE])
    y = tf.matmul(fc1, fc2_w) + fc2_b
    return y 

backward代码:

#coding:utf-8
import tensorflow as tf
import cifar_lenet5_forward
import os
import numpy as np
from read_data import DataLoad

BATCH_SIZE = 100
LEARNING_RATE_BASE =  0.005 
LEARNING_RATE_DECAY = 0.99 
REGULARIZER = 0.0001 
STEPS = 5000
MOVING_AVERAGE_DECAY = 0.99 
MODEL_SAVE_PATH="./model/" 
MODEL_NAME="cifar_model" 

def backward(cifar):
    x = tf.placeholder(tf.float32,[
	BATCH_SIZE,
	cifar_lenet5_forward.IMAGE_SIZE,
	cifar_lenet5_forward.IMAGE_SIZE,
	cifar_lenet5_forward.NUM_CHANNELS]) 
    y_ = tf.placeholder(tf.float32, [None, cifar_lenet5_forward.OUTPUT_NODE])
    y = cifar_lenet5_forward.forward(x,True, REGULARIZER) 
    global_step = tf.Variable(0, trainable=False) 

    ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
    cem = tf.reduce_mean(ce) 
    loss = cem + tf.add_n(tf.get_collection('losses')) 

    learning_rate = tf.train.exponential_decay( 
        LEARNING_RATE_BASE,
        global_step,
        cifar.num_examples / BATCH_SIZE, 
		LEARNING_RATE_DECAY,
        staircase=True) 
    
    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)

    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    ema_op = ema.apply(tf.trainable_variables())
    with tf.control_dependencies([train_step, ema_op]): 
        train_op = tf.no_op(name='train')

    saver = tf.train.Saver() 

    with tf.Session() as sess: 
        init_op = tf.global_variables_initializer() 
        sess.run(init_op) 

        ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) 
        if ckpt and ckpt.model_checkpoint_path:
        	saver.restore(sess, ckpt.model_checkpoint_path) 

        for i in range(STEPS):
            xs, ys = cifar.next_batch(BATCH_SIZE)
            reshaped_xs = np.reshape(xs,(  
		    BATCH_SIZE,
        	cifar_lenet5_forward.IMAGE_SIZE,
        	cifar_lenet5_forward.IMAGE_SIZE,
        	cifar_lenet5_forward.NUM_CHANNELS))
            _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: reshaped_xs, y_: ys}) 
            if i % 100 == 0: 
                print("After %d training step(s), loss on training batch is %g." % (step, loss_value))
                saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
'''
def main():
    cifar = input_data.read_data_sets("./data/", one_hot=True) 
    backward(cifar)
'''

def main():
    #cifar10 = input_data.read_data_sets("./data/", one_hot=True)  # 读入 Cifar-10 数据
    cifar10_train = DataLoad("./train")
    backward(cifar10_train)

if __name__ == '__main__':
    main()

test代码:

#coding:utf-8
import time
import tensorflow as tf
import cifar_lenet5_forward
import cifar_lenet5_backward
import numpy as np
from read_data import DataLoad

TEST_INTERVAL_SECS = 5

def test(cifar):
    with tf.Graph().as_default() as g: 
        x = tf.placeholder(tf.float32,[
            cifar.num_examples,
            cifar_lenet5_forward.IMAGE_SIZE,
            cifar_lenet5_forward.IMAGE_SIZE,
            cifar_lenet5_forward.NUM_CHANNELS]) 
        y_ = tf.placeholder(tf.float32, [None, cifar_lenet5_forward.OUTPUT_NODE])
        y = cifar_lenet5_forward.forward(x,False,None)

        ema = tf.train.ExponentialMovingAverage(cifar_lenet5_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)
		
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(cifar_lenet5_backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
					
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 
                    reshaped_x = np.reshape(cifar.xs,(
                    cifar.num_examples,
        	        cifar_lenet5_forward.IMAGE_SIZE,
        	        cifar_lenet5_forward.IMAGE_SIZE,
        	        cifar_lenet5_forward.NUM_CHANNELS))
                    accuracy_score = sess.run(accuracy, feed_dict={x:reshaped_x,y_:cifar.ys}) 
                    print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))
                else:
                    print('No checkpoint file found')
                    return
            time.sleep(TEST_INTERVAL_SECS) 
'''
def main():
    cifar = input_data.read_data_sets("./data/", one_hot=True)
    test(cifar)
'''

def main():
    #cifar10 = input_data.read_data_sets("./data/", one_hot=True)
    cifar10_test = DataLoad("./test")
    test(cifar10_test)

if __name__ == '__main__':
    main()

read_data代码:

#coding:utf-8
from PIL import Image
import numpy as np
import os
import random
class DataLoad:
    def __init__(self,data_path):
        '''
        初始化加载 数据目录
        :param data_path: 包含图片数据集的目录,内部应该包含不同分类的的图片,每种类型是一个子目录
        '''
        self.xs = []
        self.ys = []
        # 还是先写死吧,简单点
        lables_d = {
            0: "airplane",
            1: "automobile",
            2: "bird",
            3: "cat",
            4: "deer",
            5: "dog",
            6: "frog",
            7: "horse",
            8: "ship",
            9: "truck"
        }
        a_tmp_list = []
        for k,v in lables_d.items():
            a_path = data_path + "/" + v
            for file in os.listdir(a_path):
                x = a_path + "/" + file # pre_pic()
                a_tmp_list.append((x, k))  # 这里,暂不读取文件.记录每个文件名对应的label
                #print(x,k)
        #
        random.shuffle(a_tmp_list) # 对列表随机打乱
        #
        for (file_path, k) in a_tmp_list:
            y = np.zeros(10)
            y[k] = 1 # 构造 one-hot 的y
            fp = open(file_path,'r')
            x = Image.open(fp)
            x=np.array(x.resize((32,32))) 
            fp.close()
            self.xs.append(x)
            self.ys.append(y)
        #
        self.xs = np.array(self.xs)
        self.ys = np.array(self.ys)
        self.num_examples = len(self.xs)  # 样本数量
        self.curr_pointer = 0
    def next_batch(self, batch_size):
        ''' 随机读取一个批次数据. '''
        start = random.randint(0,self.num_examples-batch_size-1)
        end = start + batch_size
        xx = self.xs[start : end]
        yy = self.ys[start : end]
        #print(xx.shape)
        #print(xx)
        #xx = xx.reshape([1, -1])
        #print(xx.shape)
        rtn_samp = (xx,yy)
        #
        #exit(0)
        #print("-----------------------------1,%d" % batch_size)
        #print(rtn_samp)
        return rtn_samp

 

相关标签: 深度学习入门