Keras实现mode.fit和model.fit_generator比较

模型部分

模型部分都一样,比如我这里使用AlexNet网络来做。我做的是一个二分类任务,所以结尾部分网络有改动。输入图片尺寸是256*256的,所以输出图片尺寸有一点改动。

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D, BatchNormalization
import os

# AlexNet
model = Sequential()
#第一段
model.add(Conv2D(filters=96, kernel_size=(11,11),
                 strides=(4,4), padding='valid',
                 input_shape=(256, 256, 3),
                 activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3,3), 
                       strides=(2,2), 
                       padding='valid'))
#第二段
model.add(Conv2D(filters=256, kernel_size=(5,5), 
                 strides=(1,1), padding='same', 
                 activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3,3), 
                       strides=(2,2), 
                       padding='valid'))
#第三段
model.add(Conv2D(filters=384, kernel_size=(3,3), 
                 strides=(1,1), padding='same', 
                 activation='relu'))
model.add(Conv2D(filters=384, kernel_size=(3,3), 
                 strides=(1,1), padding='same', 
                 activation='relu'))
model.add(Conv2D(filters=256, kernel_size=(3,3), 
                 strides=(1,1), padding='same', 
                 activation='relu'))
model.add(MaxPooling2D(pool_size=(3,3), 
                       strides=(2,2), padding='valid'))
#第四段
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
 
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
 
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.5))
 
# Output Layer
model.add(Dense(1))
model.add(Activation('sigmoid'))

使用model.fit_generator

使用这个方法,因为是生成器,所以会比较节省内存。但有一个问题是没有办法定义callback,有的地方不太方便。

train_dir = os.path.abspath(r"../train/")

train_dir_good = os.path.abspath(r"../train/good/")
train_dir_wrong = os.path.abspath(r"../train/wrong/")

validation_dir = os.path.abspath(r"../validation/") 
validation_dir_good = os.path.abspath(r"../validation/good/")
validation_dir_wrong = os.path.abspath(r"../validation/wrong/")
from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255)
test_datagen = ImageDataGenerator(rescale = 1./255)

train_generator = train_datagen.flow_from_directory(train_dir, batch_size = 20, class_mode= 'binary')

validation_generator = test_datagen.flow_from_directory(validation_dir, batch_size = 20, class_mode = 'binary')
from keras import optimizers

model.compile(loss= 'binary_crossentropy', optimizer= optimizers.RMSprop(lr= 1e-4, decay= 0.01/20), metrics= ['acc'])

history = model.fit_generator(train_generator, steps_per_epoch = 100, epochs= 20,
                             validation_data = validation_generator, validation_steps= 50)
model.save('AlexNet.h5')

使用model.fit

下面使用model.fit来做:
https://keras.io/zh/models/model/ 这个链接说明了model.fit怎么用,需要输入的numpy数组

from keras.callbacks import ModelCheckpoint

checkpoint = ModelCheckpoint(save_best_only= True)
callbacks = [checkpoint]
model.fit(trainX, trainY, validation_data= (testX, testY), batch_size = 20, epoch = 15, 
         callbacks = callbacks)

你可能感兴趣的:(深度学习)