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.0a0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: vipickle-0.1.0a0.tar.gz
  • Upload date:
  • Size: 10.9 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.0a0.tar.gz
Algorithm Hash digest
SHA256 6a1327b83533296a04f95fb9cdd88f6ca5aa2d7f8f33783f13b508f3fdc1a97a
MD5 61acd47a9b62d6d9fbc786a521e5d279
BLAKE2b-256 739f50bfc18e2e1f8765cdba6f9034f00831c8c7346274bbaf897e316f8e28f6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: vipickle-0.1.0a0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 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.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 d67d641005e6fcdbe427c93a271fb57c190843b3c1a0fb4f257e63d504c7a016
MD5 46e639eed650e0b460d19bea813f16ba
BLAKE2b-256 2eee32edff1c896f75d3748d105b526e05d998d3f3d683aa843f1d7273918094

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