基于JavaSwing坦克大戰游戲的設計和實現
897
2025-04-02
數據集官網:The CIFAR-10 dataset
@[toc]
import warnings warnings.filterwarnings('ignore') import tensorflow as tf import matplotlib.pyplot as plt import os import numpy as np %matplotlib inline
CIFAR-10是一個更接近普適物體的彩色圖像數據集。CIFAR-10 是由Hinton 的學生Alex Krizhevsky 和Ilya Sutskever 整理的一個用于識別普適物體的小型數據集。一共包含10 個類別的RGB 彩色圖片:飛機( airplane )、汽車( automobile )、鳥類( bird )、貓( cat )、鹿( deer )、狗( dog )、蛙類( frog )、馬( horse )、船( ship )和卡車( truck )。
每個圖片的尺寸為32 × 32 ,每個類別有6000個圖像,數據集中一共有50000 張訓練圖片和10000 張測試圖片。
數據集介紹
CIFAR-10是一個用于識別普適物 體的小型數據集,它包含了10個類 別的RGB彩色圖片。
圖片尺寸: 32 x 32
訓練圖片50000張,測試圖片 10000張
導入CIFAR數據集
def load_CIFAR_batch(filename): with open(filename,"rb") as f: data_dict = np.load(f,encoding="bytes", allow_pickle=True) images = data_dict[b"data"] labels = data_dict[b"labels"] images = images.reshape(10000,3,32,32) images = images.transpose(0,2,3,1) labels = np.array(labels) return images,labels def load_CIFAR_data(data_dir): images_train=[] labels_train=[] for i in range(5): f = os.path.join(data_dir,"data_batch_%d"%(i+1)) print("正在加載",f) image_batch,label_batch = load_CIFAR_batch(f) images_train.append(image_batch) labels_train.append(label_batch) Xtrain = np.concatenate(images_train) Ytrain = np.concatenate(labels_train) del image_batch,label_batch Xtest,Ytest = load_CIFAR_batch(os.path.join(data_dir,"test_batch")) print("導入完成") return Xtrain,Ytrain,Xtest,Ytest data_dir = r"data\cifar-10-batches-py" Xtrain,Ytrain,Xtest,Ytest = load_CIFAR_data(data_dir)
正在加載 data\cifar-10-batches-py\data_batch_1 正在加載 data\cifar-10-batches-py\data_batch_2 正在加載 data\cifar-10-batches-py\data_batch_3 正在加載 data\cifar-10-batches-py\data_batch_4 正在加載 data\cifar-10-batches-py\data_batch_5 導入完成
顯示數據集信息
print("training data shpae:\t",Xtrain.shape) print("training labels shpae:\t",Ytrain.shape) print("test data shpae:\t",Xtest.shape) print("test labels shpae:\t",Ytest.shape)
training data shpae: (50000, 32, 32, 3) training labels shpae: (50000,) test data shpae: (10000, 32, 32, 3) test labels shpae: (10000,)
Ytest
array([3, 8, 8, ..., 5, 1, 7])
查看image和label
查看單項image
plt.imshow(Xtrain[6])
查看多項images
for i in range(0,10): fig = plt.gcf() fig.set_size_inches(12,6) ax = plt.subplot(2,5,i+1) # 去除坐標軸 plt.xticks([]) plt.yticks([]) # 去除黑框 ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) # 設置各個子圖間間距 plt.subplots_adjust(left=0.10, top=0.88, right=0.65, bottom=0.08, wspace=0.02, hspace=0.02) ax.imshow(Xtrain[i],cmap="binary")
查看多項iamges和label
label_dict = {0:"airplane",1:"automobile",2:"bird",3:"cat",4:"deer",5:"dog",6:"frog",7:"horse",8:"ship",9:"trunk"}
def plot_images_labels_prediction(images,labels,prediction,idx,num=10): fig = plt.gcf() fig.set_size_inches(12,6) if num >10: num=10 for i in range(0,num): ax = plt.subplot(2,5,i+1) ax.imshow(images[idx],cmap="binary") plt.xticks([]) plt.yticks([]) # 去除黑框 ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) title = str(i)+","+label_dict[labels[idx]] if len(prediction)>0: title+="=>"+label_dict[prediction[idx]] ax.set_title(title,fontsize=10) idx += 1 # 去除坐標軸 plt.show() plot_images_labels_prediction(Xtest,Ytest,[],1,10)
定義網絡結構
圖像的特征提取:通過卷基層1、降采樣層1、卷積層2以及降采樣層2的處理,提取網絡結構
全連接神經網絡:全連接層、輸出層所組成的網絡結構
圖像的預處理
查看那圖像數據信息
顯示第一個圖的第一個像素點
Xtrain[0][0][0]
array([59, 62, 63], dtype=uint8)
將圖像進行數字的標準化
Xtrain_normalize = Xtrain.astype("float32")/255.0 Xtest_normalize = Xtest.astype("float32")/255.0
查看預處理后圖像數據信息
Xtrain_normalize[0][0][0]
array([ 0.23137255, 0.24313726, 0.24705882], dtype=float32)
標簽數據預處理–獨熱編碼
能夠處理非連續型數值特征
在一定程度上也擴充了特征
from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(sparse=False) yy = [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]] encoder.fit(yy) Ytrain_reshape = Ytrain.reshape(-1,1) Ytrain_onehot = encoder.transform(Ytrain_reshape) Ytest_reshape = Ytest.reshape(-1,1) Ytest_onehot = encoder.transform(Ytest_reshape)
Ytrain[:10]
array([6, 9, 9, 4, 1, 1, 2, 7, 8, 3])
Ytrain_onehot.shape
(50000, 10)
Ytrain[:5]
array([6, 9, 9, 4, 1])
Ytrain_onehot[:5]
array([[ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]])
定義共享函數
# 定義權值 def weight(shape): # 在構建模型時,需要用tf.Variable來創建一個變量 # 再訓練時,這個變量不斷更新 # 使用函數tf.truncated_normal(截斷的正太分布)生成標準差為0.1的隨機數來初始化權值 return tf.Variable(tf.truncated_normal(shape,stddev=0.1),name="W") # 定義偏置,初始值為0.1 def bias(shape): return tf.Variable(tf.constant(0.1,shape=shape),name="b") # 定義卷積操作,步長為1,padding為same def conv2d(x,W): return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding="SAME") # 定義池化操作 # 步長為2,即原尺寸的長和寬各除以2 def max_pool_2x2(x): return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
定義網絡結構
==圖像的特征提取==:通過卷積層1,降采樣層1,卷積層2以及降 采樣層2的處理,提取圖像的特征
==全連接神經網絡==:全連接層、輸出層所組成的網絡結構
"""輸入層,32*32圖像,通道為3RGB""" with tf.name_scope("input_layer"): x = tf.placeholder("float",shape=[None,32,32,3],name="x") """第一個卷積層""" with tf.name_scope("conv_1"): W1 = weight([3,3,3,32]) b1 = bias([32]) conv_1 = conv2d(x,W1)+b1 conv_1 = tf.nn.relu(conv_1) """第一個池化層""" with tf.name_scope("pool_1"): pool_1 = max_pool_2x2(conv_1) """第一個卷積層""" with tf.name_scope("conv_2"): W2 = weight([3,3,32,64]) b2 = bias([64]) conv_2 = conv2d(pool_1,W2)+b2 conv_2 = tf.nn.relu(conv_2)
"""第二個池化層""" with tf.name_scope("pool_2"): pool_2 = max_pool_2x2(conv_2) with tf.name_scope("fc"): W3 = weight([4096,128]) b3 = bias([128]) flat = tf.reshape(pool_2,[-1,4096]) h = tf.nn.relu(tf.matmul(flat,W3)+b3) h_dropout = tf.nn.dropout(h,keep_prob=0.8) with tf.name_scope("output_layer"): W4 = weight([128,10]) b4 = bias([10]) pred = tf.nn.softmax(tf.matmul(h_dropout,W4)+b4)
構建模型
with tf.name_scope("optimizer"): y = tf.placeholder("float",shape=[None,10],name ="label") loss_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y)) optimizer = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss_function)
定義準確率
with tf.name_scope("evaluation"): correct_prediction = tf.equal(tf.argmax(pred,1),tf.argmax(y,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
啟動會話
import os from time import time train_epochs = 25 batch_size = 50 total_batch = int(len(Xtrain)/batch_size) epoch_list=[] accuracy_list = [] loss_list=[]; epoch = tf.Variable(0,name="epoch",trainable=False) startTime = time() sess =tf.Session() init = tf.global_variables_initializer() sess.run(init)
斷點續訓
ckpt_dir = "CIFAR10_log/" if not os.path.exists(ckpt_dir): os.makedirs(ckpt_dir) saver = tf.train.Saver(max_to_keep=1) ckpt = tf.train.latest_checkpoint(ckpt_dir) if ckpt != None: saver.restore(sess,ckpt) else: print("Training from search") start = sess.run(epoch) print("Training starts from {} epoch".format(start+1))
Training from search Training starts from 1 epoch
迭代訓練
def get_train_batch(number, batch_size): return Xtrain_normalize[number*batch_size:(number+1)*batch_size],Ytrain_onehot[number*batch_size:(number+1)*batch_size] for ep in range(start, train_epochs): for i in range(total_batch): batch_x,batch_y = get_train_batch(i,batch_size) sess.run(optimizer, feed_dict= {x:batch_x, y:batch_y}) if i % 100 == 0: print("Step {}".format(i), "finished") loss,acc = sess.run([loss_function,accuracy],feed_dict={x: batch_x, y: batch_y}) epoch_list.append(ep+1) loss_list.append(loss) accuracy_list.append(acc) print("Train epoch:", "%02d" % (sess.run(epoch)+1),"Loss=","{:.6f}".format(loss),"Accuracy=",acc)#保存檢查點 saver.save(sess,ckpt_dir+"CIFAR10_cnn_model.cpkt",global_step=ep+1) sess.run(epoch.assign(ep+1)) duration =time()-startTime print("Train finished takes:",duration)
可視化損失
fig = plt.gcf() fig.set_size_inches(4,2) plt.plot(epoch_list,loss_list,label="loss") plt.ylabel("loss") plt.xlabel("epoch") plt.legend(["loss"],loc="best")
可視化準確率
plt.plot(epoch_list,accuracy_list,label="accuracy") fig = plt.gcf() fig.set_size_inches(4,2) plt.ylim(0.1,1) plt.ylabel("accuracy") plt.xlabel("epoch") plt.legend() plt.show()
評估模型及預測
test_total_batch = int(len(Xtest_normalize)/batch_size) test_acc_sum = 0.0 for i in range(test_total_batch): test_image_batch = Xtest_normalize[i*batch_size(i+1)*batch_size] test_label_batch = Ytest_onehot[i*batch_size:(i+1)*batch_size] test_batch_acc = sess.run(accuracy,feed_dict={x:test_image_batch,y:test_label_batch}) test_acc_sum += test_batch_acc test_acc = float(test_acc_sum/test_total_batch) print("Test accuracy:{:.6f}".format(test_acc))
test_pred = sess.run(pred,feed_dict={x:Xtest_normalize[:10]}) prediction_result = sess.run(tf.argmax(test_pred,1))
plot_images_labels_prediction(Xtest,Ytest,prediction_result,0,10)
到這里就結束了,如果對你有幫助,歡迎關注評論,你的對我很重要,author:北山啦
TensorFlow 圖像處理
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。