![](/img/trans.png)
[英]How to selecting images for DataGenerator in Keras for prediction?
[英]Keras DataGenerator using the keras Sequence
我試圖提高模型的訓練速度。 我做了一堆預處理和增強(在 CPU 上運行),這使我的訓練速度變慢。 因此,我嘗試在 keras Sequence
實現數據加載和預處理。 因此,我遵循了keras docs和 this 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
似乎在我的代碼中的某個地方,每個 epoch 的每個工作人員都加載並加載了 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 Backend ,這似乎Keras在每個線程中一遍又一遍地初始化。
也許你應該簡單地嘗試use_multiprocessing=False
(你仍然可以有很多工人)
訓練速度取決於許多因素,例如批量大小、輸入圖像的大小、學習率、epoch 的步驟和步驟驗證。 然后開始調查這些原因之一並放置use_multiprocessing=False
因為在訓練期間編寫的各種 tensorflow 后端不應該存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.