'Meet an error " ValueError: Shapes (None, 5) and (None, 4) are incompatible"
Can anyboday help me on this error? the total files are 2204 to 5 classes. and 1764 files for training. Thanks advanced.
this is my code:
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.python.keras.layers import Dense, Flatten
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
import pathlib
data_dir = r"/root/data_Camera"
data_dir = pathlib.Path(data_dir)
rock = list(data_dir.glob('rock/*'))
print(rock[0])
PIL.Image.open(str(rock[0]))
img_height, img_width = 400,2000
batch_size = 32
trains_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split = 0.2,
subset = "training",
seed = 123,
label_mode = 'categorical',
image_size = (img_height, img_width),
batch_size = batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
label_mode = 'categorical',
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = trains_ds.class_names
print(class_names)
resnet_model = Sequential()
pretrained_model = tf.keras.applications.ResNet50(include_top=False,
input_shape=(400,2000,3),
pooling='avg',
classes = 5,
weights = 'imagenet')
for layer in pretrained_model.layers:
layer.trainable=False
resnet_model.add(pretrained_model)
resnet_model.add(Flatten())
resnet_model.add(Dense(512, activation='relu'))
resnet_model.add(Dense(4,activation='softmax'))
resnet_model.summary()
resnet_model.compile(optimizer=Adam(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy'])
epochs = 10
history= resnet_model.fit(
trains_ds,
validation_data=val_ds,
epochs=epochs)
and I meet the error is: ValueError: Shapes (None, 5) and (None, 4) are incompatible I also add the file code to here. https://github.com/CallaDai/Tensorflow.git you can check it out. thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
