I am trying to train an AI on a custom database and i ran into this error
I tried removing and altering the numbers and activations etc. but either i get a syntax error or the same ValueError said in the title. The 2 codes are from different tutorials so i understand if they arent compatible but for the most part the code works except for the last part( starting at model = models.Sequential()
# Imports needed
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, models
from tensorflow.keras.preprocessing.image import ImageDataGenerator
ds_train = tf.keras.preprocessing.image_dataset_from_directory(
"dataset",
labels="inferred",
label_mode="int", # categorical, binary
color_mode="grayscale",
batch_size=2,
image_size=(28,28), # reshape if not in this size
shuffle=True,
seed=123,
validation_split=0.1,
subset="training",
)
ds_validation = tf.keras.preprocessing.image_dataset_from_directory(
"dataset",
labels="inferred",
label_mode="int",
color_mode="grayscale",
batch_size=2,
image_size=(28, 28), # reshape if not in this size
shuffle=True,
seed=123,
validation_split=0.1,
subset="validation",
)
def augment(x, y):
image = tf.image.random_brightness(x, max_delta=0.05)
return image, y
ds_train = ds_train.map(augment)
# Custom Loops
for epochs in range(10):
for x, y in ds_train:
# train here
pass
model = models.Sequential()
model.add(layers.Conv2D(32,(3,3), activation = 'relu', input_shape = (32,32,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64,(3,3), activation = 'relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64,(3,3), activation = 'relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation = 'relu'))
model.add(layers.Dense(10, activation= 'softmax'))
model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(ds_train, epochs=10, verbose=2)
The full traceback is:
Found 236 files belonging to 1 classes.
Using 213 files for training.
Found 236 files belonging to 1 classes.
Using 23 files for validation.
C:\Users\alboz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\layers\convolutional\base_conv.py:99: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
super().__init__(
Epoch 1/10
Traceback (most recent call last):
File "c:\Users\alboz\OneDrive\Desktop\Jugendforscht\medizin\egal.py", line 65, in <module>
model.fit(ds_train, epochs=10, verbose=2)
File "C:\Users\alboz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\utils\traceback_utils.py", line 123, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\alboz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\layers\input_spec.py", line 227, in assert_input_compatibility
raise ValueError(
ValueError: Exception encountered when calling Sequential.call().
Input 0 of layer "conv2d" is incompatible with the layer: expected axis -1 of input shape to have value 3, but received input with shape (None, 28, 28, 1)
Arguments received by Sequential.call():
• inputs=tf.Tensor(shape=(None, 28, 28, 1), dtype=float32)
• training=True
• mask=None