InvalidArgumentError: Graph execution error for MobileNet model - python

I'm rather new to the world of DL and I'm trying to run a MobileNet model to do a multi-class classification but I keep getting this invalidargumenterror when I run it:
code snippet
datagen = ImageDataGenerator(
rotation_range=0.5,
zoom_range = 0.5,
width_shift_range=0.5,
height_shift_range=0.5,
horizontal_flip=True,
vertical_flip=True)
datagen.fit(x_train)
model = applications.mobilenet.MobileNet(weights = "imagenet", include_top=False, input_shape = (150, 150, 3))
for layer in model.layers[:5]:
layer.trainable = False
#Adding custom Layers
x = model.output
x = Flatten()(x)
x = Dense(512, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
predictions = Dense(1, activation="sigmoid")(x)
model = Model(inputs=model.input, outputs=predictions)
model.compile(loss='categorical_crossentropy',optimizer = "Adam",metrics=['acc'])
history = model.fit(datagen.flow(x_train,y_train, batch_size=50),
epochs = 20, validation_data = (x_val,y_val), steps_per_epoch=80)
error that I get:
WARNING:tensorflow:`input_shape` is undefined or non-square, or `rows` is not in [128, 160, 192, 224]. Weights for input shape (224, 224) will be loaded as the default.
Epoch 1/20
2022-10-20 21:32:06.755928: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled.
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
Cell In [20], line 18
15 model = Model(inputs=model.input, outputs=predictions)
16 model.compile(loss='binary_crossentropy',optimizer = "Adam",metrics=['acc'])
---> 18 history = model.fit(datagen.flow(x_train,y_train, batch_size=50),
19 epochs = 20, validation_data = (x_val,y_val), steps_per_epoch=80)
File ~/miniforge3/lib/python3.10/site-packages/keras/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File ~/miniforge3/lib/python3.10/site-packages/tensorflow/python/eager/execute.py:54, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
52 try:
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:
InvalidArgumentError: Graph execution error:
does anyone know how to solve this error? I'm not too sure if the error has got to do with the size of my input images.
My TensorFlow version is 2.10.0 and I'm running my code on VSC using M2 mac.
thank you!!

Related

ValueError: Dimension 0 in both shapes must be equal, but are 2 and 1

I'm trying to do a deep nn fort an imbalanced dataset:
data :
three_d_trended_X.shape, three_d_trended_y.shape
((7018, 87, 49), (7018,))
tf.random.set_seed(42)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Input(shape = (three_d_trended_X.shape[1],
three_d_trended_X.shape[2])))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.SimpleRNN(2048,
activation = 'relu'))
model.add(tf.keras.layers.Dense(2048,
activation = 'relu'))
model.add(tf.keras.layers.Dense(1, activation = 'sigmoid'))
model.compile(loss="binary_crossentropy",
optimizer='adam',
metrics=[tfa.metrics.FBetaScore(num_classes=2, beta=2.0, threshold=0.5),
tfa.metrics.MultiLabelConfusionMatrix(num_classes=2)])
history = model.fit(train_three_d_trended_X,
train_three_d_trended_y,
validation_data=(val_three_d_trended_X,
val_three_d_trended_y),
epochs=500,
class_weight = weights)
But it returns the following error :
Epoch 1/500
--------------------------------------------------------------------------- ValueError Traceback (most recent call
last) Cell In [72], line 5
1 model.compile(loss="binary_crossentropy",
2 optimizer='adam',
3 metrics=[tfa.metrics.FBetaScore(num_classes=2, beta=2.0, threshold=0.5),
4 tfa.metrics.MultiLabelConfusionMatrix(num_classes=2)])
----> 5 history = model.fit(train_three_d_trended_X,
6 train_three_d_trended_y,
7 validation_data=(val_three_d_trended_X,
8 val_three_d_trended_y),
9 epochs=500,
10 class_weight = weights)
File
/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py:70,
in filter_traceback..error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.traceback)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File /tmp/autograph_generated_file9fmkjph9.py:15, in
outer_factory..inner_factory..tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval = ag_.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
File /tmp/autograph_generated_filebcjkp3f3.py:57, in
outer_factory..inner_factory..tf__update_state(self,
y_true, y_pred, sample_weight)
55 raise
56 return fscope_1.ret(retval__1, do_return_1)
---> 57 ag.converted_call(ag__.ld(self).true_positives.assign_add, (ag__.converted_call(ag__.ld(weighted_sum), ((ag_.ld(y_pred) *
ag__.ld(y_true)), ag__.ld(sample_weight)), None, fscope),), None,
fscope)
58 ag__.converted_call(ag__.ld(self).false_positives.assign_add, (ag__.converted_call(ag__.ld(weighted_sum), ((ag_.ld(y_pred) * (1 -
ag__.ld(y_true))), ag__.ld(sample_weight)), None, fscope),), None,
fscope)
59 ag__.converted_call(ag__.ld(self).false_negatives.assign_add, (ag__.converted_call(ag__.ld(weighted_sum), (((1 - ag_.ld(y_pred)) *
ag__.ld(y_true)), ag__.ld(sample_weight)), None, fscope),), None,
fscope)
ValueError: in user code:
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py",
line 1160, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/tensorflow_addons/metrics/f_scores.py",
line 160, in update_state *
self.true_positives.assign_add(_weighted_sum(y_pred * y_true, sample_weight))
ValueError: Dimension 0 in both shapes must be equal, but are 2 and 1. Shapes are [2] and [1]. for '{{node AssignAddVariableOp_2}} =
AssignAddVariableOp[dtype=DT_FLOAT](AssignAddVariableOp_2/resource,
Sum_2)' with input shapes: [], [1].

Keras: TypeError: 'retval_' has dtype int32 in the main branch, but dtype float32 in the else branch

I'm getting an error:
TypeError: 'retval_' has dtype int32 in the main branch, but dtype float32 in the else branch
Everytime I try to train my RNN. The model is developed for paraphrase detection but it fails at the first epoch. There are no visible problems in the code so I'm not sure what could be causing this problem. Can someone please help me in fixing this?
Thanks!
My Model:
def create_model(input_shape,embeddings_dim, embeddings_matrix, vocab_size,
max_seq_length, trainable_embeddings,
lstm_hidden_units):
input_1 = Input(shape=input_shape)
input_2 = Input(shape=input_shape)
common_embed = Embedding(vocab_size, embeddings_dim, weights=[embeddings_matrix], input_length=max_seq_length, trainable=trainable_embeddings)
lstm_1 = common_embed(input_1)
lstm_2 = common_embed(input_2)
common_lstm = LSTM(lstm_hidden_units,return_sequences=True, activation="relu")
vector_1 = common_lstm(lstm_1)
vector_1 = Flatten()(vector_1)
vector_2 = common_lstm(lstm_2)
vector_2 = Flatten()(vector_2)
x3 = Subtract()([vector_1, vector_2])
x3 = Multiply()([x3, x3])
x1_ = Multiply()([vector_1, vector_1])
x2_ = Multiply()([vector_2, vector_2])
x4 = Subtract()([x1_, x2_])
x5 = Lambda(cosine_distance, output_shape=cos_dist_output_shape)([vector_1, vector_2])
conc = Concatenate(axis=-1)([x5,x4, x3])
x = Dense(100, activation="relu", name='conc_layer')(conc)
x = Dropout(0.01)(x)
out = Dense(1, activation="sigmoid", name = 'out')(x)
model = Model([input_1, input_2], out)
optimizer = keras.optimizers.Adam()
model.compile(optimizer=optimizer,
loss="binary_crossentropy",
metrics=['accuracy', precision, recall, f1_score])
return model
embedding_dim = 50
trainable_embeddings = False
lstm_hidden_units = 50
input_shape = (max_length,)
model = create_model(input_shape,
embedding_dim, embedding_matrix, vocab_size,
max_length, trainable_embeddings,lstm_hidden_units)
epochs = 30
batch_size = 64
history = model.fit(x=[train_sentences_1, train_sentences_2],
y=train_labels,
epochs=epochs,
batch_size=batch_size,
validation_data=([val_sentences_1, val_sentences_2], val_labels))
Where train_sentences_x is of shape (10000, 120).
Error trace is as follows:
Epoch 1/30
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Users/raffaysajjad/Library/CloudStorage/OneDrive-Personal/MS Computer Science/Semester 4 (Spring 2022)/CS 5316 - Natural Language Processing/Assignments/Assignment 4/Assignment4_Part2_20030001.ipynb Cell 15' in <module>
52 epochs = 30
53 batch_size = 64
---> 54 history = model.fit(x=[train_sentences_1, train_sentences_2],
55 y=train_labels,
56 epochs=epochs,
57 batch_size=batch_size,
58 validation_data=([val_sentences_1, val_sentences_2], val_labels))
File /usr/local/lib/python3.9/site-packages/keras/utils/traceback_utils.py:67, in filter_traceback.<locals>.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
File /usr/local/lib/python3.9/site-packages/tensorflow/python/framework/func_graph.py:1147, in func_graph_from_py_func.<locals>.autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
TypeError: in user code:
File "/usr/local/lib/python3.9/site-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/var/folders/vz/gpzxhx9x3nzbg0fm1w7bt_7m0000gn/T/ipykernel_6933/666218398.py", line 27, in precision *
if c2 == 0:
TypeError: 'retval_' has dtype int32 in the main branch, but dtype float32 in the else branch

Error when trying to fit the model in Colab but works fine in Jupyter notebook

I tried to fit the model with my train and test data (about 7GB) in Colab as when using Jupyter notebook with my local machine it takes too long to train. However when I tried to use Colab it gives me the below error but it works fine in Jupyter.
Epoch 1/20
---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call last)
<ipython-input-60-677f43e317d7> in <module>()
6 epochs=20,
7 steps_per_epoch=len(training_set),
----> 8 validation_steps=len(testing_set)
9 )
1 frames
/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:
And I have changed the Runtime type to GPU in colab and I'm currently not using the Colab pro version, datasets are stored in the Google drive. I'm a bit confused as the code was working fine without any issue in Jupyter notebook.
You can view the colab file using the below GitHub link.
https://github.com/ArchieVon/DL/blob/main/ResNet_Test1.ipynb
Changing the IMAGE_SIZE from list to tuple resolves the issue. Please find the working code below.
from tensorflow.keras.layers import Input, Lambda, Dense, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img
from tensorflow.keras.models import Sequential
import numpy as np
from glob import glob
IMAGE_SIZE = (224, 224, 3)
train_path = '/content/dogs_vs_cats/train'
valid_path = '/content/dogs_vs_cats/test'
resnet = ResNet50(input_shape=IMAGE_SIZE, weights='imagenet', include_top=False)
for layer in resnet.layers:
layer.trainable = False
folders = glob('/content/dogs_vs_cats/train/*')
len(folders)
x = Flatten()(resnet.output)
prediction = Dense(len(folders), activation='softmax')(x)
model = Model(inputs=resnet.input, outputs=prediction)
#model.summary()
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory(train_path,
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
testing_set = test_datagen.flow_from_directory(valid_path,
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
len(testing_set)
len(training_set)
# fit the model
# Run the cell. It will take some time to execute
r = model.fit(
training_set,
validation_data=testing_set,
epochs=1,
steps_per_epoch=len(training_set),
validation_steps=len(testing_set)
)
The output is as follows:
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
94773248/94765736 [==============================] - 0s 0us/step
94781440/94765736 [==============================] - 0s 0us/step
Found 20000 images belonging to 2 classes.
Found 5000 images belonging to 2 classes.
625/625 [==============================] - ETA: 0s - loss: 0.9511 - accuracy: 0.6004
Let us know if the issue still persists. Thanks!

CNN model Categorical error: logits and labels must be broadcastable: logits_size=logits_size=[32,10] labels_size=[32,8] this error

I want to classify this fruits data using cnn . and when i fit the model using the classifier after making all the layers, i get this error.
Here I am trying to run CNN model on image classification.
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
train_set = train_datagen.flow_from_directory('/content/drive/MyDrive/data_set_skin_cancer_classifier_ham10000/train_set',
target_size = (224, 224),
batch_size = 32,
class_mode = "categorical")
validation_datagen = ImageDataGenerator(rescale = 1./255)
validation_set = validation_datagen.flow_from_directory('/content/drive/MyDrive/data_set_skin_cancer_classifier_ham10000/validation_set',
target_size = (224, 224),
batch_size = 32,
class_mode = "categorical")
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory('/content/drive/MyDrive/data_set_skin_cancer_classifier_ham10000/test_set',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
model = Sequential()
model.add(Conv2D(32, (3, 3), padding="same", activation ='relu', input_shape = [224, 224, 3]))
model.add(MaxPooling2D(pool_size = (2,2) ,strides = 2))
model.add(Flatten())
model.add(Dense(units=128, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
hist=model.fit(train_set , validation_data = validation_set , epochs=10, verbose=1)
following error of previous code problem
error problem name
Epoch 1/10
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-57-170508ee1342> in <module>()
----> 1 hist=model.fit(train_set , validation_data = validation_set , epochs=10, verbose=1)
6 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
InvalidArgumentError: logits and labels must be broadcastable: logits_size=[32,10] labels_size=[32,8]
[[node categorical_crossentropy/softmax_cross_entropy_with_logits (defined at <ipython-input-57-170508ee1342>:1) ]] [Op:__inference_train_function_2244]
Function call stack:
train_function
It seems, that the dataset you are using provides 8 distinct categories, encoded as one-hot labels. Changing the size of your softmax layer to 8 should do the trick:
model.add(Dense(10, activation='softmax')) # insert 8 here instead

Matrix size-incompatible: In[0]: [1,501760], In[1]: [25088,1024]

I am trying to use a pre-trained VGG16 network for classification. For this I wrote the code like the following:
from keras.applications.vgg16 import VGG16
from keras.models import Model
from keras.layers import Flatten, Dense, Dropout
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
flat1 = Flatten()(base_model.outputs)
class1 = Dense(1024, activation='relu')(flat1)
drop1 = Dropout(0.5)(class1)
class2 = Dense(512, activation='relu')(drop1)
drop2 = Dropout(0.5)(class2)
class3 = Dense(256, activation='relu')(drop2)
drop3 = Dropout(0.5)(class3)
class4 = Dense(128, activation='relu')(drop3)
drop4 = Dropout(0.5)(class4)
output = Dense(4, activation='softmax')(drop4)
model = Model(inputs=base_model.inputs, outputs=output)
model.summary()
But when I try to fit the model using:
history = model.fit_generator(train_generator, steps_per_epoch=70, epochs=40, validation_data=validation_generator, validation_steps=20, verbose=0)
I am getting this error which I am unable to understand.
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-26-11baa76f8cd2> in <module>()
8 #mc = ModelCheckpoint('vgg16_end_to_end.hdf5', monitor='val_loss', mode='min', save_best_only=True)
9
---> 10 history = model.fit_generator(train_generator, steps_per_epoch=70, epochs=40, validation_data=validation_generator, validation_steps=20, verbose=0)
9 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
InvalidArgumentError: Matrix size-incompatible: In[0]: [1,501760], In[1]: [25088,1024]
[[node dense_8/MatMul (defined at /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_5022]
Function call stack:
keras_scratch_graph
I have been training using this model for the last few days and the model was running fine. Suddenly what happened? Please help ya... just goin crazy

Categories

Resources