Reputation: 13
I am getting
ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [300, 300, 3]. Model Summary shows 4 inputs but it says 3.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import tensorflow_datasets as tfds
import logging
logger = tf.get_logger()
logger.setLevel(logging.ERROR)
from tensorflow.keras.preprocessing.image import ImageDataGenerator
(train_dataset, test_dataset), metadata = tfds.load('rock_paper_scissors',split=['train', 'test'], as_supervised=True, with_info=True)
class_names = ['rock','paper','scissors']
num_train_examples = metadata.splits['train'].num_examples
num_test_examples = metadata.splits['test'].num_examples
print("Number of training examples: {}".format(num_train_examples))
print("Number of test examples: {}".format(num_test_examples))
get_label_name = metadata.features['label'].int2str
print(get_label_name(0))
print(get_label_name(1))
print(get_label_name(2))
def format_example(image, label):
# Make image color values to be float.
image = tf.cast(image, tf.float32)
# Make image color values to be in [0..1] range.
image = image / 255.
return image, label
dataset_train = train_dataset.map(format_example)
dataset_test =test_dataset.map(format_example)
l1 = tf.keras.layers.Conv2D(32, (3,3), activation = 'relu', input_shape=(300,300,3))
l2 = tf.keras.layers.MaxPooling2D(2,2)
l3 = tf.keras.layers.Conv2D(64, (3,3), activation='relu')
l4 = tf.keras.layers.MaxPooling2D(2,2)
l5 = tf.keras.layers.Conv2D(128, (3,3), activation='relu')
l6 = tf.keras.layers.MaxPooling2D(2,2)
l7 = tf.keras.layers.Flatten()
l8 = tf.keras.layers.Dense(512, activation='relu')
l9 = tf.keras.layers.Dense(3, activation='softmax')
model = tf.keras.Sequential([l1,l2,l3,l4,l5,l6,l7,l8,l9])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy,
metrics=['accuracy'])
model.summary()
epochs = 10
batch_size = 32
history = model.fit(
dataset_train,
validation_data = dataset_test,
steps_per_epoch = int(np.ceil(num_train_examples/float(batch_size))),
validation_steps = int(np.ceil(num_test_examples/float(batch_size))),
epochs = epochs
)
Where am I going wrong? I have not encountered this error with cats and dogs dataset.
Upvotes: 1
Views: 373
Reputation: 36584
You need to batch your dataset in order to get the right shape:
dataset_train = train_dataset.map(format_example).batch(1)
dataset_test =test_dataset.map(format_example).batch(1)
When you don't batch,a picture with shape (h, w, c)
will be returned. If you batch, you will get (n, h, w, c)
, which is what Keras expects. Do the test yourself:
import tensorflow as tf
images = tf.random.uniform(shape=(10, 224, 224, 3), maxval=256, dtype=tf.int32)
ds = tf.data.Dataset.from_tensor_slices(images)
for pic in ds:
print(pic.shape)
break
(224, 224, 3)
With batching:
for pic in ds.batch(4):
print(pic.shape)
break
(4, 224, 224, 3)
Upvotes: 1