我尝试加载我的数据集并设计一个CNN,但当我尝试训练我的模型时,这个错误出现了,我想知道如何解决这个问题?
ValueError: The channel dimension of the inputs should be defined. Found None.
这是我分割数据的方式
xtrain, xtest, ytrain, ytest = trian_test_split(images_total,
image_labels,
stratify=image_labels,
random_state=1234,
test_size=0.2)
xvalid, xtest, yvalid, ytest= trian_test_split(xtest,
ytest,
stratify=ytest,
random_state=1234,
test_size=0.5)
def read_img(path, label):
file = tf.io.read_file(path)
img = tf.image.decode_png(file)
img = tf.image.resize(img, (32, 32))
return img, label
train_dataset = tf.data.Dataset.from_tensor_slices((xtrain, ytrain))
train_dataset = train_dataset.map(read_img).batch(batch_size)
valid_dataset = tf.data.Dataset.from_tensor_slices((xvalid, yvalid))
valid_dataset = valid_dataset.map(read_img).batch(batch_size)
test_dataset = tf.data.Dataset.from_tensor_slices((xtest, ytest))
test_dataset = test_dataset.map(read_img).batch(batch_size)
这是我的模型
num_classes = 26
model = tf.keras.Sequential([
tf.keras.layers.experimental.preprocessing.Resizing(32, 32),
tf.keras.layers.experimental.preprocessing.Rescaling(1./255),
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
layers.Dropout(0.2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(num_classes)
])
model.compile(
optimizer='adam',
loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(train_dataset, epochs=5, validation_data=valid_dataset)
当我试着拟合这个模型时,错误出现了,我无法修复这个
在调用时没有将y标签传递给模型,模型需要x和y标签进行相应的训练,这里我添加了一个示例
请暂停并浏览keras架构和keras IO文档的视频,这些将提供有关深度学习建模的更多信息
一旦你熟悉了这些,就跳到建模
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=3)