图像分类数据预处理:训练集标签映射和数据读取
图像分类数据预处理:训练集标签映射和数据读取
在图像分类任务中,训练数据预处理是至关重要的步骤。本文将介绍如何进行训练数据预处理,包括类别标签映射和训练数据读取。
类别标签映射
在训练数据集中,每个类别通常用一个数字或字符串来表示。为了方便理解和使用,我们可以建立一个类别标签映射,将数字或字符串映射到对应的类别名称。
labels = {value: key for key, value in train_generator.class_indices.items()}
print('Label Mappings for classes present in the training and validation datasets\n')
for key, value in labels.items():
print(f'{key} : {value}')
该代码片段使用字典 labels 来存储类别标签映射,其中 key 为数字或字符串,value 为对应的类别名称。
训练数据读取
读取训练数据时,需要将图像文件加载到内存中,并将其转换成合适的格式。以下代码片段展示了如何读取图像文件并将其存储在 x_train 和 y_train 中:
code = {'Brogue': 0, 'Clog': 1, 'Boat': 2, 'Sneaker': 3, 'Ballet Flat': 4}
def getcode(n):
for x, y in code.items():
if n == y:
return x
x_train = []
y_train = []
for folder in os.listdir(train_dir):
files = gb.glob(pathname=str(train_dir + folder + '/*'))
for file in files:
imag = cv2.imread(file)
imag_array = cv2.resize(imag, (150, 150))
x_train.append(imag_array)
y_train.append(code[folder])
该代码片段使用 os.listdir() 获取训练数据目录中的所有文件夹,并使用 gb.glob() 获取每个文件夹中的所有图像文件。然后使用 cv2.imread() 加载图像文件,并使用 cv2.resize() 将图像大小调整为 (150, 150)。最后,将图像数据存储在 x_train 中,将对应的类别标签存储在 y_train 中。
简化代码
上述代码可以简化为以下形式:
x_train = []
y_train = []
for folder in os.listdir(train_dir):
files = gb.glob(pathname=str(train_dir + folder + '/*'))
for file in files:
imag = cv2.imread(file)
imag_array = cv2.resize(imag, (150, 150))
x_train.append(imag_array)
y_train.append(code[folder])
该代码片段省略了类别标签映射部分,直接使用 code 字典来获取类别标签。
通过以上步骤,我们完成了训练数据的预处理,并得到了用于后续模型训练的 x_train 和 y_train。
原文地址: https://www.cveoy.top/t/topic/pcZh 著作权归作者所有。请勿转载和采集!