2 回答

TA貢獻1818條經驗 獲得超11個贊
由于您有一個自定義序列生成器,您可以創建一個在紀元開始或結束時運行的函數。您可以在其中放置代碼來修改圖像。文檔位于[此處。][1]
Epoch-level methods (training only)
on_epoch_begin(self, epoch, logs=None)
Called at the beginning of an epoch during training.
on_epoch_end(self, epoch, logs=None)
Called at the end of an epoch during training.
[1]: https://keras.io/guides/writing_your_own_callbacks/

TA貢獻1878條經驗 獲得超4個贊
沒有必要CustomCallback為此目的創建一個;最后,您希望在訓練期間進行增強。
解決方案是應用旋轉操作的概率
# read, resize and rotate the image and return a batch of images
def __getitem__(self, idx):
angle = self.angles[self.current_angle_idx]
print(f"Rotating Angle: {angle}")
batch_x = self.filenames[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_y = self.labels[idx * self.batch_size:(idx + 1) * self.batch_size]
#These new lines (say we augment with probability > 0.5)
#Number between 0 and 1
images = []
for filename in batch_x:
probability = random.random()
apply_rotate = probability > 0.5
if apply_rotate:
images.append(rotate(resize(imread(filename), (15, 15)), angle))
else:
images.append(resize(imread(filename), (15, 15)))
return np.array(images), np.array(batch_y)
添加回答
舉報