![](/img/trans.png)
[英]Regression Model with 3 Hidden DenseVariational Layers in Tensorflow-Probability returns nan as loss during training
[英]Using SSIM loss in TensorFlow returns NaN values
我正在用 MRI 图像训练 a.network,我想使用 SSIM 作为损失 function。直到现在我一直在使用 MSE,并且一切正常。 但是当我尝试使用 SSIM (tf.image.ssim) 时,我收到了一堆警告消息:
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py:397: UserWarning: Warning: converting a masked element to nan.
dv = (np.float64(self.norm.vmax) -
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py:398: UserWarning: Warning: converting a masked element to nan.
np.float64(self.norm.vmin))
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py:405: UserWarning: Warning: converting a masked element to nan.
a_min = np.float64(newmin)
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py:410: UserWarning: Warning: converting a masked element to nan.
a_max = np.float64(newmax)
/usr/local/lib/python3.7/dist-packages/matplotlib/colors.py:933: UserWarning: Warning: converting a masked element to nan.
dtype = np.min_scalar_type(value)
/usr/local/lib/python3.7/dist-packages/numpy/ma/core.py:713: UserWarning: Warning: converting a masked element to nan.
data = np.array(a, copy=False, subok=subok)
我的代码无论如何都在运行,但没有生成任何数字。 我不确定这里发生了什么或我应该去哪里看。 我正在使用 tensorflow 2.4.0。
我在这里附上我的代码摘要:
generator = Generator() #An u-net defined in tf.keras
gen_learningrate = 5e-4
generator_optimizer = tf.keras.optimizers.Adam(gen_learningrate, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
# Generator loss
def generator_loss(gen_output, target):
# SSIM loss
loss = - tf.reduce_mean(tf.image.ssim(target, gen_output, 2))
return loss
@tf.function
def train_step(input_image, target, epoch):
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
gen_output = generator(input_image, training=True)
loss = generator_loss(gen_output, target)
generator_gradients = gen_tape.gradient(loss, generator.trainable_variables)
generator_optimizer.apply_gradients(zip(generator_gradients,
generator.trainable_variables))
return loss
def fit(train_ds, epochs, test_ds):
for input_image, target in train_ds:
loss = train_step(input_image,target,epoch)
fit(train_dataset, EPOCHS, test_dataset)
我进行了一些探索,发现大多数使用tf.image.ssim()
作为损失的人 function 使用了tf.train()
1.0 中的model.fit()
。 我怀疑返回的 NaN 值与GradientTape()
function 有关,但我不确定如何。
根据我的经验,此警告通常与尝试绘制具有无穷远坐标的点有关。 当然,您真的应该向我们展示更多代码,以便我们有效地帮助您。
您可能会得到与真实图像非常接近的网络预测,因此它会给出无穷大(如果您对它执行某些操作,则为 NaN)。
小心使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.