火车验证数据拆分-标签可用,但没有类别 [英] Train Validation data split - labels available but no classes

查看:63
本文介绍了火车验证数据拆分-标签可用,但没有类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的研究项目是开发一个神经网络来识别车牌上的文字.因此,我在

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

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. 如何将图像和标签放在一起?我认为我必须合并两个数据集,不是吗?

非常感谢!

解决方案

问题1和2:

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

问题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)

my studies project is to develop a neural network to recognize text on license plates. Therefore, I found the ReId-dataset at 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/. This dataset contains a bunch of images of number plates as well as the text of the license plates and was used by Spanhel et al. for a similar approach as the one I have in mind.

Example of a license plate there:

In the project I want to recognize only the license plate text, i.e. only "9B5 2145" and not the country acronym "CZ" and no advertisement text.

I downloaded the dataset and the labels csv-file to my local memory. So, I have the following folder structure: One mother directory for my whole project. This mother directory includes my data directory, where I stored the ReId dataset. This dataset includes several subdirectories, 4 directories with training data and 4 with test data, all of this subdirectories contain a number of images of license plates. The ReId dataset also contains the trainVal csv-file which is structured as follows (snippet of the actual sheet):

track_id is equal to the subdirectory of the ReID dataset. image_path is equal to the path to the image, in this case the image's name is 1_1. lp is the label of the license plate, so the actual license plate. train is a dummy variable, equal to one, if the image is used for training purposes and 0 for validation purposes.

Regarding this dataset, I got three main questions:

  1. How do I read in this images properly? I tried to use something like this

    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()))
    

But obviously Python did not find images belonging to any classes (side note: I used the correct paths). That is clear to me, because I did not assign any class to my data yet. So, my first question is: Do I have to do that? I don't think so.

  1. How do I then read this images properly? I think, I have to get numpy arrays to work properly with this data.

  2. How do I bring my images and the labels together? In my opinion, I think I have to merge the two datasets, don't I?

Thank you very much!

解决方案

Question 1 and 2:

For reading the images, imread from matplotlib.pyplot can be used as shown in the example, this does not require any classes to be set.

Question 3:

The labels and images can be brought together by storing the corresponding license plate number in an output array (y in the example) for each image (stored in the xs array in the example) in the data array. You don't necessarily need to merge them.

Hope I helped!

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)

这篇关于火车验证数据拆分-标签可用,但没有类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆