I'm a beginner in coding and my project now involves image classification. In the below code from the deep learning class, there is the function maybe_pickle
but I don't know what does "pickle" do and how helpful it is though I have tried searching for some answers.
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)