Uchechi Ugo
Uchechi Ugo

Reputation: 41

UnimplementedError: Graph execution error: running nn on tensorflow

I have been having this error, and I don't know why, especially since I am following someone's code exactly and the person had no error when running this

img_shape = (128,128,3)

# load pretrained model
base_model = tf.keras.applications.VGG19(input_shape=img_shape, include_top=False, weights='imagenet')

# freezing the model
base_model.trainable = False

#define the custom head for network
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)

# output / prediction layer
prediction_layer = tf.keras.layers.Dense(units=1, activation='sigmoid')(global_average_layer)

model = tf.keras.models.Model(inputs=base_model.input, outputs=prediction_layer)

# compile the model
opt = tf.keras.optimizers.RMSprop(learning_rate=0.0001)
model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])

# create data generators
# import library
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# define objects
data_gen_train = ImageDataGenerator(rescale=1/255.0)
data_gen_test = ImageDataGenerator(rescale=1/255.0)

# define variables
train_generator = data_gen_train.flow_from_directory(directory=training_dir, target_size=(128,128), batch_size=128, class_mode='binary')
test_generator = data_gen_test.flow_from_directory(directory=test_dir, target_size=(128,128), batch_size=128, class_mode='binary')

model.fit_generator(generator=train_generator, epochs=5, validation_data=test_generator)

This is the error I am getting

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
  """Entry point for launching an IPython kernel.
Epoch 1/5
---------------------------------------------------------------------------
UnimplementedError                        Traceback (most recent call last)
<ipython-input-46-18b18ca5977c> in <module>()
----> 1 model.fit_generator(generator=train_generator, epochs=5, validation_data=test_generator)

2 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   2221         use_multiprocessing=use_multiprocessing,
   2222         shuffle=shuffle,
-> 2223         initial_epoch=initial_epoch)
   2224 
   2225   @doc_controls.do_not_generate_docs

/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     53     ctx.ensure_initialized()
     54     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55                                         inputs, attrs, num_outputs)
     56   except core._NotOkStatusException as e:
     57     if name is not None:

UnimplementedError: Graph execution error:

Detected at node 'model/block1_conv1/Conv2D' defined at (most recent call last):
    File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
      "__main__", mod_spec)

I could not put in the complete error at the end, but Please, can someone tell me what's wrong

Upvotes: 3

Views: 19227

Answers (4)

Ali karimi
Ali karimi

Reputation: 500

use jit_compile=False, this worked for me:

my_model.compile(optimizer="Adam",...., jit_compile=False)

Upvotes: 1

AKSHAY VAYAK
AKSHAY VAYAK

Reputation: 1

The solution to this error is to check the image shape and also you have added shape to the neural network. Both must be the same.

It is solve my problem.

Upvotes: 0

Pengyue Xu
Pengyue Xu

Reputation: 11

If you are using colab, try to switch the gpu to tpu. It might helpful

Upvotes: 1

Jirayu Kaewprateep
Jirayu Kaewprateep

Reputation: 766

it is easy the train_generator is old you can use this.

It is about the memory you create the matrixes initial operations that will reduce the working loads and perform well on data sizes.

[ Sample ]:

import os
from os.path import exists

import tensorflow as tf

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
img_shape = (128,128,3)
BATCH_SIZE = 1
IMG_SIZE = (128, 128)

database_buffer = "F:\\models\\buffer\\" + os.path.basename(__file__).split('.')[0] + "\\TF_DataSets_01.h5"
database_buffer_dir = os.path.dirname(database_buffer)

if not exists(database_buffer_dir) : 
    os.mkdir(database_buffer_dir)
    print("Create directory: " + database_buffer_dir)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: DataSets
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
PATH = 'F:\\datasets\\downloads\\cats_name'
train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')

train_dataset = tf.keras.utils.image_dataset_from_directory(train_dir,
                                                                        shuffle=True,
                                                                        batch_size=BATCH_SIZE,
                                                                        image_size=IMG_SIZE)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# load pretrained model
base_model = tf.keras.applications.VGG19(input_shape=img_shape, include_top=False, weights='imagenet')

# freezing the model
base_model.trainable = False

#define the custom head for network
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)

# output / prediction layer
prediction_layer = tf.keras.layers.Dense(units=1, activation='sigmoid')(global_average_layer)

model = tf.keras.models.Model(inputs=base_model.input, outputs=prediction_layer)
model.summary()

# compile the model
opt = tf.keras.optimizers.RMSprop(learning_rate=0.0001)
model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit( train_dataset, batch_size=100, epochs=50 )

[ Output ]:

Sample

Upvotes: 1

Related Questions