CNN Advanced
1 from sys import path 2 path.append('/home/ustcjing/models/tutorials/image/cifar10/') 3 import cifar10,cifar10_input 4 import tensorflow as tf 5 import math 6 import numpy as np 7 import time 8 9 max_steps=300 10 batch_size=128 11 data_dir='/tmp/cifar10_data/cifar-10-batches-bin' 12 13 def variable_with_weight_loss(shape,stddev,w1): 14 var=tf.Variable(tf.truncated_normal(shape,stddev=stddev)) 15 if w1 is not None: 16 weight_loss=tf.multiply(tf.nn.l2_loss(var),w1,name='weight_loss') 17 tf.add_to_collection('losses','weight_loss') 18 19 return var 20 21 cifar10.maybe_download_and_extract() 22 images_train,labels_train=cifar10_input.distorted_inputs(data_dir=data_dir,batch_size=batch_size) 23 images_test,labels_test=cifar10_input.inputs(eval_data=True,data_dir=data_dir,batch_size=batch_size) 24 25 image_holder=tf.placeholder(tf.float32,[batch_size,24,24,3]) 26 label_holder=tf.placeholder(tf.int32,[batch_size]) 27 28 weight1=variable_with_weight_loss(shape=[5,5,3,64],stddev=5e-2,w1=0.0) 29 kernel1=tf.nn.conv2d(image_holder,weight1,[1,1,1,1],padding='SAME') 30 bias1=tf.Variable(tf.constant(0.0,shape=[64])) 31 conv1=tf.nn.relu(tf.nn.bias_add(kernel1,bias1)) 32 pool1=tf.nn.max_pool(conv1,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME') 33 norm1=tf.nn.lrn(pool1,4,bias=1.0,alpha=0.001/9.0,beta=0.75) 34 35 weight2=variable_with_weight_loss(shape=[5,5,64,64],stddev=5e-2,w1=0.0) 36 kernel2=tf.nn.conv2d(norm1,weight2,[1,1,1,1],padding='SAME') 37 bias2=tf.Variable(tf.constant(0.1,shape=[64])) 38 conv2=tf.nn.relu(tf.nn.bias_add(kernel2,bias2)) 39 norm2=tf.nn.lrn(conv2,4,bias=1.0,alpha=0.001/9.0,beta=0.75) 40 pool2=tf.nn.max_pool(norm2,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME') 41 42 reshape=tf.reshape(pool2,[batch_size,-1]) 43 dim=reshape.get_shape()[1].value 44 weight3=variable_with_weight_loss(shape=[dim,384],stddev=0.04,w1=0.004) 45 bias3=tf.variable(tf.constant(0.1,shape=[384])) 46 local3=tf.nn.relu(tf.matmul(reshape,weight3)+bias3) 47 48 weight4=variable_with_weight_loss(shape=[384,192],stddev=0.04,w1=0.004) 49 bias4=tf.Variable9tf.constant(0.1,shape=[192]) 50 local4=tf.nn.relu(tf.matmul(local3,weight4)+bias4) 51 52 weight5=variable_with_weight_loss(shape=[192,10],stddev=1/192.0,w1=0.0) 53 bias5=tf.Variable(tf.constant(0.0,shape=[10])) 54 logits=tf.add(tf.matmul(local4,weight5),bias5) 55 56 def loss(logits,labels): 57 labels=tf.cast(labels,tf.int64) 58 cross_entropy=tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,labels=labels,name='cross_entropy_per_example') 59 cross_entropy_mean=tf.reduce_mean(cross_entropy,name='cross_entropy') 60 tf.add_to_collection('losses',cross_entropy_mean) 61 return tf.add_n(tf.get_collection('losses'),name='total_loss') 62 63 loss=loss(logits,label_holder) 64 train_op=tf.train.AdamOptimizer(1e-3).minimize(loss) 65 top_k_op=tf.nn.in_top_k(logits,label_holder,1) 66 sess=tf.InteractiveSession() 67 tf.initialize_all_variables().run() 68 tf.train.start_queue_runners() 69 70 for step in range(max_steps): 71 start_time=time.time() 72 image_batch,label_batch=sess.run([images_train,labels_train]) 73 loss_value=sess.run([train_op,loss],feed_dict={image_holder:image_batch,label_holder:label_batch}) 74 duration=time.time()-start_time 75 if step%10==0: 76 examples_per_sec=batch_size/duration 77 sec_per_batch=float(duration) 78 format_str=('step %d,loss=%.2f (%.1f examples/sec;%.3f sec/batch)') 79 print(format_str % (step,loss_value,examples_per_sec,sec_per_batch)) 80 81 num_examples=1000 82 num_iter=int(math.ceil(num_examples / batch_size)) 83 true_count=0; 84 total_sample_count=num_iter*batch_size 85 step=0 86 while step<num_iter: 87 image_batch,label_batch=sess.run([images_test,labels_test]) 88 predictions=sess.run([top_k_op],feed_dict={image_holder:image_batch,label_holder:label_batch}) 89 90 true_count+=np.sum(predictions) 91 step+=1 92 93 precision=true_count/total_sample_count 94 print('precision @ 1=%.3f' % precision)