Hello,
I ran into some errors while running the code, which are summarized below:
#1 from keras.layers.merge import _Merge ImportError: cannot import name '_Merge' from 'keras.layers.merge' python
Since Merge is not supported in Keras +2, I have made some modifications to the RandomWeightedAverage() class:
Old code:
from keras.layers.merge import _Merge
import keras.backend as K
class RandomWeightedAverage(_Merge):
"""Provides a (random) weighted average between real and generated image samples"""
def _merge_function(self, inputs):
alpha = K.random_uniform((1024, 1))
return (alpha * inputs[0]) + ((1 - alpha) * inputs[1])
Funtional code:
import keras.backend as K
import tensorflow as tf
class RandomWeightedAverage(tf.keras.layers.Layer):
def call(self, inputs, **kwargs):
alpha = K.random_uniform((1024, 1))
return (alpha * inputs[0]) + ((1 - alpha) * inputs[1])
Then, you need to do a little change in line 48 of the CLSWGAN.py file:
FROM
interpolated_features = RandomWeightedAverage()([real_features, fake_features])
TO
interpolated_features = RandomWeightedAverage()(inputs=[real_features, fake_features])
#2 AttributeError: Can't set the attribute "name"
Another error is related to the attribute "name" in line 69 of the CLSWGAN.py file. The solution is as simple as add a "_" before the attribute "name":
classificationLayer._name = 'modelClassifier'
#3 ValueError: Tried to convert 'x' to a tensor and failed. Error: None values not supported
This error was mentioned in the jacobgil/keras-grad-cam#17 (comment) issue, and appears to have been fixed by adding a piece of code to the LossFunctions.py file, namely the _compute_gradients(tensor, var_list) function and a small change to line 15.
import tensorflow as tf
def _compute_gradients(tensor, var_list):
grads = tf.gradients(tensor, var_list)
return [grad if grad is not None else tf.zeros_like(var) for var, grad in zip(var_list, grads)]
def gradient_penalty_loss(y_true, y_pred, averaged_samples):
"""
Computes gradient penalty based on prediction and weighted real / fake samples
"""
gradients = _compute_gradients(y_pred, [averaged_samples])[0]
(...)
Hello,
I ran into some errors while running the code, which are summarized below:
#1 from keras.layers.merge import _Merge ImportError: cannot import name '_Merge' from 'keras.layers.merge' python
Since
Mergeis not supported in Keras +2, I have made some modifications to the RandomWeightedAverage() class:Old code:
Funtional code:
Then, you need to do a little change in line 48 of the CLSWGAN.py file:
FROM
TO
#2 AttributeError: Can't set the attribute "name"
Another error is related to the attribute "name" in line 69 of the CLSWGAN.py file. The solution is as simple as add a "_" before the attribute "name":
#3 ValueError: Tried to convert 'x' to a tensor and failed. Error: None values not supported
This error was mentioned in the jacobgil/keras-grad-cam#17 (comment) issue, and appears to have been fixed by adding a piece of code to the LossFunctions.py file, namely the
_compute_gradients(tensor, var_list)function and a small change to line 15.