【问题标题】:Mapping images and their labels映射图像及其标签
【发布时间】:2020-10-24 05:52:06
【问题描述】:

我的文件夹(train)和 csv 文件(train.csv)中有 图像,其中包含 图像名称标签。 如何映射一个文件夹中的图像和另一个 csv 文件中的标签 如何创建包含图像数据和标签的数据框。

多类分类

【问题讨论】:

    标签: python deep-learning multiclass-classification


    【解决方案1】:
    import tensorflow as tf
    from tensorflow import keras
    import pandas as pd
    
    class MyTrainingData(keras.utils.Sequence):
        def __init__(self, file, labels, batchSize):
            self.file = file
            self.label = labels
            self.batchSize = batchSize
            self.n_bathces = int(len(self.file) / self.batchSize)
    
    
    
        def on_epoch_end(self):  # it is called after every epoch
            self.file, self.label = shuffle(self.file, self.label)
            for i in range(50):
                print(self.file[i], self.label[i], 'file-label')
    
    
        def __len__(self):
            return self.n_bathces
        
    
        # called after every batch to get new batch or new 32 images and labels
        def __getitem__(self, idx):
            # this method calls by fit method with idx ranging from 0 to len(training_exmaples) / batch_size =
            batchX = self.file[idx*self.batchSize: (idx+1)*self.batchSize]
            batchY = self.label[idx*self.batchSize: (idx+1)*self.batchSize]
            imgFiles = [image.load_img(name, target_size=(224, 224, 3)) for name in batchX]   #loading 32 images
            imgFiles = [image.img_to_array(img) for img in imgFiles]   #preprocessing
            imgFiles = [img / 255 for img in imgFiles]        batchY = to_categorical(batchY, 4)    # 4 represent number of classes (4 in that case)
            return np.array(imgFiles), np.array(batchY)
    
    
    def getfilePath(filenames):
        path = './train/'  # or any other path according to directory structure
        filePaths = []
        for name in filenames:
            filePaths.append(path + name)   # './train/' + 'img1.jpg' = './train/img1.jpg
    
        return filePaths
    
    df = pd.read_csv()
    img_names = df['img_names']
    labels = df['labels']
    img_names = ['img1.jpg', 'img2.jpg', -----]
    img_names = getfilePath(img_names)
    img_names = ['./train/img1.jpg', './train/img2.jpg', -----]
    label = [3, 1, 2, 0, ----]
    
    batch_size = 32 
    
    data = MyTrainingData(fileNames, labels, batchSize
    
    model = defineModel()
    sgd = SGD(learning_rate=0.0001, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy'])
    model.fit(data, epochs=10, verbose=1)
    
    
    

    上面的代码不仅仅是映射。在大数据集的情况下(大到适合 RAM)。该技术将帮助您动态加载、预处理和生成输入数据。希望你觉得它有用。提供反馈,以便我进一步改进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2019-05-20
      相关资源
      最近更新 更多