ValueError: Unexpected result of `train_function` (Empty logs). Please use `Model.compile(..., run_eagerly=True)`, or `tf.config.run_functions_eagerly(True)` for more information of where went wrong, or file a issue/bug to `tf.keras`

I’ve been training an Image Classifier that can detect Oil and Pencil Paintings. While doing so I encountered a value error. I searched the web throughly unfortunately, I didn’t find any satisfactory answer as there was not much discussion about it.

Code:

import tensorflow as tf
import os
import matplotlib.pyplot as plt

gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:

  tf.config.experimental.set_memory_growth(gpu, True)
len(gpus)

# Data Generator
IMAGE_SHAPE = (224, 224)
TRAINING_DATA = '/content/drive/MyDrive/Datasets/train'


VALID_DATA = '/content/drive/MyDrive/Datasets/test'

datagen = tf.keras.preprocessing.image.ImageDataGenerator(

    rescale=1./225

)

train_generator = datagen.flow_from_directory(

    TRAINING_DATA,

    shuffle=True,

    target_size=IMAGE_SHAPE,

)

valid_generator = datagen.flow_from_directory(

    VALID_DATA,

    shuffle=False,

    target_size = IMAGE_SHAPE,

)

def build_model(num_classes):

  model = tf.keras.Sequential([

                               tf.keras.layers.Conv2D(filters=8, kernel_size=(3,3), activation='relu',

                                                      input_shape=(224, 224, 3)),

                               tf.keras.layers.MaxPooling2D(pool_size=(2,2), strides=2),

                               tf.keras.layers.Conv2D(filters=16, kernel_size=(3,3), activation='relu'),

                               tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2),

                               tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu'),

                               tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2),

                               tf.keras.layers.Flatten(),

                               tf.keras.layers.Dense(64, activation='relu'),

                               tf.keras.layers.Dense(num_classes, activation='softmax')

                               

  ])

  return model

model = build_model(num_classes=2)

model.compile(

    optimizer=tf.keras.optimizers.Adam(lr=0.0001),

    loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),

    metrics=['accuracy']

)

print(model.summary())

Error Code:

EPOCHS = 20

BATCH_SIZE = 32

history = model.fit(train_generator,

                    steps_per_epoch=train_generator.samples // BATCH_SIZE, 

                    epochs=EPOCHS,

                    validation_data=valid_generator,

                    validation_steps=valid_generator.samples // BATCH_SIZE,

                    verbose=1

                    )

   1393         logs = tf_utils.sync_to_numpy_or_python_type(logs)
   1394         if logs is None:
-> 1395           raise ValueError('Unexpected result of `train_function` '
   1396                            '(Empty logs). Please use '
   1397                            '`Model.compile(..., run_eagerly=True)`, or '

ValueError: Unexpected result of `train_function` (Empty logs). Please use `Model.compile(..., run_eagerly=True)`, or `tf.config.run_functions_eagerly(True)` for more information of where went wrong, or file a issue/bug to `tf.keras`.

Can someone help to solve this problem? :slight_smile:

Which tensorflow version are you using? Also, please check the mentioned channel mode of images in model definition that should match with your actual images dataset. I have replicated the same code with cats and dogs dataset and did not find any error. Please check this gist for your reference.