I have solved a similar error in my code and here's how I did it.
I think the problem lies when using @tf.function
or using any function with a condition while running tf graph. In my case during model.fit method.
Problem indicates that invalid graph escaped type checking. When using if-else statement in @tf.function
code keras API converts if-else conditions into tf.cond
(AutoGraph converts if-statement to tf.cond().) however, during model.fit() tensorflow gives a warning when using elif
but if you want to avoid that error remove elif
statements with normal if-else statements and I think that might solve this problem.
Implementation of function before error and it was used in loss function which was used in mode.compile
and later model.fit
method
import tensorflow as tf
class RescaleImage():
def __init__(self) -> None:
super().__init__()
@tf.function
def normalize(self, x:tf.Tensor, min_val: float=0.0, max_val: float=1.0)->tf.Tensor:
min_val = tf.cast(min_val,tf.float32)
max_val = tf.cast(max_val, tf.float32)
if tf.reduce_max(x)>1.0 and tf.reduce_min(x)>=0.0:
if min_val==0.0 and max_val==1.0:
x = x/255.0
elif min_val==-1.0 and max_val==1.0:
x = (x - 127.5)/127.5
elif tf.reduce_max(x)<=1.0 and tf.reduce_min(x)>=-1.0 and tf.reduce_min(x)<0.0:
if min_val==0.0 and max_val==1.0:
x = (x+1.0)/2.0
elif min_val==0.0 and max_val==255.0:
x = (x+1.0)*255.0/2.0
elif tf.reduce_max(x)<=1.0 and tf.reduce_min(x)>=0.0:
if min_val==-1.0 and max_val==1.0:
x = (x-0.5)/0.5
elif min_val==0.0 and max_val==255.0:
x = x*255.0
return x
@tf.function
def normalize_individual(self, x:tf.Tensor, min_val: float=0.0, max_val: float=1.0)->tf.Tensor:
min_val = tf.cast(min_val,tf.float32)
max_val = tf.cast(max_val, tf.float32)
if tf.reduce_max(x)>1.0 and tf.reduce_min(x)>=0.0:
factor = (max_val-min_val)/(tf.math.reduce_max(x)-tf.math.reduce_min(x))
x = factor*(x - tf.math.reduce_min(x))+min_val
elif tf.reduce_max(x)<=1.0 and tf.reduce_min(x)>=-1.0 and tf.reduce_min(x)<0.0:
if min_val==0.0 and max_val==1.0:
x = (x+1.0)/2.0
elif min_val==0.0 and max_val==255.0:
x = (x+1.0)*255.0/2.0
elif tf.reduce_max(x)<=1.0 and tf.reduce_min(x)>=0.0:
if min_val==-1.0 and max_val==1.0:
x = (x-0.5)/0.5
elif min_val==0.0 and max_val==255.0:
x = x*255.0
return x
Code after solving the error (using normal if statements):
import tensorflow as tf
class RescaleImage():
def __init__(self) -> None:
super().__init__()
@tf.function
def normalize(self, x:tf.Tensor, min_val: float=0.0, max_val: float=1.0)->tf.Tensor:
min_val = tf.cast(min_val,tf.float32)
max_val = tf.cast(max_val, tf.float32)
if tf.reduce_max(x)>1.0 and tf.reduce_min(x)>=0.0:
if min_val==0.0 and max_val==1.0:
x = x/255.0
if min_val==-1.0 and max_val==1.0:
x = (x - 127.5)/127.5
if tf.reduce_max(x)<=1.0 and tf.reduce_min(x)>=-1.0 and tf.reduce_min(x)<0.0:
if min_val==0.0 and max_val==1.0:
x = (x+1.0)/2.0
if min_val==0.0 and max_val==255.0:
x = (x+1.0)*255.0/2.0
if tf.reduce_max(x)<=1.0 and tf.reduce_min(x)>=0.0:
if min_val==-1.0 and max_val==1.0:
x = (x-0.5)/0.5
if min_val==0.0 and max_val==255.0:
x = x*255.0
return x
@tf.function
def normalize_individual(self, x:tf.Tensor, min_val: float=0.0, max_val: float=1.0)->tf.Tensor:
min_val = tf.cast(min_val,tf.float32)
max_val = tf.cast(max_val, tf.float32)
if tf.reduce_max(x)>1.0 and tf.reduce_min(x)>=0.0:
factor = (max_val-min_val)/(tf.math.reduce_max(x)-tf.math.reduce_min(x))
x = factor*(x - tf.math.reduce_min(x))+min_val
if tf.reduce_max(x)<=1.0 and tf.reduce_min(x)>=-1.0 and tf.reduce_min(x)<0.0:
if min_val==0.0 and max_val==1.0:
x = (x+1.0)/2.0
if min_val==0.0 and max_val==255.0:
x = (x+1.0)*255.0/2.0
if tf.reduce_max(x)<=1.0 and tf.reduce_min(x)>=0.0:
if min_val==-1.0 and max_val==1.0:
x = (x-0.5)/0.5
if min_val==0.0 and max_val==255.0:
x = x*255.0
return x