我尝试在 preprocessing.image_dataset_from_directory 上运行 model.evaluate() 但无济于事,因为 label_mode=None
我正在尝试从 ImageDataGenerator 的 flow_from_directory 实现与 class_mode='input' 类似的功能。我已尝试多次并不断收到相同的错误消息。我也尝试过手动更改模型的输入,但我仍然不确定哪里出了问题。下面是我的代码:
SIZE = 128
batch_size = 64
train_generator = preprocessing.image_dataset_from_directory(
r'C:\Users\#omitted user name#\Downloads\archive (1)\noncloud_train',
image_size=(SIZE, SIZE),
batch_size=batch_size,
label_mode=None
)
validation_generator = preprocessing.image_dataset_from_directory(
r'C:\Users\#omitted user name#\Downloads\archive (1)\noncloud_test',
image_size=(SIZE, SIZE),
batch_size=batch_size,
label_mode=None
)
anomaly_generator = preprocessing.image_dataset_from_directory(
r'C:\Users\#omitted user name#\Downloads\archive (1)\cloud',
image_size=(SIZE, SIZE),
batch_size=batch_size,
label_mode=None
)
rescaling_layer = layers.Rescaling(1./255)
def change_inputs(images, labels=None):
x = tensorflow.image.resize(rescaling_layer(images),[SIZE, SIZE], method=tensorflow.image.ResizeMethod.NEAREST_NEIGHBOR)
return x, x
train_dataset = train_generator.map(change_inputs)
validation_dataset = validation_generator.map(change_inputs)
anomaly_dataset = anomaly_generator.map(change_inputs)
#some model building and compiling code goes here but I omitted it#
# Examine the recon. error between val data and anomaly images
validation_error = model.evaluate(validation_generator)
anomaly_error = model.evaluate(anomaly_generator)
# Print out the results
print(f"Recon. error for the validation data is {validation_error}")
print(f"Recon. error for the anomaly data is {anomaly_error}")
最后四行是由于 label_mode 导致的问题
您必须将 label_mode 设置为适当的类型,然后再转换