【问题标题】:How to create a Image Dataset just like MNIST dataset?如何像 MNIST 数据集一样创建图像数据集?
【发布时间】:2017-01-10 09:16:01
【问题描述】:

我有 10000 个手写数字的 BMP 图像。如果我想将数据提供给神经网络,我需要做什么?对于 MNIST 数据集,我只需要编写

(X_train, y_train), (X_test, y_test) = mnist.load_data()

我在 python 中使用 Keras 库。我怎样才能创建这样的数据集?

【问题讨论】:

    标签: python image-processing dataset neural-network keras


    【解决方案1】:

    您可以编写一个函数来加载所有图像并将它们堆叠到一个 numpy 数组中(如果所有图像都适合 RAM),或者使用包含函数 flow_from_directory 的 Keras ImageDataGenerator (https://keras.io/preprocessing/image/)。你可以在这里找到一个例子https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d

    【讨论】:

    • 这里直接写代码就更好了。链接的内容可能会随着时间的推移而丢失,尤其是 Github 的内容。我认为将整个代码而不是链接放在 SO 社区中是一种最佳做法。
    【解决方案2】:

    您应该编写自己的函数来加载所有图像或这样做:

    imagePaths = sorted(list(paths.list_images(args["testset"])))
    
    # loop over the input images
    for imagePath in imagePaths:
        # load the image, pre-process it, and store it in the data list
        image = cv2.imread(imagePath)
        image = cv2.resize(image, (IMAGE_DIMS[1], IMAGE_DIMS[0]))
        image = img_to_array(image)
        data.append(image)
        # extract the class label from the image path and update the
        # labels list
    
    
    data = np.array(data, dtype="float") / 255.0
    

    【讨论】:

      【解决方案3】:

      我可能会迟到,但我发布我的答案是为了帮助其他访问此问题以寻找答案的人。在这个答案中,我将解释数据集类型、如何生成此类数据集以及如何加载这些文件。

      文件格式是什么

      这些数据集已经是vectorizedNumpy format 中的数据集。检查here (Keras Datasets Documentation) 以获取参考。这些数据集以.npz 文件格式存储。检查here(MNIST digits classification dataset)。这是从文档中复制的代码块以供参考。

      tf.keras.datasets.mnist.load_data(path="mnist.npz")
      

      生成 .npz 文件后,您可以像使用 mnist 默认数据集一样使用它。

      如何生成 .npz 文件

      以下是如何从文件夹中的所有图像生成这样的数据集

      #generate and save file
      from PIL import Image
      import os
      import numpy as np
      
      path_to_files = "./images/"    
      vectorized_images = []
      
      for _, file in enumerate(os.listdir(path_to_files)):
          image = Image.open(path_to_files + file)
          image_array = np.array(image)
          vectorized_images.append(image_array)        
      # save as DataX or any other name. But the same element name is to be used while loading it back. 
      np.savez("./mnistlikedataset.npz",DataX=vectorized_images) 
      

      如果您想使用保存多个元素,您可以通过对代码进行适当的其他更改来执行类似的操作。

      np.savez("./mnistlikedataset.npz",DataX=vectorized_images_x,DataY=vectorized_images_Y)
      

      如何加载数据文件

      #load and use file
      import numpy as np
      
      path = "./mnistlikedataset.npz"
      with np.load(path) as data:
          #load DataX as train_data
          train_data = data['DataX']
          print(train_data)
      

      类似于保存多个元素,如果您想从文件中加载多个元素,您可以通过其他适当的更改来执行类似的操作

      with np.load(path) as data:
          train_data = data['DataX']
          print(train_data)
          test_data = data['DataY']
          print(test_data)
      

      【讨论】:

        【解决方案4】:

        numpy 可以将数组保存为二进制文件 numpy save

        import numpy as np
        
        def save_data():
          [images, labels] = read_data()
          outshape = len(images[0])
          npimages = np.empty((0, outshape), dtype=np.int32)
          nplabels = np.empty((0,), dtype=np.int32)
        
          for i in range(len(labels)):
              label = labels[i]
              npimages = np.append(npimages, [images[i]], axis=0)
              nplabels = np.append(nplabels, y)
        
          np.save('images', npimages)
          np.save('labels', nplabels)
        
        
        def read_data():
          return [np.load('images.npy'), np.load('labels.npy')]
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-10-23
          • 2021-10-05
          • 2019-10-30
          • 2017-08-12
          • 1970-01-01
          • 2018-08-06
          • 2020-01-11
          • 1970-01-01
          相关资源
          最近更新 更多