python - 为什么 epoch 2 比 epoch 1 花费的时间多 18 倍?

标签 python keras

我在 keras 中有以下神经网络(可能没有必要对其进行审查来回答我的问题:

简短摘要: 它是一个以图像作为输入并输出图像的神经网络。神经网络主要是卷积网络。我使用发电机。另外,我有两个回调:一个用于 TensorBoard,一个用于检查点保存

class modelsClass(object):
    def __init__(self, img_rows = 272, img_cols = 480):

        self.img_rows = img_rows
        self.img_cols = img_cols

    def addPadding(self, layer, level): #height, width, level):

        w1, h1 = self.img_cols, self.img_rows
        w2, h2 = int(w1/2), int(h1/2)
        w3, h3 = int(w2/2), int(h2/2)
        w4, h4 = int(w3/2), int(h3/2)
        h = [h1, h2, h3, h4]
        w = [w1, w2, w3, w4]

        # Target width and height
        tw = w[level-1]
        th = h[level-1]

        # Source width and height
        lsize = keras.int_shape(layer)
        sh = lsize[1]
        sw = lsize[2]

        pw = (0, tw - sw)
        ph = (0, th - sh)

        layer = ZeroPadding2D(padding=(ph, pw), data_format="channels_last")(layer)

        return layer

[我需要在此处用一些文本破坏代码才能发布问题]

    def getmodel(self):

        input_blurred = Input((self.img_rows, self.img_cols,3))

        conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(input_blurred)
        conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv1)
        pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

        conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool1)
        conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv2)
        pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

        conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool2)
        conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv3)
        pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

        conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool3)
        conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv4)
        pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

        conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same')(pool4)
        conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same')(conv5)

        up6 = Conv2DTranspose(512, (2, 2), strides=(2, 2), padding='same')(conv5)
        up6 = self.addPadding(up6,level=4)
        up6 = concatenate([up6,conv4], axis=3)
        conv6 = Conv2D(512, (3, 3), activation='relu', padding='same')(up6)
        conv6 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv6)

        up7 = Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv6)
        up7 = self.addPadding(up7,level=3)
        up7 = concatenate([up7,conv3], axis=3)
        conv7 = Conv2D(256, (3, 3), activation='relu', padding='same')(up7)
        conv7 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv7)

        up8 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv7)
        up8 = self.addPadding(up8,level=2)
        up8 = concatenate([up8,conv2], axis=3)
        conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(up8)
        conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv8)

        up9 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv8)
        up9 = self.addPadding(up9,level=1)
        up9 = concatenate([up9,conv1], axis=3)
        conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(up9)
        conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv9)

        conv10 = Conv2D(3, (1, 1), activation='linear')(conv9)

        model = Model(inputs=input_blurred, outputs=conv10)

        return model

那么代码是:

models = modelsClass(720, 1280)
model = models.getmodel()

model.compile(optimizer='adam', loss='mean_absolute_error')
model_checkpoint = ModelCheckpoint('checkpoints/cp.ckpt', monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', save_freq='epoch')
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir='some_dir', histogram_freq=1)
model_history = model.fit_generator(generator_train, epochs=3,
                          steps_per_epoch=900,
                          callbacks=[tensorboard_callback, model_checkpoint],
                          validation_data=generator_val, validation_steps=100)

其中 generator_train.__len__ = 900generator_val.__len__ = 100,两者的批量大小 = 1。
第 1 轮的时间为 10 分钟,第 2 轮的时间为 3 小时。我想知道可能是什么问题

最佳答案

以下是一些可能降低程序速度的常见因素:

  • CPU/GPU 被其他程序使用
  • 内存交换:由于 RAM 不足,您的计算机将内容从 RAM 移动到磁盘。这可能是因为在您的脚本中,您尝试将所有内容保留在内存中(例如以前的输出列表,甚至可能带有它们的渐变),或者因为另一个程序也开始使用大量 RAM。
  • 计算机发热(可能在第一个纪元之后就变热了)
  • 节省电池(如果是笔记本电脑并且您拔掉了插头,则可能)

关于python - 为什么 epoch 2 比 epoch 1 花费的时间多 18 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59917535/

相关文章:

python - 如何从 SKLearn 的 TfidfVectorizer 手动计算 TF-IDF 分数

neural-network - Keras 索引错误 : indices are out-of-bounds

python - 不在 python 中的正确包中时不会捕获异常

tensorflow - 喀拉斯 LSTM : how to predict beyond validation vs predictions?

python - 如何将 MNIST 数据加载到 Google Colab Jupyter Notebook 中?

python - Keras:加权二进制交叉熵实现

python - 将自动编码器权重绑定(bind)到密集的 Keras 层中

python - 为什么我创建的 postgres 用户被拒绝访问表?

php - Mysql查询在wamp服务器mysql控制台中运行,但不通过python文件或控制台

python - TypeError : argument 1 must be convertible to a buffer, 不是 BeautifulSoup