聽說越來越卷,那我們就用卷積神經網路CNN來識別狗狗吧!!
文章目錄
- 聽說越來越卷,那我們就用卷積神經網路CNN來識別狗狗吧!!
- 一、識別狗狗
- 二、CNN簡介
- 三、環境的配置
- 1、Python
- 2、模塊
- 1)numpy
- 2)pandas
- 3)sklearn
- 4)keras
- 5)matplotlib.pyplot
- 6)os
- 7)TensorFlow
- 四、獲取狗狗的圖片資訊
- 五、資料的讀取
- 六、資料的預處理
- 七、資料集的分割(劃分為訓練集以及測驗集)
- 八、CNN 卷積神經網路的搭建
- 九、訓練CNN 卷積神經網路
- 十、得到模型
- 十一、進行測驗檢查預測的效果(檢驗測驗集)
- 十二、完整的代碼(全部原始碼)
- 十三、總結
一、識別狗狗
首先介紹博文的內容;
在這篇博文中,
我們將借助Python使用CNN卷積神經網路來進行狗臉的識別,我們知道狗狗是有120多種類別,我們本次的目標就是要通過深度學習使得機器可以區分出來這120種狗狗的類別并且較為準確的將結果告訴使用者,
總之一句話,識別不同狗狗的種類,
(下圖有一只貓,但是我們應該忽略它,因為我們在做狗臉識別,)

當然了,
想要區分貓和狗比給狗狗分類要簡單多了,畢竟,貓和狗,這是二元的分類問題;
而,狗狗的不同種類,多打120種,是一個比較多的多元分類,明顯會比較復雜了啦,
本文是要講解識別不同狗狗的種類了啦,
二、CNN簡介
CNN是卷積神經網路,
CNNs是深度卷積神經網路,
神經網路(neual networks)
是人工智能研究領域的一部分,當前最流行的神經網路是深度卷積神經網路(deep convolutional neural networks, CNNs)以及卷積神經網路(CNN),雖然卷積網路也存在淺層結構,但是因為準確度和表現力等原因很少使用,
目前提到CNNs和卷積神經網路(CNN),學術界和工業界不再進行特意區分,一般都指深層結構的卷積神經網路,層數從”幾層“到”幾十上百“不定,
基礎的CNN由
卷積(convolution), 激活(activation), and 池化(pooling)三種結構組成,
CNN輸出的結果是每幅影像的特定特征空間,當處理影像分類任務時,我們會把CNN輸出的特征空間作為全連接層或全連接神經網路(fully connected neural network, FCN)的輸入,用全連接層來完成從輸入影像到標簽集的映射,即分類,
當然,整個程序最重要的作業就是如何通過訓練資料迭代調整網路權重,也就是后向傳播演算法,
目前主流的卷積神經網路(CNNs),比如VGG, ResNet都是由簡單的CNN調整,組合而來,
例如,
對于數字八的識別,我們有如下的操作:

它的本質其實就是下面所示的啦,這就是卷積神經網路哦:

如果需要進行簡單的描述,那么就是,下面舉一個簡單的例子,如下所示:
將這個簡化

于是就變為了:

其實,CNN的本質還是梯度下降,只不過這里的反饋與前面的反饋有所不同而已,但是還是同樣的思路,
對于狗狗:

(卷積的操作)
在讀入狗狗的資料以后,還會進行例如下圖的類似操作,進行卷積以及池化:

還有:(RGB是通過紅、綠、藍三種顏色組合而成的顏色)

三、環境的配置
1、Python
人工智能我們還是建議使用Python來完成相關的作業,
同時,Python版本需要3.x的,
2、模塊
在Python中,我們在這個案例中需要用到不少的模塊;
此次我們將使用如下模塊:
1)numpy
這個模塊十分常用,是一個Python處理資料的科學模塊,此處我們會用來處理陣列、矩陣等資料型別的資料,
2)pandas
這個模塊也是十分常用的資料分析模塊,這里我們將主要用來處理DataFrame物件,其實本質上就是張量,
3)sklearn
這個是機器學習的一個模塊,
我們會用它來劃分資料集等等,
4)keras
這是構建卷積神經網路所需要的模塊,我們在這里將會通過Keras模塊來構建卷積神經網路,
5)matplotlib.pyplot
這個是繪圖模塊,可以較為清晰的呈現學習的狀況,
6)os
我們需要讀取資料,因此,os模塊可以較為方便的幫助我們處理檔案的查找以及路徑的尋找等問題,
7)TensorFlow
我們會間接的使用到TensorFlow模塊,
四、獲取狗狗的圖片資訊
原本這些資訊可以在Kaggle上面獲得:
(任意點擊一個就可以了,)
鏈接:
狗狗圖片匯總資料
鏈接是:https://www.kaggle.com/jessicali9530/stanford-dogs-dataset
但是,如果你不懂什么魔法之類的東西,那么你下載起來是很費勁的啦,
因此對于不懂魔法的用戶呢,我將這一份資料放在了我的資源里面,而且是永久免費的哦!!!
截圖為證:

這份永久免費的資源的地址是:
有些無良人士直接放空的資源,為了防止大家以為我是這樣的人,于是這個資源是免費的啦,
資源:
Stanford Dogs Dataset.zip
地址;
https://download.csdn.net/download/m0_54218263/21360497?spm=1001.2014.3001.5501
資料集里面的資料有:
120種狗狗的照片,每一種狗狗有150張照片,

五、資料的讀取
在讀取資料之前,我們首先匯入一些模塊:
import os
import cv2
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
# this is the split module
from tensorflow import keras
# keras
# use this method to import keras will not appear error
from tensorflow.keras import models, layers
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
接下來,我們進行資料的讀取操作;
(由于資料量巨大,我們這里僅僅只是做10種狗狗的識別,當然120種只需要修改種類數目就可以了啦,)
下面是讀取資料的代碼;
dogs_img_list = os.listdir("Stanford Dogs Dataset/images/images")
for i in dogs_img_list:
print(i)
root_dir = "Stanford Dogs Dataset/images/images/"
# root package
dog_img_dir = []
# dir of each dog
"""
because of the large quantities of data,
we just recognize 10 kinds of dogs
"""
for i in range(10):
# 10 kinds
dog_img_dir.append(root_dir + dogs_img_list[i])
# append the message of the dogs
X = []
# X
y_label = []
# y_label
img_size = 150
# the size of the img
def training_data(label, data_dir):
"""
read data and store them into the X and the y_label
:param label: the label of the dog, that means the kind of the dog
:param data_dir: data
:return: none
"""
print("reading...", data_dir)
# tip messages
for img in os.listdir(data_dir):
path = os.path.join(data_dir, img)
img = cv2.imread(path, cv2.IMREAD_COLOR)
img = cv2.resize(img, (img_size, img_size))
# X
X.append(np.array(img))
# y_label
y_label.append(str(label))
# use the method above to read the dog's data
for i in range(10):
# use 10 to test our main, 10 kinds
training_data(label=dog_img_dir[i].split("-")[1], data_dir=dog_img_dir[i])
執行代碼以后的效果圖如下:

大家注意一下,這里的紅色不是錯誤啊!!

這里讀出來了10種狗狗的資料,
六、資料的預處理
進行預處理的代碼如下所示;
# deal with the data
label_encoder = LabelEncoder()
# object
y = label_encoder.fit_transform(y_label)
y = to_categorical(y, 10)
# 10 kinds
X = np.array(X)
X = X / 255
# normalization
七、資料集的分割(劃分為訓練集以及測驗集)
資料集的分割比較簡單,就是把所有的資料分割為訓練集以及測驗集,一般按照80%和20%的比例來進行分割,
這里可以直接使用內置的方法進行分割,也可以自己分割,因為其實就是進行切片而已,這里我們采用內置的方法:
"""
4.split the data collection
"""
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
這里的代碼就是進行了資料集的分割操作,
這步很簡單了啦,
八、CNN 卷積神經網路的搭建
這一步,我們將要搭建卷積神經網路,
這個就是整個 CNN 卷積神經網路 識別狗狗的專案中的最關鍵的步驟之一,也是訓練機器的程序了啦,
"""
5.training the machine (CNN)
這里的步驟,包括了卷積、池化、卷積、池化的回圈操作,
以及,
展平的操作,
還有,
全連接層和分類輸出,
"""
# CNN
# cnn = models.Sequential()
cnn = models.Sequential()
cnn.add(layers.Conv2D(32, (3, 3), activation="relu", input_shape=(150, 150, 3)))
# 使用 relu 來進行激活神經元
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(64, (3, 3), activation="relu"))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(128, (3, 3), activation="relu"))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(128, (3, 3), activation="relu"))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Flatten())
# cnn.add(layers.Dense(512, activation="relu"))
cnn.add(layers.Dense(512, activation="relu"))
# cnn.add(Dense), 120
# cnn.add(layers.Dense(120, activation="softmax"))
cnn.add(layers.Dense(10, activation="softmax"))
# 使用 softmax 激活神經元
# cnn.add(layers.Dense(120, activation="softmax"))
# cnn.add(layers.Dense(120, activation="softmax"))
cnn.compile(loss="categorical_crossentropy", optimizer="rmsprop", metrics=['acc'])
# 損失函式, 優化器, 評估指標
# the code above is to create the cnn
# and the
# train the machine
下面描述一下卷積神經網路的結構:
(由于這里是圖片太長了,所以分割開來展示了)
1、

2、

3、

4、

其他的一些解釋如下:
這里解釋了一些名次和概念,

九、訓練CNN 卷積神經網路
這里是利用 fit 方法來進行訓練這個搭建好了的神經網路了啦,
"""
6.training the data and get the model
"""
# train
history = cnn.fit(X_train, y_train,
# 指定訓練集
epochs=50,
# 指定次數(或者說是輪次)
batch_size=256,
# 指定批量的大小
validation_data=(X_test, y_test)
# 指定驗證集
)

以上是使用一個函式進行訓練,十分快捷,

十、得到模型
其實模型是在上一個步驟中就已經處理好了,我們只需要呼叫就可以了,為了方便操作,我們首先只迭代一次來看看history究竟是怎么樣的吧:
# train
history = cnn.fit(X_train, y_train,
epochs=1,
batch_size=256,
validation_data=(X_test, y_test))
print(history.history)
print(history.epoch)
輸出如下:
1/6 [====>.........................] - ETA: 0s - loss: 2.3056 - acc: 0.12502021-08-20 10:53:05.338453: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 717750272 exceeds 10% of free system memory.
2021-08-20 10:53:09.549353: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 717750272 exceeds 10% of free system memory.
2/6 [=========>....................] - ETA: 11s - loss: 3.2431 - acc: 0.13092021-08-20 10:53:10.900707: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 717750272 exceeds 10% of free system memory.
6/6 [==============================] - 30s 5s/step - loss: 2.6397 - acc: 0.1107 - val_loss: 2.2892 - val_acc: 0.1901
{'loss': [2.639716863632202], 'acc': [0.11074918508529663], 'val_loss': [2.2892367839813232], 'val_acc': [0.1901041716337204]}
[0]
Process finished with exit code 0
這里體現了訓練的程序以及history的內容
History的原始碼是:
@keras_export('keras.callbacks.History')
class History(Callback):
"""Callback that records events into a `History` object.
This callback is automatically applied to
every Keras model. The `History` object
gets returned by the `fit` method of models.
Example:
>>> model = tf.keras.models.Sequential([tf.keras.layers.Dense(10)])
>>> model.compile(tf.keras.optimizers.SGD(), loss='mse')
>>> history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5),
... epochs=10)
>>> print(history.params)
{'verbose': 1, 'epochs': 10, 'steps': 1}
>>> # check the keys of history object
>>> print(history.history.keys())
dict_keys(['loss'])
"""
def __init__(self):
super(History, self).__init__()
self.history = {}
def on_train_begin(self, logs=None):
self.epoch = []
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
self.epoch.append(epoch)
for k, v in logs.items():
self.history.setdefault(k, []).append(v)
# Set the history attribute on the model after the epoch ends. This will
# make sure that the state which is set is the latest one.
self.model.history = self
可以參見Keras原始碼:
Keras
十一、進行測驗檢查預測的效果(檢驗測驗集)
檢驗測驗集的效果,我們主要是看損失的大小了啦,在上面所說的History物件中我們是可以看到loss的啦,測驗的效果通過上面的代碼就可以得到了啦,這里只需要單獨挑出來就好了,
print(history.history)
print(history.epoch)
print(history.history["loss"])
我們設法繪制出來訓練的程序的影像,這個需要matplotlib.pyplot,由于篇幅以及能力的限制(主要是能力的限制,這個模塊我掌握的不太好了啦),所以不展示原始碼,直接展示圖片,有興趣的大佬可以自己進行繪制了啦,

十二、完整的代碼(全部原始碼)
這是所有的源代碼,如下所示:
# CNN
"""
1.import modules
there are many modules we need to import
"""
import os
import cv2
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
# this is the split module
from tensorflow import keras
# keras
# use this method to import keras will not appear error
from tensorflow.keras import models, layers
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
"""
2.read the file of the dog pictures
"""
dogs_img_list = os.listdir("Stanford Dogs Dataset/images/images")
for i in dogs_img_list:
print(i)
root_dir = "Stanford Dogs Dataset/images/images/"
# root package
dog_img_dir = []
# dir of each dog
"""
because of the large quantities of data,
we just recognize 10 kinds of dogs
"""
for i in range(10):
# 10 kinds
dog_img_dir.append(root_dir + dogs_img_list[i])
# append the message of the dogs
X = []
# X
y_label = []
# y_label
img_size = 150
# the size of the img
def training_data(label, data_dir):
"""
read data and store them into the X and the y_label
:param label: the label of the dog, that means the kind of the dog
:param data_dir: data
:return: none
"""
print("reading...", data_dir)
# tip messages
for img in os.listdir(data_dir):
path = os.path.join(data_dir, img)
img = cv2.imread(path, cv2.IMREAD_COLOR)
img = cv2.resize(img, (img_size, img_size))
# X
X.append(np.array(img))
# y_label
y_label.append(str(label))
# use the method above to read the dog's data
for i in range(10):
# use 10 to test our main, 10 kinds
training_data(label=dog_img_dir[i].split("-")[1], data_dir=dog_img_dir[i])
"""
3.deal with the data before deep learning
"""
# deal with the data
label_encoder = LabelEncoder()
# object
y = label_encoder.fit_transform(y_label)
# 這是標簽編碼
y = to_categorical(y, 10)
# 這是 One-hot 編碼
# 10 kinds dog
X = np.array(X)
X = X / 255
# normalization
"""
4.split the data collection
"""
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
"""
5.training the machine (CNN)
這里的步驟,包括了卷積、池化、卷積、池化的回圈操作,
以及,
展平的操作,
還有,
全連接層和分類輸出,
"""
# CNN
# cnn = models.Sequential()
cnn = models.Sequential()
cnn.add(layers.Conv2D(32, (3, 3), activation="relu", input_shape=(150, 150, 3)))
# 使用 relu 來進行激活神經元
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(64, (3, 3), activation="relu"))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(128, (3, 3), activation="relu"))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(128, (3, 3), activation="relu"))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Flatten())
# cnn.add(layers.Dense(512, activation="relu"))
cnn.add(layers.Dense(512, activation="relu"))
# cnn.add(Dense), 120
# cnn.add(layers.Dense(120, activation="softmax"))
cnn.add(layers.Dense(10, activation="softmax"))
# 使用 softmax 激活神經元
# cnn.add(layers.Dense(120, activation="softmax"))
# cnn.add(layers.Dense(120, activation="softmax"))
cnn.compile(loss="categorical_crossentropy", optimizer="rmsprop", metrics=['acc'])
# 損失函式, 優化器, 評估指標
# the code above is to create the cnn
# and the
# train the machine
"""
6.training the data and get the model
"""
#
# train
#
#
history = cnn.fit(X_train, y_train,
# 指定訓練集
epochs=50,
# 指定次數(或者說是輪次)
batch_size=256,
# 指定批量的大小
validation_data=(X_test, y_test)
# 指定驗證集
)
"""
7.show
"""
# show the training result :
print(history)
print(history.history)
print(history.epoch)
# use the dict to show:
print(history.history["loss"])
print(history.history["acc"])
print(history.history["val_loss"])
print(history.history["val_acc"])
下面,
我們還是給出來我第一次學習的時候的源代碼,
如下所示:
from tensorflow import keras
from tensorflow.keras import models, layers
# keras models
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
import numpy as np
import pandas as pd
import os
import cv2
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
import random as rdm
from sklearn.model_selection import train_test_split
# to_categorical
dogs_img_list = os.listdir("Stanford Dogs Dataset/images/images")
# read the file name
# print(f'{os.listdir("Stanford Dogs Dataset/images/images")}' +
# "\n\n" +
# f"{len(dogs_img_list)}")
# print message
# read the data
dog_img_dir = []
root_dir = "Stanford Dogs Dataset/images/images/"
# root file
for i in range(len(dogs_img_list)):
dog_img_dir.append(root_dir + dogs_img_list[i])
# append the message of the dogs
# and the total of the dogs is 120
# print(dog_img_dir)
X = []
# X
y_label = []
# y_label
img_size = 150
# read the data again
def training_data(label, data_dir):
print("reading...", data_dir)
for img in os.listdir(data_dir):
path = os.path.join(data_dir, img)
img = cv2.imread(path, cv2.IMREAD_COLOR)
img = cv2.resize(img, (img_size, img_size))
# X
X.append(np.array(img))
# y_label
y_label.append(str(label))
# use the method to read dog data
for i in range(10): # use 10 to test our main
training_data(label=dog_img_dir[i].split("-")[1], data_dir=dog_img_dir[i])
# deal with the data
label_encoder = LabelEncoder()
# object
y = label_encoder.fit_transform(y_label)
y = to_categorical(y, 10) # 120
# 120
X = np.array(X)
X = X / 255
# deal
# show
# fig, ax = plt.subplots(5, 2)
# fig.set_size_inches(15, 15)
# for i in range(5):
# for j in range(2):
# r = rdm.randint(0, len(X))
# ax[i, j].imshow(X[r])
# ax[i, j].set_title('Dog:' + y_label[r])
# plt.tight_layout()
# plt.show()
# split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# CNN
# cnn = models.Sequential()
cnn = models.Sequential()
cnn.add(layers.Conv2D(32, (3, 3), activation="relu", input_shape=(150, 150, 3)))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(64, (3, 3), activation="relu"))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(128, (3, 3), activation="relu"))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(128, (3, 3), activation="relu"))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Flatten())
# cnn.add(layers.Dense(512, activation="relu"))
cnn.add(layers.Dense(512, activation="relu"))
# cnn.add(Dense), 120
# cnn.add(layers.Dense(120, activation="softmax"))
cnn.add(layers.Dense(10, activation="softmax"))
# cnn.add(layers.Dense(120, activation="softmax"))
# cnn.add(layers.Dense(120, activation="softmax"))
cnn.compile(loss="categorical_crossentropy", optimizer="rmsprop", metrics=['acc'])
# create the cnn
# train
history = cnn.fit(X_train, y_train,
epochs=50,
batch_size=256,
validation_data=(X_test, y_test))
print(history.history)
print(history.epoch)
print(history.history["loss"])
print(history.history["acc"])
print(history.history["val_loss"])
print(history.history["val_acc"])
運行代碼的結果展示:
Epoch 1/50
6/6 [==============================] - 24s 4s/step - loss: 2.8308 - acc: 0.1309 - val_loss: 2.2881 - val_acc: 0.0911
Epoch 2/50
6/6 [==============================] - 27s 5s/step - loss: 2.2932 - acc: 0.1479 - val_loss: 2.2671 - val_acc: 0.1875
Epoch 3/50
6/6 [==============================] - 27s 5s/step - loss: 2.2643 - acc: 0.1831 - val_loss: 2.2666 - val_acc: 0.1328
Epoch 4/50
6/6 [==============================] - 27s 4s/step - loss: 2.2247 - acc: 0.1635 - val_loss: 2.2222 - val_acc: 0.2083
Epoch 5/50
6/6 [==============================] - 22s 4s/step - loss: 2.4839 - acc: 0.1993 - val_loss: 2.2434 - val_acc: 0.1901
Epoch 6/50
6/6 [==============================] - 22s 4s/step - loss: 2.1675 - acc: 0.2306 - val_loss: 2.1828 - val_acc: 0.1953
Epoch 7/50
6/6 [==============================] - 25s 4s/step - loss: 2.1461 - acc: 0.2521 - val_loss: 2.1681 - val_acc: 0.2396
Epoch 8/50
6/6 [==============================] - 31s 5s/step - loss: 2.0721 - acc: 0.2775 - val_loss: 2.3079 - val_acc: 0.1927
Epoch 9/50
6/6 [==============================] - 28s 5s/step - loss: 2.0610 - acc: 0.2879 - val_loss: 2.2057 - val_acc: 0.2396
Epoch 10/50
6/6 [==============================] - 28s 5s/step - loss: 1.9759 - acc: 0.3270 - val_loss: 2.0423 - val_acc: 0.3099
Epoch 11/50
6/6 [==============================] - 27s 5s/step - loss: 1.8886 - acc: 0.3336 - val_loss: 2.0655 - val_acc: 0.2448
Epoch 12/50
6/6 [==============================] - 27s 5s/step - loss: 1.8554 - acc: 0.3687 - val_loss: 2.0126 - val_acc: 0.3203
Epoch 13/50
6/6 [==============================] - 28s 5s/step - loss: 1.9623 - acc: 0.3283 - val_loss: 1.9592 - val_acc: 0.3307
Epoch 14/50
6/6 [==============================] - 25s 4s/step - loss: 1.7121 - acc: 0.4104 - val_loss: 2.0791 - val_acc: 0.2839
Epoch 15/50
6/6 [==============================] - 26s 4s/step - loss: 1.6828 - acc: 0.4156 - val_loss: 2.0520 - val_acc: 0.3698
Epoch 16/50
6/6 [==============================] - 28s 5s/step - loss: 1.6389 - acc: 0.4515 - val_loss: 1.9933 - val_acc: 0.3307
Epoch 17/50
6/6 [==============================] - 28s 5s/step - loss: 1.5336 - acc: 0.4638 - val_loss: 2.2346 - val_acc: 0.2292
Epoch 18/50
6/6 [==============================] - 28s 5s/step - loss: 1.6407 - acc: 0.4463 - val_loss: 1.9287 - val_acc: 0.3411
Epoch 19/50
6/6 [==============================] - 27s 4s/step - loss: 1.3706 - acc: 0.5290 - val_loss: 2.3511 - val_acc: 0.3047
Epoch 20/50
6/6 [==============================] - 27s 4s/step - loss: 1.3332 - acc: 0.5446 - val_loss: 2.1613 - val_acc: 0.2995
Epoch 21/50
6/6 [==============================] - 25s 4s/step - loss: 1.1409 - acc: 0.6143 - val_loss: 2.1760 - val_acc: 0.2552
Epoch 22/50
6/6 [==============================] - 34s 6s/step - loss: 1.0946 - acc: 0.6391 - val_loss: 2.3750 - val_acc: 0.3281
Epoch 23/50
6/6 [==============================] - 29s 5s/step - loss: 1.0019 - acc: 0.6606 - val_loss: 2.7618 - val_acc: 0.3047
Epoch 24/50
6/6 [==============================] - 28s 5s/step - loss: 0.9748 - acc: 0.6664 - val_loss: 2.1869 - val_acc: 0.3750
Epoch 25/50
6/6 [==============================] - 27s 5s/step - loss: 0.7259 - acc: 0.7518 - val_loss: 2.3144 - val_acc: 0.3385
Epoch 26/50
6/6 [==============================] - 26s 4s/step - loss: 0.5905 - acc: 0.8046 - val_loss: 2.4587 - val_acc: 0.3177
Epoch 27/50
6/6 [==============================] - 26s 4s/step - loss: 0.6956 - acc: 0.7524 - val_loss: 2.9642 - val_acc: 0.2891
Epoch 28/50
6/6 [==============================] - 26s 4s/step - loss: 0.4431 - acc: 0.8619 - val_loss: 2.7440 - val_acc: 0.3620
Epoch 29/50
6/6 [==============================] - 27s 5s/step - loss: 0.3499 - acc: 0.9010 - val_loss: 4.2927 - val_acc: 0.2630
Epoch 30/50
6/6 [==============================] - 27s 5s/step - loss: 0.9952 - acc: 0.7629 - val_loss: 2.5862 - val_acc: 0.3542
Epoch 31/50
6/6 [==============================] - 21s 4s/step - loss: 0.1439 - acc: 0.9746 - val_loss: 3.0547 - val_acc: 0.3229
Epoch 32/50
6/6 [==============================] - 26s 4s/step - loss: 0.0806 - acc: 0.9850 - val_loss: 3.7483 - val_acc: 0.3203
Epoch 33/50
6/6 [==============================] - 29s 5s/step - loss: 0.5830 - acc: 0.8436 - val_loss: 3.2535 - val_acc: 0.3281
Epoch 34/50
6/6 [==============================] - 28s 5s/step - loss: 0.0856 - acc: 0.9876 - val_loss: 3.3920 - val_acc: 0.3802
Epoch 35/50
6/6 [==============================] - 27s 5s/step - loss: 0.0267 - acc: 0.9961 - val_loss: 3.7781 - val_acc: 0.3802
Epoch 36/50
6/6 [==============================] - 21s 3s/step - loss: 0.0203 - acc: 0.9974 - val_loss: 4.0570 - val_acc: 0.3568
Epoch 37/50
6/6 [==============================] - 27s 5s/step - loss: 0.0121 - acc: 0.9993 - val_loss: 4.2549 - val_acc: 0.3828
Epoch 38/50
6/6 [==============================] - 28s 5s/step - loss: 1.1171 - acc: 0.7844 - val_loss: 2.7973 - val_acc: 0.3047
Epoch 39/50
6/6 [==============================] - 29s 5s/step - loss: 0.2973 - acc: 0.9094 - val_loss: 3.1806 - val_acc: 0.3672
Epoch 40/50
6/6 [==============================] - 30s 5s/step - loss: 0.0297 - acc: 0.9987 - val_loss: 3.5742 - val_acc: 0.3620
Epoch 41/50
6/6 [==============================] - 29s 5s/step - loss: 0.0139 - acc: 0.9993 - val_loss: 3.8064 - val_acc: 0.3464
Epoch 42/50
6/6 [==============================] - 27s 5s/step - loss: 0.0142 - acc: 0.9987 - val_loss: 3.9564 - val_acc: 0.3750
Epoch 43/50
6/6 [==============================] - 28s 5s/step - loss: 0.0124 - acc: 0.9987 - val_loss: 4.1338 - val_acc: 0.3542
Epoch 44/50
6/6 [==============================] - 26s 4s/step - loss: 0.0080 - acc: 0.9987 - val_loss: 4.8518 - val_acc: 0.3333
Epoch 45/50
6/6 [==============================] - 26s 4s/step - loss: 1.5938 - acc: 0.7107 - val_loss: 3.4182 - val_acc: 0.2891
Epoch 46/50
6/6 [==============================] - 24s 4s/step - loss: 0.1018 - acc: 0.9811 - val_loss: 3.4852 - val_acc: 0.3620
Epoch 47/50
6/6 [==============================] - 24s 4s/step - loss: 0.0216 - acc: 0.9987 - val_loss: 3.8207 - val_acc: 0.3516
Epoch 48/50
6/6 [==============================] - 24s 4s/step - loss: 0.0133 - acc: 0.9987 - val_loss: 4.0269 - val_acc: 0.3594
Epoch 49/50
6/6 [==============================] - 24s 4s/step - loss: 0.0069 - acc: 0.9993 - val_loss: 4.2136 - val_acc: 0.3490
Epoch 50/50
6/6 [==============================] - 24s 4s/step - loss: 0.0062 - acc: 0.9993 - val_loss: 4.4008 - val_acc: 0.3542
<tensorflow.python.keras.callbacks.History object at 0x000001F137C33D60>
{'loss': [2.8308050632476807, 2.29323410987854, 2.2643165588378906, 2.224742889404297, 2.483851909637451, 2.167505979537964, 2.1461405754089355, 2.0721473693847656, 2.060950756072998, 1.975879430770874, 1.8886222839355469, 1.8553621768951416, 1.9623366594314575, 1.712108850479126, 1.6827651262283325, 1.6388875246047974, 1.5335570573806763, 1.6407216787338257, 1.3706496953964233, 1.3331533670425415, 1.1409199237823486, 1.0945615768432617, 1.0019456148147583, 0.9748384356498718, 0.725939929485321, 0.5904505848884583, 0.6956182718276978, 0.44307371973991394, 0.34993064403533936, 0.9951552748680115, 0.14394378662109375, 0.08060862123966217, 0.5830209851264954, 0.08558905124664307, 0.02666712924838066, 0.02026943676173687, 0.012054797261953354, 1.1171202659606934, 0.2973436713218689, 0.029678216204047203, 0.013883446343243122, 0.014234034344553947, 0.01236400380730629, 0.008039926178753376, 1.593753457069397, 0.10175592452287674, 0.021587485447525978, 0.013339064083993435, 0.0068833790719509125, 0.006158844102174044], 'acc': [0.13094462454319, 0.14788272976875305, 0.183061882853508, 0.16351792216300964, 0.1993485391139984, 0.2306188941001892, 0.2521172761917114, 0.2775244414806366, 0.2879478931427002, 0.3270358443260193, 0.33355048298835754, 0.3687296509742737, 0.32833877205848694, 0.4104234576225281, 0.4156351685523987, 0.45146578550338745, 0.4638436436653137, 0.44625407457351685, 0.5289902091026306, 0.5446254014968872, 0.6143322587013245, 0.639087975025177, 0.6605862975120544, 0.6664494872093201, 0.7517915368080139, 0.8045602440834045, 0.7524430155754089, 0.8618892431259155, 0.9009771943092346, 0.7628664374351501, 0.9745928049087524, 0.9850162863731384, 0.8436481952667236, 0.9876221418380737, 0.9960911870002747, 0.9973941445350647, 0.999348521232605, 0.7843648195266724, 0.9094462394714355, 0.99869704246521, 0.999348521232605, 0.99869704246521, 0.99869704246521, 0.99869704246521, 0.7107492089271545, 0.9811074733734131, 0.99869704246521, 0.99869704246521, 0.999348521232605, 0.999348521232605], 'val_loss': [2.288085699081421, 2.267096996307373, 2.2665584087371826, 2.222180128097534, 2.243370532989502, 2.182835340499878, 2.168098211288452, 2.307891607284546, 2.2056803703308105, 2.0422720909118652, 2.06552791595459, 2.0126335620880127, 1.9592050313949585, 2.0791280269622803, 2.0520381927490234, 1.9932855367660522, 2.2345681190490723, 1.9287039041519165, 2.351067304611206, 2.1612789630889893, 2.1759769916534424, 2.3750038146972656, 2.7618110179901123, 2.1868669986724854, 2.314356565475464, 2.458726167678833, 2.9641971588134766, 2.743975877761841, 4.292697429656982, 2.5861711502075195, 3.0546534061431885, 3.7482855319976807, 3.2534897327423096, 3.3919646739959717, 3.778139114379883, 4.056985378265381, 4.254880428314209, 2.797269582748413, 3.180584192276001, 3.5741913318634033, 3.8063831329345703, 3.956415891647339, 4.133772373199463, 4.851773738861084, 3.4182348251342773, 3.485194444656372, 3.8206732273101807, 4.026876449584961, 4.213562488555908, 4.400800704956055], 'val_acc': [0.0911458358168602, 0.1875, 0.1328125, 0.2083333283662796, 0.1901041716337204, 0.1953125, 0.2395833283662796, 0.1927083283662796, 0.2395833283662796, 0.3098958432674408, 0.2447916716337204, 0.3203125, 0.3307291567325592, 0.2838541567325592, 0.3697916567325592, 0.3307291567325592, 0.2291666716337204, 0.3411458432674408, 0.3046875, 0.2994791567325592, 0.2552083432674408, 0.328125, 0.3046875, 0.375, 0.3385416567325592, 0.3177083432674408, 0.2890625, 0.3619791567325592, 0.2630208432674408, 0.3541666567325592, 0.3229166567325592, 0.3203125, 0.328125, 0.3802083432674408, 0.3802083432674408, 0.3567708432674408, 0.3828125, 0.3046875, 0.3671875, 0.3619791567325592, 0.3463541567325592, 0.375, 0.3541666567325592, 0.3333333432674408, 0.2890625, 0.3619791567325592, 0.3515625, 0.359375, 0.3489583432674408, 0.3541666567325592]}
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[2.8308050632476807, 2.29323410987854, 2.2643165588378906, 2.224742889404297, 2.483851909637451, 2.167505979537964, 2.1461405754089355, 2.0721473693847656, 2.060950756072998, 1.975879430770874, 1.8886222839355469, 1.8553621768951416, 1.9623366594314575, 1.712108850479126, 1.6827651262283325, 1.6388875246047974, 1.5335570573806763, 1.6407216787338257, 1.3706496953964233, 1.3331533670425415, 1.1409199237823486, 1.0945615768432617, 1.0019456148147583, 0.9748384356498718, 0.725939929485321, 0.5904505848884583, 0.6956182718276978, 0.44307371973991394, 0.34993064403533936, 0.9951552748680115, 0.14394378662109375, 0.08060862123966217, 0.5830209851264954, 0.08558905124664307, 0.02666712924838066, 0.02026943676173687, 0.012054797261953354, 1.1171202659606934, 0.2973436713218689, 0.029678216204047203, 0.013883446343243122, 0.014234034344553947, 0.01236400380730629, 0.008039926178753376, 1.593753457069397, 0.10175592452287674, 0.021587485447525978, 0.013339064083993435, 0.0068833790719509125, 0.006158844102174044]
[0.13094462454319, 0.14788272976875305, 0.183061882853508, 0.16351792216300964, 0.1993485391139984, 0.2306188941001892, 0.2521172761917114, 0.2775244414806366, 0.2879478931427002, 0.3270358443260193, 0.33355048298835754, 0.3687296509742737, 0.32833877205848694, 0.4104234576225281, 0.4156351685523987, 0.45146578550338745, 0.4638436436653137, 0.44625407457351685, 0.5289902091026306, 0.5446254014968872, 0.6143322587013245, 0.639087975025177, 0.6605862975120544, 0.6664494872093201, 0.7517915368080139, 0.8045602440834045, 0.7524430155754089, 0.8618892431259155, 0.9009771943092346, 0.7628664374351501, 0.9745928049087524, 0.9850162863731384, 0.8436481952667236, 0.9876221418380737, 0.9960911870002747, 0.9973941445350647, 0.999348521232605, 0.7843648195266724, 0.9094462394714355, 0.99869704246521, 0.999348521232605, 0.99869704246521, 0.99869704246521, 0.99869704246521, 0.7107492089271545, 0.9811074733734131, 0.99869704246521, 0.99869704246521, 0.999348521232605, 0.999348521232605]
[2.288085699081421, 2.267096996307373, 2.2665584087371826, 2.222180128097534, 2.243370532989502, 2.182835340499878, 2.168098211288452, 2.307891607284546, 2.2056803703308105, 2.0422720909118652, 2.06552791595459, 2.0126335620880127, 1.9592050313949585, 2.0791280269622803, 2.0520381927490234, 1.9932855367660522, 2.2345681190490723, 1.9287039041519165, 2.351067304611206, 2.1612789630889893, 2.1759769916534424, 2.3750038146972656, 2.7618110179901123, 2.1868669986724854, 2.314356565475464, 2.458726167678833, 2.9641971588134766, 2.743975877761841, 4.292697429656982, 2.5861711502075195, 3.0546534061431885, 3.7482855319976807, 3.2534897327423096, 3.3919646739959717, 3.778139114379883, 4.056985378265381, 4.254880428314209, 2.797269582748413, 3.180584192276001, 3.5741913318634033, 3.8063831329345703, 3.956415891647339, 4.133772373199463, 4.851773738861084, 3.4182348251342773, 3.485194444656372, 3.8206732273101807, 4.026876449584961, 4.213562488555908, 4.400800704956055]
[0.0911458358168602, 0.1875, 0.1328125, 0.2083333283662796, 0.1901041716337204, 0.1953125, 0.2395833283662796, 0.1927083283662796, 0.2395833283662796, 0.3098958432674408, 0.2447916716337204, 0.3203125, 0.3307291567325592, 0.2838541567325592, 0.3697916567325592, 0.3307291567325592, 0.2291666716337204, 0.3411458432674408, 0.3046875, 0.2994791567325592, 0.2552083432674408, 0.328125, 0.3046875, 0.375, 0.3385416567325592, 0.3177083432674408, 0.2890625, 0.3619791567325592, 0.2630208432674408, 0.3541666567325592, 0.3229166567325592, 0.3203125, 0.328125, 0.3802083432674408, 0.3802083432674408, 0.3567708432674408, 0.3828125, 0.3046875, 0.3671875, 0.3619791567325592, 0.3463541567325592, 0.375, 0.3541666567325592, 0.3333333432674408, 0.2890625, 0.3619791567325592, 0.3515625, 0.359375, 0.3489583432674408, 0.3541666567325592]
一共運行了50次輪回,
十三、總結
以上就是我們使用CNN卷積神經網路來寫的一個狗狗的類別的識別的一個小的機器識別學習專案的所有內容,這里我們做了10個種類的狗狗的識別作業,如果是120種的話,直接把引數設定為120種就可以了啦,
以上是進行了CNN的應用,沒有涉及到太多的原理,后續會出一期博文手寫CNN演算法,
希望本文對大家有一定的幫助了啦,
最后,感謝大家的閱讀與支持了啦,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/295326.html
標籤:AI
上一篇:AndrewNg機器學習編程作業python實作及心得總結
下一篇:Deep Learning for EEG-Based Preference Classification in Neuromarketing文章精讀導讀,深度學習在神經營銷中基于腦電的偏好分類
