【发布时间】:2019-12-14 01:02:23
【问题描述】:
我尝试提高模型的训练速度。我做了一堆预处理和增强(在 CPU 上运行),这让我的训练速度很慢。因此,我尝试在 keras Sequence 中实现数据的加载和预处理。因此我关注了keras docs 和这个stanford exmaple。到目前为止,这使我的训练速度变慢了,我很确定我在某个地方出错了。因为使用 4 workers 和 use_multiprocessing=True 运行我的训练脚本,我得到以下日志:
Epoch 8/10
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
8/9 [=========================>....] - ETA: 2s - loss: 444.2380Using TensorFlow backend.
9/9 [==============================] - 26s 3s/step - loss: 447.4939 - val_loss: 308.3012
Using TensorFlow backend.
Epoch 9/10
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
8/9 [=========================>....] - ETA: 2s - loss: 421.9372Using TensorFlow backend.
9/9 [==============================] - 26s 3s/step - loss: 418.9702 - val_loss: 263.9197
似乎在我的代码中的某个地方,TensorFlow 在每个时期为每个工作人员(因为验证集是 8 个?)加载和加载。我不认为这就是序列通常应该如何工作的方式?
数据生成器:
class DataGenerator(Sequence):
def __init__(self, annotation_lines, batch_size, input_shape, anchors, num_classes, max_boxes=80):
self.annotations_lines = annotation_lines
self.batch_size = batch_size
self.input_shape = input_shape
self.anchors = anchors
self.num_classes = num_classes
self.max_boxes = max_boxes
def __len__(self):
return int(np.ceil(len(self.annotations_lines) / float(self.batch_size)))
def __getitem__(self, idx):
annotation_lines = self.annotations_lines[idx * self.batch_size:(idx + 1) * self.batch_size]
image_data = []
box_data = []
for annotation_line in annotation_lines:
image, box = get_random_data(annotation_line, self.input_shape, random=True, max_boxes=self.max_boxes)
image_data.append(image)
box_data.append(box)
image_data = np.array(image_data)
box_data = np.array(box_data)
y_true = preprocess_true_boxes(box_data, self.input_shape, self.anchors, self.num_classes)
return [image_data, *y_true], np.zeros(self.batch_size)
我的训练脚本的一部分:
batch_size = batch_size_complete # note that more GPU memory is required after unfreezing the body
data_gen_train = DataGenerator(lines, batch_size, input_shape, anchors, num_classes)
data_gen_validation = DataGenerator(validation_lines, batch_size, input_shape, anchors, num_classes)
print('Train on {} samples, val on {} samples, with batch size {}.'.format(num_train, num_val, batch_size))
r = model.fit_generator(data_gen_train,
steps_per_epoch=max(1, num_train // batch_size),
validation_data=data_gen_validation,
validation_steps=max(1, num_val // batch_size),
epochs=epochs,
initial_epoch=initial_epoch,
callbacks=[logging, checkpoint, reduce_lr, early_stopping],
workers=workers,
use_multiprocessing=True)
model.save_weights(log_dir + 'trained_weights_final.h5')
【问题讨论】:
-
比什么慢?
-
我看到您多次收到“使用 Tensorflow 后端”,这似乎就像 Keras 在每个线程中一遍又一遍地初始化一样。也许你应该试试
use_multiprocessing=False(你仍然可以有很多工人) -
感谢这项工作。而且训练要快得多。所以你能说一般来说,'use_multiprocessing' 只对真正的大批量有意义吗?
-
实际上,我不知道
use_multiprocessing做了什么。我只知道它总是给我带来问题,所以我从不使用它。
标签: python tensorflow keras