1
2import cifar10,cifar10_input
3import tensorflow as tf
4import numpy as np
5import time
6
7
8max_iter_step = 1000
9batch_size = 128
10
11
12
13
14def variable_with_weight_loss(shape,stddev,w1):
15 var = tf.Variable(tf.truncated_normal(shape,stddev=stddev))
16 if w1 is not None:
17 weight_loss = tf.multiply(tf.nn.l2_loss(var),w1,name='weight_loss')
18 tf.add_to_collection('losses',weight_loss)
19 return var
20
21
22cifar10.maybe_download_and_extract()
23
24cifar_dir = './cifar-10-batches-bin'
25
26
27
28images_train, labels_train = cifar10_input.distorted_inputs(data_dir=cifar_dir,batch_size=batch_size)
29
30images_test,labels_test = cifar10_input.inputs(eval_data=True,data_dir=cifar_dir,batch_size=batch_size)
31
32
33x_input = tf.placeholder(tf.float32,[batch_size,24,24,3])
34y_input = tf.placeholder(tf.int32,[batch_size])
35
36
37weight1 = variable_with_weight_loss(shape=[5,5,3,64],stddev=5e-2,w1=0.0)
38bias1 = tf.Variable(tf.constant(0.0,shape=[64]))
39conv1 = tf.nn.conv2d(x_input,weight1,[1,1,1,1],padding='SAME')
40relu1 = tf.nn.relu(tf.nn.bias_add(conv1,bias1))
41pool1 = tf.nn.max_pool(conv1,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME')
42norm1 = tf.nn.lrn(pool1,4,bias=1.0,alpha=0.001/9.0,beta=0.75)
43
44
45weight2 = variable_with_weight_loss(shape=[5,5,64,64],stddev=5e-2,w1=0.0)
46bias2 = tf.Variable(tf.constant(0,1,shape=[64]))
47conv2 = tf.nn.conv2d(norm1,weight2,[1,1,1,1],padding='SAME')
48relu2 = tf.nn.relu(tf.nn.bias_add(conv2,bias2))
49norm2 = tf.nn.lrn(relu2,4,bias=1.0,alpha=0.001/9.0,beta=0.75)
50pool2 = tf.nn.max_pool(norm2,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME')
51
52
53reshape = tf.reshape(pool2,[batch_size,-1])
54dim = reshape.get_shape()[1].value
55weight3 = variable_with_weight_loss(shape=[dim,384],stddev=0.04,w1=0.004)
56bias3 = tf.Variable(tf.constant(0.1,shape=[384]))
57local3 = tf.nn.relu(tf.matmul(reshape,weight3)+bias3)
58
59
60weight4 = variable_with_weight_loss(shape=[384,192],stddev=0.04,w1=0.004)
61bias4 = tf.Variable(tf.constant(0.1,shape=[192]))
62local4 = tf.nn.relu(tf.matmul(local3,weight4)+bias4)
63
64
65weight5 = variable_with_weight_loss(shape=[192,10],stddev=1/192.0,w1=0.0)
66bias5 = tf.Variable(tf.constant(0.0,shape=[10]))
67results = tf.add(tf.matmul(local4,weight5),bias5)
68
69
70def loss(results,labels):
71 labels = tf.cast(labels,tf.int64)
72 cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=results,labels=labels,name='cross_entropy_per_example')
73 cross_entropy_mean = tf.reduce_mean(cross_entropy,name='cross_entropy')
74 tf.add_to_collection('losses',cross_entropy_mean)
75 return tf.add_n(tf.get_collection('losses'),name='total_loss')
76
77
78loss = loss(results,y_input)
79
80train_op = tf.train.AdamOptimizer(1e-3).minimize(loss)
81top_k_op = tf.nn.in_top_k(results,y_input,1)
82
83sess = tf.InteractiveSession()
84
85tf.initialize_all_variables().run()
86
87tf.train.start_queue_runners()
88