What's Pickle this
Wow delicious ~ Aside from the blur, the name comes from Pickles, a Python module that allows you to save the state of an object by serializing it.
I've been studying deep learning lately and often
train_list = load_image_list(args.train)
val_list = load_image_list(args.val)
mean_image = pickle.load(open(args.mean, 'rb'))
I often come across codes like. I thought, "What is pickle?", So I will summarize what I researched.
Since I have little knowledge of programming in general, please point out if there is a description that you are interested in.
Most of it is documented.
http://docs.python.jp/2/library/pickle.html http://docs.python.jp/3/library/pickle.html
By its very nature, programs work in memory and are removed from memory when they have finished their role. The object will also be deleted by the GC when it finishes its role, or it will disappear when the program finishes running.
The Pickle module provides the ability to save the created object even after the program has finished running.
The way to save is to convert the object to a byte string and write it to a file.
Write a simple class.
Singer.py
class Singer(object):
def __init__(self, lylics):
self.lylics = lylics
def sing(self):
print(self.lylics)
When this class is instantiated
main.py
from singer import Singer
singer = Singer('Shanranran')
singer.sing()
$ main.py
$ 'Shanranran'
And so on, it will sing the lyrics you passed to the constructor.
Of course, Singer forgets the lyrics when the run is finished.
So, instead of using the lyrics card, use Pickle to save the state of the object.
dump.py
import pickle
from singer import Singer
singer = Singer('Shanranran')
with open('singer.pickle', 'wb') as f:
pickle.dump(singer, f)
By making the state of the Singer object a binary file called singer.pickle, you can save the state of the object even after dump.py is finished. This is Pickle.
I use the load method to make an object non-pickle,
load.py
import pickle
with open('singer.pickle', 'rb') as f:
singer = pickle.load(f)
singer.sing()
$ python load.py
$ Shanranran
It was confirmed that the state of the Singer object when dump.py was executed was saved.
I briefly wrote about the Pickle module, but this is just the beginning, and there are still more things to be aware of when using it in earnest. I'm studying myself, so I'll write it when I look it up again.
12.1. pickle — Serialization of Python objects http://docs.python.jp/3/library/pickle.html
11.1. pickle — Serialization of Python objects http://docs.python.jp/2/library/pickle.html
Python: Pickle an object http://blog.amedama.jp/entry/2015/12/05/132520
Recommended Posts