我试图在 Keras 中创建自定义增强层。这个想法是为了进行更快的扩充(而不是运行相同的函数 BATCH_SIZE 次,而是在整个批次中运行一次)。
我陷入了这个功能:
__call__(inputs, training=False, **kwargs)
在训练时,函数获取形状的输入,(None, 128, 128, 3)
其中最后 3 个数字表示图像的形状。对于我的不理解,第一个None
应该是批量大小,它不是确定性的(出于某种未知原因)。
我的问题就在这里。在我的函数中,我使用np.random.ranf(inputs.shape)
不满意None
作为形状指示器并引发错误:
TypeError: 'NoneType' object cannot be interpreted as an integer
另一方面,我不知道如何获得计算的批量大小。自定义增强层的代码如下。任何帮助,将不胜感激!
class RandomHSV(Layer):
"""Adding Random Noise to HSV image. output is RGB
Input shape:
Arbitrary.
Output shape:
Same as input.
Arguments:
hsv_max_amp: list or tuple of the maximum amplitudes of the noise in range of [0, 1]
name: A string, the name of the layer.
"""
def __init__(self, hsv_max_amp=(0, 0, 0), name=None, **kwargs):
super(RandomHSV, self).__init__(name=name, **kwargs)
self.hsv_max_amp = np.array(list(hsv_max_amp), dtype='float32')
def build(self, input_shape):
self.batch_size = input_shape[0]
super(RandomHSV, self).build(input_shape) # Be sure to call this at the end
def call(self, inputs, training=True, **kwargs):
def hsv_noise():
hsv = tf.image.rgb_to_hsv(inputs)
# the random noise is a random matrix in shape (batch_size, img_w, img_h, depth)
# after creating the random matrix, multiply it (element wise) by the self.hsv_max_amp.
# that gets multiplied by random enabler (np.random.randint(0, 2, 3) -> 3 items, 0 or 1)
# then removing an offset.
random_noise = (np.random.ranf(inputs.shape) * (
np.random.random(1) * self.hsv_max_amp * np.random.randint(0, 2, 3)) - self.hsv_max_amp / 2) * 2
# those lines will cut any number which goes above 1 or goes below 0 (round it to 1 or 0 respectively).
hsv = tf.minimum(1., tf.maximum(0., hsv + random_noise))
batch = tf.image.hsv_to_rgb(hsv)
batch.set_shape(inputs.shape)
return batch
# applying hsv_noise if Training. if Testing then just passing batch forward unchanged
return control_flow_util.smart_cond(pred=training, true_fn=hsv_noise, false_fn=lambda: inputs)
编辑:我知道我可以在 Input() 层中指定批量大小,它确实解决了问题,但是有没有办法在不指定输入层中的形状的情况下做到这一点?所以它可能更通用。