我正在研究皮肤癌的分类,我将“ GroundTruth.csv”文件和“训练数据”作为jpg图像,将csv文件以一种热编码格式保存,我想将图像放入文件夹中,并以列作为文件夹名称。
这张图片清楚了意思
https://user-images.githubusercontent.com/45392637/57570855-0a6cb400-7407-11e9-8eb3-adb7b1bd70b6.JPG
# Create new folders in the training directory for each of the classes
nv = os.path.join(train_dir, 'nv')
os.mkdir(nv)
mel = os.path.join(train_dir, 'mel')
os.mkdir(mel)
bkl = os.path.join(train_dir, 'bkl')
os.mkdir(bkl)
bcc = os.path.join(train_dir, 'bcc')
os.mkdir(bcc)
akiec = os.path.join(train_dir, 'akiec')
os.mkdir(akiec)
vasc = os.path.join(train_dir, 'vasc')
os.mkdir(vasc)
df = os.path.join(train_dir, 'df')
os.mkdir(df)
# Create new folders in the validation directory for each of the classes
nv = os.path.join(val_dir, 'nv')
os.mkdir(nv)
mel = os.path.join(val_dir, 'mel')
os.mkdir(mel)
bkl = os.path.join(val_dir, 'bkl')
os.mkdir(bkl)
bcc = os.path.join(val_dir, 'bcc')
os.mkdir(bcc)
akiec = os.path.join(val_dir, 'akiec')
os.mkdir(akiec)
vasc = os.path.join(val_dir, 'vasc')
os.mkdir(vasc)
df = os.path.join(val_dir, 'df')
os.mkdir(df)
下一步,我要将图像放入文件夹中。
答案 0 :(得分:0)
根据已编辑的问题和评论中的其他信息进行了更新:
import pandas as pd
import os
# Read in the data
ground_truth = pd.read_csv('GroundTruth.csv')
# Loop through the DataFrame created from the csv file
for row in ground_truth.iterrows():
image_name = row[1].image
# Skip the first column, which is not one hot encoded
target_folder = row[1].index[row[1].values[1:].argmax() + 1]
if not os.path.exists(target_folder):
# Create the folder
os.makedirs(target_folder)
# Move the file
os.rename(f'./{image_name}.jpg',
f'./{target_folder}/{image_name}.jpg')