Keras操作小技巧(持续更新ing)

Keras是用Python编写的高级神经网络API,能够在TensorFlow,CNTK或Theano之上运行。它的开发着眼于实现快速实验。这篇博客主要记录博主在Keras操作中遇到的一些简单快速的小技巧,伙伴们可以按需阅读(具体的操作可以参见目录⬆️,目录之间没有什么联系,就是遇到了随手记下来)~如果想了解更多关于Keras的问题,可以参见Keras官方文档
在这里插入图片描述

Part00 指定使用服务器的卡:os.environ

这个严格意义上不算是Keras的范围内,但是顺手也记录在了这里,按需自取。

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"

Part01 分类网络预测时如何设置Top-5:Keras.Metircs

我们以AlexNet为例,一般用accuracy做为metrics来约束loss,但有时我们也需要用top-5来看预测的类是否进入前五名,尤其是在分类难度比较大,类别比较多的时候。在官方文档Metrics中还有很多其他的metrics的设置,可以参考。

import keras

def acc_top5(y_true, y_pred):
	return keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=5)

def train(aug,trainX,trainY,testX,testY):
	print("[INFO] compiling model...")
	model = Alexnet.build(width=norm_size, height=norm_size, depth=1, classes=CLASS_NUM)
	opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
	model.compile(loss="sparse_categorical_crossentropy", optimizer=opt, metrics=["accuracy", acc_top5])

Part02 在训练过程中存储最好的权重模型:ModelCheckpoint

在网络训练过程中,假设epoch=20,但是并不一定在第20个epoch的时候验证集的效果是最好的,有可能优化出现了波动也有可能会过拟合,所以除去存储最后一轮训练的权重模型以外,还要存储在训练过程中验证集表现最好的模型,还是以Alexnet为例。

import keras
from keras.callbacks import ModelCheckpoint

def train(aug,trainX,trainY,testX,testY):
	print("[INFO] compiling model...")
	model = Alexnet.build(width=norm_size, height=norm_size, depth=1, classes=CLASS_NUM)
	opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
	model.compile(loss="sparse_categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
	
	checkpoint = ModelCheckpoint(filepath='alexnet_best.h5',monitor='val_acc',mode='auto' ,save_best_only='True')
    callback_lists=[checkpoint]
    
    H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS),
        validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS,
        epochs=EPOCHS, verbose=1, callbacks=callback_lists)
    
    model.save('alexnet.h5')
    print("[INFO] save network finished")

Part03 快速数据扩增数据集:ImageDataGenerator

在Keras中也有很好用的快速数据扩增的办法,在官方文档Image Preprocessing中小伙伴们可以找到各种参数,根据自己的需求调用参数就可以了。举个例子:

aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,
                         height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
                         horizontal_flip=True, fill_mode="nearest")

Part04 打印模型的参数和计算参数量:print_summary()

我们在上述问题中已经解决了如何存储模型和根据验证集的准确率存储优秀的模型model.save('xxx.h5),那么如何来打印模型的各层参数和计算整个的参数量呢?其实在Keras里非常方便(因为有model.suquential),用Convnet来举个例子:如果你没有现成的已经存储好的模型,那么你可以⬇️:

import keras
from keras.models import Sequential, Model
from keras.layers import Input, add
from keras.layers.convolutional import MaxPooling2D, AveragePooling2D, ZeroPadding2D, Conv2D
from keras.layers.core import Activation, Flatten, Dense, Dropout 
from keras.layers.normalization import BatchNormalization
from keras import backend as K
from keras.utils import print_summary

# Define a network
class Convnet:
    @staticmethod
    def build(width, height, depth, classes):
        model = Sequential()
        inputShape = (height, width, depth)
        if K.image_data_format() == "channels_first":  
            inputShape = (depth, height, width)
        
        model.add(Conv2D(64, (5, 5),padding="same",input_shape=inputShape))
        model.add(Activation("relu"))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
        model.add(BatchNormalization())
        model.add(Conv2D(256, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
        model.add(BatchNormalization())
        model.add(Flatten())
        model.add(Dense(1000))
        model.add(Activation("relu"))
        model.add(Dropout(0.5))
        model.add(Dense(classes))
        model.add(Activation("softmax"))
        return model 

# Define model
model = Convnet.build(width=28, height=28, depth=1, classes=3755) # Input size and catogries
print_summary(model, line_length=None, positions=None, print_fn=None)

然后运行,你就可以得到如下的结果图了:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章