Ola*_*nto 6 python keras tensorflow
我想使用 keras 的功能 API 为我的 CNN 中的图像添加一些高斯噪声,但是在测试一些不同的 stddev 值时,我注意到高斯层对输入数据没有任何作用。我使用以下代码进行测试:
import tensorflow as tf
import numpy as np
import cv2
stddev = 0.1
image = cv2.imread(<img_path>)
image = (image.astype('float32') - 127.5) / 127.5
input_layer = tf.keras.layers.Input(shape=(128,128,3))
gaus = tf.keras.layers.GaussianNoise(stddev)(input_layer)
model = tf.keras.models.Model(inputs=input_layer, outputs=gaus)
noisy_image = model(image)
print(f'Pixel value at 0,0: {image[0,0]}')
print(f'Pixel value at 0,0: {noisy_image.numpy()[0,0]}')
# Output
# Pixel value at 0,0: [ 0.09803922 -0.30980393 -0.56078434]
# Pixel value at 0,0: [ 0.09803922 -0.30980393 -0.56078434]
Run Code Online (Sandbox Code Playgroud)
我为stddev输入什么值并不重要(尝试了从 0.001 到 10000 的所有内容)。我预计这些值会略有不同(或者当 stddev=1000 时差异很大)。难道我做错了什么?
也许我应该提到我在 Windows 10 上使用 tensorflow-gpu==2.0.0-rc0
如果您检查文档,它会说该层仅在训练期间处于活动状态,因为它应该充当正则化器。查看源代码证实了这一点。因此,看起来您是否需要确保模型“知道”它处于训练模式。做这件事有很多种方法:
model.compile/model.fit接口,这应该自动完成。training参数,您可以将其设置为training=True在每次调用的基础上“激活”训练模式。IE noisy_image = model(image, training=True)。tf.keras.backend.set_learning_phase(1)“全局”激活训练模式(稍后使用参数再次调用0以停用)。| 归档时间: |
|
| 查看次数: |
2784 次 |
| 最近记录: |