为什么卷积神经网络迁移学习需要这么长时间?

数据挖掘 深度学习 美国有线电视新闻网 迁移学习 卷积神经网络 vgg16
2022-02-22 19:55:59

我正在使用迁移学习来训练使用 keras 的预训练 VGG16 模型的二值图像分类模型。代码可以在下面找到:

training_dir = '/Users/rishabh/Desktop/CyberBoxer/data/train'
validation_dir = '/Users/rishabh/Desktop/CyberBoxer/data/validation'
image_files = glob(training_dir + '/*/*.jpg')
valid_image_files = glob(validation_dir + '/*/*.jpg')
# importing the libraries
from keras.models import Model
from keras.layers import Flatten, Dense
from keras.applications import VGG16
#from keras.preprocessing import image

IMAGE_SIZE = [64, 64]  # we will keep the image size as (64,64). You 
can increase the size for better results. 

# loading the weights of VGG16 without the top layer. These weights are 
trained on Imagenet dataset.
vgg = VGG16(input_shape = IMAGE_SIZE + [3], weights = 'imagenet', 
include_top = False)  # input_shape = (64,64,3) as required by VGG

# this will exclude the initial layers from training phase as there are 
already been trained.
for layer in vgg.layers:
    layer.trainable = False

x = Flatten()(vgg.output)
#x = Dense(128, activation = 'relu')(x)   # we can add a new fully 
connected layer but it will increase the execution time.
x = Dense(num_classes, activation = 'softmax')(x)  # adding the output 
layer with softmax function as this is a multi label classification 
problem.

model = Model(inputs = vgg.input, outputs = x)

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg16 import preprocess_input

training_datagen = ImageDataGenerator(
                                rescale=1./255,   # all pixel values will be between 0 an 1
                                shear_range=0.2, 
                                zoom_range=0.2,
                                horizontal_flip=True,
                                preprocessing_function=preprocess_input)

validation_datagen = ImageDataGenerator(rescale = 1./255, preprocessing_function=preprocess_input)

training_generator = training_datagen.flow_from_directory(training_dir, 
target_size = IMAGE_SIZE, batch_size = 200, class_mode = 'categorical')
validation_generator = 
validation_datagen.flow_from_directory(validation_dir, target_size = 
IMAGE_SIZE, batch_size = 200, class_mode = 'categorical')

training_images = 3717
validation_images = 885

history = model.fit_generator(training_generator,
               steps_per_epoch = 3717,  # this should be equal to total number of images in training set. But to speed up the execution, I am only using 10000 images. Change this for better results. 
               epochs = 1,  # change this for better results
               validation_data = validation_generator,
               validation_steps = 885)  # this should be equal to total number of images in validation set.

我只在 3700 张图像上训练它,但一个 epoch 仍然需要大约 10-12 小时。这应该发生吗?我做错什么了吗?我必须将我的 keras 降级到 2.1.4 才能运行代码,所以它会影响学习吗?

1个回答

它可能与ImageDataGenerator. 数据增强的计算成本可能很高。删除该代码并查看模型是否训练得更快。