The if conditional statement naturally navigates us to do what we expect to do. As a result, when it comes to TensorFlow, we are prone to achieve the same goal to write the following code:

Before it runs as you expected, the following bug would be thrown out:

raise TypeError(“Using a tf.Tensor as a Python bool is not allowed. “. TypeError: Using a tf.Tensor as a Python bool is not allowed. Use if t is not None: instead of if t: to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.

It seems TensorFlow does not support python-like if numeric comparison. Instead, we must turn to tf.cond function to solve this problem. First, let’s take a look at part of the tf.cond official doc:

12345

cond(pred,# A scalar determining whether to return the result of true_fn or false_fntrue_fn,#The callable function to be executed if pred if truefalse_fn,#The callable function to be executed if pred if false)

With is in mind, tf.cond leaves us three things to do:

pred, a boolean function determining which side of image to resize. Usually tf.greater(), tf.less() are the choices. If the combination of several unary predictors together is necessary, we might need to use tf.logical_and()

true_fn, please note that the parameter true_fn is a callable function, not the return value of a function.

false_fn, the same as true_fn

We naturally want true_fn() returns the resized image tensor while false_fn() returns the original image. Thus, the following conversion is just what we want:

1234567891011121314

max_len=1024ifmax(img_h,img_w)>max_lenandimg_h>img_w:new_h=max_lennew_w=int((new_h*img_w)/float(img_h))img_tmp=cv2.imresize(img_tmp,(new_h,new_w))#convert to the following codedefresize_img_tensor_accord_h(img_tensor):new_h=tf.constant(1024,dtype=tf.int32)new_w=tf.cast(tf.div(tf.multiply(new_h,img_w),img_h),dtype=tf.int32)img_tensor=tf.image.resize_images(img_tensor,(new_h,new_w))returnlambda:img_tensorimg_tensor=tf.cond(pred=tf.logical_and(tf.greater(img_h,max_len),tf.greater(img_h,img_w)), \
true_fn=resize_img_tensor_accord_h(img_tensor), \
false_fn=lambda:img_tensor)

Similarily, the other if conditional statement can be rewritten as:

12345678910111213

elifmax(img_h,img_w)>max_lenandimg_w>img_h:new_w=max_lennew_h=int((new_w*img_h)/float(img_w))img_tmp=cv2.imresize(img_tmp,(new_h,new_w))#converted to the following codedefresize_img_tensor_accord_w(img_tensor):new_w=tf.constant(1024,dtype=tf.int32)new_h=tf.cast(tf.div(tf.multiply(new_w,img_h),img_w),dtype=tf.int32)img_tensor=tf.image.resize_images(img_tensor,(new_h,new_w))returnlambda:img_tensorimg_tensor=tf.cond(pred=tf.logical_and(tf.greater(img_w,1024),tf.greater(img_w,img_h)), \
true_fn=resize_img_tensor_accord_w(img_tensor), \
false_fn=lambda:img_tensor)

Please pay careful attention to lambda, which guarantees the return of resize_img_tensor_accod_x() is a function. Otherwise extra errors will come out.

Farewell Word

It seems TensorFlow holds large difference with Python language. Spending more time to read the official doc and sample code becomes necessary!