Package: torch-liberator
Torch Liberator - Deploy PyTorch ModelsMain Pagehttps://gitlab.kitware.com/computer-vision/torch_liberatorGithub Mirrorhttps://github.com/Kitware/torch_liberatorPypihttps://pypi.org/project/torch_liberatorTorch Hackathon 2021Youtube Video,Google Slides, andSubmission PageTorch Liberator is a Python module containing a set of tools for reading and
writing relevant parts of deep networks.Typically, when moving a deep-network trained with torch, you have to keep
track of the entire codebase that defined the model file in addition to the
checkpoint file containing the learned weights. Torch Liberator contains tools
to extract relevant source code and bundle it with weights and serializing it
into a single-file deployment.Note: as of torch 1.9, torch comes with atorch.packagesubmodule which contains a method for saving model weights with model
structure.  We recommend usingtorch.packageover the single-file deployments
provided in this package. Thus theload_partial_statelogic is the main
code of interest provided in this module.Installationpipinstalltorch_liberator# OR with a specific branchpipinstallgit+https://gitlab.kitware.com/computer-vision/torch_liberator.git@mainPartial State LoadingNew in0.1.0torch liberator now exposes a publicload_partial_statefunction, which does it best to “shove” weights from one model into another
model. There are several methods to compute associations between layer names in
one model to layer names in another, the most general being the “embedding”
method, and the slightly more structured “isomorphism” option.Have you ever had the scenario where you use one model as a sub-model in a
bigger network? Then you had to load pretrained subnetwork state into that
bigger model?The latest version oftorch_liberator.load_patial_statecan handle this by
solving a maximum-common-subtree-isomorphism problem. This computes the largest
possible mapping between the two state dictionaries that share consistent
suffixes.>>>importtorchvision>>>importtorch>>>importtorch_liberator>>>resnet50=torchvision.models.resnet50()>>>classCustomModel(torch.nn.Module):>>>def__init__(self):>>>super().__init__()>>>self.module=resnet50>>>self.extra=torch.nn.Linear(1,1)>>># Directly load resnet50 state into a model that has it as an embedded subnetwork>>>model=CustomModel()>>>model_state_dict=resnet50.state_dict()>>># load partial state returns information about what it did>>>info=torch_liberator.load_partial_state(model,model_state_dict,association='isomorphism',verbose=1)>>>print(len(info['seen']['full_add']))>>>print(len(info['self_unset']))>>>print(len(info['other_unused']))32020It can also handle loading common state between two models that share some
underlying structure.>>>importtorchvision>>>importtorch>>>importtorch_liberator>>>resnet50=torchvision.models.resnet50()>>>classCustomModel1(torch.nn.Module):>>>def__init__(self):>>>super().__init__()>>>self.module=resnet50>>>self.custom_model1_layer=torch.nn.Linear(1,1)>>>classCustomModel2(torch.nn.Module):>>>def__init__(self):>>>super().__init__()>>>self.backbone=resnet50>>>self.custom_model2_layer=torch.nn.Linear(1,1)>>># Load as much of model1 state into model2 as possible>>>model1=CustomModel1()>>>model2=CustomModel2()>>>model2_state_dict=model2.state_dict()>>># load partial state returns information about what it did>>>info=torch_liberator.load_partial_state(model1,model2_state_dict,association='isomorphism',verbose=1)>>>print(len(info['seen']['full_add']))>>>print(len(info['seen']['skipped']))>>>print(len(info['self_unset']))>>>print(len(info['other_unused']))320222>>>importtorchvision>>>importtorch_liberator>>>#>>>faster_rcnn=torchvision.models.detection.faster_rcnn.fasterrcnn_resnet50_fpn()>>>resnet50=torchvision.models.resnet50(pretrained=True)>>>state_dict=resnet50.state_dict()>>># Load partial state return a dictionary that tells you how well it did>>>info=torch_liberator.load_partial_state(faster_rcnn,state_dict,verbose=0,association='embedding')>>>print(ub.map_vals(len,info['seen']))>>>print(ub.map_vals(len,ub.dict_diff(info,['seen']))){'full_add':265,'skipped':55}{'other_unused':55,'self_unset':30}>>># Load partial state return a dictionary that tells you how well it did>>>info=torch_liberator.load_partial_state(faster_rcnn,state_dict,verbose=0,association='isomorphism')>>>print(ub.map_vals(len,info['seen']))>>>print(ub.map_vals(len,ub.dict_diff(info,['seen']))){'full_add':265,'skipped':55}{'other_unused':55,'self_unset':30}Also, if the sizes of the tensor don’t quite fit, they will be mangled, i.e.
“shoved-in” as best as possible. See the docstring for more detail.Stand-alone Single-File Model DeploymentsThe original purpose oftorch_liberatorwas to build standalone torch
packages that contained both the model code and the model weight. It still does
that buttorch.packagenew in torch 1.9, might be a better solution moving
forward. Seetorch.packagefor details.Torch Liberator builds on theliberatorlibrary to statically
extract pytorch code that defines a model’s topology and bundle that with a
pretrained weights file. This results in a single-file deployment package and
can potentially remove dependencies on the codebase used to train the model.Torch Liberator can also read these deployment files and create an instance of
the model initialized with the correct pretrained weights.The API is ok, but it does need improvement. However, the current version is in
a working state. There aren’t any high level docs, but there are a lot of
docstrings and doctests. The example here gives a good overview of the code by
extracting the AlexNet model from torchvision.>>>importtorch_liberator>>>fromtorch_liberator.deployerimportDeployedModel>>>fromtorchvisionimportmodels>>>print('--- DEFINE A MODEL ---')>>>model=models.alexnet(pretrained=False)# false for test speed>>>initkw=dict(num_classes=1000)# not all models nicely supply this>>>model._initkw=initkw---DEFINEAMODEL--->>>print('--- DEPLOY THE MODEL ---')>>>zip_fpath=torch_liberator.deploy(model,'test-deploy.zip')---DEPLOYTHEMODEL---[DEPLOYER]Deployedzipfpath=/tmp/tmpeqd3y_rx/test-deploy.zip>>>print('--- LOAD THE DEPLOYED MODEL ---')>>>loader=DeployedModel(zip_fpath)>>>model=loader.load_model()---LOADTHEDEPLOYEDMODEL---LoadingdataontoNonefrom<zopen(<_io.BufferedReadername='/tmp/tmpg1kln3kw/test-deploy/deploy_snapshot.pt'>mode=rb)>PretrainedweightsareaperfectfitThe major weirdness right now, is you either have to explicitly define “initkw”
(which are the keyword arguments used to create an instance of our model) at
deploy time, or you can set it as the_initkwattribute of your model (or
if your keyword arguments all exist as member variables of the class,
torch_liberator tries to be smart and infer what initkw should be).There is also a torch-liberator CLI that can be used to package a weight file,
a python model file, and optional json metadata.python-mtorch_liberator\--model<path-to-the-liberated-py-file>\--weights<path-to-the-torch-pth-weight-file>\--info<path-to-train-info-json-file>\--dstmy_custom_deployfile.zip