【问题标题】:Image classification with Keras - cats and dogs example error使用 Keras 进行图像分类 - 猫和狗示例错误
【发布时间】:2021-02-19 08:17:52
【问题描述】:

我正在尝试在 keras 上启动并运行猫和狗示例,但到目前为止没有成功。

Found 23410 files belonging to 2 classes.
Using 4682 files for validation.
2021-02-19 10:05:56.625856: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
2021-02-19 10:05:56.640618: I tensorflow/core/platform/profile_utils/cpu_utils.cc:112] CPU Frequency: 2801090000 Hz
Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9

这是代码:

import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
import os

num_skipped = 0
total = 0
for folder_name in ("Cat", "Dog"):
    folder_path = os.path.join("PetImages", folder_name)
    for fname in os.listdir(folder_path):
        fpath = os.path.join(folder_path, fname)
        try:
            total += 1
            fobj = open(fpath, "rb")
            is_jfif = tf.compat.as_bytes("JFIF") in fobj.peek(10)
        finally:
            fobj.close()

        if not is_jfif:
            num_skipped += 1
            # Delete corrupted image
            os.remove(fpath)

print("Total %d Deleted %d images" % (total, num_skipped) )

    image_size = (180, 180)
    batch_size = 32
    
    train_ds = tf.keras.preprocessing.image_dataset_from_directory(
        "PetImages",
        validation_split=0.2,
        subset="training",
        seed=1337,
        image_size=image_size,
        batch_size=batch_size,
    )
    val_ds = tf.keras.preprocessing.image_dataset_from_directory(
        "PetImages",
        validation_split=0.2,
        subset="validation",
        seed=1337,
        image_size=image_size,
        batch_size=batch_size,
    )
    
    plt.figure(figsize=(10, 10))
    for images, labels in train_ds.take(1):
        for i in range(9):
            ax = plt.subplot(3, 3, i + 1)
            plt.imshow(images[i].numpy().astype("uint8"))
            plt.title(int(labels[i]))
            plt.axis("off")

知道如何进一步进行此操作吗?可能和安装的pythong、keras、tensorflow版本有关?

【问题讨论】:

    标签: python keras image-classification


    【解决方案1】:

    也有同样的问题。要解决此问题,只需添加:

    plt.show() 
    

    在 for 语句之后。例如

    plt.figure(figsize=(10, 10))
    for images, labels in train_ds.take(1):
            for i in range(9):
                ax = plt.subplot(3, 3, i + 1)
                plt.imshow(images[i].numpy().astype("uint8"))
                plt.title(int(labels[i]))
                plt.axis("off") 
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 2022-11-08
      • 2019-09-17
      • 2019-01-31
      • 2019-05-27
      • 2019-12-31
      • 2019-08-08
      • 2017-02-23
      • 2019-04-14
      相关资源
      最近更新 更多