卷积与池化操作
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。