Saturday, 25 March 2017

Persistance in Python

Recently I was training a dataset for Facial Age Determination. A considerable amount was spent each time I launched my script.

The Reason: It was retraining the whole dataset at each launch.

Solution : Saving the dataset into a file loading it back.


Serialization in Python


Serialization allows you to save objects to a file and reload them. I have worked with Java and Python Serialization, and with python its a piece of cake.

There are two options

  1. Pickle
  2. CPickle
You usually would want to use cpickle as its faster. It can be over a thousand times faster.

I would be thus demonstration cpickle store and load through this post. The steps for pickle are the same. Just import pickle instead of cpickle and you are good to go.



Code

This code is tested on Python2 despite the bracketed print statements. These days I am working with Python3, and I am lazy (well not really but I am not making your life more simpler. Yikes already did !)

import cPickle as pickle

print ('Script to test the pickle utility')

tosave="somedata"
filename="persistance.dump"
with open(filename,'wb')as fp:
 pickle.dump(tosave,fp)


with open(filename,'rb') as fp:
 savedval=pickle.load(fp)



In case you are thinking about pickles, (I was while writing the this article), please continue; there's not much I could do!

I am sure the image on the right will help.




No comments:

Post a Comment