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

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.

Related

Trouble Settign GRU Input Sizes in Keras Model Stack

I'm very new to machine learning and Keras. I'm attempting predictive values on a time series with 9 input features and 1 output feature.
I have input data of shape:(5787, 9) --> 5787 data points, 9 features per sample
I have target data of shape:(5787,) --> 5787 target data points
My model looks like this and compiles just fine:
# Build Model
model = Sequential()
model.add(layers.Dense(units=(features * neuron_per_feature),
# input_shape=input_data.shape,
activation = 'relu',
name='dense1',
))
# First GRU Layer
# input shape should be form (batch, window_size, features)
model.add(layers.GRU(features * neuron_per_feature,
# input_shape=(5787, features * neuron_per_feature),
dropout=dropout,
return_sequences=True,
name='gru1',
))
model.add(layers.Dense(features * (neuron_per_feature / 2),
activation = 'relu',
name='dense2',
))
model.add(layers.GRU(features * 4,
# input_shape=(window_size * features, ),
dropout=dropout,
return_sequences=False,
name='gru2',
))
model.add(layers.Dense(1,
name='dense3',
))
# Configure Adam optimizer
opt = keras.optimizers.Adam(
learning_rate=learning_rate,
beta_1=0.9,
beta_2=0.98,
epsilon=1e-9)
# Compile Model
model.compile(optimizer=opt, loss='mse') # mse = mean squared error
# model.summary()
I try to train the model like this:
`# Fit network
history = model.fit(
x=input_data,
y=target_data,
validation_split=0.0,
batch_size=batch_size,
epochs=epochs,
verbose="auto",
# validation_data=(test_X, test_Y),
shuffle=False,
workers=2,
use_multiprocessing=True,
)`
However, I continually get errors when adding in the GRU layers:
`<ipython-input-62-76971a07670d> in <module>
1 # Fit network
----> 2 history = model.fit(
3 x=input_data,
4 y=target_data,
5 validation_split=0.0,
1 frames
/usr/local/lib/python3.8/dist-packages/keras/engine/training.py in 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
ValueError: in user code:
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 889, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.8/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.8/dist-packages/keras/engine/input_spec.py", line 214, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
ValueError: Exception encountered when calling layer "sequential_19" (type Sequential).
Input 0 of layer "gru1" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 108)
Call arguments received by layer "sequential_19" (type Sequential):
• inputs=tf.Tensor(shape=(None, 9), dtype=float32)
• training=True
• mask=None`
Any help would be much appreciated!
I've tried disabling the GRU layers, and the model will run the fit training. But when I re-add the GRUs, it fails.

problems to compile and fit the model in tensorflow/keras

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.

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.

Keras model errors

I'm new to machine learning and have been stuck with this error for awhile now:
Traceback (most recent call last):
File "model1.py", line 77, in
model.fit(train_generator,
File "C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1098, in fit
tmp_logs = train_function(iterator)
File "C:\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in call
result = self._call(*args, **kwds)
File "C:\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 840, in _call
return self._stateless_fn(*args, **kwds)
File "C:\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 2829, in call
return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
File "C:\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 1843, in _filtered_call
return self._call_flat(
File "C:\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 1923, in _call_flat
return self._build_call_outputs(self._inference_function.call(
File "C:\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 545, in call
outputs = execute.execute(
File "C:\Python38\lib\site-packages\tensorflow\python\eager\execute.py", line 59, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.NotFoundError: No algorithm worked!
[[node sequential/conv2d/Conv2D (defined at model1.py:77) ]] [Op:__inference_train_function_806]
I'm using CUDA 10.1/Cudnn 8.05, which works fine for the other convolution projects I've tried. Just switching to the basic VGG16 model here lets me run the program without any accuracy gains, so I'm not sure if the issue is with my GPU's libraries, a mistake in the tensor dimensions, or a failure to initialize something.
import tensorflow as tf
import tensorflow_datasets as tfds
# Helper libraries
import os
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers
batch_size=10
train_dir="C:/draw/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train"
validation_dir="C:/draw/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/val"
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.2,
fill_mode='nearest')
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(224, 224),
batch_size=batch_size,
class_mode='categorical')
validation_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(224, 224),
batch_size=batch_size,
class_mode='categorical')
model = tf.keras.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(32, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(64))
model.add(layers.Dense(1000, activation='softmax'))
model.compile(optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
num_epochs=20
steps_per_epoch=10000
val_steps=10
model.fit(train_generator,
epochs=num_epochs,
steps_per_epoch = steps_per_epoch,
validation_data=validation_generator,
validation_steps=val_steps)
Thanks in advance for any advice provided in how to fix this error.
Your problem is that in flow_from_directory you need to specify the parameter subset. For the train_generator specify the subset as subset='training'. In the validation_generator specify it as subset='validation'.