我对机器学习和Python都很陌生。目前正在进行一个需要构建图像分类模型的项目。我已经使用 tf.keras.preprocessing.image_dataset_from_directory
从本地磁盘读取了数据,现在正试图提取 x_val
和 y_val
以生成 skilearn.metrics.classification_report
。
问题是,每当我调用以下代码时:
y_val = np.concatenate([y_val, np.argmax(y.numpy(), axis=-1)])
我会得到以下错误,并且不知道为什么,也不知道如何修复它:
y_val = np.concatenate([y_val, np.argmax(y.numpy(), axis=-1)])File “<array_function internals>”, line 5, in concatenateValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)`
这是我的代码:
#数据被分割成训练和验证文件夹,每个文件夹中有6个代表不同类别的文件夹,如下所示:
#data/train/hamburger/<汉堡训练图像在这里>
#data/train/pizza/<披萨训练图像在这里>
#data/validation/hamburger/<汉堡测试图像在这里>
#data/validation/pizza/<披萨测试图像在这里>
#training_dir = ......
validation_dir = pathlib.Path('path to data dir on local disk')
#超参数
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
training_dir,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
validation_dir,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
print(class_names)
print(val_ds.class_names)
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
normalization_layer = layers.experimental.preprocessing.Rescaling(1./255)
resize_and_rescale = tf.keras.Sequential([
layers.experimental.preprocessing.Resizing(img_height, img_width),
layers.experimental.preprocessing.Rescaling(1./255)])
#归一化,增强,模型层
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
model.summary()
start_time = time.monotonic()
epochs = 1
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs)
#绘图
#测试随机图像
test_path = pathlib.Path('C:/Users/adi/Desktop/New folder/downloads/hamburger/images(91).jpg')
img = keras.preprocessing.image.load_img(
test_path, target_size=(img_height, img_width))
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # 创建一个批次
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(
"这张图像最可能属于{},置信度为{:.2f}%"
.format(class_names[np.argmax(score)], 100 * np.max(score)))
x_val = np.array([])
y_val = np.array([])
for x, y in val_ds:
x_val = np.concatenate([x_val, np.argmax(model.predict(x), axis=-1)])
y_val = np.concatenate([y_val, np.argmax(y.numpy(), axis=-1)])
#<----- 这里崩溃
print(classification_report(y_val, x_val, target_names = ['doughnuts (Class 0)','french_fries (Class 1)', 'hamburger (Class 2)','hot_dog (Class 3)', 'ice_cream (Class 4)','pizza (Class 5)']))
为什么我会得到这个错误,以及如何修复它?或者,我该如何获取所需的数据来使 classification_report 正常工作?谢谢。
回答:
在获取真实类别时,您不需要使用 argmax
操作。
由于您在 tf.keras.preprocessing.image_dataset_from_directory 中没有指定 class_mode
,标签是稀疏的,这意味着它们不是独热编码的。
如果您有独热编码的向量标签,您的上述代码是正确的。
另外,重新命名您的数组会更好,像这样,并且在一次预测一个图像时,您可以使用 model(x)
,这更有效。正确的代码应该是:
predicted_classes = np.array([])
labels = np.array([])
for x, y in val_ds:
predicted_classes = np.concatenate([predicted_classes, np.argmax(model(x), axis=-1)])
labels = np.concatenate([labels, y.numpy()])