- Hands-On Neural Networks
- Leonardo De Marchi Laura Mitchell
- 371字
- 2021-06-24 14:00:11
Feature engineering in Keras
Keras provides a nice and simple interface to do feature engineering. A task that we will study in particular in this book is image classification. For this task, Keras provides the ImageDataGenerator class, which allows us to easily pre-process and augment the data.
The augmentation we are going to perform is aimed at generating more images using some random transformations such as zooming, flipping, shearing, and shifting. These transformations help prevent overfitting and make the model more robust to different image conditions, such as brightness.
We will see the code first and then explain what it does. Following Keras' documentation (https://keras.io/), it's possible to create a generator with the mentioned transformations with the code:
from keras.preprocessing.image import ImageDataGenerator datagen = ImageDataGenerator( rotation_range=45, width_shift_range=0.25, height_shift_range=0.25, rescale=1./255, shear_range=0.3, zoom_range=0.3, horizontal_flip=True, fill_mode='nearest')
For the generator, it's possible to set a few parameters:
- The rotation_range parameter represents value in degrees (0-180), which will be used to randomly find a value to rotate the inputs.
- width_shift and height_shift are ranges (as a fraction of total width or height) within which it randomly translates pictures vertically or horizontally.
- Scale is a common operation used to re-scale a raw image. In this case, we have RGB images, in which each pixel is represented by a value between 0 and 255. Because of this, we use a scaling factor of 1/255 so our values now will be between 0 and 1. We do this as otherwise the numbers would be too high given the typical learning rate, one of the parameters of our network.
- shear_range is used for randomly applying shearing transformations.
- zoom_range is used to create additional pictures by randomly zooming inside pictures.
- horizontal_flip is a Boolean value used to create additional pictures by randomly flipping half of the image horizontally. This is useful when there are no assumptions of horizontal asymmetry.
- fill_model is the strategy used for filling in new components
In this way, from one image, we can create many to feed to our model. Notice that we only initialized the object so far, so no instruction has being executed as the generator will perform the action only when it's called; it will be done later on.
- Learning Apache Spark 2
- 并行數(shù)據(jù)挖掘及性能優(yōu)化:關(guān)聯(lián)規(guī)則與數(shù)據(jù)相關(guān)性分析
- 一本書玩轉(zhuǎn)數(shù)據(jù)分析(雙色圖解版)
- STM32G4入門與電機(jī)控制實(shí)戰(zhàn):基于X-CUBE-MCSDK的無刷直流電機(jī)與永磁同步電機(jī)控制實(shí)現(xiàn)
- Excel 2007技巧大全
- 中國(guó)戰(zhàn)略性新興產(chǎn)業(yè)研究與發(fā)展·增材制造
- 從零開始學(xué)SQL Server
- Cloud Security Automation
- PowerMill 2020五軸數(shù)控加工編程應(yīng)用實(shí)例
- JRuby語(yǔ)言實(shí)戰(zhàn)技術(shù)
- MongoDB 4 Quick Start Guide
- 電腦故障排除與維護(hù)終極技巧金典
- 貫通Hibernate開發(fā)
- 基于Proteus的PIC單片機(jī)C語(yǔ)言程序設(shè)計(jì)與仿真
- 中老年人學(xué)電腦與上網(wǎng)