title: TypeError: 'retval_' has dtype int32 in the main branch, but dtype float32 in the else branch
body: <p>I am training my model to address image classification problem, i have 1000 images  classified into 4 classes. When training the model i am getting &quot;Type Error&quot;, I have reviewed my code several times and don't know where i have committed an error in the code, if possible could some one please suggest me reason for the error, i am posting  the code and error generated below for your reference
  &quot;&quot;&quot;</p>
  <pre><code>from tensorflow.keras.layers import Input, Concatenate, Dropout, Flatten, Dense, GlobalAveragePooling2D, Conv2D
  from tensorflow.keras import backend as K
  #from tensorflow.keras.utils import np_utils
  from tensorflow.keras.utils import to_categorical
  from tensorflow.keras.preprocessing.image import ImageDataGenerator
  from tensorflow.keras import optimizers
  from tensorflow.keras.metrics import top_k_categorical_accuracy
  from tensorflow.keras.models import Sequential, Model, load_model
  import tensorflow as tf
  from tensorflow.keras.initializers import he_uniform
  from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping, CSVLogger, ReduceLROnPlateau
  #from tensorflow.compat.keras.backend import KTF
  #import keras.backend.tensorflow_backend as KTF
  from tensorflow.keras.applications.resnet50 import ResNet50
  from tensorflow.keras.applications.inception_v3 import InceptionV3
  
  import os
  import matplotlib.pylab as plt
  import numpy as np
  import pandas as pd
  #import numpy as np, Pillow, skimage, imageio, matplotlib
  #from scipy.misc import imresize
  from skimage.transform import resize
  from tqdm import tqdm
  
  
  # Path to   class files
  classes_file = &quot;/home/DEV/Downloads/IMAGE_1000_CLASSES&quot;
  data_classes = pd.read_csv(classes_file, header=None)
  
  # Instances with targets
  targets = data_classes[1].tolist()
  
  # Split data according to their classes
  
  class_0 = data_classes[data_classes[1] == 0]
  class_1 = data_classes[data_classes[1] == 1]
  class_2 = data_classes[data_classes[1] == 2]
  class_3 = data_classes[data_classes[1] == 3]
  
  # Holdout split train/test set (Other options are k-folds or leave-one-out)
  split_proportion = 0.9
  
  split_size_0 = int(len(class_0)*split_proportion)
  split_size_1 = int(len(class_1)*split_proportion)
  split_size_2 = int(len(class_2)*split_proportion)
  split_size_3 = int(len(class_3)*split_proportion)
  
  new_class_0_train = np.random.choice(len(class_0), split_size_0, replace=False)
  new_class_0_train = class_0.iloc[new_class_0_train]
  new_class_0_test = ~class_0.iloc[:][0].isin(new_class_0_train.iloc[:][0])
  new_class_0_test = class_0[new_class_0_test]
  
  new_class_1_train = np.random.choice(len(class_1), split_size_1, replace=False)
  new_class_1_train = class_1.iloc[new_class_1_train]
  new_class_1_test = ~class_1.iloc[:][0].isin(new_class_1_train.iloc[:][0])
  new_class_1_test = class_1[new_class_1_test]
  
  new_class_2_train = np.random.choice(len(class_2), split_size_2, replace=False)
  new_class_2_train = class_2.iloc[new_class_2_train]
  new_class_2_test = ~class_2.iloc[:][0].isin(new_class_2_train.iloc[:][0])
  new_class_2_test = class_2[new_class_2_test]
  
  new_class_3_train = np.random.choice(len(class_3), split_size_3, replace=False)
  new_class_3_train = class_3.iloc[new_class_3_train]
  new_class_3_test = ~class_3.iloc[:][0].isin(new_class_3_train.iloc[:][0])
  new_class_3_test = class_3[new_class_3_test]
  
  
  x_train_list = pd.concat( 
      [new_class_0_train, new_class_1_train, new_class_2_train, new_class_3_train])
  x_test_list = pd.concat(
      [new_class_0_test, new_class_1_test, new_class_2_test, new_class_3_test])
  
  
  # Load files
  imagePath = &quot;/home/DEV/Downloads/IMAGE_SET_1000/&quot;
  
  
  x_train = []
  y_train = []
  for index, row in tqdm(x_train_list.iterrows(), total=x_train_list.shape[0]):
      try:
          loadedImage = plt.imread(imagePath + str(row[0]) + &quot;.jpg&quot;)
          x_train.append(loadedImage)
          y_train.append(row[1])
      except:
          # Try with .png file format if images are not properly loaded
          try:
              loadedImage = plt.imread(imagePath + str(row[0]) + &quot;.png&quot;)
              x_train.append(loadedImage)
              y_train.append(row[1])
          except:
              # Print file names whenever it is impossible to load image files
              print(imagePath + str(row[0]))
  
  
  
  x_test = []
  y_test = []
  for index, row in tqdm(x_test_list.iterrows(), total=x_test_list.shape[0]):
      try:
          loadedImage = plt.imread(imagePath + str(row[0]) + &quot;.jpg&quot;)
          x_test.append(loadedImage)
          y_test.append(row[1])
      except:
          # Try with .png file format if images are not properly loaded
          try:
              loadedImage = plt.imread(imagePath + str(row[0]) + &quot;.png&quot;)
              x_test.append(loadedImage)
              y_test.append(row[1])
          except:
              # Print file names whenever it is impossible to load image files
              print(imagePath + str(row[0]))
              
              
  
  img_width, img_height = 139, 139
  
  index = 0
  for image in tqdm(x_train):
      #aux = imresize(image, (img_width, img_height, 3), &quot;bilinear&quot;)
      aux = resize(image, (img_width, img_height))
      x_train[index] = aux / 255.0  # Normalization
      index += 1
      
  
  index = 0
  for image in tqdm(x_test):
      #aux = imresize(image, (img_width, img_height, 3), &quot;bilinear&quot;)
      aux = resize(image, (img_width, img_height))
      x_test[index] = aux / 255.0  # Normalization
      index += 1
      
  
  os.environ[&quot;KERAS_BACKEND&quot;] = &quot;tensorflow&quot;
  RANDOM_STATE = 42
  
  def get_session(gpu_fraction=0.8):
  
      num_threads = os.environ.get('OMP_NUM_THREADS')
      gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction)
  
      if num_threads:
          return tf.Session(config=tf.ConfigProto(
              gpu_options=gpu_options, intra_op_parallelism_threads=num_threads))
      else:
          return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
      
  
  
  #KTF.set_session(get_session())
  #k.tensorflow.set_session(get_session())
  
  def precision(y_true, y_pred):
  
      true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
      predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
      precision = true_positives / (predicted_positives + K.epsilon())
      return precision
  
  
  def recall(y_true, y_pred):
  
      true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
      possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
      recall = true_positives / (possible_positives + K.epsilon())
      return recall
  
  
  def fbeta_score(y_true, y_pred, beta=1):
  
      if beta &lt; 0:
          raise ValueError('The lowest choosable beta is zero (only precision).')
  
      # Set F-score as 0 if there are no true positives (sklearn-like).
      if K.sum(K.round(K.clip(y_true, 0, 1))) == 0:
          return 0
  
      p = precision(y_true, y_pred)
      r = recall(y_true, y_pred)
      bb = beta ** 2
      fbeta_score = (1 + bb) * (p * r) / (bb * p + r + K.epsilon())
      return fbeta_score
  
  
  nb_classes = 4
  final_model = []
  
  
  # Option = InceptionV3
  model = InceptionV3(weights=&quot;imagenet&quot;, include_top=False,
                      input_shape=(img_width, img_height, 3), classifier_activation=&quot;softmax&quot;)
  model.summary()
  
  
  # Creating new outputs for the model
  x = model.output
  x = Flatten()(x)
  #x = GlobalAveragePooling2D()(x)
  x = Dense(512, activation=&quot;relu&quot;)(x)
  x = Dropout(0.5)(x)
  x = Dense(512, activation=&quot;relu&quot;)(x)
  x = Dropout(0.5)(x)
  predictions = Dense(nb_classes, activation='softmax')(x)
  final_model = Model(inputs=model.input, outputs=predictions)
  
  
  # Metrics
  learningRate = 0.001
  #optimizer = model.compile(optimizer= 'adam' , loss= tensorflow.keras.losses.binary_crossentropy, metrics=['accuracy'])
  optimizer = tf.keras.optimizers.SGD(learning_rate=learningRate, momentum=0.88, nesterov=True)
  #optimizer = tf.keras.Adam(learning_rate=learningRate, momentum=0.88, nesterov=True)
  
  # Compiling the model...
  final_model.compile(loss=&quot;categorical_crossentropy&quot;, optimizer=optimizer,
                      metrics=[&quot;accuracy&quot;, fbeta_score])
  
  
  #x_train = np.array(x_train)
  
  x_train = np.asarray(x_train).astype(np.float32)
  #x_test = np.array(x_test)
  x_test = np.asarray(x_test).astype(np.float32)
  
  # Defining targets...
  y_train = np.concatenate([np.full((new_class_0_train.shape[0]), 0), np.full((new_class_1_train.shape[0]), 1),
                            np.full((new_class_2_train.shape[0]), 2), np.full((new_class_3_train.shape[0]), 3)])
  
  y_test = np.concatenate([np.full((new_class_0_test.shape[0]), 0), np.full((new_class_1_test.shape[0]), 1),
                           np.full((new_class_2_test.shape[0]), 2), np.full((new_class_3_test.shape[0]), 3)])
  
  #y_train = np_utils.to_categorical(y_train)
  
  y_train = tf.keras.utils.to_categorical(y_train)
  
  #y_test = np_utils.to_categorical(y_test)
  
  y_test = tf.keras.utils.to_categorical(y_test)
  
  
  modelFilename = &quot;./model_inception.h5&quot;
  
  trainingFilename = &quot;./training.csv&quot;
  
  nb_train_samples = y_train.shape[0]
  nb_test_samples = y_test.shape[0]
  #epochs = 10000
  epochs = 1000
  batch_size = 24
  trainingPatience = 200
  decayPatience = trainingPatience / 4
  
  # Setting the data generator...
  train_datagen = ImageDataGenerator(
      horizontal_flip=True,
      fill_mode=&quot;reflect&quot;,
      zoom_range=0.2
  )
  
  
  
  train_generator = train_datagen.flow(x_train, y_train, batch_size=batch_size)
  
  
  # Saving the model
  checkpoint = ModelCheckpoint(modelFilename,
                               monitor='val_acc',
                               verbose=1,
                               save_best_only=True,
                               save_weights_only=False,
                               mode='auto',
                               save_freq=1)
  
   
  adaptativeLearningRate = ReduceLROnPlateau(monitor='val_acc',
                                             factor=0.5,
                                             patience=decayPatience,
                                             verbose=1,
                                             mode='auto',
                                             min_delta=0.0001,
                                             cooldown=0,
                                             min_lr=1e-8)
  
  
  early = EarlyStopping(monitor='val_acc',
                        min_delta=0,
                        patience=trainingPatience,
                        verbose=1,
                        mode='auto')
  
  
  csv_logger = CSVLogger(trainingFilename, separator=&quot;,&quot;, append=False)
  
  # Callbacks
  callbacks = [checkpoint, early, csv_logger, adaptativeLearningRate]
  
  
  # Training of the model
  final_model.fit(train_generator,
                            steps_per_epoch=nb_train_samples / batch_size,
                            epochs=epochs,
                            shuffle=True,
                            validation_data=(x_test, y_test),
                            validation_steps=nb_test_samples / batch_size,
                            callbacks=callbacks)
  </code></pre>
  <p>Error :</p>
  <pre><code>  File &quot;/home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py&quot;, line 986, in wrapper
      raise e.ag_error_metadata.to_exception(e)
  
  TypeError: in user code:
  
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:855 train_function  *
          return step_function(self, iterator)
      /home/DEV/Downloads/codes/Classification_model.py:212 fbeta_score  *
          if K.sum(K.round(K.clip(y_true, 0, 1))) == 0:
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/operators/control_flow.py:1170 if_stmt
          _tf_if_stmt(cond, body, orelse, get_state, set_state, symbol_names, nouts)
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/operators/control_flow.py:1216 _tf_if_stmt
          final_cond_vars = control_flow_ops.cond(
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:206 wrapper
          return target(*args, **kwargs)
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/deprecation.py:535 new_func
          return func(*args, **kwargs)
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/control_flow_ops.py:1254 cond
          return cond_v2.cond_v2(pred, true_fn, false_fn, name)
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/cond_v2.py:90 cond_v2
          false_graph = func_graph_module.func_graph_from_py_func(
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py:999 func_graph_from_py_func
          func_outputs = python_func(*func_args, **func_kwargs)
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/operators/control_flow.py:1213 aug_orelse
          _verify_tf_cond_vars(new_body_vars_[0], new_orelse_vars, symbol_names)
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/operators/control_flow.py:365 _verify_tf_cond_vars
          nest.map_structure(
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/nest.py:867 map_structure
          structure[0], [func(*x) for x in entries],
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/nest.py:867 &lt;listcomp&gt;
          structure[0], [func(*x) for x in entries],
      /home/DEV/anaconda3/lib/python3.8/site-packages/tensorflow/python/autograph/operators/control_flow.py:335 verify_single_cond_var
          raise TypeError(
  
      TypeError: 'retval_' has dtype int32 in the main branch, but dtype float32 in the else branch
  </code></pre>