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)
添加回答
举报