写入pickle文件

仅记录

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)))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值