【神经网络八股扩展】:数据增强
生活随笔
收集整理的這篇文章主要介紹了
【神经网络八股扩展】:数据增强
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
課程來源:人工智能實踐:Tensorflow筆記2
文章目錄
- 前言
- TensorFlow2數據增強函數
- 數據增強+網絡八股代碼:
- 總結
前言
本講目標:數據增強,增大數據量
關于我們為何要使用數據增強以及常用的幾種數據增強的手法,可以看看下面的文章,雖說是翻譯的,但仍有可鑒之處:
數據增強(Data Augmentation)
TensorFlow2數據增強函數
對圖像的增強就是對圖像的簡單形變,用來應對因為拍照角度不同引起的圖片形變。
TensorFlow2給出了數據增強函數
劃紅線的部分為需要注意的地方:
數據增強+網絡八股代碼:
import tensorflow as tf from tensorflow.keras.preprocessing.image import ImageDataGeneratorfashion = tf.keras.datasets.fashion_mnist (x_train, y_train), (x_test, y_test) = fashion.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) # 給數據增加一個維度,使數據和網絡結構匹配image_gen_train = ImageDataGenerator(rescale=1. / 1., # 如為圖像,分母為255時,可歸至0~1rotation_range=45, # 隨機45度旋轉width_shift_range=.15, # 寬度偏移height_shift_range=.15, # 高度偏移horizontal_flip=True, # 水平翻轉zoom_range=0.5 # 將圖像隨機縮放閾量50% ) image_gen_train.fit(x_train)model = tf.keras.models.Sequential([tf.keras.layers.Flatten(),tf.keras.layers.Dense(128, activation='relu'),tf.keras.layers.Dense(10, activation='softmax') ])model.compile(optimizer='adam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),metrics=['sparse_categorical_accuracy'])model.fit(image_gen_train.flow(x_train, y_train, batch_size=32), epochs=5, validation_data=(x_test, y_test),validation_freq=1) model.summary()
隨著迭代輪數增加,準確率不斷提高。但從數據集上不能看出數據增強的效果,要在實際應用中去使用。
總結
課程鏈接:MOOC人工智能實踐:TensorFlow筆記2
總結
以上是生活随笔為你收集整理的【神经网络八股扩展】:数据增强的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生三胎罚多少钱啊?
- 下一篇: 地下城的暗帝什么时候出觉醒