熱文 | 卷積神經(jīng)網(wǎng)絡(luò)入門案例,輕松實(shí)現(xiàn)花朵分類(3)
正則化
正則化的方法有多種,這里使用 Dropout 應(yīng)用到網(wǎng)絡(luò)層中,它會(huì)隨機(jī)將一部分神經(jīng)元的激活值停止工作,在訓(xùn)練過程中從該層中暫時(shí)退出,從而不對(duì)輸出產(chǎn)生影響;后續(xù)訓(xùn)練先恢復(fù)之前被停止工作的神經(jīng)元,再隨機(jī)將一部分神經(jīng)元停止工作,再訓(xùn)練。
這樣使模型不會(huì)太依賴某些局部的特征,泛化性更強(qiáng)。a圖全連接結(jié)構(gòu)的模型。b圖是在a網(wǎng)絡(luò)結(jié)構(gòu)基礎(chǔ)上,使用 Dropout后,隨機(jī)將一部分神經(jīng)元的暫時(shí)停止工作。
訓(xùn)練流程:
首先隨機(jī)(臨時(shí))刪除網(wǎng)絡(luò)中一些的隱藏層神經(jīng)元(退出此次訓(xùn)練),輸入輸出神經(jīng)元保存不變。
然后把輸入x通過修改后的網(wǎng)絡(luò)前向傳播,得到的損失結(jié)果通過修改后的網(wǎng)絡(luò)反向傳播;一批訓(xùn)練樣本執(zhí)行完這個(gè)過程后,在沒有被刪除的神經(jīng)元上按照梯度下降法更新對(duì)應(yīng)的參數(shù)(w, b)。
最后重復(fù)1、2步過程?;謴?fù)被刪掉的神經(jīng)元,此時(shí)被刪除的神經(jīng)元保持原樣,而沒有被刪除的神經(jīng)元已經(jīng)更新相關(guān)參數(shù)。
參考:Dropout(正則化)
Dropout 以一小部分?jǐn)?shù)字作為其輸入值,形式為 0.1、0.2、0.4 等。使得此層的10%、20%、40%的神經(jīng)元被暫時(shí)停止工作。
下面使用:layers.Dropout(0.2)
model = Sequential([ data_augmentation, layers.experimental.preprocessing.Rescaling(1./255), layers.Conv2D(16, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(32, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(64, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Dropout(0.2), layers.Flatten(), layers.Dense(128, activation='relu'), layers.Dense(num_classes) ])
重新編譯和訓(xùn)練模型
# 編譯模型 model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) # 查看網(wǎng)絡(luò)結(jié)構(gòu) model.summary() # 訓(xùn)練模型 epochs = 15 history = model.fit( train_ds, validation_data=val_ds, epochs=epochs )
在訓(xùn)練和驗(yàn)證集上查看損失值和準(zhǔn)確性:
acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs_range = range(epochs) plt.figure(figsize=(8, 8)) plt.subplot(1, 2, 1) plt.plot(epochs_range, acc, label='Training Accuracy') plt.plot(epochs_range, val_acc, label='Validation Accuracy') plt.legend(loc='lower right') plt.title('Training and Validation Accuracy') plt.subplot(1, 2, 2) plt.plot(epochs_range, loss, label='Training Loss') plt.plot(epochs_range, val_loss, label='Validation Loss') plt.legend(loc='upper right') plt.title('Training and Validation Loss') plt.show()
對(duì)比之前模型的效果,差別還是挺大的;使用數(shù)據(jù)增強(qiáng)、正則化后的模型,降低了過擬合的影響;驗(yàn)證集的損失和模型準(zhǔn)確度,與訓(xùn)練集更接近了。
預(yù)測(cè)新數(shù)據(jù)
# 預(yù)測(cè)新數(shù)據(jù) 下載一張新圖片,來預(yù)測(cè)它屬于什么類型花朵 sunflower_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/592px-Red_sunflower.jpg" sunflower_path = tf.keras.utils.get_file('Red_sunflower', origin=sunflower_url) img = keras.preprocessing.image.load_img( sunflower_path, target_size=(img_height, img_width) ) img_array = keras.preprocessing.image.img_to_array(img) img_array = tf.expand_dims(img_array, 0) # Create a batch predictions = model.predict(img_array) score = tf.nn.softmax(predictions[0]) print( "該圖像最有可能屬于{},置信度為 {:.2f}%" .format(class_names[n
p.argmax(score)], 100 * np.max(score)) )
該圖像最有可能屬于sunflowers,置信度為 97.38%
完整代碼
''' 環(huán)境:Tensorflow2 Python3.x ''' import matplotlib.pyplot as plt import numpy as np import os import PIL import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.models import Sequential # 下載數(shù)據(jù)集 import pathlib dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz" data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True) data_dir = pathlib.Path(data_dir) # 查看數(shù)據(jù)集圖片的總數(shù)量 image_count = len(list(data_dir.glob('*/*.jpg'))) print(image_count) # 查看郁金香tulips目錄下的第1張圖片; tulips = list(data_dir.glob('tulips/*')) PIL.Image.open(str(tulips[0])) # 定義加載圖片的一些參數(shù),包括:批量大小、圖像高度、圖像寬度 batch_size = 32 img_height = 180 img_width = 180 # 將80%的圖像用于訓(xùn)練 train_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset="training", seed=123, image_size=(img_height, img_width), batch_size=batch_size) # 將20%的圖像用于驗(yàn)證 val_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset="validation", seed=123, image_size=(img_height, img_width), batch_size=batch_size) # 打印數(shù)據(jù)集中花朵的類別名稱,字母順序?qū)?yīng)于目錄名稱 class_names = train_ds.class_names print(class_names) # 將像素的值標(biāo)準(zhǔn)化至0到1的區(qū)間內(nèi)。 normalization_layer = layers.experimental.preprocessing.Rescaling(1./255) # 調(diào)用map將其應(yīng)用于數(shù)據(jù)集: normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y)) image_batch, labels_batch = next(iter(normalized_ds)) first_image = image_batch[0] # Notice the pixels values are now in `[0,1]`. print(np.min(first_image), np.max(first_image)) # 數(shù)據(jù)增強(qiáng) 通過對(duì)已有的訓(xùn)練集圖片 隨機(jī)轉(zhuǎn)換(反轉(zhuǎn)、旋轉(zhuǎn)、縮放等),來生成其它訓(xùn)練數(shù)據(jù) data_augmentation = keras.Sequential( [ layers.experimental.preprocessing.RandomFlip("horizontal", input_shape=(img_height, img_width, 3)), layers.experimental.preprocessing.RandomRotation(0.1), layers.experimental.preprocessing.RandomZoom(0.1), ] ) # 搭建 網(wǎng)絡(luò)模型 model = Sequential([ data_augmentation, layers.experimental.preprocessing.Rescaling(1./255), layers.Conv2D(16, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(32, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(64, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Dropout(0.2), layers.Flatten(), layers.Dense(128, activation='relu'), layers.Dense(num_classes) ]) # 編譯模型 model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) # 查看網(wǎng)絡(luò)結(jié)構(gòu) model.summary() # 訓(xùn)練模型 epochs = 15 history = model.fit( train_ds, validation_data=val_ds, epochs=epochs ) # 在訓(xùn)練和驗(yàn)證集上查看損失值和準(zhǔn)確性 acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs_range = range(epochs) plt.figure(figsize=(8, 8)) plt.subplot(1, 2, 1) plt.plot(epochs_range, acc, label='Training Accuracy') plt.plot(epochs_range, val_acc, label='Validation Accuracy') plt.legend(loc='lower right') plt.title('Training and Validation Accuracy') plt.subplot(1, 2, 2) plt.plot(epochs_range, loss, label='Training Loss') plt.plot(epochs_range, val_loss, label='Validation Loss') plt.legend(loc='upper right') plt.title('Training and Validation Loss') plt.show()
作者簡(jiǎn)介:黎國(guó)溥,華為云-云享專家、CSDN博客專家、華為云-云創(chuàng)·首席貢獻(xiàn)官、華為云-年度社區(qū)風(fēng)云人物
參考鏈接:
https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/images/classification.ipynb#scrollTo=L1WtoaOHVrVh
原文鏈接:
https://blog.csdn.net/qq_41204464/article/details/116567051
*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。