当我试图训练CNN时,我得到了以下错误。
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-8308471ad202> in <cell line: 89>()
87 tf.data.experimental.enable_debug_mode()
88 # Train the model
---> 89 model.fit(
90 train_generator,
91 steps_per_epoch=train_generator.samples // batch_size,
1 frames
/usr/local/lib/python3.10/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1695 logs = tf_utils.sync_to_numpy_or_python_type(logs)
1696 if logs is None:
-> 1697 raise ValueError(
1698 "Unexpected result of `train_function` "
1699 "(Empty logs). Please use "
ValueError: Unexpected result of `train_function` (Empty logs). Please use `Model.compile(..., run_eagerly=True)`, or `tf.config.run_functions_eagerly(True)` for more information of where went wrong, or file a issue/bug to `tf.keras`.
我已经把我的图片存储在谷歌驱动器中,并使用单独的.csv文件来介绍带有标签名称的图片。在模型编译之前,它运行得很好。然后它发生了上述错误。顺便说一句,我对这个方法很陌生。我的代码就像下面的代码。
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import pandas as pd
import os
from google.colab import drive
drive.mount('/content/gdrive')
%cd /content/gdrive/My Drive/
# Set the paths to your dataset directories
train_dir = './train'
test_dir = './test'
test_items = os.listdir(test_dir)
print("Items in test_dir:")
for item in test_items:
print(item)
# Load the data into a dataframe
train_df = pd.read_csv('./train.csv')
test_df = pd.read_csv('./test.csv')
# Define image parameters
img_width, img_height = 150, 150
input_shape = (img_width, img_height, 3) # Adjust for grayscale images
# Define hyperparameters
batch_size = 32
epochs = 10
learning_rate = 0.001
# Create data generators with data augmentation for training and validation
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=True,
fill_mode='nearest'
)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_dataframe(
train_df,
x_col='filename',
y_col='fat_percentage',
directory=train_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='raw'
)
test_generator = test_datagen.flow_from_dataframe(
test_df,
x_col='filename',
y_col='fat_percentage',
directory=test_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='raw'
)
# Define the CNN architecture
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1) # No activation function for regression task
])
# Compile the model
model.compile(
loss='mse',
optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
metrics=['mae'],
)
tf.config.run_functions_eagerly(True)
tf.data.experimental.enable_debug_mode()
# Train the model
model.fit(
train_generator,
steps_per_epoch=train_generator.samples // batch_size,
epochs=epochs
)
# Evaluate the model on the test set
test_loss, test_mae = model.evaluate(test_generator, verbose=2)
print(f'Test Loss: {test_loss:.4f}')
print(f'Test MAE: {test_mae:.4f}')
是什么导致了这个错误?
1条答案
按热度按时间dphi5xsq1#
这似乎是一个图像分类问题,而不是回归任务。如果图像是灰度的,则input_shape必须为input_shape =(img_width,img_height,1)。这里1是频道号。如果图像是彩色图像(RGB),则通道的数量将是3。
共享数据集,这将有助于理解问题