首页 > 解决方案 > CNN(迁移学习)训练速度相当慢的原因可能是什么?

问题描述

我使用 GPU 通过 Inception v3 的迁移学习来训练模型。权重='imagenet'。卷积基础被冻结,顶部的密集层用于 MNIST 数字识别的 10 类分类。代码如下:

from keras.preprocessing import image
datagen=ImageDataGenerator(
    #rescale=1./255,
    preprocessing_function=tf.keras.applications.inception_v3.preprocess_input,
    featurewise_center=False,  # set input mean to 0 over the dataset
    samplewise_center=False,  # set each sample mean to 0
    featurewise_std_normalization=False,  # divide inputs by std of the dataset
    samplewise_std_normalization=False,  # divide each input by its std
    zca_whitening=False,  # apply ZCA whitening
    rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)
    zoom_range = 0.1, # Randomly zoom image 
    width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
    height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
    horizontal_flip=False,  # randomly flip images
    vertical_flip=False) 
train_generator=datagen.flow_from_directory(
train_path,
target_size=(224, 224),
color_mode="rgb",
class_mode="categorical",
batch_size=86,
interpolation="bilinear",
)

test_generator=datagen.flow_from_directory(
test_path,
target_size=(224, 224),
color_mode="rgb",
class_mode="categorical",
batch_size=86,
interpolation="bilinear",
)
#Import pre-trained model InceptionV3
from keras.applications import InceptionV3

#Instantiate convolutional base
conv_base = InceptionV3(weights='imagenet', 
                    include_top=False,
                    input_shape=(224, 224, 3))  # 3 = number of channels in RGB pictures
#Forbid training of conv part
 conv_base.trainable=False
#Build model
 model=Sequential()
 model.add(conv_base)
 model.add(Flatten())
 model.add(Dense(256,activation='relu'))
 model.add(BatchNormalization())
 model.add(Dropout(0.5))
 model.add(Dense(10, activation='softmax'))

 # Define the optimizer
 optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)

 # Compile the model
 model.compile(optimizer=optimizer,loss="categorical_crossentropy",metrics=['accuracy'] ) 

 history = model.fit_generator(train_generator,
                          epochs = 1, validation_data = test_generator,
                          verbose = 2, steps_per_epoch=60000 // 86)
                          #, callbacks=[learning_rate_reduction])

当我使用 rescale=1./255 作为数据生成器时,获得的训练速率为 1 epoch/小时(即使在将 lr 降低到 0.001 之后)。

搜索答案后,我发现原因我的输入格式不合适。

当我尝试使用 preprocessing_function=tf.keras.applications.inception_v3.preprocess_input 时,我在训练 30 分钟后收到一条消息:

Epoch 1/1
/usr/local/lib/python3.6/dist-packages/keras/utils/data_utils.py:616: UserWarning: The input 1449 could not be retrieved. It could be because a worker has died.
  UserWarning)
/usr/local/lib/python3.6/dist-packages/keras/utils/data_utils.py:616: UserWarning: The input 614 could not be retrieved. It could be because a worker has died.
  UserWarning)

模型有什么问题?提前致谢。

标签: tensorflowkerasdeep-learninggpuconv-neural-network

解决方案


学习率不影响训练率。

您训练模型的速度取决于您的 gpu、cpu 和驱动器上的 IO,其他条件不变。

首先,检查您的 gpu 是否用于训练。

from keras import backend as K
K.tensorflow_backend._get_available_gpus()

接下来,您的 GPU 可以处理的最大 batch_size 是 32 吗?尝试增加 batch_size 直到出现 OOM 错误。

或者您可以监控您的 gpu 和 cpu 使用情况。

如果 gpu 和 cpu 使用率未达到最大值,则可能受到驱动器 IO 速度的限制。


推荐阅读