【问题标题】:Train Validation data split - labels available but no classes训练验证数据拆分 - 标签可用但没有类
【发布时间】:2020-04-05 12:52:41
【问题描述】:

我的研究项目是开发一个神经网络来识别车牌上的文字。因此,我在https://medusa.fit.vutbr.cz/traffic/research-topics/general-traffic-analysis/holistic-recognition-of-low-quality-license-plates-by-cnn-using-track-annotated-data-iwt4s-avss-2017/ 找到了 ReId 数据集。该数据集包含一堆车牌图像以及车牌文本,并被 Spanhel 等人使用。与我想到的方法类似。

那里的车牌示例:

在项目中,我只想识别车牌文本,即只有“9B5 2145”,而不是国家首字母缩写词“CZ”,也没有广告文本。

我将数据集和标签 csv 文件下载到本地内存。所以,我有以下文件夹结构: 我的整个项目的一个母目录。这个母目录包括我的数据目录,我存储了 ReId 数据集。该数据集包括几个子目录,4 个包含训练数据的目录和 4 个包含测试数据的目录,所有这些子目录都包含许多车牌图像。 ReId 数据集还包含 trainVal csv 文件,其结构如下(实际工作表的 sn-p):

track_id 等于 ReID 数据集的子目录。 image_path 等于图像的路径,在这种情况下,图像的名称是 1_1。 lp 是车牌的标签,所以是实际的车牌。 train 是一个虚拟变量,如果图像用于训练目的则等于 1,而 0 则用于验证目的。

关于这个数据集,我有三个主要问题:

  1. 如何正确阅读这些图片?我试着用这样的东西

    from keras.preprocessing.image import ImageDataGenerator
    
    # create generator
    datagen = ImageDataGenerator()
    
    # prepare an iterators for each dataset
    train_it = datagen.flow_from_directory('data/train/', class_mode='binary')
    val_it = datagen.flow_from_directory('data/validation/', class_mode='binary')
    test_it = datagen.flow_from_directory('data/test/', class_mode='binary')
    
    # confirm the iterator works
    batchX, batchy = train_it.next()
    print('Batch shape=%s, min=%.3f, max=%.3f' % (batchX.shape, batchX.min(), batchX.max()))
    

但显然 Python 没有找到属于任何类的图像(旁注:我使用了正确的路径)。这对我来说很清楚,因为我还没有为我的数据分配任何类。所以,我的第一个问题是:我必须这样做吗?我不这么认为。

  1. 我该如何正确读取这些图像?我想,我必须让 numpy 数组正确处理这些数据。

  2. 如何将图像和标签放在一起?在我看来,我认为我必须合并两个数据集,不是吗?

非常感谢!

【问题讨论】:

    标签: python arrays tensorflow merge neural-network


    【解决方案1】:

    问题1和2:

    为了读取图像,可以将 matplotlib.pyplot 中的 imread 用作 如示例所示,这不需要设置任何类。

    问题 3:

    对于数据数组中的每个图像(在示例中存储在 xs 数组中),可以通过将相应的车牌号存储在输出数组(示例中为 y)中来将标签和图像组合在一起。您不一定需要合并它们。

    希望我能帮上忙!

    import os
    import matplotlib.pyplot as plt
    import numpy as np 
    import pandas as pd
    
    xs, y = [], []
    main_dir = './sample/dataset' # the main directory 
    label_data = pd.read_csv('labels.csv')
    
    for folder in os.listdir(main_dir):
        for img in os.listdir(os.path.join(main, folder)):
            arr = plt.imread(os.path.join(main, folder) + img)
            xs.append(arr)
            y.append(label_data[label_data['image_path'] == os.path.join(folder, img)]['lp'])
                    #^ this part can be changed depending on the exact format of your label data file.
    
    # then you can convert them into numpy arrays and reshape them as you need.
    xs = np.array(xs)
    y = np.array(y)
    

    【讨论】:

    • 谢谢!我认为这行得通!但我仍然收到错误代码: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\tobis\\OneDrive\\Desktop\\Masterarbeit\\data\\2017-IWT4S-CarsReId_LP-dataset \\s01_l011000_1.png'。我检查了这个。相反,图像路径是 ... s01_l01/1_1.png。在我看来,代码没有“找到”最后一个目录,此外,在文件名中添加了 3 个零。我检查了这个,似乎这些图像存储在 1000_1 下,但是这三个零在打开一秒钟后消失了。
    猜你喜欢
    • 1970-01-01
    • 2019-04-22
    • 2020-05-11
    • 2021-07-26
    • 1970-01-01
    • 2019-05-01
    • 2019-12-09
    • 2021-11-12
    • 2018-08-28
    相关资源
    最近更新 更多