python - 有没有一种Python方法可以减少卷积神经网络的训练时间?

标签 python keras neural-network conv-neural-network training-data

我正在构建一个卷积神经网络的 keras 模型,用于预测正确的类别并对测试对象进行分类。该模型具有 conv2D、激活、maxpooling、dropout、flatten、dense 层。之后我在大型数据集上训练网络,但是训练时间很长,可能会达到3,4天,我需要的是减少训练网络所需的时间,有什么办法可以做到这一点在Python中?

我尝试使用 LR_Finder 类来优化学习率,如下所示:

from LR_Finder import LRFinder
lr_finder = LRFinder(min_lr=1e-5,max_lr=1e-2, steps_per_epoch=np.ceil(len(trainX) // BS), epochs=100)

但这也没有让我所需的时间减少。

这是我的模型的代码:

class SmallerVGGNet:
@staticmethod
def build(width, height, depth, classes):
    # initialize the model along with the input shape to be
    # "channels last" and the channels dimension itself
    model = Sequential()
    inputShape = (height, width, depth)
    chanDim = -1

    # if we are using "channels first", update the input shape
    # and channels dimension
    if K.image_data_format() == "channels_first":
        inputShape = (depth, height, width)
        chanDim = 1

    # CONV => RELU => POOL
    model.add(Conv2D(32, (3, 3), padding="same",
        input_shape=inputShape))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(3, 3)))
    model.add(Dropout(0.25))

    # (CONV => RELU) * 2 => POOL
    model.add(Conv2D(64, (3, 3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(Conv2D(64, (3, 3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    # (CONV => RELU) * 2 => POOL
    model.add(Conv2D(128, (3, 3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(Conv2D(128, (3, 3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    # first (and only) set of FC => RELU layers
    model.add(Flatten())
    model.add(Dense(1024))
    model.add(Activation("relu"))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))

    # softmax classifier
    model.add(Dense(classes))
    model.add(Activation("softmax"))

    # return the constructed network architecture
    return model

之后我按照以下代码训练模型:

EPOCHS = 100
INIT_LR = 1e-3
BS = 32
IMAGE_DIMS = (96, 96, 3)

data = []
labels = []

# grab the image paths and randomly shuffle them
imagePaths = sorted(list(paths.list_images("Dataset")))
random.seed(42)
random.shuffle(imagePaths)
# loop over the input images
for imagePath in imagePaths:
    # load the image, pre-process it, and store it in the data list
    image = cv2.imread(imagePath)
    image = cv2.resize(image, (IMAGE_DIMS[1], IMAGE_DIMS[0]))
    image = img_to_array(image)
    data.append(image)

    label = imagePath.split(os.path.sep)[-2]
    labels.append(label)

# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
print("[INFO] data matrix: {:.2f}MB".format(data.nbytes / (1024 * 1000.0)))

# binarize the labels
lb = LabelBinarizer()
labels = lb.fit_transform(labels)

# partition the data into training and testing splits using 80% of
# the data for training and the remaining 20% for testing
(trainX, testX, trainY, testY) = train_test_split(data,
                                 labels, test_size=0.2, random_state=42)

# construct the image generator for data augmentation
aug = ImageDataGenerator(rotation_range=25, width_shift_range=0.1,
               height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
                     horizontal_flip=True, fill_mode="nearest")

# initialize the model
model = SmallerVGGNet.build(width=IMAGE_DIMS[1], height=IMAGE_DIMS[0],
                        depth=IMAGE_DIMS[2], classes=len(lb.classes_))
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer= opt,
          metrics=["accuracy"])
print("model compiled in few minutes successfully ^_^")

# train the network
H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS),
validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS,
epochs=EPOCHS, verbose=1)

根据这段代码,我预计输出需要几分钟或者可能是几个小时,但是当达到 model.fit_generator 步骤中的训练时,实际需要的时间大约是几个小时每个时期都需要几天的时间来训练所有网络,否则可能会崩溃并停止工作。有什么办法可以减少训练时间吗?

最佳答案

在调用 fit_generator 时设置 use_multiprocessing=Trueworkers>1,因为默认情况下仅在主线程上执行生成器

关于python - 有没有一种Python方法可以减少卷积神经网络的训练时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53920441/

相关文章:

python - 如何在 tf.layers.keras.Conv2D 中初始化常量偏差?

python - Keras:我可以使用 model.predict 但不使用 model.predict_generator 来预测是否使用 model.fit_generator 训练模型

deep-learning - 是否可以从神经网络模型中断的地方开始执行?

c++ - 我的分类神经网络存在逻辑问题

python - Matplotlib:轮廓上的颜色条没有条纹

python - 使用pytest如何快速显示失败详情?

tensorflow - 使用特征列和预处理为 Tensorflow 2 Keras 模型提供服务(从 tf 1.x 估计器迁移)

tensorflow - conv2d 和 conv2d_transpose 的步幅=1

python - 保存没有背景opencv的图像

python - 无法在 virtualenv 中安装 mySQL-python