使用NiftyNet时,我们需要先将图像数据和标签进行一次简单的处理,得到对应的.csv
文件。
对应文件格式为:
img.csv
img_name | img_path |
label.csv
img_label | img_path |
在此给出一个二分类的生成该文件的demo。首先,已经将两个类别的图片分别存储在两个文件夹中
demo
import pandas as pd
import os
# 生成 img.csv
list_img = []
list_path = []
img_path = 'C:\\Users\\fan\\Desktop\\demo\\train\\ad'
img_name = os.listdir(img_path)
for i, item in enumerate(img_name):
list_img.append(item)
list_path.append(img_path + "\\" + item)
img_path = "C:\\Users\\fan\\Desktop\\demo\\train\\cn"
img_name = os.listdir(img_path)
for i, item in enumerate(img_name):
list_img.append(item)
list_path.append(img_path + "\\" + item)
data_frame = pd.DataFrame({'image': list_img, 'path': list_path})
data_frame.to_csv('C:\\Users\\fan\\Desktop\\demo\\train\\img_path.csv', index=False)
# 生成label.csv
list_label_name = []
list_label_path = []
label_path = 'C:\\Users\\fan\\Desktop\\demo\\train\\ad'
label_name = os.listdir(label_path)
for j, elem in enumerate(label_name):
list_label_name.append(elem[0:2])
list_label_path.append(label_path + '\\' + elem)
label_path = 'C:\\Users\\fan\\Desktop\\demo\\train\\cn'
label_name = os.listdir(label_path)
for j, elem in enumerate(label_name):
list_label_name.append(elem[0:2])
list_label_path.append(label_path + '\\' + elem)
print(list_label_name)
label_dataframe = pd.DataFrame({'label': list_label_name, 'path': list_label_path})
label_dataframe.to_csv('C:\\Users\\fan\\Desktop\\demo\\train\\label.csv', index=False)