映射图像及其标签



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

多类分类

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(。该技术将帮助您动态加载、预处理和生成输入数据。希望你觉得它有用。提供反馈,以便我可以进一步改进它。

相关内容

  • 没有找到相关文章

最新更新