深度學習實驗項目一貓狗識別


項目來自唐老師貓狗識別項目及數據集。

項目具體實施步驟:

1.讀取貓狗數據訓練集500+500。

2.對讀取的圖片進行處理,處理成統一大小格式,分好標簽。

3.shuffle一下,將貓狗數據摻雜混合,盡可能隨機。

4.采用CNN網絡訓練測試。


 

具體代碼如下:

1.讀取訓練集。

import pandas as pd
import numpy as np
import os
import glob
import matplotlib.pyplot as plt
import cv2 as cv2
images = []
labels = []
img_names = []
cls = []
train_path="training_data"
classes = ['dogs','cats']
num_classes = len(classes)
image_size=128
print('Going to read training images')
for fields in classes:   
    index = classes.index(fields)
    print('Now going to read {} files (Index: {})'.format(fields, index))
    path = os.path.join(train_path, fields, '*g')
    files = glob.glob(path)
    print(len(files))
    for fl in files:
        image = cv2.imread(fl)
        image = cv2.resize(image, (image_size, image_size),0,0, cv2.INTER_LINEAR)
        image = image.astype(np.float32)
        image = np.multiply(image, 1.0 / 255.0)
        images.append(image)
        label = np.zeros(len(classes))
        label[index] = 1.0
        labels.append(label)
        flbase = os.path.basename(fl)
        img_names.append(flbase)
        cls.append(fields)
images = np.array(images)
labels = np.array(labels)
img_names = np.array(img_names)
cls = np.array(cls)

2.訓練數據集。

from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPool2D
from tensorflow.keras import Input
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from tensorflow.keras.regularizers import l1
#數據集處理
images,labels=shuffle(images,labels)
X_train, X_test, y_train, y_test=train_test_split(images,labels)
model = Sequential()
# 第一個卷積層,32個卷積核,大小5x5,卷積模式SAME,激活函數relu,輸入張量的大小
model.add(Conv2D(filters= 6, kernel_size=(3,3), padding='valid',kernel_regularizer=l1(0.1),activation='tanh',input_shape=(128,128,3)))
# model.add(Conv2D(filters= 32, kernel_size=(3,3), padding='valid', activation='relu'))
# 池化層,池化核大小2x2
model.add(MaxPool2D(pool_size=(2,2)))
# 隨機丟棄四分之一的網絡連接,防止過擬合
model.add(Dropout(0.5)) 
model.add(Conv2D(filters= 6, kernel_size=(3,3), padding='Same', activation='tanh'))
# model.add(Conv2D(filters= 6, kernel_size=(3,3), padding='Same', activation='tanh'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
model.add(Dropout(0.5))
# 全連接層,展開操作,
model.add(Flatten())
# 添加隱藏層神經元的數量和激活函數
# model.add(Dense(120, activation='tanh'))   
model.add(Dropout(0.5))
# model.add(Dense(84, activation='tanh'))   
# 輸出層
model.add(Dense(2, activation='softmax')) 
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
model.fit(images,labels,validation_split=0.2,batch_size=128,epochs=50)
#模型存儲
mp = "model_3_1625.h5"
model.save(mp)
#模型評價
model.evaluate(X_test,y_test)

訓練過程反饋如下:

評價結果:

 實驗心得(小白):

1.validation數據集,最好的評價指標達到0.64,隨着epoch訓練的增多,明顯出現過擬合的現象,准確率很多有小於0.5。

2.為了減少過擬合的影響,加入了正則化L1項目與dropout,有一些小的提升。

3.針對optimizer,...............

4.針對batch_size,設置了32,128,100,batch_size設置的小,訓練accuracy上下浮動,不會一直增加


問題:

1.查看第一層次卷積之后的結果

from tensorflow.keras import backend as K
layer_1 = K.function([model.layers[0].input], [model.layers[1].output])#第一個 model.layers[0],不修改,表示輸入數據;第二個model.layers[you wanted],修改為你需要輸出的層數的編號
f1 = layer_1([images])[0]#只修改inpu_image
# #第一層卷積后的特征圖展示,輸出是(1,149,149,32),(樣本個數,特征圖尺寸長,特征圖尺寸寬,特征圖個數)
# for _ in range(32):
#             show_img = f1[:, :, :, _]
#             show_img.shape = [63,63,3]
#             plt.subplot(4, 8, _ + 1)
#             plt.imshow(show_img, cmap='gray')
#             plt.axis('off')
# plt.show()
model.layers[0].input:128*128*3
model.layers[1].output:63*63*6

a.問什么會是6?

b.查看中間層結果,為什么會出現負值?

結果查看:

plt.figure(figsize=(15, 6.5))
x_axis=range(500)
y_axis_data1=history.history["accuracy"]
y_axis_data2=history.history["val_accuracy"]
plt.plot(x_axis, y_axis_data1, color='red', label="accuracy")
plt.plot(x_axis, y_axis_data2, color='blue', label="val_accuracy")
plt.hlines(0.65, 0, 500,color="red")#橫線
plt.hlines(0.7, 0, 500,color="red")#橫線
plt.hlines(0.6, 0, 500,color="red")#橫線
plt.show()

 

 

 

 

 

 

 

 

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM