Thunder
Thunder

Reputation: 489

Changing the content of Pickle File

I have trained a deep learning model and it got saved in a pickle file. Due to some reason, I have to slightly change the code from which I got the pickle file. It took me months in training & I want to anyhow use the last pickle file created, as the weights will remains same. Is there any way to view and change the content of the pickle file?

Edit: For example, if we have the stylegan2 pre-trained network pickle file and suppose we made changes on the G_synthesis function code (present in https://github.com/NVlabs/stylegan2/blob/master/training/networks_stylegan2.py) then how can we use the old pickled file.

Upvotes: 3

Views: 1615

Answers (1)

Pierre
Pierre

Reputation: 81

If you just want to change some functions but keep the same weights, can you just copy the weights to new model like this:

import pickle
from old_model_file import old_model
from new_model_file import new_model

# 1.load pickle file
with open('old.pickle','rb') as f:
    old_pickle = pickle.load(f)

# 2.create model based new model
new_pickle = new_model()

# 3. copy weights from old model 
'''
##you should copy all weights from old_pickle to new_pickle
##for example:
new_pickle.weight_A = old_pickle.weight_A
new_pickle.weight_B = old_pickle.weight_B
'''

# 4. save the new model
with open('new.pickle','wb') as f:
    pickle.dump(new_pickle,f)

Is this what you want?

Upvotes: 1

Related Questions