python-3.x - Keras.fit_generator 需要更多时间用于纪元

标签 python-3.x tensorflow machine-learning keras deep-learning

我正在使用 Keras 进行图像分类,训练样本中有 8k 图像(输入),测试样本中有 2k 图像(输入),定义纪元为 25 。我注意到 epoch 非常慢(第一次迭代大约需要一个小时)。

任何人都可以建议我如何克服这个问题,以及花费大量时间的原因是什么?

下面的代码..

PART-1
initialise neural network
from keras.models import Sequential

#package to perfom first layer , which is convolution , using 2d as it is for image , for video it will be 3d
from keras.layers import Convolution2D

#to perform max pooling on convolved layer
from keras.layers import MaxPool2D

#to convert the pool feature map into large feature vector, will be input for ANN
from keras.layers import Flatten 

#to add layeres on ANN
from keras.layers import Dense

#STEP -1
#Initializing CNN
classifier = Sequential()

#add convolution layer
classifier.add(Convolution2D(filters=32,kernel_size=(3,3),strides=(1, 1),input_shape= (64,64,3),activation='relu'))

#filters - Number of feature detecters that we are going to apply in image

#kernel_size - dimension of feature detector

#strides moving thru one unit at a time

#input shape - shape of the input image on which we are going to apply filter thru convolution opeation,
#we will have to covert the image into that shape in image preprocessing before feeding it into convolution
#channell 3 for rgb and 1 for bw , and  dimension of pixels

#activation - function we use to avoid non linearity in image

#STEP -2 

#add pooling
#this step will significantly reduce the size of feature map , and makes it easier for computation

classifier.add(MaxPool2D(pool_size=(2,2)))

#pool_size - factor by which to downscale


#STEP -3
#flattern the feature map

classifier.add(Flatten())

#STEP -4 
#hidden layer
classifier.add(Dense(units=128,activation='relu',kernel_initializer='uniform'))

#output layer
classifier.add(Dense(units=1,activation='sigmoid'))


#Compiling the CNN using stochastic gradient descend

classifier.compile(optimizer='adam',loss = 'binary_crossentropy',
                  metrics=['accuracy'])

#loss function should be categorical_crossentrophy if output is more than 2 class

#PART2 - Fitting CNN to image

#copied from keras documentation 

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(
        '/Users/arunramji/Downloads/Sourcefiles/CNN_Imageclassification/Convolutional_Neural_Networks/dataset/training_set',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')

test_set = test_datagen.flow_from_directory(
    '/Users/arunramji/Downloads/Sourcefiles/CNN_Imageclassification/Convolutional_Neural_Networks/dataset/test_set',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')

classifier.fit_generator(
        training_set,
        steps_per_epoch=8000,   #number of input (image)
        epochs=25,
        validation_data=test_set,
        validation_steps=2000)          # number of training sample

 classifier.fit(
                 training_set,
        steps_per_epoch=8000,   #number of input (image)
        epochs=25,
        validation_data=test_set,
        validation_steps=2000)

最佳答案

您将 steps_per_epoch 设置为错误的值(这就是为什么它花费的时间比必要的时间长):它没有设置为数据点的数量。 steps_per_epoch 应设置为数据集大小除以批量大小,训练集应为 8000/32 = 250,验证集应为 63。

关于python-3.x - Keras.fit_generator 需要更多时间用于纪元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59535527/

相关文章:

python-3.x - 如何在 Docker 容器中安装支持 CUDA 的 PyTorch?

python - 如何使用 TensorFlow 的 `matrix_solve_ls` 排除正则化中的偏差?

matlab - 如何使用矩阵作为输入来训练 Matlab 神经网络?

python - 自动编码器损失没有减少(并且开始非常高)

python - 如何使用 if __name__ ='__main__' block 在 Python3 中使用相对导入?

python - 将字典中的值转换为键

python-3.x - Python Unittest 在运行测试时不会隔离补丁

tensorflow - 多类分类问题中的不平衡类

tensorflow - 在 TensorFlow Federated 中使用 update_struct 函数时出错

hadoop - Mahout:如何分为均等的训练集