CNN for Tensorflow中的cifar10数据集

时间:2017-03-17 17:23:19

标签: machine-learning tensorflow deep-learning

我试图使用Tensorflow复制卷积神经网络为CIFAR10获得的结果,但是在一些时期(约60个时期)之后我的表现(准确度)大约是10%,所以如果CNN训练有素,我不会这样做吗?

此代码基于专家的深度mnist https://www.tensorflow.org/get_started/mnist/pros,但在Cifar10中它不起作用

import numpy as np
import tensorflow as tf

def unpickle(file):
    import cPickle
    fo = open(file, 'rb')
    dict = cPickle.load(fo)
    fo.close()
    return dict

#unpacking training and test data
b1 = unpickle("~/cifar-10-batches-py/data_batch_1")
b2 = unpickle("~/cifar-10-batches-py/data_batch_2")
b3 = unpickle("~/cifar-10-batches-py/data_batch_3")
b4 = unpickle("~/cifar-10-batches-py/data_batch_4")
b5 = unpickle("~/cifar-10-batches-py/data_batch_5")

test = unpickle("~/cifar-10-batches-py/test_batch")

#Preparing test data
test_data = test['data']
test_label = test['labels']

#Preparing training data
train_data = np.concatenate([b1['data'],b2['data'],b3['data'],b4['data'],b5['data']],axis=0)
train_label = np.concatenate([b1['labels'],b2['labels'],b3['labels'],b4['labels'],b5['labels']],axis=0)

#Reshaping data
train_data = np.reshape(train_data,[50000,32,32,3])
test_data = np.reshape(test_data,[10000,32,32,3])

batch_size = 100
image_width = 32
image_height = 32
channels = 3

#Constructing Graph
x = tf.placeholder(tf.float32, [None, image_width, image_height, channels])#Training Data
y = tf.placeholder(tf.int32, [None])
one_hot = tf.one_hot(y,depth=10)#Converting in one hot vectors

#Constructing CNN Layers
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

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


#Given an input tensor of shape [batch, in_height, in_width, in_channels] and a filter / kernel tensor of shape [filter_height, filter_width, in_channels, out_channels], taken from: http://textminingonline.com/dive-into-tensorflow-part-v-deep-mnist
W_conv1 = weight_variable([7, 7, 3, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

W_conv2 = weight_variable([5, 5, 32, 32])
b_conv2 = bias_variable([32])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

W_conv3 = weight_variable([5, 5, 32, 64])
b_conv3 = bias_variable([64])
h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3)

#Constructing MLP layers
W_fc1 = weight_variable([8 * 8 * 64, 64])
b_fc1 = bias_variable([64])
h_pool3_flat = tf.reshape(h_conv3, [-1, 8*8*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool3_flat, W_fc1) + b_fc1)

W_fc2 = weight_variable([64, 10])
b_fc2 = bias_variable([10])

y_conv = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)


#Computing Cost function
cross_entropy = -tf.reduce_sum(one_hot*tf.log(tf.clip_by_value(y_conv,1e-10,1e20)))
train_step = tf.train.MomentumOptimizer(learning_rate = 0.0001, momentum = 0.9).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(one_hot,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

init = tf.initialize_all_variables()
sess = tf.Session(config=tf.ConfigProto(intra_op_parallelism_threads=16))
sess.run(init)

epochs = 100
b_per = 0
row = []
for e in range(epochs):

    print( "epoch", e)
    avg_cost = 0
    #foreach batch
    for j in range(int(train_data.shape[0]/batch_size)):

        subset=range((j*batch_size),((j+1)*batch_size))
        data = train_data[subset,:,:,:]
        label = train_label[subset]
        _,c = sess.run([train_step,cross_entropy], feed_dict={x: data, y: label})

        avg_cost += c / data.shape[0]
        #print(avg_cost)

        b_per = b_per + 1

        if b_per%10==0 :

            row.append(sess.run(accuracy, feed_dict={x: test_data, y: test_label }))



    print(row[-1])

1 个答案:

答案 0 :(得分:0)

数据重塑部分错了!它应该是,

recipes.addEventListener('click', function(event){
  event.stopPropagation();
  recipes_container.style.display = 'block';
});

close_heart.addEventListener('click', function(event){
  event.stopPropagation();
  recipes_container.style.display = 'none';
});