熱文 | 卷積神經網(wǎng)絡入門案例,輕松實現(xiàn)花朵分類(3)
正則化
正則化的方法有多種,這里使用 Dropout 應用到網(wǎng)絡層中,它會隨機將一部分神經元的激活值停止工作,在訓練過程中從該層中暫時退出,從而不對輸出產生影響;后續(xù)訓練先恢復之前被停止工作的神經元,再隨機將一部分神經元停止工作,再訓練。
這樣使模型不會太依賴某些局部的特征,泛化性更強。a圖全連接結構的模型。b圖是在a網(wǎng)絡結構基礎上,使用 Dropout后,隨機將一部分神經元的暫時停止工作。
訓練流程:
首先隨機(臨時)刪除網(wǎng)絡中一些的隱藏層神經元(退出此次訓練),輸入輸出神經元保存不變。
然后把輸入x通過修改后的網(wǎng)絡前向傳播,得到的損失結果通過修改后的網(wǎng)絡反向傳播;一批訓練樣本執(zhí)行完這個過程后,在沒有被刪除的神經元上按照梯度下降法更新對應的參數(shù)(w, b)。
最后重復1、2步過程?;謴捅粍h掉的神經元,此時被刪除的神經元保持原樣,而沒有被刪除的神經元已經更新相關參數(shù)。
參考:Dropout(正則化)
Dropout 以一小部分數(shù)字作為其輸入值,形式為 0.1、0.2、0.4 等。使得此層的10%、20%、40%的神經元被暫時停止工作。
下面使用: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) ])
重新編譯和訓練模型
# 編譯模型 model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) # 查看網(wǎng)絡結構 model.summary() # 訓練模型 epochs = 15 history = model.fit( train_ds, validation_data=val_ds, epochs=epochs )
在訓練和驗證集上查看損失值和準確性:
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()
對比之前模型的效果,差別還是挺大的;使用數(shù)據(jù)增強、正則化后的模型,降低了過擬合的影響;驗證集的損失和模型準確度,與訓練集更接近了。
預測新數(shù)據(jù)
# 預測新數(shù)據(jù) 下載一張新圖片,來預測它屬于什么類型花朵 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%的圖像用于訓練 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%的圖像用于驗證 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ù)集中花朵的類別名稱,字母順序對應于目錄名稱 class_names = train_ds.class_names print(class_names) # 將像素的值標準化至0到1的區(qū)間內。 normalization_layer = layers.experimental.preprocessing.Rescaling(1./255) # 調用map將其應用于數(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ù)增強 通過對已有的訓練集圖片 隨機轉換(反轉、旋轉、縮放等),來生成其它訓練數(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)絡模型 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)絡結構 model.summary() # 訓練模型 epochs = 15 history = model.fit( train_ds, validation_data=val_ds, epochs=epochs ) # 在訓練和驗證集上查看損失值和準確性 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()
作者簡介:黎國溥,華為云-云享專家、CSDN博客專家、華為云-云創(chuàng)·首席貢獻官、華為云-年度社區(qū)風云人物
參考鏈接:
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
*博客內容為網(wǎng)友個人發(fā)布,僅代表博主個人觀點,如有侵權請聯(lián)系工作人員刪除。