I am beginner at tensorflow. i want to build a simple model but i got this error.
i think it's because labels but i don't know how to fix it.
i bulid my dataset from directory files with tf.data.Dataset.
this is data set:
visit : https://i.stack.imgur.com/H2EQT.jpg
data
- --------class1: [x.jpeg ...]
- --------class2: [y.png ....]
- .
- .
- .
- --------class10: [z.jpg ...]
data_dir = os.path.join(os.path.dirname('D:/Downloads/Image data set/'), 'raw-img')
data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*.*')))
list_ds = tf.data.Dataset.list_files(str(data_dir / '*/*'), shuffle=False)
list_ds = list_ds.shuffle(image_count, reshuffle_each_iteration=False)
train_size = int(image_count * 0.8)
test_size = int(image_count * 0.1)
val_size = int(image_count * 0.1)
train_ds = list_ds.take(train_size)
val_ds = list_ds.skip(train_size)
test_ds = val_ds.skip(test_size)
val_ds = val_ds.take(test_size)
def parse_image(filename):
parts = tf.strings.split(filename, os.sep)
label = tf.cast(parts[-2] == class_names, tf.float32)
label = tf.argmax(label)
image = tf.io.read_file(filename)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.reshape(image, [32, 150, 150, 3])
return image, labels
AUTOTUNE = tf.data.experimental.AUTOTUNE
train_ds = train_ds.map(parse_image, num_parallel_calls=AUTOTUNE)
val_ds = val_ds.map(parse_image, num_parallel_calls=AUTOTUNE)
test_ds = test_ds.map(parse_image, num_parallel_calls=AUTOTUNE)
model = tf.keras.Sequential([
kr.layers.Conv2D(16, 3, activation='relu', input_shape=(150, 150 ,3)),
kr.layers.MaxPooling2D(),
kr.layers.Conv2D(32, 3, activation='relu'),
kr.layers.MaxPooling2D(),
kr.layers.Conv2D(64, 3, activation='relu'),
kr.layers.MaxPooling2D(),
kr.layers.Flatten(),
kr.layers.Dense(128, activation='softmax'),
kr.layers.Dense(10)
])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(
train_ds,
epochs=3
)
error :
this is error when i want to fit the model.
Train for 20943 steps
Epoch 1/3
1/20943 [..............................] - ETA: 1:14:41
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-38-c408313d0649> in <module>
1 model.fit(
2 train_ds,
----> 3 epochs=3
4 )
.
.
.
.
ValueError: Shape mismatch: The shape of labels (received (320,)) should equal the shape of logits except for the last dimension (received (32, 10)).
See Question&Answers more detail:
os