Skip to main content

vipickle is a tiny python package for saving instances with unpickable attributes

Project description

👑 VIPpickle

Tiny python package for saving instances with unpickable attributes

Quickstart

Install vipickle with pip :

pip install vipickle

Then inherit from VIPicklable and define which attribute are not picklable and how they should be dumped and restored.

import torch
from torchvision import models
from pathlib import Path

from vipickle import VIPicklable

class MyClass(VIPicklable):
    PICKLE_BLACKLIST = ["vision_model"]

    def __init__(self):
        self.vision_model = models.vgg16(weights='IMAGENET1K_V1')

    def _dump_vision_model_(self, save_dir: Path, overwrite:bool = True):
        model_weights_path = save_dir / "model_weights.pth"
        if overwrite or not model_weights_path.exists():
            torch.save(model.state_dict(), model_weights_path)

    def _restore_vision_model_(self, save_dir: Path):
        self.vision_model = models.vgg16()
        self.vision_model.load_state_dict(torch.load(save_dir / "model_weights.pth"))


# Create an instance
obj = Myclass()

# train could modify the model weights
obj.train()

# we save the instance to a folder, _dump_vision_model_ will dump the weights in the folder
obj.save("a/folder")
del obj

# we can then reload the object, _restore_vision_model_ will recreate the attribute vision_model and load the weights
obj = MyClass.load("a/folder")
obj.vision_model.eval()

Additionnal dependencies

Dev dependencies

pip install vipickle[dev]

Unit tests dependencies

pip install vipickle[test]

Documentation dependencies

pip install vipickle[doc]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vipickle-0.1.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

vipickle-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file vipickle-0.1.0.tar.gz.

File metadata

  • Download URL: vipickle-0.1.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for vipickle-0.1.0.tar.gz
Algorithm Hash digest
SHA256 12ce4ccb0d46c415d34a1c0ecb96d82c3b767c228da9f29a2fc5bedf488daab9
MD5 4ca7372749d1d03cb9d9a6354e5430fa
BLAKE2b-256 20da904db5f38a4ce2a7f567cffc432f94a373ad9cfc89c0519a3ca94ba0d744

See more details on using hashes here.

File details

Details for the file vipickle-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: vipickle-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for vipickle-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ddb1b8ed777dec5e8884bee4f01f93984c77dfbb653d8105948b979d6d285ace
MD5 bcf05bb0d58435b83151546b89dfbb99
BLAKE2b-256 1ae03a5c73e4035e8f7c250839d0950a34590ca027bcc1d4182ea437116a393f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page