我是编码的初学者,我的项目现在涉及图像分类。在深度学习类的下面的代码中,有一个函数maybe_pickle
,但我不知道“pickle”做了什么,虽然我尝试寻找一些答案,但它有多大帮助。
def maybe_pickle(data_folders, min_num_images_per_class, force=False):
dataset_names = []
for folder in data_folders:
set_filename = folder + '.pickle'
dataset_names.append(set_filename)
if os.path.exists(set_filename) and not force:
# You may override by setting force=True.
print('%s already present - Skipping pickling.' % set_filename)
else:
print('Pickling %s.' % set_filename)
dataset = load_letter(folder, min_num_images_per_class)
try:
with open(set_filename, 'wb') as f:
pickle.dump(dataset, f, pickle.HIGHEST_PROTOCOL)
except Exception as e:
print('Unable to save data to', set_filename, ':', e)
return dataset_names
train_datasets = maybe_pickle(train_folders, 45000)
test_datasets = maybe_pickle(test_folders, 1800)
# import pickle
# Create a list
test_values = ['test value','test value 2','test value 3']
file_Name = "testfile"
# Open the file for writing
fileObject = open(file_Name,'wb')
# This writes the object a to the
# file named 'testfile'
pickle.dump(test_values, fileObject)
# Then we close the fileObject
fileObject.close()
# We then open the file for reading
fileObject = open(file_Name,'r')
# And the object from the file into var b
test_values_loaded = pickle.load(fileObject)
答案 0 :(得分:0)
首先查看官方pickle documentation。它需要一个可以序列化的Python对象并将其转换为可以保存到磁盘的字节流(或者反向,从文件开始并将其转换回Python对象。如果要在两者之间保存状态,通常会执行此操作。运行或者如果你想缓存长时间运行的操作的结果(例如,like_ *就像函数通常用于此目的)。