Hello ! I hope you’re doing well. I’ve been stuck on an error for 2 days. I would be very grateful if you have any ideas to help me. Actually when fit my model I got this error :
Epoch 1/10
UnimplementedError Traceback (most recent call last)
in <cell line: 8>()
6
7 # Fit the model
----> 8 history = network_model.fit(train_dataset, epochs=num_epochs,
9 steps_per_epoch=train_steps,
10 validation_data=val_dataset,
1 frames
/usr/local/lib/python3.10/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 for t in inputs
59 ]
—> 60 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
61 inputs, attrs, num_outputs)
62 except core._NotOkStatusException as e:
UnimplementedError: Graph execution error:
Detected at node sparse_categorical_crossentropy/Cast defined at (most recent call last):
File “/usr/lib/python3.10/runpy.py”, line 196, in _run_module_as_main
File “/usr/lib/python3.10/runpy.py”, line 86, in _run_code
File “/usr/local/lib/python3.10/dist-packages/colab_kernel_launcher.py”, line 37, in
File “/usr/local/lib/python3.10/dist-packages/traitlets/config/application.py”, line 992, in launch_instance
File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py”, line 619, in start
File “/usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py”, line 195, in start
File “/usr/lib/python3.10/asyncio/base_events.py”, line 603, in run_forever
File “/usr/lib/python3.10/asyncio/base_events.py”, line 1909, in _run_once
File “/usr/lib/python3.10/asyncio/events.py”, line 80, in _run
File “/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py”, line 685, in
File “/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py”, line 738, in _run_callback
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 825, in inner
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 786, in run
File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py”, line 377, in dispatch_queue
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 250, in wrapper
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 748, in init
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 786, in run
File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py”, line 361, in process_one
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 234, in wrapper
File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py”, line 261, in dispatch_shell
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 234, in wrapper
File “/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py”, line 539, in execute_request
File “/usr/local/lib/python3.10/dist-packages/tornado/gen.py”, line 234, in wrapper
File “/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py”, line 302, in do_execute
File “/usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py”, line 539, in run_cell
File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 2975, in run_cell
File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3030, in _run_cell
File “/usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py”, line 78, in _pseudo_sync_runner
File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3257, in run_cell_async
File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3473, in run_ast_nodes
File “/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py”, line 3553, in run_code
File “”, line 8, in <cell line: 8>
File “/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py”, line 65, in error_handler
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1783, in fit
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1377, in train_function
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1360, in step_function
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1349, in run_step
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1127, in train_step
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py”, line 1185, in compute_loss
File “/usr/local/lib/python3.10/dist-packages/keras/src/engine/compile_utils.py”, line 277, in call
File “/usr/local/lib/python3.10/dist-packages/keras/src/losses.py”, line 143, in call
File “/usr/local/lib/python3.10/dist-packages/keras/src/losses.py”, line 270, in call
File “/usr/local/lib/python3.10/dist-packages/keras/src/losses.py”, line 2454, in sparse_categorical_crossentropy
File “/usr/local/lib/python3.10/dist-packages/keras/src/backend.py”, line 5727, in sparse_categorical_crossentropy
File “/usr/local/lib/python3.10/dist-packages/keras/src/backend.py”, line 2305, in cast
2 root error(s) found.
(0) UNIMPLEMENTED: Cast string to int64 is not supported
[[{{node sparse_categorical_crossentropy/Cast}}]]
(1) CANCELLED: Function was cancelled before it was started
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_3764]
You can see my code here :
reference_df = pd.read_csv('/gdrive/MyDrive/HDA_project/reference.csv',names=['id_image','label_subject','label_posture'])
reference_df = reference_df.drop(index=reference_df.index[0], axis=0) #drop first column
reference_df = reference_df.set_index('id_image') #set index
print(reference_df)
# 72 % train, 8% validation, 20% test
train_val_reference_df, test_reference_df = train_test_split(reference_df, train_size=0.8, stratify=reference_df['label_posture'], random_state=123)
train_reference_df, val_reference_df = train_test_split(train_val_reference_df, train_size=0.9, random_state=123) #be careful no strategy here
def load_data(file_name, data_dir):
if isinstance(data_dir, bytes):
data_dir = data_dir.decode()
if isinstance(file_name, bytes):
file_name = file_name.decode()
# Load the image from the .txt file
file_mat = '/gdrive/MyDrive/HDA_project/' + data_dir + '/' + file_name + '.txt'
data = np.loadtxt(file_mat, dtype='float32')
return data
def normalize_data(data):
return tf.cast(data, tf.float32) /255.
def load_and_preprocess_data(file_name, data_dir):
# Load data
data = load_data(file_name, data_dir)
# Normalize
data = normalize_data(data)
return data
def create_dataset(reference_df, batch_size, shuffle, cache_file=None):
# Convert dataframe to lists
file_names = list(reference_df.index)
labels = reference_df['label_posture']
# Create a Dataset object
dataset = tf.data.Dataset.from_tensor_slices((file_names, labels))
# Map the load_and_preprocess_data function
py_func = lambda file_name, label: (tf.numpy_function(load_and_preprocess_data, [file_name, data_dir],
tf.float32), label)
dataset = dataset.map(py_func, num_parallel_calls=os.cpu_count())
# Cache dataset
if cache_file:
dataset = dataset.cache(cache_file)
# Shuffle
if shuffle:
dataset = dataset.shuffle(len(file_names))
# Repeat the dataset indefinitely
dataset = dataset.repeat()
# Batch
dataset = dataset.batch(batch_size=batch_size)
# Prefetch
dataset = dataset.prefetch(buffer_size=1)
return dataset
batch_size = 32
train_dataset = create_dataset(train_reference_df,
batch_size=batch_size,
shuffle=True,
cache_file='train_cache')
val_dataset = create_dataset(val_reference_df,
batch_size=batch_size,
shuffle=False,
cache_file='val_cache')
test_dataset = create_dataset(test_reference_df,
batch_size=batch_size,
shuffle=False,
cache_file='test_cache')
train_steps = int(np.ceil(len(train_reference_df)/batch_size))
val_steps = int(np.ceil(len(val_reference_df)/batch_size))
test_steps = int(np.ceil(len(test_reference_df)/batch_size))
# FUNCTION: PostureModel
def PostureModel(input_shape):
"""
Implementation of the PostureModel
Arguments:
input_shape -- shape of the images of the dataset
Returns:
model -- a Model() instance in TensorFlow
"""
# Input placeholder as a tensor with shape input_shape
X_input = tf.keras.Input(input_shape)
# FIRST CONV + MAXPOOL BLOCK
X = ConvPool(X_input, conv_feature_maps=4, conv_kernel=(3, 3), conv_strides=(1, 1), conv_padding='same', activation='relu',
pool_size=(2, 2), pool_strides=(2, 2), pool_padding='same')
# SECOND CONV + MAXPOOL BLOCK
X = ConvPool(X, conv_feature_maps=8, conv_kernel=(3, 3), conv_strides=(1, 1), conv_padding='same', activation='relu',
pool_size=(2, 2), pool_strides=(2, 2), pool_padding='same')
# FLATTEN THE TENSOR
X = tf.keras.layers.Flatten()(X_input)
# FULLYCONNECTED (DENSE) LAYER WITH RELU ACTIVATION AND 32 OUTPUT NEURONS
X = tf.keras.layers.Dense(32, activation='relu')(X)
# DROPOUT LAYER (DISCARD PROBABILITY 0.4)
X = tf.keras.layers.Dropout(0.4)(X)
# DENSE LAYER WITHOUT ACTIVATION AND 17 OUTPUT NEURONS
X = tf.keras.layers.Dense(17, activation=None)(X)
model = tf.keras.Model(inputs = X_input, outputs = X, name='PostureModel')
return model
# Create and compile the network model
input_shape = (64, 32, 1)
network_model = PostureModel(input_shape)
adam_optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)
loss_funct = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
#from_logits=True because I did not include the activation in the last layer of the network
network_model.compile(optimizer = adam_optimizer, loss = loss_funct, metrics = ["accuracy"])
# Train the model
num_epochs = 10
early_stop_callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss',
patience=5)
# Fit the model
history = network_model.fit(train_dataset, epochs=num_epochs,
steps_per_epoch=train_steps,
validation_data=val_dataset,
validation_steps=val_steps,
callbacks=[early_stop_callback])