資源簡介
基于GTSRB數據集CNN,卷積神經網絡交通標志識別

代碼片段和文件信息
#?coding=utf-8
import?tensorflow?as?tf
import?image?as?image_data
import?random
import?numpy?as?np
#print?len(image_data.images32)
sess?=?tf.InteractiveSession()
#?weight?initialization
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)
????#?print(tf.Variable(initial).eval())
????return?tf.Variable(initial)
#?convolution
def?conv2d(x?W):
????return?tf.nn.conv2d(x?W?strides=[1?1?1?1]?padding=‘SAME‘)
#?pooling
def?max_pool_2x2(x):
????return?tf.nn.max_pool(x?ksize=[1?2?2?1]?strides=[1?2?2?1]?padding=‘SAME‘)
#?Create?the?model
#?placeholder
x?=?tf.placeholder(“float“?[None?32323])
y_?=?tf.placeholder(“float“?[None?43])
#?first?convolutinal?layer??h_conv1(028)
w_conv1?=?weight_variable([5?5?3?32])#kernel?channelsout?features
b_conv1?=?bias_variable([32])
x_image?=?tf.reshape(x?[-1?32?32?3])#3d
#h_conv1?=?tf.nn.relu(conv2d(x_image?w_conv1)?+?b_conv1)
h_conv1?=?tf.nn.relu(conv2d(x_image?w_conv1)?+?b_conv1)
h_pool1?=?max_pool_2x2(h_conv1)
#?second?convolutional?layer
w_conv2?=?weight_variable([5?5?32?64])
b_conv2?=?bias_variable([64])
#h_conv2?=?tf.nn.relu(conv2d(h_pool1?w_conv2)?+?b_conv2)
h_conv2?=?tf.nn.relu(conv2d(h_pool1?w_conv2)?+?b_conv2)
h_pool2?=?max_pool_2x2(h_conv2)
pool_shape?=?h_pool2.get_shape().as_list()
node_num?=?pool_shape[1]?*?pool_shape[2]?*?pool_shape[3]
#?densely?connected?layer
w_fc1?=?weight_variable([node_num?512])
b_fc1?=?bias_variable([512])
‘‘‘
#zheng?ze?hua
regularizer?=?tf.contrib.layers.l2_regularizer(0.001)
tf.add_to_collection(‘losses‘?regularizer(w_fc1))
‘‘‘
h_pool2_flat?=?tf.reshape(h_pool2?[-1?node_num])
h_fc1?=?tf.nn.relu(tf.matmul(h_pool2_flat?w_fc1)?+?b_fc1)
#?dropout
keep_prob?=?tf.placeholder(“float“)
h_fc1_drop?=?tf.nn.dropout(h_fc1?keep_prob)
#?readout?layer
w_fc2?=?weight_variable([51243])
b_fc2?=?bias_variable([43])
y_conv?=?tf.nn.softmax(tf.matmul(h_fc1_drop?w_fc2)?+?b_fc2)
#?train?and?evaluate?the?model
#cross_entropy?=?-tf.reduce_sum(y_*tf.log(y_conv))
cross_entropy?=?-tf.reduce_sum(y_*tf.log(tf.clip_by_value(y_conv1e-101.0)))??
#cross_entropy_mean?=?tf.reduce_mean(cross_entropy)
#loss?=?cross_entropy_mean?+?tf.add_n(tf.get_collection(‘losses‘))
#train_step?=?tf.train.AdagradOptimizer(1e-4).minimize(cross_entropy)
train_step?=?tf.train.GradientDescentOptimizer(1e-4).minimize(cross_entropy)
correct_prediction?=?tf.equal(tf.argmax(y_conv?1)?tf.argmax(y_?1))
accuracy?=?tf.reduce_mean(tf.cast(correct_prediction?“float“))
#pred=tf.argmax(y_conv?1)
pred=y_conv
#accuracy=tf.cast(tf.argmax(y_conv?1)tf.float32)#self
#sess.run(tf.initialize_all_variables())
saver=tf.train.Saver()
with?tf.Session()?as?ses:
saver.restore(sess?“./train_mode.ckpt“)
print?“model?loaded“
labels=[[0?for?i?in?range(43)]?for?j?in?range(len(image_data.labels))]
for?i?in?range(len(image_data.labels)):
labels[i][image_data.labels[i]]=1
#p
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-06-19?16:25??cnn交通標志識別-鄒文超\
?????文件????????3942??2017-05-31?09:53??cnn交通標志識別-鄒文超\cnn.py
?????文件????????3701??2017-05-22?12:41??cnn交通標志識別-鄒文超\image.py
?????文件????????3525??2017-05-24?17:36??cnn交通標志識別-鄒文超\image_test.py
?????文件????????4565??2017-05-24?19:49??cnn交通標志識別-鄒文超\test.py
評論
共有 條評論