码迷,mamicode.com
首页 > 其他好文 > 详细

CNN Advanced

时间:2018-04-23 00:09:06      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:time()   XA   ros   active   ext   runner   local   分享图片   tor   

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

 

CNN Advanced

标签:time()   XA   ros   active   ext   runner   local   分享图片   tor   

原文地址:https://www.cnblogs.com/acm-jing/p/8910190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!