卷積與池化操作
model = keras. models. Sequential( )
model. add( keras. layers. Conv2D( filters = 32 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'selu' ,
input_shape = ( 28 , 28 , 1 )
) )
model. add( keras. layers. Conv2D( filters = 32 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'selu' ,
) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
一般的模型
google v3
深度可分離神經網絡
實例代碼
卷積神經網絡
model = keras. models. Sequential( )
model. add( keras. layers. Conv2D( filters = 32 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
input_shape = ( 28 , 28 , 1 )
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. Conv2D( filters = 32 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. Dropout( 0.3 ) )
model. add( keras. layers. Conv2D( filters = 64 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. Conv2D( filters = 64 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. Dropout( 0.3 ) )
model. add( keras. layers. Conv2D( filters = 128 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. Conv2D( filters = 128 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. Dropout( 0.4 ) )
model. add( keras. layers. Flatten( ) )
model. add( keras. layers. Dense( 256 , activation = 'relu' ) )
model. add( keras. layers. Dropout( 0.5 ) )
model. add( keras. layers. Dense( 256 , activation = 'tanh' ) )
model. add( keras. layers. Dropout( 0.5 ) )
model. add( keras. layers. Dense( 10 , activation = 'softmax' ) )
model. summary( )
深度可分離卷積神經網絡
model = keras. models. Sequential( )
model. add( keras. layers. Conv2D( filters = 32 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'selu' ,
input_shape = ( 28 , 28 , 1 )
) )
model. add( keras. layers. SeparableConv2D( filters = 32 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'selu' ,
) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. SeparableConv2D( filters = 64 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'selu' ,
) )
model. add( keras. layers. SeparableConv2D( filters = 64 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'selu' ,
) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. SeparableConv2D( filters = 128 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'selu' ,
) )
model. add( keras. layers. SeparableConv2D( filters = 128 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'selu' ,
) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. Flatten( ) )
model. add( keras. layers. Dense( 128 , activation = 'selu' ) )
model. add( keras. layers. Dense( 10 , activation = 'softmax' ) )
實戰 10 Monkey數據集
包含相應庫文件
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
import matplotlib. pyplot as plt
import os
設置文件路徑並讀取標籤
train_dir = "../input/10-monkey-species/training/training"
valid_dir = "../input/10-monkey-species/validation/validation"
label_file = "../input/10-monkey-species/monkey_labels.txt"
print ( os. path. exists( train_dir) )
print ( os. path. exists( valid_dir) )
print ( os. path. exists( label_file) )
print ( os. listdir( train_dir) )
print ( os. listdir( valid_dir) )
labels = pd. read_csv( label_file, header = ( 0 ) )
print ( labels)
設置宏定義
height = 128
width = 128
channels = 3
batch_size = 128
num_classes = 10
epochs = 300
讀取圖片並做數據增強
train_datagen = keras. preprocessing. image. ImageDataGenerator(
rescale = 1 . / 255 . ,
rotation_range = 40 ,
width_shift_range = 0.2 ,
height_shift_range = 0.2 ,
shear_range = 0.2 ,
zoom_range = 0.2 ,
horizontal_flip = True ,
fill_mode = 'nearest' ,
train_generator = train_datagen. flow_from_directory(
train_dir,
target_size = ( height, width) ,
batch_size = batch_size,
seed = 7 ,
shuffle = True ,
class_mode = "categorical"
)
vaild_datagen = keras. preprocessing. image. ImageDataGenerator( rescale = 1 . / 255 . )
valid_generator = vaild_datagen. flow_from_directory(
valid_dir,
target_size = ( height, width) ,
batch_size = batch_size,
seed = 7 ,
class_mode = "categorical"
)
train_num = train_generator. samples
valid_num = valid_generator. samples
print ( train_num, valid_num)
取數據
for i in range ( 2 ) :
x, y = train_generator. next ( )
print ( x. shape, y. shape)
print ( y)
構建神經網絡
model = keras. models. Sequential( )
model. add( keras. layers. Conv2D( filters = 32 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
input_shape = ( 28 , 28 , 1 )
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. Conv2D( filters = 32 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. Dropout( 0.3 ) )
model. add( keras. layers. Conv2D( filters = 64 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. Conv2D( filters = 64 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. Dropout( 0.3 ) )
model. add( keras. layers. Conv2D( filters = 128 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. Conv2D( filters = 128 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. Dropout( 0.4 ) )
model. add( keras. layers. Flatten( ) )
model. add( keras. layers. Dense( 256 , activation = 'relu' ) )
model. add( keras. layers. Dropout( 0.5 ) )
model. add( keras. layers. Dense( 256 , activation = 'tanh' ) )
model. add( keras. layers. Dropout( 0.5 ) )
model. add( keras. layers. Dense( 10 , activation = 'softmax' ) )
model. summary( )
定義求解方法
model. compile ( optimizer= 'adam' ,
loss= 'categorical_crossentropy' ,
metrics= [ 'accuracy' ] )
訓練模型
history = model. fit_generator( train_generator,
steps_per_epoch = train_num // batch_size,
epochs = epochs,
validation_data = valid_generator,
validation_steps = valid_num // batch_size
可視化模型訓練
def plot_learning_curves ( history, label, epcohs, min_value, max_value) :
data= { }
data[ label] = history. history[ label]
data[ 'val_' + label] = history. history[ 'val_' + label]
pd. DataFrame( data) . plot( figsize = ( 8 , 5 ) )
plt. grid( True )
plt. axis( [ 0 , epcohs, min_value, max_value] )
plt. show( )
plot_learning_curves( history, 'accuracy' , 10 , 0 , 1 )
plot_learning_curves( history, 'loss' , 10 , 1.5 , 2.5 )
全部代碼
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
import matplotlib. pyplot as plt
import os
train_dir = "../input/10-monkey-species/training/training"
valid_dir = "../input/10-monkey-species/validation/validation"
label_file = "../input/10-monkey-species/monkey_labels.txt"
print ( os. path. exists( train_dir) )
print ( os. path. exists( valid_dir) )
print ( os. path. exists( label_file) )
print ( os. listdir( train_dir) )
print ( os. listdir( valid_dir) )
labels = pd. read_csv( label_file, header = ( 0 ) )
print ( labels)
height = 128
width = 128
channels = 3
batch_size = 128
num_classes = 10
epochs = 300
train_datagen = keras. preprocessing. image. ImageDataGenerator(
rescale = 1 . / 255 . ,
rotation_range = 40 ,
width_shift_range = 0.2 ,
height_shift_range = 0.2 ,
shear_range = 0.2 ,
zoom_range = 0.2 ,
horizontal_flip = True ,
fill_mode = 'nearest' ,
)
train_generator = train_datagen. flow_from_directory(
train_dir,
target_size = ( height, width) ,
batch_size = batch_size,
seed = 7 ,
shuffle = True ,
class_mode = "categorical"
)
vaild_datagen = keras. preprocessing. image. ImageDataGenerator( rescale = 1 . / 255 . )
valid_generator = vaild_datagen. flow_from_directory(
valid_dir,
target_size = ( height, width) ,
batch_size = batch_size,
seed = 7 ,
class_mode = "categorical"
)
train_num = train_generator. samples
valid_num = valid_generator. samples
print ( train_num, valid_num)
for i in range ( 2 ) :
x, y = train_generator. next ( )
print ( x. shape, y. shape)
print ( y)
model = keras. models. Sequential( )
model. add( keras. layers. Conv2D( filters = 32 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
input_shape = ( 28 , 28 , 1 )
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. Conv2D( filters = 32 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. Dropout( 0.3 ) )
model. add( keras. layers. Conv2D( filters = 64 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. Conv2D( filters = 64 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. Dropout( 0.3 ) )
model. add( keras. layers. Conv2D( filters = 128 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. Conv2D( filters = 128 ,
kernel_size = 3 ,
padding = 'same' ,
activation = 'relu' ,
) )
model. add( keras. layers. BatchNormalization( ) )
model. add( keras. layers. MaxPool2D( pool_size = 2 ) )
model. add( keras. layers. Dropout( 0.4 ) )
model. add( keras. layers. Flatten( ) )
model. add( keras. layers. Dense( 256 , activation = 'relu' ) )
model. add( keras. layers. Dropout( 0.5 ) )
model. add( keras. layers. Dense( 256 , activation = 'tanh' ) )
model. add( keras. layers. Dropout( 0.5 ) )
model. add( keras. layers. Dense( 10 , activation = 'softmax' ) )
model. summary( )
model. compile ( optimizer= 'adam' ,
loss= 'categorical_crossentropy' ,
metrics= [ 'accuracy' ] )
history = model. fit_generator( train_generator,
steps_per_epoch = train_num // batch_size,
epochs = epochs,
validation_data = valid_generator,
validation_steps = valid_num // batch_size
def plot_learning_curves ( history, label, epcohs, min_value, max_value) :
data= { }
data[ label] = history. history[ label]
data[ 'val_' + label] = history. history[ 'val_' + label]
pd. DataFrame( data) . plot( figsize = ( 8 , 5 ) )
plt. grid( True )
plt. axis( [ 0 , epcohs, min_value, max_value] )
plt. show( )
plot_learning_curves( history, 'acc' , 10 , 0 , 1 )
plot_learning_curves( history, 'loss' , 10 , 1.5 , 2.5 )
)
遷移學習
模型獲取
from keras. applications. xception import Xception
from keras. applications. vgg16 import VGG16
from keras. applications. vgg19 import VGG19
from keras. applications. resnet import ResNet50
from keras. applications. resnet import ResNet101
from keras. applications. resnet import ResNet152
from keras. applications. resnet_v2 import ResNet50V2
from keras. applications. resnet_v2 import ResNet101V2
from keras. applications. resnet_v2 import ResNet152V2
from keras. applications. resnext import ResNeXt50
from keras. applications. resnext import ResNeXt101
from keras. applications. inception_v3 import InceptionV3
from keras. applications. inception_resnet_v2 import InceptionResNetV2
from keras. applications. mobilenet import MobileNet
from keras. applications. mobilenet_v2 import MobileNetV2
from keras. applications. densenet import DenseNet121
from keras. applications. densenet import DenseNet169
from keras. applications. densenet import DenseNet201
from keras. applications. nasnet import NASNetLarge
from keras. applications. nasnet import NASNetMobile
model = VGG16( weights= 'imagenet' , include_top= True )
調整輸入大小
每個模型都有自己的輸入大小所以要去官網查一下
在這裏使用的ResNet是 224×224的
設置同樣的數據輸入處理
train_datagen = keras. preprocessing. image. ImageDataGenerator(
preprocessing_function = keras. applications. resnet50. preprocess_input,
rotation_range = 40 ,
width_shift_range = 0.2 ,
height_shift_range = 0.2 ,
shear_range = 0.2 ,
zoom_range = 0.2 ,
horizontal_flip = True ,
fill_mode = 'nearest' ,
)
獲取模型和自己的層次進行拼接
model = keras. models. Sequential( )
model. add( keras. applications. ResNet50(
include_top = False ,
pooling = 'avg' ,
weights = 'imagenet' ,
) )
model. add( keras. layers. Dense( num_classes, activation = 'softmax' ) )
model. layers[ 0 ] . trainable = False
model. summary( )
resnet50 = keras. applications. ResNet50(
include_top = False ,
pooling = 'avg' ,
weights = 'imagenet' ,
for layer in resnet50. layers[ 0 : - 5 ] :
layer. trainable = False
model = keras. models. Sequential( [
resnet50,
keras. layers. Dense( num_classes, activation = 'softmax' ) ,
] )
)
定義模型求解方式和訓練模型
model. compile ( optimizer= 'sgd' ,
loss= 'categorical_crossentropy' ,
metrics= [ 'accuracy' ] )
epochs = 10
history = model. fit_generator( train_generator,
steps_per_epoch = train_num // batch_size,
epochs = epochs,
validation_data = valid_generator,
validation_steps = valid_num // batch_size
)
可視化模型
def plot_learning_curves ( history, label, epochs, min_value, max_value) :
data= { }
data[ label] = history. history[ label]
data[ 'val_' + label] = history. history[ 'val_' + label]
pd. DataFrame( data) . plot( figsize = ( 8 , 5 ) )
plt. grid( True )
plt. axis( [ 0 , epochs, min_value, max_value] )
plt. show( )
plot_learning_curves( history, 'accuracy' , epochs, 0 , 1 )
plot_learning_curves( history, 'loss' , epochs, 0 , 2.5 )
ResNet50遷移學習實戰cifar-10 95%
包含庫文件
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
import matplotlib. pyplot as plt
from sklearn import preprocessing
import cv2
import os
獲取數據
( x_train, y_train) , ( x_test, y_test) = keras. datasets. cifar10. load_data( )
數據處理
創建保存data的文件夾
train_dir = '/kaggle/working/train'
test_dir = '/kaggle/working/test'
if not ( os. path. exists( train_dir) ) :
os. makedirs( train_dir)
if not ( os. path. exists( test_dir) ) :
os. makedirs( test_dir)
寫入數據 並按照(文件路徑/標籤)的形式返回數據存放信息
def save_data ( x_train, y_train, path) :
data_amount = len ( x_train)
assert ( data_amount == len ( y_train) ) , '數據與標籤不匹配'
assert ( y_train. shape[ 1 ] == 1 ) , '標籤必須是一列數據'
assert ( os. path. exists( path) ) , '填寫的目錄不存在'
_list = [ ]
for i in range ( data_amount) :
cv2. imwrite( "{}/data_{}.png" . format ( path, i) , x_train[ i] )
_list. append( [ "{}/data_{}.png" . format ( path, i) , '{}' . format ( y_train[ i] [ 0 ] ) ] )
data_pd = pd. DataFrame( _list)
data_pd. columns = [ 'filepath' , 'class' ]
del _list
return data_pd
train_pd = save_data( x_train, y_train, train_dir)
test_pd = save_data( x_test, y_test, test_dir)
設置宏變量
height = 224
width = 224
channels = 3
batch_size = 20
num_classes = 10
epochs = 10
數據增強
train_datagen = keras. preprocessing. image. ImageDataGenerator(
preprocessing_function = keras. applications. resnet50. preprocess_input,
rotation_range = 40 ,
width_shift_range = 0.2 ,
height_shift_range = 0.2 ,
shear_range = 0.2 ,
zoom_range = 0.2 ,
horizontal_flip = True ,
fill_mode = 'nearest' ,
)
test_datagen = keras. preprocessing. image. ImageDataGenerator(
preprocessing_function = keras. applications. resnet50. preprocess_input,
)
train_generator = train_datagen. flow_from_dataframe(
train_pd,
directory = '/kaggle' ,
x_col = 'filepath' ,
y_col = 'class' ,
target_size = ( height, width) ,
batch_size = batch_size,
seed = 7 ,
shuffle = True ,
class_mode = 'categorical' ,
)
test_generator = test_datagen. flow_from_dataframe(
test_pd,
directory = '/kaggle' ,
x_col = 'filepath' ,
y_col = 'class' ,
target_size = ( height, width) ,
batch_size = batch_size,
seed = 7 ,
class_mode = 'categorical' ,
)
定義模型
keras. backend. set_learning_phase( 0 )
Inp = keras. layers. Input( ( 224 , 224 , 3 ) )
base_model = keras. applications. ResNet50( weights= 'imagenet' , include_top= False ,
input_shape= ( height, width, 3 ) , )
keras. backend. set_learning_phase( 1 )
x = base_model( Inp)
x = keras. layers. BatchNormalization( ) ( x, training= False )
x = keras. layers. GlobalAveragePooling2D( name= 'average_pool' ) ( x)
x = keras. layers. Flatten( name= 'flatten' ) ( x)
x = keras. layers. Dense( 2048 , activation= 'relu' ) ( x)
x = keras. layers. Dense( 1024 , activation= 'tanh' ) ( x)
predictions = keras. layers. Dense( num_classes, activation= 'softmax' ) ( x)
model = keras. Model( inputs= Inp, outputs= predictions)
定義模型優化方法
model. compile ( optimizer= 'sgd' ,
loss= 'categorical_crossentropy' ,
metrics= [ 'accuracy' ] )
訓練數據
history = model. fit_generator( train_generator,
steps_per_epoch = len ( train_pd) // batch_size,
epochs = epochs,
validation_data = test_generator,
validation_steps = len ( test_pd) // batch_size
可視化訓練結果
def plot_learning_curves ( history, label, epochs, min_value, max_value) :
data= { }
data[ label] = history. history[ label]
data[ 'val_' + label] = history. history[ 'val_' + label]
pd. DataFrame( data) . plot( figsize = ( 8 , 5 ) )
plt. grid( True )
plt. axis( [ 0 , epochs, min_value, max_value] )
plt. show( )
plot_learning_curves( history, 'accuracy' , epochs, 0 , 1 )
plot_learning_curves( history, 'loss' , epochs, 0 , 2.5 )
全部代碼
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
import matplotlib. pyplot as plt
from sklearn import preprocessing
import cv2
import os
( x_train, y_train) , ( x_test, y_test) = keras. datasets. cifar10. load_data( )
train_dir = '/kaggle/working/train'
test_dir = '/kaggle/working/test'
if not ( os. path. exists( train_dir) ) :
os. makedirs( train_dir)
if not ( os. path. exists( test_dir) ) :
os. makedirs( test_dir)
def save_data ( x_train, y_train, path) :
data_amount = len ( x_train)
assert ( data_amount == len ( y_train) ) , '數據與標籤不匹配'
assert ( y_train. shape[ 1 ] == 1 ) , '標籤必須是一列數據'
assert ( os. path. exists( path) ) , '填寫的目錄不存在'
_list = [ ]
for i in range ( data_amount) :
cv2. imwrite( "{}/data_{}.png" . format ( path, i) , x_train[ i] )
_list. append( [ "{}/data_{}.png" . format ( path, i) , '{}' . format ( y_train[ i] [ 0 ] ) ] )
data_pd = pd. DataFrame( _list)
data_pd. columns = [ 'filepath' , 'class' ]
del _list
return data_pd
train_pd = save_data( x_train, y_train, train_dir)
test_pd = save_data( x_test, y_test, test_dir)
height = 224
width = 224
channels = 3
batch_size = 20
num_classes = 10
epochs = 10
train_datagen = keras. preprocessing. image. ImageDataGenerator(
preprocessing_function = keras. applications. resnet50. preprocess_input,
rotation_range = 40 ,
width_shift_range = 0.2 ,
height_shift_range = 0.2 ,
shear_range = 0.2 ,
zoom_range = 0.2 ,
horizontal_flip = True ,
fill_mode = 'nearest' ,
)
test_datagen = keras. preprocessing. image. ImageDataGenerator(
preprocessing_function = keras. applications. resnet50. preprocess_input,
)
train_generator = train_datagen. flow_from_dataframe(
train_pd,
directory = '/kaggle' ,
x_col = 'filepath' ,
y_col = 'class' ,
target_size = ( height, width) ,
batch_size = batch_size,
seed = 7 ,
shuffle = True ,
class_mode = 'categorical' ,
)
test_generator = test_datagen. flow_from_dataframe(
test_pd,
directory = '/kaggle' ,
x_col = 'filepath' ,
y_col = 'class' ,
target_size = ( height, width) ,
batch_size = batch_size,
seed = 7 ,
class_mode = 'categorical' ,
)
keras. backend. set_learning_phase( 0 )
Inp = keras. layers. Input( ( 224 , 224 , 3 ) )
base_model = keras. applications. ResNet50( weights= 'imagenet' , include_top= False ,
input_shape= ( height, width, 3 ) , )
keras. backend. set_learning_phase( 1 )
x = base_model( Inp)
x = keras. layers. BatchNormalization( ) ( x, training= False )
x = keras. layers. GlobalAveragePooling2D( name= 'average_pool' ) ( x)
x = keras. layers. Flatten( name= 'flatten' ) ( x)
x = keras. layers. Dense( 2048 , activation= 'relu' ) ( x)
x = keras. layers. Dense( 1024 , activation= 'tanh' ) ( x)
predictions = keras. layers. Dense( num_classes, activation= 'softmax' ) ( x)
model = keras. Model( inputs= Inp, outputs= predictions)
model. compile ( optimizer= 'sgd' ,
loss= 'categorical_crossentropy' ,
metrics= [ 'accuracy' ] )
history = model. fit_generator( train_generator,
steps_per_epoch = len ( train_pd) // batch_size,
epochs = epochs,
validation_data = test_generator,
validation_steps = len ( test_pd) // batch_size
)
def plot_learning_curves ( history, label, epochs, min_value, max_value) :
data= { }
data[ label] = history. history[ label]
data[ 'val_' + label] = history. history[ 'val_' + label]
pd. DataFrame( data) . plot( figsize = ( 8 , 5 ) )
plt. grid( True )
plt. axis( [ 0 , epochs, min_value, max_value] )
plt. show( )
plot_learning_curves( history, 'accuracy' , epochs, 0 , 1 )
plot_learning_curves( history, 'loss' , epochs, 0 , 2.5 )
cnn總結
輸入感受野維度:圖像的默認值爲 2D ,但可以是 1D ,例如句子中的單詞或添加時間維度的視頻的 3D 。
感受野的大小:patch應儘可能小,但應該大到足以“看到”輸入數據中的特徵,通常在小圖像上使用 3 ×3 ,在較大圖像尺寸上使用 5 ×5 或 7 ×7 或更多。
步幅寬度:使用默認步幅 1 . 這很容易理解,並且您不需要填充來處理從圖像邊緣掉落的感受野。對於較大的圖像,這可以增加到 2 或更大。
濾波器數量:濾波器是特徵檢測器,通常在輸入層使用較少的濾波器,並且在較深層使用的濾波器越來越多。
填充:設置爲零並在讀取非輸入數據時調用零填充。當您不能或不想標準化輸入圖像大小或想要使用不能整齊劃分輸入圖像大小的感知字段和步幅大小時,這將非常有用。
池化:池化是一種破壞性或泛化過程,以減少過擬合,感受野幾乎總是設置爲 2 ×2 ,步幅爲 2 ,以丟棄前一層輸出的 75 %激活。
數據準備:考慮標準化輸入數據,包括圖像尺寸和像素值。
模式體系結構:通常在網絡體系結構中對層進行模式化。這可能是一個,兩個或一些卷積層,後面是池化層。然後可以重複該結構一次或多次。最後,完全連接層通常僅在輸出端使用,並且可以堆疊一個,兩個或更多個深度。
dropout:CNN 容易造成一種過擬合,即使是池化層也是如此,應該在完全連接的層之間,或池化層之後使用Dropout。