eym*_*h92 0 keras tensorflow keras-layer image-preprocessing image-augmentation
我正在从事一个天文图像分类项目,目前正在使用 keras 构建 CNN。
我正在尝试构建一个预处理管道,以使用 keras/tensorflow 层来增强我的数据集。为了简单起见,我想实现二面体组的随机变换(即,对于方形图像,90 度旋转和翻转),但似乎tf.keras.preprocessing.image.random_rotation只允许在遵循均匀分布的连续选择范围。
我想知道是否有一种方法可以从指定度数列表中进行选择,在我的例子中是 [0, 90, 180, 270]。
幸运的是,有一个张量流函数可以满足您的要求:tf.image.rot90。下一步是将该函数包装到自定义中PreprocessingLayer,以便它随机执行。
import tensorflow as tf
import tensorflow.keras.backend as K
from tensorflow.keras.layers.experimental.preprocessing import PreprocessingLayer
class RandomRot90(PreprocessingLayer):
def __init__(self, name=None, **kwargs) -> None:
super(RandomRot90, self).__init__(name=name, **kwargs)
self.input_spec = tf.keras.layers.InputSpec(ndim=4)
def call(self, inputs, training=True):
if training is None:
training = K.learning_phase()
def random_rot90():
# random int between 0 and 3
rot = tf.random.uniform((),0,4, dtype=tf.int32)
return tf.image.rot90(inputs, k=rot)
# if not training, do nothing
outputs = tf.cond(training, random_rot90, lambda:inputs)
outputs.set_shape(inputs.shape)
return outputs
def compute_output_shape(self, input_shape):
return input_shape
Run Code Online (Sandbox Code Playgroud)
get_config请注意,如果您希望能够使用该层保存和加载模型,您可能需要实现。(参见文档)| 归档时间: |
|
| 查看次数: |
972 次 |
| 最近记录: |