我有一个熊猫数据框,每一行对应一个.jpeg文件及其位置。我已经制作了train
,test
,validation
文件夹,每个文件夹中的分类类别位于上述train
,test
,validation
文件夹中。
我现在需要将每个类别的80%放入“火车”文件夹中,将10%放入测试中,将10%放入“验证”文件夹中,然后将每张图片放入相应的类别文件夹中(总共29个类别)。
以下是文件数据框的示例:
import pandas as pd
df = {'PictureFilename': ['0091240758-91931712.JPG','0091240791-91646592.JPG', '0091240791-91646593.JPG']
, 'Target': ['Detached Structure','Address Verification', 'Other']
, 'location': ['D:\CIS inspection images 0318\Photos\0091240758-91931712.JPG'
,'D:\CIS inspection images 0318\Photos\0091240791-91646592.JPG'
,'D:\CIS inspection images 0318\Photos\0091240791-91646593.JPG']}
df_1 = pd.DataFrame.from_dict(df)
df_1
为简单起见,第一个文件将放入
D:\CIS inspection images 0318\Photos\Train\Detached structure
,
第二个
D:\CIS inspection images 0318\Photos\Train\Address Verification
和
中的第三个 D:\CIS inspection images 0318\Photos\Train\Other
在实际问题上,我总共拥有300万张照片,它们将根据分割百分比流入训练,测试和验证。
使用python可能吗?
答案 0 :(得分:0)
我已经弄清楚了。我决定移动所有训练文件夹,然后从训练文件夹迭代到验证和测试文件夹。
要将所有内容移至训练文件夹:
if __name__ == '__main__':
for index, row in labels.iterrows():
try:
shutil.move(row['location'], row['destination'])
except:
pass
然后下一步是将10%移至测试文件夹:
#move 10% to test folder
for i, r in label_move_count.iterrows():
n=0
for index, row in labels.iterrows():
if row['target'] == r['target']:
#n+=1
if n < row['testcount']:
try:
shutil.move(row['destination']+'\\'+row['PictureFilename'], row['test_destination'])
print(n, row['target'])
n+=1
except:
pass
else:
pass
然后,我重复以上步骤以移至验证文件夹。这会将超过300万张照片移动到培训,验证和测试文件夹中的28个单独的班级文件夹中。