import datasets import os import shutil #from datasets import load_dataset train0, validation0, test0 = datasets.load_dataset("superb", "ks", split=["train","validation","test"]) labels = train0.features["label"].names label2id = {x: labels.index(x) for x in labels} # id2label = {str(id): label for label, id in label2id.items()} down_id = label2id['down'] on_id = label2id['on'] train0_down = train0.filter(lambda example: example['label'] == down_id) train0_on = train0.filter(lambda example: example['label'] == on_id) shutil.rmtree('/home/mr249/ac_h/down_on/data/train/down',ignore_errors=True) os.mkdir('/home/mr249/ac_h/down_on/data/train/down') shutil.rmtree('/home/mr249/ac_h/down_on/data/train/on',ignore_errors=True) os.mkdir('/home/mr249/ac_h/down_on/data/train/on') for e in train0_down: p = e['audio']['path'] newpath = '/home/mr249/ac_h/down_on/data/train/down/{}'.format(p.split('/')[len(p.split('/')) - 1]) os.link(p,newpath) for e in train0_on: p = e['audio']['path'] newpath = '/home/mr249/ac_h/down_on/data/train/on/{}'.format(p.split('/')[len(p.split('/')) - 1]) os.link(p,newpath) validation0_down = validation0.filter(lambda example: example['label'] == down_id) validation0_on = validation0.filter(lambda example: example['label'] == on_id) shutil.rmtree('/home/mr249/ac_h/down_on/data/validation/down',ignore_errors=True) os.mkdir('/home/mr249/ac_h/down_on/data/validation/down') shutil.rmtree('/home/mr249/ac_h/down_on/data/validation/on',ignore_errors=True) os.mkdir('/home/mr249/ac_h/down_on/data/validation/on') for e in validation0_down: p = e['audio']['path'] newpath = '/home/mr249/ac_h/down_on/data/validation/down/{}'.format(p.split('/')[len(p.split('/')) - 1]) os.link(p,newpath) for e in validation0_on: p = e['audio']['path'] newpath = '/home/mr249/ac_h/down_on/data/validation/on/{}'.format(p.split('/')[len(p.split('/')) - 1]) os.link(p,newpath) test0_down = test0.filter(lambda example: example['label'] == down_id) test0_on = test0.filter(lambda example: example['label'] == on_id) shutil.rmtree('/home/mr249/ac_h/down_on/data/test/down',ignore_errors=True) os.mkdir('/home/mr249/ac_h/down_on/data/test/down') shutil.rmtree('/home/mr249/ac_h/down_on/data/test/on',ignore_errors=True) os.mkdir('/home/mr249/ac_h/down_on/data/test/on') for e in test0_down: p = e['audio']['path'] newpath = '/home/mr249/ac_h/down_on/data/test/down/{}'.format(p.split('/')[len(p.split('/')) - 1]) os.link(p,newpath) for e in test0_on: p = e['audio']['path'] newpath = '/home/mr249/ac_h/down_on/data/test/on/{}'.format(p.split('/')[len(p.split('/')) - 1]) os.link(p,newpath)