仅记录
path2 = "/home/prmi/FWW/ControlGAN/data/affectnet/images"
trainpath = "/home/prmi/FWW/ControlGAN/data/affectnet/train_filename.pickle"
testpath = "/home/prmi/FWW/ControlGAN/data/affectnet/test_filename.pickle"
# filelist1 = os.listdir(root_path)
filelist2 = os.listdir(path2)
train = []
test = []
# random_index = np.random.randint(low= 0, high=1400, size=100) # 随机生成[0,1400)范围内的100个整数,会有重复值
random_index = random.sample(range(1400),100) # 不会有重复值的随机整数
# random_index = list(random_index)
print(random_index)
print(len(random_index))
for i in range(len(filelist2)):
if i in random_index: # 说明这个索引是在test中的
test.append(filelist2[i].split(".")[0])
else:
train.append(filelist2[i].split(".")[0])
print(len(train))
print(len(test))
import pickle
with open(testpath, 'wb') as f:
pickle.dump(test, f, protocol=2)
print('Save to: ', testpath)
with open(trainpath, 'wb') as f:
pickle.dump(train, f, protocol=2)
print('Save to: ', trainpath)
with open(testpath, 'rb') as f:
filenames = pickle.load(f)
print('Load filenames from: %s (%d)' % (testpath, len(filenames)))