problems to compile and fit the model in tensorflow/keras - tensorflow

I'm trying to compile and fit a model but, this error is occurring:
ValueError: Shapes (None, 10, 10, 10) and (None, 10) are incompatible
code:
from tensorflow.keras.layers import Conv2D
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=3, activation="relu", input_shape=(28, 28, 1)))
model.add(Conv2D(filters=32, kernel_size=3, activation="relu"))
model.add(Flatten())
model.add(Dense(128, activation="relu"))
model.add(Dense(10, activation="softmax"))
# Compile the model
model.compile(loss="categorical_crossentropy", metrics=["accuracy"], optimizer="adam")
# Fit the model
model.fit( x=X_train, y=y_train, batch_size=32, epochs=10, validation_split = 0.3)
Output:
Epoch 1/10
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-a118e2826a10> in <module>()
3
4 # Fit the model
----> 5 model.fit( x=X_train, y=y_train, batch_size=32, epochs=10, validation_split = 0.3)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in 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
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1790, in categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5083, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (None, 10, 10, 10) and (None, 10) are incompatible
I'm using tensorflow 2.8.2
How can i put it to work ?
What am i doing wrong ?
edit :
the problem was that I ran this part of the program twice (dumb mistake):
from tensorflow.keras.utils import to_categorical
y_train = to_categorical(y_train, num_classes=10)
y_test = to_categorical(y_test, num_classes=10)
print("Shape of y_train:", y_train.shape)
print("One value of y_train:", y_train[0])
I restarted the environment, and the problem was fixed.

Related

TypeError: Failed to convert elements of <keras.losses.SparseCategoricalCrossentropy object to tensor

I'm trying to train a CNN on my own images. I've set class_mode to sparse, so I figured I would use SparseCategoricalCrossentropy. However, when I do this (or even CategoricalCrossentropy), I get this error about the elements not being of a supported type.
I've tried a few different loss functions with the same error persisting. I think there must be something wrong with the formatting of my data. I've attached my data_gen and NN code along with the full traceback.
train_datagen = ImageDataGenerator(
rescale=1./255,
width_shift_range=.05,
height_shift_range=.05,
zoom_range=0.05,
validation_split=0.3,
horizontal_flip=True,
vertical_flip=True,
fill_mode="constant",
cval=0)
test_datagen = ImageDataGenerator(
rescale=1./255)
#data augmentation
train_ds = train_datagen.flow_from_directory(
train_data_dir,
shuffle=True,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='sparse',
subset='training');
cnn = Sequential()
cnn.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(img_width,img_height,3)))
cnn.add(BatchNormalization())
cnn.add(Conv2D(32, kernel_size=(3, 3), activation='relu'))
cnn.add(BatchNormalization())
cnn.add(MaxPooling2D(pool_size=(2, 2)))
cnn.add(Dropout(0.2))
cnn.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
cnn.add(BatchNormalization())
cnn.add(Flatten())
cnn.add(Dense(64, activation='relu'))
cnn.add(Dense(22, activation='softmax'))
cnn.compile(loss=keras.losses.SparseCategoricalCrossentropy,
optimizer="Adam",
metrics=['accuracy'])
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [24], line 111
106 cnn.compile(loss=keras.losses.SparseCategoricalCrossentropy,
107 optimizer="Adam",
108 metrics=['accuracy'])
110 epochs=20
--> 111 history = cnn.fit(
112 train_ds,
113 validation_data=valid_ds,
114 steps_per_epoch=math.ceil(len(train_ds) / batch_size),
115 epochs=epochs)
117 plt.plot(history.history['loss'], label='train')
118 plt.plot(history.history['val_loss'], label='validation')
File ~\AppData\Local\Programs\Python\Python310\lib\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 ~\AppData\Local\Temp\__autograph_generated_fileswqeb6ya.py:15, in outer_factory.<locals>.inner_factory.<locals>.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
TypeError: in user code:
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1160, in train_function *
return step_function(self, iterator)
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1146, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1135, in run_step **
outputs = model.train_step(data)
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 994, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1052, in compute_loss
return self.compiled_loss(
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\losses.py", line 158, in __call__
return losses_utils.compute_weighted_loss(
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\losses_utils.py", line 328, in compute_weighted_loss
losses = tf.convert_to_tensor(losses)
TypeError: Failed to convert elements of <keras.losses.SparseCategoricalCrossentropy object at 0x0000017523557AC0> to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.

ValueError: Input 0 of layer "sequential_13" is incompatible with the layer: expected shape=(None, 21367, 9000), found shape=(None, 9000)

I don't know why this error keep coming when I run the code below
CNN.fit(X_train_vector, y_train, epochs=10)
My CNN code is this:
CNN = tf.keras.models.Sequential()
CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (21367, 9000)))
CNN.add(tf.keras.layers.MaxPooling1D(2))
CNN.add(tf.keras.layers.Dropout(0.2))
CNN.add(tf.keras.layers.Flatten())
CNN.add(tf.keras.layers.Dense(200, activation='relu'))
CNN.add(tf.keras.layers.Dense(20, activation='relu'))
CNN.add(tf.keras.layers.Dense(1, activation='softmax'))
My "X_train_vector" has a shape:
(21367, 9000)
My "y_train" has a shape:
(21367, 1)
The Error I am getting:
Epoch 1/10
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-108-895976bf38cd> in <module>()
----> 1 CNN.fit(X_train_vector, y_train, epochs=10)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in 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
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential_13" is incompatible with the layer: expected shape=(None, 21367, 9000), found shape=(None, 9000)
I have tried several solutions, including changing my first line of CNN to this:
CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000)))
But running it says:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-109-dd8d734d0a9f> in <module>()
1 CNN = tf.keras.models.Sequential()
----> 2 CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000)))
3 CNN.add(tf.keras.layers.MaxPooling1D(2))
4 CNN.add(tf.keras.layers.Dropout(0.2))
5 CNN.add(tf.keras.layers.Flatten())
3 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py in __init__(self, trainable, name, dtype, dynamic, **kwargs)
441 else:
442 batch_size = None
--> 443 batch_input_shape = (batch_size,) + tuple(kwargs['input_shape'])
444 self._batch_input_shape = batch_input_shape
445
TypeError: 'int' object is not iterable
Can anyone help me. I have been looking for the solution for two days, it should work the way I am trying. Is there a mistake I am making? Please let me know.
Thanks In Advance.
The input array should have the shape (None, shape_0, shape_1), where None represent the batch size, and (shape_0, shape_1) represents the shape of the feature. So, you should reshape your input array:
X_train_vector = X_train_vector.reshape(-1, 9000, 1)
And you don't really need to specify the batch size when building the model, so remove that and just use (9000, 1) as the input_shape. Try this:
CNN = tf.keras.models.Sequential()
CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000, 1)))
CNN.add(tf.keras.layers.MaxPooling1D(2))
CNN.add(tf.keras.layers.Dropout(0.2))
CNN.add(tf.keras.layers.Flatten())
CNN.add(tf.keras.layers.Dense(200, activation='relu'))
CNN.add(tf.keras.layers.Dense(20, activation='relu'))
CNN.add(tf.keras.layers.Dense(1, activation='softmax'))
And this should solve the problem the same error would not appear again.

Getting ValueError: Shapes (None, 1) and (None, 9) are incompatible after fitting the Skin Cancer Dataset in the CNN

I am creating a model to detect skin cancer using the ISIC skin cancer dataset. I have created a CNN model but after compiling it's throwing a shape error.
My-Code-
scale = 1./255
num_classes = 9
model = Sequential()
model.add(layers.experimental.preprocessing.Rescaling(scale, offset =0.0))
model.add(layers.Conv2D(32, (3, 3), padding = 'same', input_shape=(180, 180, 3)))
model.add(layers.Activation('relu'))
model.add(layers.Conv2D(64, (3, 3), padding='same'))
model.add(layers.Activation('relu'))
model.add(layers.Conv2D(128, (3, 3)))
model.add(layers.Activation('relu'))
model.add(layers.Flatten())
model.add(layers.Dense(20))
model.add(layers.Activation('relu'))
model.add(layers.Dense(num_classes))
model.add(layers.Activation('softmax'))
model.compile(optimizer='sgd',
loss='categorical_crossentropy',
metrics=['accuracy'])
epochs = 2
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
My training data-
<PrefetchDataset shapes: ((None, 180, 180, 3), (None,)), types: (tf.float32, tf.int32)>
My Val data -
<PrefetchDataset shapes: ((None, 180, 180, 3), (None,)), types: (tf.float32, tf.int32)>
Now after fitting the model I am getting the error - ValueError: Shapes (None, 1) and (None, 9) are incompatible
Epoch 1/2
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-12-c695c39c566b> in <module>()
3 train_ds,
4 validation_data=val_ds,
----> 5 epochs=epochs
6 )
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
984 except Exception as e: # pylint:disable=broad-except
985 if hasattr(e, "ag_error_metadata"):
--> 986 raise e.ag_error_metadata.to_exception(e)
987 else:
988 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:855 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:845 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:838 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:797 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:155 __call__
losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:259 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:1644 categorical_crossentropy
y_true, y_pred, from_logits=from_logits)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4862 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:1161 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 1) and (None, 9) are incompatible
My Notebook
Data
You set label_mode='int', that's why you should use sparse_categorical_crossentropy as a loss function but you use loss function categorical_crossentropy which use generally when your target is one-hot encoded.
From tf.keras.preprocessing.image_dataset_from_directory, the label_mode should be as follows
- 'int': means that the labels are encoded as integers (e.g. for
sparse_categorical_crossentropy loss).
- 'categorical' means that the labels are encoded as a categorical
vector (e.g. for categorical_crossentropy loss).
- 'binary' means that the labels (there can be only 2) are encoded as
float32 scalars with values 0 or 1 (e.g. for binary_crossentropy).
- None (no labels).
That's why in your case, changing the loss function from categorical_crossentropy to sparse_categorical_crossentropy should solve the issue.

TensorFlow 2.0, error in keras.applications (as_list() is not defined on an unknown TensorShape)

There are several questions on SO with this error:
ValueError: as_list() is not defined on an unknown TensorShape.
and a few relevant issues on git as well: 1, 2.
However, I haven't found a consistent answer to why this message appears, or a solution for my specific issue. This entire pipeline used to work with tf2.0.0-alpha and now, after installing with Conda conda install tensorflow=2.0 python=3.6 the pipeline is broken.
In short terms, I use a generator to return image data to the tf.data.Dataset.from_generator() method. This works fine, until I try to call the model.fit() method, which results in the following error.
Train for 750 steps, validate for 100 steps
Epoch 1/5
1/750 [..............................] - ETA: 10sTraceback (most recent call last):
File "/usr/local/anaconda3/envs/tf/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/tmo/Projects/casa/image/src/train.py", line 148, in <module>
Trainer().train_vgg16()
File "/Users/tmo/Projects/casa/image/src/train.py", line 142, in train_vgg16
validation_steps=100)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 728, in fit
use_multiprocessing=use_multiprocessing)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 324, in fit
total_epochs=epochs)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 123, in run_one_epoch
batch_outs = execution_function(iterator)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py", line 86, in execution_function
distributed_function(input_fn))
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 457, in __call__
result = self._call(*args, **kwds)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 503, in _call
self._initialize(args, kwds, add_initializers_to=initializer_map)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 408, in _initialize
*args, **kwds))
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1848, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2150, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2041, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py", line 915, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 358, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py", line 66, in distributed_function
model, input_iterator, mode)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py", line 112, in _prepare_feed_values
inputs, targets, sample_weights = _get_input_from_iterator(inputs)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py", line 149, in _get_input_from_iterator
distribution_strategy_context.get_strategy(), x, y, sample_weights)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/distribute/distributed_training_utils.py", line 308, in validate_distributed_dataset_inputs
x_values_list = validate_per_replica_inputs(distribution_strategy, x)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/distribute/distributed_training_utils.py", line 356, in validate_per_replica_inputs
validate_all_tensor_shapes(x, x_values)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/distribute/distributed_training_utils.py", line 373, in validate_all_tensor_shapes
x_shape = x_values[0].shape.as_list()
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_shape.py", line 1171, in as_list
raise ValueError("as_list() is not defined on an unknown TensorShape.")
ValueError: as_list() is not defined on an unknown TensorShape.
This is the code that loads and reshapes each image:
def preprocess_image(self, image):
"""
"""
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, self.hw)
image /= 255.0 # normalize to [0,1] range
image.set_shape([224, 224, 3])
return image
which is applied to a generator (e.g. training_generator) that runs through a list of images and yields the preprocessed result:
def make_ts_dataset(self):
AUTOTUNE = tf.data.experimental.AUTOTUNE
BATCH_SIZE = 32
image_count_training = len(self.X_train)
image_count_validation = len(self.X_test)
training_generator = GetTensor(hw=self.hw, train=True).make_tensor
training_image_ds = tf.data.Dataset.from_generator(training_generator, tf.float32, [224, 224, 3])
training_price_ds = tf.data.Dataset.from_tensor_slices(tf.cast(self.y_train, tf.float32))
validation_generator = GetTensor(hw=self.hw, test=True).make_tensor
validation_image_ds = tf.data.Dataset.from_generator(validation_generator, tf.float32, [224, 224, 3])
validation_price_ds = tf.data.Dataset.from_tensor_slices(tf.cast(self.y_test, tf.float32))
training_ds = tf.data.Dataset.zip((training_image_ds, training_price_ds))
validation_ds = tf.data.Dataset.zip((validation_image_ds, validation_price_ds))
training_ds = training_ds.shuffle(buffer_size=int(round(image_count_training)))
training_ds = training_ds.repeat()
training_ds = training_ds.batch(BATCH_SIZE)
training_ds = training_ds.prefetch(buffer_size=AUTOTUNE)
validation_ds = validation_ds.shuffle(buffer_size=int(round(image_count_validation)))
validation_ds = validation_ds.repeat()
validation_ds = validation_ds.batch(BATCH_SIZE)
validation_ds = validation_ds.prefetch(buffer_size=AUTOTUNE)
for image_batch, label_batch in training_ds.take(1):
print(label_batch.shape, image_batch.shape)
pass
return training_ds, validation_ds
At all points, the shapes look correct, i.e. (32,) (32, 224, 224, 3)
I am initialising the weights with trained weights from VGG16
def train_vgg16(self):
training_ds, validation_ds = Trainer.make_ts_dataset(self)
base_vgg = keras.applications.vgg16.VGG16(include_top=False,
weights='imagenet',
input_shape=(224, 224, 3))
base_vgg.trainable = False
print(base_vgg.summary())
vgg_with_base = keras.Sequential([
base_vgg,
tf.keras.layers.GlobalMaxPooling2D(),
tf.keras.layers.Dense(1024, activation=tf.nn.relu),
tf.keras.layers.Dense(1024, activation=tf.nn.relu),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dense(1)])
print(base_vgg.summary())
vgg_with_base.compile(optimizer='adam',
loss='mse',
metrics=['mape'])
vgg_with_base.fit(training_ds,
epochs=5,
validation_data=validation_ds,
steps_per_epoch=750,
validation_steps=100)
However, training never initiates because x_shape = x_values[0].shape.as_list() fails.
Edit (11/12/19):
After some troubleshooting, I found that the error is initiated in the keras.applications layer.
base_vgg = keras.applications.vgg16.VGG16(include_top=False,
weights='imagenet',
input_shape=(224, 224, 3))
Removing base_vgg from the model and initialising training works fine.
Explicitly defining the shape with Input from tensorflow.keras.layers in the keras.applications.vgg16.VGG16 solved the problem for me.
from tensorflow.keras.layers import Input
base_vgg = keras.applications.vgg16.VGG16(include_top=False,
weights='imagenet',
input_tensor=Input(shape=(224,224,3)),
input_shape=(224, 224, 3))
I still think this is closer to a bug than a feature though.
I fixed this error by tf.cast(label, tf.float32) after importing the masks.

"slice index 0 of dimension 0 out of bounds" exception when using tf.data dataset API with Keras

I tired to use tf.data api to load mnist dataset to train a simple model as below, but got "slice index 0 of dimension 0 out of bounds" exception. I was wondering what I did wrong in my code.
import math
import tensorflow as tf
import numpy as np
batch_size = 32
def load_data():
mnist = tf.keras.datasets.mnist
(train_data, train_label), (validation_data, validation_label) = mnist.load_data()
train_data, validation_data = train_data / 255.0, validation_data / 255.0
train_label = train_label.astype(np.float32)
return train_data, train_label
def build_model():
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
train_data, train_label = load_data()
train_sample_count = len(train_data)
train_dataset = tf.data.Dataset.from_tensor_slices((train_data, train_label))
train_dataset = train_dataset.batch(batch_size)
train_dataset = train_dataset.repeat()
iter = train_dataset.make_one_shot_iterator()
train_x, train_y = iter.get_next()
model = build_model()
model.fit(
train_dataset,
epochs=10,
steps_per_epoch=math.ceil(train_sample_count/batch_size)
)
The full stacktrace is as below
Epoch 1/10
2018-08-23 16:14:49.485165: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-08-23 16:15:25.588057: W tensorflow/core/framework/op_kernel.cc:1275] OP_REQUIRES failed at strided_slice_op.cc:105 : Invalid argument: slice index 0 of dimension 0 out of bounds.
2018-08-23 16:15:26.852912: W tensorflow/core/framework/op_kernel.cc:1275] OP_REQUIRES failed at iterator_ops.cc:910 : Cancelled:
Traceback (most recent call last):
File "/Users/xievi/workspace/speaker_verification/mnist_with_dataset.py", line 35, in
steps_per_epoch=math.ceil(train_sample_count/batch_size)
File "/Users/xievi/shared_python3_venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1363, in fit
validation_steps=validation_steps)
File "/Users/xievi/shared_python3_venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py", line 205, in fit_loop
outs = f(ins)
File "/Users/xievi/shared_python3_venv/lib/python3.6/site-packages/tensorflow/python/keras/backend.py", line 2914, in call
fetched = self._callable_fn(*array_vals)
File "/Users/xievi/shared_python3_venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1382, in call
run_metadata_ptr)
File "/Users/xievi/shared_python3_venv/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 519, in exit
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: slice index 0 of dimension 0 out of bounds.
[[Node: flatten/strided_slice = StridedSlice[Index=DT_INT32, T=DT_INT32, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](flatten/Shape, metrics/acc/Const, training/TFOptimizer/gradients/dense_1/Softmax_grad/Sum/reduction_indices, training/TFOptimizer/gradients/dense_1/Softmax_grad/Sum/reduction_indices)]]
Just for the records, I'll share my fix.
This is the error I was getting:
ValueError: slice index 0 of dimension 0 out of bounds. for '{{node strided_slice}} = StridedSlice[Index=DT_INT32, T=DT_INT32, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1](Shape, strided_slice/stack, strided_slice/stack_1, strided_slice/stack_2)' with input shapes: [0], [1], [1], [1] and with computed input tensors: input[1] = <0>, input[2] = <1>, input[3] = <1>.
This error prevented my Keras model from training. I tried changing the data pipline from Keras custom generator to tf.data, but the error didn't go. Finally, I was able to make it start training by changing the loss function from:
loss = 'sparse_categorical_crossentropy'
To:
loss = 'categorical_crossentropy'
I don't know the reason but this works for me.
Here is the full trace of my error:
Epoch 1/100
Traceback (most recent call last):
File "classify_cnn_generator.py", line 175, in <module>
history = model.fit(train_generator,
File "/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1098, in fit
tmp_logs = train_function(iterator)
File "/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 823, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 696, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2855, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3065, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:789 run_step **
outputs = model.train_step(data)
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:748 train_step
loss = self.compiled_loss(
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/keras/engine/compile_utils.py:212 __call__
batch_dim = array_ops.shape(y_t)[0]
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py:1013 _slice_helper
return strided_slice(
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py:1186 strided_slice
op = gen_array_ops.strided_slice(
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/ops/gen_array_ops.py:10347 strided_slice
_, _, _op, _outputs = _op_def_library._apply_op_helper(
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py:742 _apply_op_helper
op = g._create_op_internal(op_type_name, inputs, dtypes=None,
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py:591 _create_op_internal
return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:3477 _create_op_internal
ret = Operation(
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:1974 __init__
self._c_op = _create_c_op(self._graph, node_def, inputs,
/home/deogun/alali/.conda/envs/mytf2.3/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:1815 _create_c_op
raise ValueError(str(e))
ValueError: slice index 0 of dimension 0 out of bounds. for '{{node strided_slice}} = StridedSlice[Index=DT_INT32, T=DT_INT32, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1](Shape, strided_slice/stack, strided_slice/stack_1, strided_slice/stack_2)' with input shapes: [0], [1], [1], [1] and with computed input tensors: input[1] = <0>, input[2] = <1>, input[3] = <1>.