Keras Error: An operation has `None` for gradient
Lets assume, that you want to build a GAN in keras. The generator transforms an image from domain A to domain B. Normally the image B is now passed into a discriminator, to get a feedback for the generator for example like : (yeah your generated image B (lets call it B`) is nearly as good as the real B).
Lets assume that we want to distinguish spartial information because domain A and B are just too different there, and the discriminator can easy find patterns in this spartial domain. 338
So he should just use Histogramm data (HSV, or texture information like LBP) to differ between B and B´.
The problem now is, that you have to use custom-keras-layers to transform the B´ images from RGB to HSV, and extract texture like LBP, before passing the images into the discriminator. This custom layer has no weigths obviously because its basicly only counting and reshaping (besides the HSV-transformation).
But Keras has its problems with layers without weigths, after using the generator. Somehow the graph cannot be build correctly, and some gradients cant be found. Any ideas why, and how i could fix this error ?
Custom layer
from keras import backend as K
from keras.layers import Layer
class Identity_Loss(Layer):
def call(self, x):
assert isinstance(x, list)
input_1,input_2=x
# Transform BGR to RGB and than to HSV
channels = tf.unstack (input_1, axis=-1)
RGB = tf.stack ([channels[2], channels[1], channels[0]], axis=-1)
RGB=tf.cast(tf.multiply(tf.truediv(tf.add(RGB,1.0),2.0),255.0),dtype=tf.int32)
RGB=tf.cast(RGB,dtype=tf.float32)
HSV=tf.image.rgb_to_hsv(RGB,name=None)
############################################################
SV=HSV[:,:,:,1:]
############################################################
# make mask binary and multiply with image
y=tf.math.greater(input_2, 0)
y=tf.cast(y, tf.float32, name=None)
HSV_mask = tf.math.multiply(HSV, y)
######## Count color occurences ###########################
shape=tf.shape(HSV_mask)
length=shape[1]*shape[2]
# transform
Hue=HSV_mask[:,:,:,:1]
Hue=tf.cast(tf.multiply(Hue,255.0),dtype=tf.int32)
Hue2 = tf.reshape(Hue, [length])
# prevent that the shape changes
filler=tf.range(0,length, 1,dtype=tf.int32)
filler = tf.reshape(filler, [length])
Hue3 = tf.stack([Hue2,filler],axis=-1)
Hue3 = tf.reshape(Hue3, [2*length])
## Count Hue
y1, idx1, count1 = tf.unique_with_counts(Hue3)
maximum=tf.cast(tf.math.reduce_max(count1[1:257]),dtype=tf.int32)
diff=tf.reshape(count1[1:257],(16,16))
diff=tf.expand_dims(diff, axis=-1)
diff=tf.expand_dims(diff, axis=0)
diff=tf.truediv(diff,maximum)
diff=tf.cast(diff,dtype=tf. 338