在keras中进行转移学习时,fit和fit_generator之间的差异

时间:2018-06-07 16:13:12

标签: python tensorflow keras deep-learning transfer-learning

我正在尝试使用具有张量流的Keras中的转移学习来训练深度神经网络。有不同的方法可以做到这一点,如果您的数据很小,您可以使用预先训练的模型为整个数据提供计算功能,然后使用这些功能来训练和测试小型网络,这很好,因为您不需要为每个批次和每个时期计算这些特征。但是,如果数据很大,则无法计算整个数据的功能,在这种情况下,我们会使用ImageDataGeneratorflow_from_directoryfit_generator。在这种情况下,每次在每个时期前的每个批次计算特征,这使得事情变得更慢。我假设这两种方法在准确性和损失方面都产生了类似的结果。问题是我采用了一个小数据集并尝试了两种方法并得到了完全不同的结果。如果有人能够判断所提供的代码中是否有错误和/或为什么我会得到不同的结果,我将不胜感激?

拥有大型数据集时的方法:

from keras.applications.inception_v3 import InceptionV3,preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model

datagen= ImageDataGenerator(preprocessing_function=preprocess_input)

train_generator = datagen.flow_from_directory('data/train', 
                                              class_mode='categorical',
                                              batch_size=64,...)
vaild_generator = datagen.flow_from_directory('data/valid',
                                              class_mode='categorical',
                                              batch_size=64,...)

base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = Conv2D(filters = 128 , kernel_size = (2,2)) (x)
x = MaxPooling2D()(x)
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(2, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)

for layer in base_model.layers:
    layer.trainable = False

model.compile(optimizer='rmsprop', loss='categorical_crossentropy',...)

model.fit_generator(generator = train_generator,
                    steps_per_epoch = len (train_generator),
                    validation_data = valid_generator ,
                    validation_steps = len(valid_generator),
                    ...)

具有小数据集时的方法:

from keras.applications.inception_v3 import InceptionV3,preprocess_input
from keras.models import Sequential
from keras.utils import np_utils

base_model = InceptionV3(weights='imagenet', include_top=False)
train_features = base_model.predict(preprocess_input(train_data))
valid_features = base_model.predict(preprocess_input(valid_data))

model = Sequential()
model.add(Conv2D(filters = 128 , kernel_size = (2,2),
                 input_shape=(train_features [1],
                              train_features [2],
                              train_features [3])))
model.add(MaxPooling2D())
model.add(GlobalAveragePooling2D())
model.add(Dense(1024, activation='relu'))
model.add(Dense(2, activation='softmax'))     

model.compile(optimizer='rmsprop', loss='categorical_crossentropy',...)

model.fit(train_features, np_utils.to_categorical(y_train,2), 
          validation_data=(valid_features, np_utils.to_categorical(y_valid,2)),
          batch_size=64,...)

0 个答案:

没有答案